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.

XSSFCellBorder.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.apache.poi.ss.usermodel.BorderStyle;
  17. import org.apache.poi.xssf.model.ThemesTable;
  18. import org.apache.poi.xssf.usermodel.IndexedColorMap;
  19. import org.apache.poi.xssf.usermodel.XSSFColor;
  20. import org.apache.poi.util.Internal;
  21. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
  22. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
  23. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
  24. /**
  25. * This element contains border formatting information, specifying border definition formats (left, right, top, bottom, diagonal)
  26. * for cells in the workbook.
  27. * Color is optional.
  28. */
  29. public class XSSFCellBorder {
  30. private IndexedColorMap _indexedColorMap;
  31. private ThemesTable _theme;
  32. private CTBorder border;
  33. /**
  34. * Creates a Cell Border from the supplied XML definition
  35. * @param border
  36. * @param theme
  37. * @param colorMap
  38. */
  39. public XSSFCellBorder(CTBorder border, ThemesTable theme, IndexedColorMap colorMap) {
  40. this(border, colorMap);
  41. this._theme = theme;
  42. }
  43. /**
  44. * Creates a Cell Border from the supplied XML definition
  45. * @param border
  46. */
  47. public XSSFCellBorder(CTBorder border) {
  48. this(border, null);
  49. }
  50. /**
  51. *
  52. * @param border
  53. * @param colorMap
  54. */
  55. public XSSFCellBorder(CTBorder border, IndexedColorMap colorMap) {
  56. this.border = border;
  57. this._indexedColorMap = colorMap;
  58. }
  59. /**
  60. * Creates a new, empty Cell Border.
  61. * You need to attach this to the Styles Table
  62. */
  63. public XSSFCellBorder() {
  64. border = CTBorder.Factory.newInstance();
  65. }
  66. /**
  67. * Records the Themes Table that is associated with
  68. * the current font, used when looking up theme
  69. * based colours and properties.
  70. */
  71. public void setThemesTable(ThemesTable themes) {
  72. this._theme = themes;
  73. }
  74. /**
  75. * The enumeration value indicating the side being used for a cell border.
  76. */
  77. public static enum BorderSide {
  78. TOP, RIGHT, BOTTOM, LEFT
  79. }
  80. /**
  81. * Returns the underlying XML bean.
  82. *
  83. * @return CTBorder
  84. */
  85. @Internal
  86. public CTBorder getCTBorder() {
  87. return border;
  88. }
  89. /**
  90. * Get the type of border to use for the selected border
  91. *
  92. * @param side - - where to apply the color definition
  93. * @return borderstyle - the type of border to use. default value is NONE if border style is not set.
  94. * @see BorderStyle
  95. */
  96. public BorderStyle getBorderStyle(BorderSide side) {
  97. CTBorderPr ctBorder = getBorder(side);
  98. STBorderStyle.Enum border = ctBorder == null ? STBorderStyle.NONE : ctBorder.getStyle();
  99. return BorderStyle.values()[border.intValue() - 1];
  100. }
  101. /**
  102. * Set the type of border to use for the selected border
  103. *
  104. * @param side - - where to apply the color definition
  105. * @param style - border style
  106. * @see BorderStyle
  107. */
  108. public void setBorderStyle(BorderSide side, BorderStyle style) {
  109. getBorder(side, true).setStyle(STBorderStyle.Enum.forInt(style.ordinal() + 1));
  110. }
  111. /**
  112. * Get the color to use for the selected border
  113. *
  114. * @param side - where to apply the color definition
  115. * @return color - color to use as XSSFColor. null if color is not set
  116. */
  117. public XSSFColor getBorderColor(BorderSide side) {
  118. CTBorderPr borderPr = getBorder(side);
  119. if(borderPr != null && borderPr.isSetColor()) {
  120. XSSFColor clr = new XSSFColor(borderPr.getColor(), _indexedColorMap);
  121. if(_theme != null) {
  122. _theme.inheritFromThemeAsRequired(clr);
  123. }
  124. return clr;
  125. } else {
  126. // No border set
  127. return null;
  128. }
  129. }
  130. /**
  131. * Set the color to use for the selected border
  132. *
  133. * @param side - where to apply the color definition
  134. * @param color - the color to use
  135. */
  136. public void setBorderColor(BorderSide side, XSSFColor color) {
  137. CTBorderPr borderPr = getBorder(side, true);
  138. if (color == null) borderPr.unsetColor();
  139. else
  140. borderPr.setColor(color.getCTColor());
  141. }
  142. private CTBorderPr getBorder(BorderSide side) {
  143. return getBorder(side, false);
  144. }
  145. private CTBorderPr getBorder(BorderSide side, boolean ensure) {
  146. CTBorderPr borderPr;
  147. switch (side) {
  148. case TOP:
  149. borderPr = border.getTop();
  150. if (ensure && borderPr == null) borderPr = border.addNewTop();
  151. break;
  152. case RIGHT:
  153. borderPr = border.getRight();
  154. if (ensure && borderPr == null) borderPr = border.addNewRight();
  155. break;
  156. case BOTTOM:
  157. borderPr = border.getBottom();
  158. if (ensure && borderPr == null) borderPr = border.addNewBottom();
  159. break;
  160. case LEFT:
  161. borderPr = border.getLeft();
  162. if (ensure && borderPr == null) borderPr = border.addNewLeft();
  163. break;
  164. default:
  165. throw new IllegalArgumentException("No suitable side specified for the border");
  166. }
  167. return borderPr;
  168. }
  169. public int hashCode() {
  170. return border.toString().hashCode();
  171. }
  172. public boolean equals(Object o) {
  173. if (!(o instanceof XSSFCellBorder)) return false;
  174. XSSFCellBorder cf = (XSSFCellBorder) o;
  175. return border.toString().equals(cf.getCTBorder().toString());
  176. }
  177. }