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.

TableStyleType.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.ss.usermodel;
  16. import org.apache.poi.ss.util.CellRangeAddress;
  17. import org.apache.poi.ss.util.CellRangeAddressBase;
  18. import org.apache.poi.ss.util.CellReference;
  19. /**
  20. * Ordered list of table style elements, for both data tables and pivot tables.
  21. * Some elements only apply to pivot tables, but any style definition can omit any number,
  22. * so having them in one list should not be a problem.
  23. * <p>
  24. * The order is the specification order of application, with later elements overriding previous
  25. * ones, if style properties conflict.
  26. * <p>
  27. * Processing could iterate bottom-up if looking for specific properties, and stop when the
  28. * first style is found defining a value for that property.
  29. * <p>
  30. * Enum names match the OOXML spec values exactly, so {@link #valueOf(String)} will work.
  31. *
  32. * @since 3.17 beta 1
  33. */
  34. public enum TableStyleType {
  35. /***/
  36. wholeTable {
  37. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  38. return new CellRangeAddress(table.getStartRowIndex(), table.getEndRowIndex(), table.getStartColIndex(), table.getEndColIndex());
  39. }
  40. },
  41. /***/
  42. pageFieldLabels, // pivot only
  43. /***/
  44. pageFieldValues, // pivot only
  45. /***/
  46. firstColumnStripe{
  47. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  48. TableStyleInfo info = table.getStyle();
  49. if (! info.isShowColumnStripes()) return null;
  50. DifferentialStyleProvider c1Style = info.getStyle().getStyle(firstColumnStripe);
  51. DifferentialStyleProvider c2Style = info.getStyle().getStyle(secondColumnStripe);
  52. int c1Stripe = c1Style == null ? 1 : Math.max(1, c1Style.getStripeSize());
  53. int c2Stripe = c2Style == null ? 1 : Math.max(1, c2Style.getStripeSize());
  54. int firstStart = table.getStartColIndex();
  55. int secondStart = firstStart + c1Stripe;
  56. final int c = cell.getCol();
  57. // look for the stripe containing c, accounting for the style element stripe size
  58. // could do fancy math, but tables can't be that wide, a simple loop is fine
  59. // if not in this type of stripe, return null
  60. while (firstStart <= c) {
  61. if (c <= secondStart -1) {
  62. return new CellRangeAddress(table.getStartRowIndex(), table.getEndRowIndex(), firstStart, secondStart - 1);
  63. }
  64. firstStart = secondStart + c2Stripe;
  65. secondStart = firstStart + c1Stripe;
  66. }
  67. return null;
  68. }
  69. },
  70. /***/
  71. secondColumnStripe{
  72. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  73. TableStyleInfo info = table.getStyle();
  74. if (! info.isShowColumnStripes()) return null;
  75. DifferentialStyleProvider c1Style = info.getStyle().getStyle(firstColumnStripe);
  76. DifferentialStyleProvider c2Style = info.getStyle().getStyle(secondColumnStripe);
  77. int c1Stripe = c1Style == null ? 1 : Math.max(1, c1Style.getStripeSize());
  78. int c2Stripe = c2Style == null ? 1 : Math.max(1, c2Style.getStripeSize());
  79. int firstStart = table.getStartColIndex();
  80. int secondStart = firstStart + c1Stripe;
  81. final int c = cell.getCol();
  82. // look for the stripe containing c, accounting for the style element stripe size
  83. // could do fancy math, but tables can't be that wide, a simple loop is fine
  84. // if not in this type of stripe, return null
  85. while (firstStart <= c) {
  86. if (c >= secondStart && c <= secondStart + c2Stripe -1) {
  87. return new CellRangeAddress(table.getStartRowIndex(), table.getEndRowIndex(), secondStart, secondStart + c2Stripe - 1);
  88. }
  89. firstStart = secondStart + c2Stripe;
  90. secondStart = firstStart + c1Stripe;
  91. }
  92. return null;
  93. }
  94. },
  95. /***/
  96. firstRowStripe {
  97. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  98. TableStyleInfo info = table.getStyle();
  99. if (! info.isShowRowStripes()) return null;
  100. DifferentialStyleProvider c1Style = info.getStyle().getStyle(firstRowStripe);
  101. DifferentialStyleProvider c2Style = info.getStyle().getStyle(secondRowStripe);
  102. int c1Stripe = c1Style == null ? 1 : Math.max(1, c1Style.getStripeSize());
  103. int c2Stripe = c2Style == null ? 1 : Math.max(1, c2Style.getStripeSize());
  104. int firstStart = table.getStartRowIndex() + table.getHeaderRowCount();
  105. int secondStart = firstStart + c1Stripe;
  106. final int c = cell.getRow();
  107. // look for the stripe containing c, accounting for the style element stripe size
  108. // could do fancy math, but tables can't be that wide, a simple loop is fine
  109. // if not in this type of stripe, return null
  110. while (firstStart <= c) {
  111. if (c <= secondStart -1) {
  112. return new CellRangeAddress(firstStart, secondStart - 1, table.getStartColIndex(), table.getEndColIndex());
  113. }
  114. firstStart = secondStart + c2Stripe;
  115. secondStart = firstStart + c1Stripe;
  116. }
  117. return null;
  118. }
  119. },
  120. /***/
  121. secondRowStripe{
  122. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  123. TableStyleInfo info = table.getStyle();
  124. if (! info.isShowRowStripes()) return null;
  125. DifferentialStyleProvider c1Style = info.getStyle().getStyle(firstRowStripe);
  126. DifferentialStyleProvider c2Style = info.getStyle().getStyle(secondRowStripe);
  127. int c1Stripe = c1Style == null ? 1 : Math.max(1, c1Style.getStripeSize());
  128. int c2Stripe = c2Style == null ? 1 : Math.max(1, c2Style.getStripeSize());
  129. int firstStart = table.getStartRowIndex() + table.getHeaderRowCount();
  130. int secondStart = firstStart + c1Stripe;
  131. final int c = cell.getRow();
  132. // look for the stripe containing c, accounting for the style element stripe size
  133. // could do fancy math, but tables can't be that wide, a simple loop is fine
  134. // if not in this type of stripe, return null
  135. while (firstStart <= c) {
  136. if (c >= secondStart && c <= secondStart +c2Stripe -1) {
  137. return new CellRangeAddress(secondStart, secondStart + c2Stripe - 1, table.getStartColIndex(), table.getEndColIndex());
  138. }
  139. firstStart = secondStart + c2Stripe;
  140. secondStart = firstStart + c1Stripe;
  141. }
  142. return null;
  143. }
  144. },
  145. /***/
  146. lastColumn {
  147. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  148. if (! table.getStyle().isShowLastColumn()) return null;
  149. return new CellRangeAddress(table.getStartRowIndex(), table.getEndRowIndex(), table.getEndColIndex(), table.getEndColIndex());
  150. }
  151. },
  152. /***/
  153. firstColumn {
  154. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  155. if (! table.getStyle().isShowFirstColumn()) return null;
  156. return new CellRangeAddress(table.getStartRowIndex(), table.getEndRowIndex(), table.getStartColIndex(), table.getStartColIndex());
  157. }
  158. },
  159. /***/
  160. headerRow {
  161. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  162. if (table.getHeaderRowCount() < 1) return null;
  163. return new CellRangeAddress(table.getStartRowIndex(), table.getStartRowIndex() + table.getHeaderRowCount() -1, table.getStartColIndex(), table.getEndColIndex());
  164. }
  165. },
  166. /***/
  167. totalRow {
  168. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  169. if (table.getTotalsRowCount() < 1) return null;
  170. return new CellRangeAddress(table.getEndRowIndex() - table.getTotalsRowCount() +1, table.getEndRowIndex(), table.getStartColIndex(), table.getEndColIndex());
  171. }
  172. },
  173. /***/
  174. firstHeaderCell {
  175. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  176. if (table.getHeaderRowCount() < 1) return null;
  177. return new CellRangeAddress(table.getStartRowIndex(), table.getStartRowIndex(), table.getStartColIndex(), table.getStartColIndex());
  178. }
  179. },
  180. /***/
  181. lastHeaderCell {
  182. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  183. if (table.getHeaderRowCount() < 1) return null;
  184. return new CellRangeAddress(table.getStartRowIndex(), table.getStartRowIndex(), table.getEndColIndex(), table.getEndColIndex());
  185. }
  186. },
  187. /***/
  188. firstTotalCell {
  189. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  190. if (table.getTotalsRowCount() < 1) return null;
  191. return new CellRangeAddress(table.getEndRowIndex() - table.getTotalsRowCount() +1, table.getEndRowIndex(), table.getStartColIndex(), table.getStartColIndex());
  192. }
  193. },
  194. /***/
  195. lastTotalCell {
  196. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  197. if (table.getTotalsRowCount() < 1) return null;
  198. return new CellRangeAddress(table.getEndRowIndex() - table.getTotalsRowCount() +1, table.getEndRowIndex(), table.getEndColIndex(), table.getEndColIndex());
  199. }
  200. },
  201. /* these are for pivot tables only */
  202. /***/
  203. firstSubtotalColumn,
  204. /***/
  205. secondSubtotalColumn,
  206. /***/
  207. thirdSubtotalColumn,
  208. /***/
  209. blankRow,
  210. /***/
  211. firstSubtotalRow,
  212. /***/
  213. secondSubtotalRow,
  214. /***/
  215. thirdSubtotalRow,
  216. /***/
  217. firstColumnSubheading,
  218. /***/
  219. secondColumnSubheading,
  220. /***/
  221. thirdColumnSubheading,
  222. /***/
  223. firstRowSubheading,
  224. /***/
  225. secondRowSubheading,
  226. /***/
  227. thirdRowSubheading,
  228. ;
  229. /**
  230. * A range is returned only for the part of the table matching this enum instance and containing the given cell.
  231. * Null is returned for all other cases, such as:
  232. * <ul>
  233. * <li>Cell on a different sheet than the table
  234. * <li>Cell outside the table
  235. * <li>this Enum part is not included in the table (i.e. no header/totals row)
  236. * <li>this Enum is for a table part not yet implemented in POI, such as pivot table elements
  237. * </ul>
  238. * The returned range can be used to determine how style options may or may not apply to this cell.
  239. * For example, {@link #wholeTable} borders only apply to the outer boundary of a table, while the
  240. * rest of the styling, such as font and color, could apply to all the interior cells as well.
  241. *
  242. * @param table table to evaluate
  243. * @param cell to evaluate
  244. * @return range in the table representing this class of cells, if it contains the given cell, or null if not applicable.
  245. * Stripe style types return only the stripe range containing the given cell, or null.
  246. */
  247. public CellRangeAddressBase appliesTo(Table table, Cell cell) {
  248. if (cell == null) return null;
  249. return appliesTo(table, new CellReference(cell.getSheet().getSheetName(), cell.getRowIndex(), cell.getColumnIndex(), true, true));
  250. }
  251. /**
  252. * A range is returned only for the part of the table matching this enum instance and containing the given cell reference.
  253. * Null is returned for all other cases, such as:
  254. * <ul>
  255. * <li>Cell on a different sheet than the table
  256. * <li>Cell outside the table
  257. * <li>this Enum part is not included in the table (i.e. no header/totals row)
  258. * <li>this Enum is for a table part not yet implemented in POI, such as pivot table elements
  259. * </ul>
  260. * The returned range can be used to determine how style options may or may not apply to this cell.
  261. * For example, {@link #wholeTable} borders only apply to the outer boundary of a table, while the
  262. * rest of the styling, such as font and color, could apply to all the interior cells as well.
  263. *
  264. * @param table table to evaluate
  265. * @param cell CellReference to evaluate
  266. * @return range in the table representing this class of cells, if it contains the given cell, or null if not applicable.
  267. * Stripe style types return only the stripe range containing the given cell, or null.
  268. */
  269. public CellRangeAddressBase appliesTo(Table table, CellReference cell) {
  270. if (table == null || cell == null) return null;
  271. if ( ! cell.getSheetName().equals(table.getSheetName())) return null;
  272. if ( ! table.contains(cell)) return null;
  273. final CellRangeAddressBase range = getRange(table, cell);
  274. if (range != null && range.isInRange(cell.getRow(), cell.getCol())) return range;
  275. // else
  276. return null;
  277. }
  278. /**
  279. * Calls {@link #getRange(Table, CellReference)}. Use that instead for performance.
  280. * @param table
  281. * @param cell
  282. * @return default is unimplemented/null
  283. * @see #getRange(Table, CellReference)
  284. */
  285. public final CellRangeAddressBase getRange(Table table, Cell cell) {
  286. if (cell == null) return null;
  287. return getRange(table, new CellReference(cell.getSheet().getSheetName(), cell.getRowIndex(), cell.getColumnIndex(), true, true));
  288. }
  289. /**
  290. *
  291. * @param table
  292. * @param cell
  293. * @return default is unimplemented/null
  294. */
  295. public CellRangeAddressBase getRange(Table table, CellReference cell) {
  296. return null;
  297. }
  298. }