You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSSFPatternFormatting.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xssf.usermodel;
  20. import org.apache.poi.ss.usermodel.Color;
  21. import org.apache.poi.ss.usermodel.PatternFormatting;
  22. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
  23. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
  24. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  25. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
  26. public class XSSFPatternFormatting implements PatternFormatting {
  27. IndexedColorMap _colorMap;
  28. CTFill _fill;
  29. XSSFPatternFormatting(CTFill fill, IndexedColorMap colorMap) {
  30. _fill = fill;
  31. _colorMap = colorMap;
  32. }
  33. public XSSFColor getFillBackgroundColorColor() {
  34. if(!_fill.isSetPatternFill()) return null;
  35. return XSSFColor.from(_fill.getPatternFill().getBgColor(), _colorMap);
  36. }
  37. public XSSFColor getFillForegroundColorColor() {
  38. if(!_fill.isSetPatternFill() || ! _fill.getPatternFill().isSetFgColor())
  39. return null;
  40. return XSSFColor.from(_fill.getPatternFill().getFgColor(), _colorMap);
  41. }
  42. public short getFillPattern() {
  43. if(!_fill.isSetPatternFill() || !_fill.getPatternFill().isSetPatternType()) return NO_FILL;
  44. return (short)(_fill.getPatternFill().getPatternType().intValue() - 1);
  45. }
  46. public short getFillBackgroundColor() {
  47. XSSFColor color = getFillBackgroundColorColor();
  48. if (color == null) return 0;
  49. return color.getIndexed();
  50. }
  51. public short getFillForegroundColor() {
  52. XSSFColor color = getFillForegroundColorColor();
  53. if (color == null) return 0;
  54. return color.getIndexed();
  55. }
  56. public void setFillBackgroundColor(Color bg) {
  57. XSSFColor xcolor = XSSFColor.toXSSFColor(bg);
  58. if (xcolor == null) setFillBackgroundColor((CTColor)null);
  59. else setFillBackgroundColor(xcolor.getCTColor());
  60. }
  61. public void setFillBackgroundColor(short bg) {
  62. CTColor bgColor = CTColor.Factory.newInstance();
  63. bgColor.setIndexed(bg);
  64. setFillBackgroundColor(bgColor);
  65. }
  66. private void setFillBackgroundColor(CTColor color) {
  67. CTPatternFill ptrn = _fill.isSetPatternFill() ? _fill.getPatternFill() : _fill.addNewPatternFill();
  68. if (color == null) {
  69. ptrn.unsetBgColor();
  70. } else {
  71. ptrn.setBgColor(color);
  72. }
  73. }
  74. public void setFillForegroundColor(Color fg) {
  75. XSSFColor xcolor = XSSFColor.toXSSFColor(fg);
  76. if (xcolor == null) setFillForegroundColor((CTColor)null);
  77. else setFillForegroundColor(xcolor.getCTColor());
  78. }
  79. public void setFillForegroundColor(short fg) {
  80. CTColor fgColor = CTColor.Factory.newInstance();
  81. fgColor.setIndexed(fg);
  82. setFillForegroundColor(fgColor);
  83. }
  84. private void setFillForegroundColor(CTColor color) {
  85. CTPatternFill ptrn = _fill.isSetPatternFill() ? _fill.getPatternFill() : _fill.addNewPatternFill();
  86. if (color == null) {
  87. ptrn.unsetFgColor();
  88. } else {
  89. ptrn.setFgColor(color);
  90. }
  91. }
  92. public void setFillPattern(short fp){
  93. CTPatternFill ptrn = _fill.isSetPatternFill() ? _fill.getPatternFill() : _fill.addNewPatternFill();
  94. if(fp == NO_FILL) ptrn.unsetPatternType();
  95. else ptrn.setPatternType(STPatternType.Enum.forInt(fp + 1));
  96. }
  97. }