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.

XSSFCellFill.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel.extensions;
  16. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
  17. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
  18. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
  19. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
  20. import org.apache.poi.xssf.usermodel.IndexedColorMap;
  21. import org.apache.poi.xssf.usermodel.XSSFColor;
  22. import org.apache.poi.util.Internal;
  23. /**
  24. * This element specifies fill formatting.
  25. * A cell fill consists of a background color, foreground color, and pattern to be applied across the cell.
  26. */
  27. public final class XSSFCellFill {
  28. private IndexedColorMap _indexedColorMap;
  29. private CTFill _fill;
  30. /**
  31. * Creates a CellFill from the supplied parts
  32. *
  33. * @param fill - fill
  34. */
  35. public XSSFCellFill(CTFill fill, IndexedColorMap colorMap) {
  36. _fill = fill;
  37. _indexedColorMap = colorMap;
  38. }
  39. /**
  40. * Creates an empty CellFill
  41. */
  42. public XSSFCellFill() {
  43. _fill = CTFill.Factory.newInstance();
  44. }
  45. /**
  46. * Get the background fill color.
  47. *
  48. * @return fill color, null if color is not set
  49. */
  50. public XSSFColor getFillBackgroundColor() {
  51. CTPatternFill ptrn = _fill.getPatternFill();
  52. if (ptrn == null) return null;
  53. CTColor ctColor = ptrn.getBgColor();
  54. return ctColor == null ? null : new XSSFColor(ctColor, _indexedColorMap);
  55. }
  56. /**
  57. * Set the background fill color represented as a indexed color value.
  58. *
  59. * @param index
  60. */
  61. public void setFillBackgroundColor(int index) {
  62. CTPatternFill ptrn = ensureCTPatternFill();
  63. CTColor ctColor = ptrn.isSetBgColor() ? ptrn.getBgColor() : ptrn.addNewBgColor();
  64. ctColor.setIndexed(index);
  65. }
  66. /**
  67. * Set the background fill color represented as a {@link XSSFColor} value.
  68. *
  69. * @param color
  70. */
  71. public void setFillBackgroundColor(XSSFColor color) {
  72. CTPatternFill ptrn = ensureCTPatternFill();
  73. ptrn.setBgColor(color.getCTColor());
  74. }
  75. /**
  76. * Get the foreground fill color.
  77. *
  78. * @return XSSFColor - foreground color. null if color is not set
  79. */
  80. public XSSFColor getFillForegroundColor() {
  81. CTPatternFill ptrn = _fill.getPatternFill();
  82. if (ptrn == null) return null;
  83. CTColor ctColor = ptrn.getFgColor();
  84. return ctColor == null ? null : new XSSFColor(ctColor, _indexedColorMap);
  85. }
  86. /**
  87. * Set the foreground fill color as a indexed color value
  88. *
  89. * @param index - the color to use
  90. */
  91. public void setFillForegroundColor(int index) {
  92. CTPatternFill ptrn = ensureCTPatternFill();
  93. CTColor ctColor = ptrn.isSetFgColor() ? ptrn.getFgColor() : ptrn.addNewFgColor();
  94. ctColor.setIndexed(index);
  95. }
  96. /**
  97. * Set the foreground fill color represented as a {@link XSSFColor} value.
  98. *
  99. * @param color - the color to use
  100. */
  101. public void setFillForegroundColor(XSSFColor color) {
  102. CTPatternFill ptrn = ensureCTPatternFill();
  103. ptrn.setFgColor(color.getCTColor());
  104. }
  105. /**
  106. * get the fill pattern
  107. *
  108. * @return fill pattern type. null if fill pattern is not set
  109. */
  110. public STPatternType.Enum getPatternType() {
  111. CTPatternFill ptrn = _fill.getPatternFill();
  112. return ptrn == null ? null : ptrn.getPatternType();
  113. }
  114. /**
  115. * set the fill pattern
  116. *
  117. * @param patternType fill pattern to use
  118. */
  119. public void setPatternType(STPatternType.Enum patternType) {
  120. CTPatternFill ptrn = ensureCTPatternFill();
  121. ptrn.setPatternType(patternType);
  122. }
  123. private CTPatternFill ensureCTPatternFill() {
  124. CTPatternFill patternFill = _fill.getPatternFill();
  125. if (patternFill == null) {
  126. patternFill = _fill.addNewPatternFill();
  127. }
  128. return patternFill;
  129. }
  130. /**
  131. * Returns the underlying XML bean.
  132. *
  133. * @return CTFill
  134. */
  135. @Internal
  136. public CTFill getCTFill() {
  137. return _fill;
  138. }
  139. public int hashCode() {
  140. return _fill.toString().hashCode();
  141. }
  142. public boolean equals(Object o) {
  143. if (!(o instanceof XSSFCellFill)) return false;
  144. XSSFCellFill cf = (XSSFCellFill) o;
  145. return _fill.toString().equals(cf.getCTFill().toString());
  146. }
  147. }