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.0KB

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