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.

BorderPropertyTemplate.java 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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.util;
  16. import java.util.Collections;
  17. import java.util.HashMap;
  18. import java.util.HashSet;
  19. import java.util.Map;
  20. import java.util.Map.Entry;
  21. import java.util.Set;
  22. import org.apache.poi.ss.SpreadsheetVersion;
  23. import org.apache.poi.ss.usermodel.BorderStyle;
  24. import org.apache.poi.ss.usermodel.Cell;
  25. import org.apache.poi.ss.usermodel.IndexedColors;
  26. import org.apache.poi.ss.usermodel.Row;
  27. import org.apache.poi.ss.usermodel.Sheet;
  28. import org.apache.poi.util.Beta;
  29. /**
  30. * <p>
  31. * A BorderPropertyTemplate is a template that can be applied to any sheet in
  32. * a project. It contains all the border type and color attributes needed to
  33. * draw all the borders for a single sheet. That template can be applied to any
  34. * sheet in any workbook.
  35. *
  36. * <p>
  37. * This class requires the full spreadsheet to be in memory so
  38. * {@link org.apache.poi.xssf.streaming.SXSSFWorkbook} spreadsheets may not work. The same
  39. * BorderPropertyTemplate can be reused on different types of workbooks, such as
  40. * {@link org.apache.poi.hssf.usermodel.HSSFWorkbook} and
  41. * {@link org.apache.poi.xssf.usermodel.XSSFWorkbook} instances.
  42. * Portions of the border that fall outside the max range of the
  43. * sheet are ignored.
  44. * </p>
  45. *
  46. * <p>
  47. * This may be merged with {@link RegionUtil} in the future.
  48. * </p>
  49. * @since 3.15 beta 2
  50. */
  51. @Beta
  52. public final class BorderPropertyTemplate {
  53. /**
  54. * Provides various extents of the properties being added to the template
  55. * Note that the Border BorderExtent differs from a BorderStyle. A BorderStyle
  56. * refers to the border around a single cell while a BorderExtent refers to
  57. * borders around and through an area of cells.
  58. * @since 3.15 beta 2
  59. */
  60. public enum BorderExtent {
  61. /**
  62. * No properties defined. This can be used to remove existing
  63. * properties.
  64. */
  65. NONE,
  66. /**
  67. * All borders, that is top, bottom, left and right, including interior
  68. * borders for the range. Does not include diagonals which are different
  69. * and not implemented here.
  70. */
  71. ALL,
  72. /**
  73. * All inside borders. This is top, bottom, left, and right borders, but
  74. * restricted to the interior borders for the range. For a range of one
  75. * cell, this will produce no borders.
  76. */
  77. INSIDE,
  78. /**
  79. * All outside borders. That is top, bottom, left and right borders that
  80. * bound the range only.
  81. */
  82. OUTSIDE,
  83. /**
  84. * This is just the top border for the range. No interior borders will
  85. * be produced.
  86. */
  87. TOP,
  88. /**
  89. * This is just the bottom border for the range. No interior borders
  90. * will be produced.
  91. */
  92. BOTTOM,
  93. /**
  94. * This is just the left border for the range, no interior borders will
  95. * be produced.
  96. */
  97. LEFT,
  98. /**
  99. * This is just the right border for the range, no interior borders will
  100. * be produced.
  101. */
  102. RIGHT,
  103. /**
  104. * This is all horizontal borders for the range, including interior and
  105. * outside borders.
  106. */
  107. HORIZONTAL,
  108. /**
  109. * This is just the interior horizontal borders for the range.
  110. */
  111. INSIDE_HORIZONTAL,
  112. /**
  113. * This is just the outside horizontal borders for the range.
  114. */
  115. OUTSIDE_HORIZONTAL,
  116. /**
  117. * This is all vertical borders for the range, including interior and
  118. * outside borders.
  119. */
  120. VERTICAL,
  121. /**
  122. * This is just the interior vertical borders for the range.
  123. */
  124. INSIDE_VERTICAL,
  125. /**
  126. * This is just the outside vertical borders for the range.
  127. */
  128. OUTSIDE_VERTICAL
  129. }
  130. /**
  131. * A set of the border color property names
  132. */
  133. private static final Set<String> BORDER_COLOR_PROPERTY_NAMES;
  134. static {
  135. Set<String> properties = new HashSet<String>();
  136. properties.add(CellUtil.TOP_BORDER_COLOR);
  137. properties.add(CellUtil.BOTTOM_BORDER_COLOR);
  138. properties.add(CellUtil.LEFT_BORDER_COLOR);
  139. properties.add(CellUtil.RIGHT_BORDER_COLOR);
  140. BORDER_COLOR_PROPERTY_NAMES = Collections.unmodifiableSet(properties);
  141. }
  142. /**
  143. * A set of the border direction property names
  144. */
  145. private static final Set<String> BORDER_DIRECTION_PROPERTY_NAMES;
  146. static {
  147. Set<String> properties = new HashSet<String>();
  148. properties.add(CellUtil.BORDER_TOP);
  149. properties.add(CellUtil.BORDER_BOTTOM);
  150. properties.add(CellUtil.BORDER_LEFT);
  151. properties.add(CellUtil.BORDER_RIGHT);
  152. BORDER_DIRECTION_PROPERTY_NAMES = Collections.unmodifiableSet(properties);
  153. }
  154. // this must be greater than or equal to the workbooks used in #applyBorders
  155. private static final SpreadsheetVersion _ss = SpreadsheetVersion.EXCEL2007;
  156. /**
  157. * This is a list of cell properties for one shot application to a range of
  158. * cells at a later time.
  159. *
  160. * Data types:
  161. * _propertyTemplate as {A1: property map}
  162. * property map as {property key : property value}
  163. * property key as String as (border direction as CellUtil.BORDER_*) or (border color as CellUtil.*_BORDER_COLOR)
  164. * property value as (border line style as BorderStyle) or (color as short)
  165. */
  166. private final Map<CellAddress, Map<String, Object>> _propertyTemplate;
  167. /**
  168. * Create a new Border Property Template to stage border formatting changes to a spreadsheet
  169. * @since 3.15 beta 2
  170. */
  171. public BorderPropertyTemplate() {
  172. _propertyTemplate = new HashMap<CellAddress, Map<String, Object>>();
  173. }
  174. /**
  175. * Copy constructor
  176. *
  177. * Create a template from an existing template.
  178. * Changes made to either template do not affect the other template.
  179. * @since 3.15 beta 2
  180. */
  181. public BorderPropertyTemplate(BorderPropertyTemplate prototype) {
  182. this();
  183. // deep copy the _propertyTemplate map from prototype
  184. for (Entry<CellAddress, Map<String, Object>> other : prototype._propertyTemplate.entrySet()) {
  185. CellAddress cell = other.getKey();
  186. Map<String, Object> otherMap = other.getValue();
  187. // The keys in otherMap are immutable Strings
  188. // The values in otherMap are immutable Shorts or BorderStyles
  189. // Therefore, this map's data cannot be modified through protoype
  190. Map<String, Object> map = new HashMap<String, Object>(otherMap);
  191. // CellAddress is an immutable class, therefore it is ok to reference the same instance
  192. _propertyTemplate.put(cell, map);
  193. }
  194. }
  195. /**
  196. * Add a group of cell borders for a cell range to the border property template.
  197. * The borders are not applied to the cells at this time, just the template is drawn
  198. * (<code>drawBorders</code> stages changes using the border property template).
  199. * To apply the drawn borders to a sheet, use {@link #applyBorders}.
  200. *
  201. * @param range range of cells on which borders are drawn.
  202. * @param borderType Type of border to draw.
  203. * @param extent BorderExtent of the borders to be applied.
  204. * @since 3.15 beta 2
  205. */
  206. public void drawBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) {
  207. switch (extent) {
  208. case NONE:
  209. removeBorders(range);
  210. break;
  211. case ALL:
  212. drawHorizontalBorders(range, borderType, BorderExtent.ALL);
  213. drawVerticalBorders(range, borderType, BorderExtent.ALL);
  214. break;
  215. case INSIDE:
  216. drawHorizontalBorders(range, borderType, BorderExtent.INSIDE);
  217. drawVerticalBorders(range, borderType, BorderExtent.INSIDE);
  218. break;
  219. case OUTSIDE:
  220. drawOutsideBorders(range, borderType, BorderExtent.ALL);
  221. break;
  222. case TOP:
  223. drawTopBorder(range, borderType);
  224. break;
  225. case BOTTOM:
  226. drawBottomBorder(range, borderType);
  227. break;
  228. case LEFT:
  229. drawLeftBorder(range, borderType);
  230. break;
  231. case RIGHT:
  232. drawRightBorder(range, borderType);
  233. break;
  234. case HORIZONTAL:
  235. drawHorizontalBorders(range, borderType, BorderExtent.ALL);
  236. break;
  237. case INSIDE_HORIZONTAL:
  238. drawHorizontalBorders(range, borderType, BorderExtent.INSIDE);
  239. break;
  240. case OUTSIDE_HORIZONTAL:
  241. drawOutsideBorders(range, borderType, BorderExtent.HORIZONTAL);
  242. break;
  243. case VERTICAL:
  244. drawVerticalBorders(range, borderType, BorderExtent.ALL);
  245. break;
  246. case INSIDE_VERTICAL:
  247. drawVerticalBorders(range, borderType, BorderExtent.INSIDE);
  248. break;
  249. case OUTSIDE_VERTICAL:
  250. drawOutsideBorders(range, borderType, BorderExtent.VERTICAL);
  251. break;
  252. }
  253. }
  254. /**
  255. * Add a group of cell borders and colors for a cell range to the border property template.
  256. * The borders are not applied to the cells at this time, just the template is drawn
  257. * (<code>drawBorders</code> stages changes using the border property template).
  258. * To apply the drawn borders to a sheet, use {@link #applyBorders}.
  259. *
  260. * @param range range of cells on which borders are drawn.
  261. * @param borderType Type of border to draw.
  262. * @param color Color index from {@link IndexedColors} used to draw the borders.
  263. * @param extent BorderExtent of the borders to be applied.
  264. * @since 3.15 beta 2
  265. */
  266. public void drawColoredBorders(CellRangeAddress range, BorderStyle borderType, short color, BorderExtent extent) {
  267. drawBorders(range, borderType, extent);
  268. if (borderType != BorderStyle.NONE) {
  269. drawBorderColors(range, color, extent);
  270. }
  271. }
  272. /**
  273. * <p>
  274. * Draws the top border for a range of cells
  275. * </p>
  276. *
  277. * @param range range of cells on which borders are drawn.
  278. * @param borderType Type of border to draw.
  279. */
  280. private void drawTopBorder(CellRangeAddress range, BorderStyle borderType) {
  281. int row = range.getFirstRow();
  282. int firstCol = range.getFirstColumn();
  283. int lastCol = range.getLastColumn();
  284. boolean addBottom = (borderType == BorderStyle.NONE && row > 0);
  285. for (int i = firstCol; i <= lastCol; i++) {
  286. addProperty(row, i, CellUtil.BORDER_TOP, borderType);
  287. if (addBottom) {
  288. addProperty(row - 1, i, CellUtil.BORDER_BOTTOM, borderType);
  289. }
  290. }
  291. }
  292. /**
  293. * <p>
  294. * Draws the bottom border for a range of cells
  295. * </p>
  296. *
  297. * @param range range of cells on which borders are drawn.
  298. * @param borderType Type of border to draw.
  299. */
  300. private void drawBottomBorder(CellRangeAddress range, BorderStyle borderType) {
  301. int row = range.getLastRow();
  302. int firstCol = range.getFirstColumn();
  303. int lastCol = range.getLastColumn();
  304. boolean addTop = (borderType == BorderStyle.NONE && row < _ss.getLastRowIndex());
  305. for (int i = firstCol; i <= lastCol; i++) {
  306. addProperty(row, i, CellUtil.BORDER_BOTTOM, borderType);
  307. if (addTop) {
  308. addProperty(row + 1, i, CellUtil.BORDER_TOP, borderType);
  309. }
  310. }
  311. }
  312. /**
  313. * <p>
  314. * Draws the left border for a range of cells
  315. * </p>
  316. *
  317. * @param range range of cells on which borders are drawn.
  318. * @param borderType Type of border to draw.
  319. */
  320. private void drawLeftBorder(CellRangeAddress range, BorderStyle borderType) {
  321. int firstRow = range.getFirstRow();
  322. int lastRow = range.getLastRow();
  323. int col = range.getFirstColumn();
  324. boolean addRight = (borderType == BorderStyle.NONE && col > 0);
  325. for (int i = firstRow; i <= lastRow; i++) {
  326. addProperty(i, col, CellUtil.BORDER_LEFT, borderType);
  327. if (addRight) {
  328. addProperty(i, col - 1, CellUtil.BORDER_RIGHT, borderType);
  329. }
  330. }
  331. }
  332. /**
  333. * <p>
  334. * Draws the right border for a range of cells
  335. * </p>
  336. *
  337. * @param range range of cells on which borders are drawn.
  338. * @param borderType Type of border to draw.
  339. */
  340. private void drawRightBorder(CellRangeAddress range, BorderStyle borderType) {
  341. int firstRow = range.getFirstRow();
  342. int lastRow = range.getLastRow();
  343. int col = range.getLastColumn();
  344. boolean addLeft = (borderType == BorderStyle.NONE && col < _ss.getLastColumnIndex());
  345. for (int i = firstRow; i <= lastRow; i++) {
  346. addProperty(i, col, CellUtil.BORDER_RIGHT, borderType);
  347. if (addLeft) {
  348. addProperty(i, col + 1, CellUtil.BORDER_LEFT, borderType);
  349. }
  350. }
  351. }
  352. /**
  353. * <p>
  354. * Draws the outside borders for a range of cells.
  355. * </p>
  356. *
  357. * @param range range of cells on which borders are drawn.
  358. * @param borderType Type of border to draw.
  359. * @param extent BorderExtent of the borders to be applied. Valid Values are:
  360. * <ul>
  361. * <li>Extent.ALL</li>
  362. * <li>Extent.HORIZONTAL</li>
  363. * <li>Extent.VERTICAL</li>
  364. * </ul>
  365. */
  366. private void drawOutsideBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) {
  367. switch (extent) {
  368. case ALL:
  369. drawTopBorder(range, borderType);
  370. drawBottomBorder(range, borderType);
  371. drawLeftBorder(range, borderType);
  372. drawRightBorder(range, borderType);
  373. break;
  374. case HORIZONTAL:
  375. drawTopBorder(range, borderType);
  376. drawBottomBorder(range, borderType);
  377. break;
  378. case VERTICAL:
  379. drawLeftBorder(range, borderType);
  380. drawRightBorder(range, borderType);
  381. break;
  382. default:
  383. throw new IllegalArgumentException(
  384. "Illegal BorderExtent. Allowed: ALL, HORIZONTAL, and VERTICAL");
  385. }
  386. }
  387. /**
  388. * <p>
  389. * Draws the horizontal borders for a range of cells.
  390. * </p>
  391. *
  392. * @param range range of cells on which borders are drawn.
  393. * @param borderType Type of border to draw.
  394. * @param extent BorderExtent of the borders to be applied. Valid Values are:
  395. * <ul>
  396. * <li>Extent.ALL</li>
  397. * <li>Extent.INSIDE</li>
  398. * </ul>
  399. */
  400. private void drawHorizontalBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) {
  401. int firstRow = range.getFirstRow();
  402. int lastRow = range.getLastRow();
  403. int firstCol = range.getFirstColumn();
  404. int lastCol = range.getLastColumn();
  405. switch (extent) {
  406. case ALL:
  407. for (int i = firstRow; i <= lastRow; i++) {
  408. CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
  409. drawTopBorder(row, borderType);
  410. drawBottomBorder(row, borderType);
  411. }
  412. break;
  413. case INSIDE:
  414. for (int i = firstRow; i <= lastRow; i++) {
  415. CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
  416. if (i > firstRow) drawTopBorder(row, borderType);
  417. if (i < lastRow) drawBottomBorder(row, borderType);
  418. }
  419. break;
  420. default:
  421. throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE");
  422. }
  423. }
  424. /**
  425. * <p>
  426. * Draws the vertical borders for a range of cells.
  427. * </p>
  428. *
  429. * @param range range of cells on which borders are drawn.
  430. * @param borderType Type of border to draw.
  431. * @param extent BorderExtent of the borders to be applied. Valid Values are:
  432. * <ul>
  433. * <li>Extent.ALL</li>
  434. * <li>Extent.INSIDE</li>
  435. * </ul>
  436. */
  437. private void drawVerticalBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) {
  438. int firstRow = range.getFirstRow();
  439. int lastRow = range.getLastRow();
  440. int firstCol = range.getFirstColumn();
  441. int lastCol = range.getLastColumn();
  442. switch (extent) {
  443. case ALL:
  444. for (int i = firstCol; i <= lastCol; i++) {
  445. CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
  446. drawLeftBorder(row, borderType);
  447. drawRightBorder(row, borderType);
  448. }
  449. break;
  450. case INSIDE:
  451. for (int i = firstCol; i <= lastCol; i++) {
  452. CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
  453. if (i > firstCol) drawLeftBorder(row, borderType);
  454. if (i < lastCol) drawRightBorder(row, borderType);
  455. }
  456. break;
  457. default:
  458. throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE");
  459. }
  460. }
  461. /**
  462. * Removes all border properties from this BorderPropertyTemplate for the
  463. * specified range.
  464. *
  465. * @param range - range of cells to remove borders.
  466. */
  467. private void removeBorders(CellRangeAddress range) {
  468. int firstRow = range.getFirstRow();
  469. int lastRow = range.getLastRow();
  470. int firstCol = range.getFirstColumn();
  471. int lastCol = range.getLastColumn();
  472. for (int row = firstRow; row <= lastRow; row++) {
  473. for (int col = firstCol; col <= lastCol; col++) {
  474. CellAddress cell = new CellAddress(row, col);
  475. removeProperties(cell, BORDER_DIRECTION_PROPERTY_NAMES);
  476. }
  477. }
  478. removeBorderColors(range);
  479. }
  480. /**
  481. * Applies the drawn borders to a Sheet. The borders that are applied are
  482. * the ones that have been drawn by {@link #drawColoredBorders}, {@link #drawBorders} and
  483. * {@link #drawBorderColors}.
  484. * The same border property template can be applied to multiple sheets
  485. * from the same or different workbooks.
  486. *
  487. * If the border property template contains cell addresses outside the valid range
  488. * of <code>sheet</code>, borders for those cell addresses are quietly ignored and
  489. * not applied to <code>sheet</code>.
  490. *
  491. * @param sheet Sheet on which to apply borders
  492. * @since 3.15 beta 2
  493. */
  494. public void applyBorders(Sheet sheet) {
  495. SpreadsheetVersion ss = sheet.getWorkbook().getSpreadsheetVersion();
  496. int lastValidRow = ss.getLastRowIndex();
  497. int lastValidCol = ss.getLastColumnIndex();
  498. for (Entry<CellAddress, Map<String, Object>> entry : _propertyTemplate.entrySet()) {
  499. CellAddress cellAddress = entry.getKey();
  500. int r = cellAddress.getRow();
  501. int c = cellAddress.getColumn();
  502. if (r <= lastValidRow && c <= lastValidCol) {
  503. Row row = CellUtil.getRow(r, sheet);
  504. Cell cell = CellUtil.getCell(row, c);
  505. Map<String, Object> properties = entry.getValue();
  506. CellUtil.setCellStyleProperties(cell, properties);
  507. }
  508. }
  509. }
  510. /**
  511. * Sets the color for a group of cell borders for a cell range to the border property template.
  512. * The borders are not applied to the cells at this time, just the template is drawn
  513. * (<code>drawBorders</code> stages changes using the border property template).
  514. * If the borders do not exist, a {@link BorderStyle#THIN} border is used.
  515. * To apply the drawn borders to a sheet, use {@link #applyBorders}.
  516. *
  517. * @param range range of cells on which colors are set.
  518. * @param color Color index from {@link IndexedColors} used to draw the borders.
  519. * @param extent BorderExtent of the borders for which colors are set.
  520. * @since 3.15 beta 2
  521. */
  522. public void drawBorderColors(CellRangeAddress range, short color, BorderExtent extent) {
  523. switch (extent) {
  524. case NONE:
  525. removeBorderColors(range);
  526. break;
  527. case ALL:
  528. drawHorizontalBorderColors(range, color, BorderExtent.ALL);
  529. drawVerticalBorderColors(range, color, BorderExtent.ALL);
  530. break;
  531. case INSIDE:
  532. drawHorizontalBorderColors(range, color, BorderExtent.INSIDE);
  533. drawVerticalBorderColors(range, color, BorderExtent.INSIDE);
  534. break;
  535. case OUTSIDE:
  536. drawOutsideBorderColors(range, color, BorderExtent.ALL);
  537. break;
  538. case TOP:
  539. drawTopBorderColor(range, color);
  540. break;
  541. case BOTTOM:
  542. drawBottomBorderColor(range, color);
  543. break;
  544. case LEFT:
  545. drawLeftBorderColor(range, color);
  546. break;
  547. case RIGHT:
  548. drawRightBorderColor(range, color);
  549. break;
  550. case HORIZONTAL:
  551. drawHorizontalBorderColors(range, color, BorderExtent.ALL);
  552. break;
  553. case INSIDE_HORIZONTAL:
  554. drawHorizontalBorderColors(range, color, BorderExtent.INSIDE);
  555. break;
  556. case OUTSIDE_HORIZONTAL:
  557. drawOutsideBorderColors(range, color, BorderExtent.HORIZONTAL);
  558. break;
  559. case VERTICAL:
  560. drawVerticalBorderColors(range, color, BorderExtent.ALL);
  561. break;
  562. case INSIDE_VERTICAL:
  563. drawVerticalBorderColors(range, color, BorderExtent.INSIDE);
  564. break;
  565. case OUTSIDE_VERTICAL:
  566. drawOutsideBorderColors(range, color, BorderExtent.VERTICAL);
  567. break;
  568. }
  569. }
  570. /**
  571. * <p>
  572. * Sets the color of the top border for a range of cells.
  573. * </p>
  574. *
  575. * @param range range of cells on which colors are set.
  576. * @param color Color index from {@link IndexedColors} used to draw the borders.
  577. */
  578. private void drawTopBorderColor(CellRangeAddress range, short color) {
  579. int row = range.getFirstRow();
  580. int firstCol = range.getFirstColumn();
  581. int lastCol = range.getLastColumn();
  582. for (int i = firstCol; i <= lastCol; i++) {
  583. CellAddress cell = new CellAddress(row, i);
  584. // if BORDER_TOP is not set on BorderPropertyTemplate, make a thin border so that there's something to color
  585. if (borderIsNotSet(cell, CellUtil.BORDER_TOP)) {
  586. drawTopBorder(new CellRangeAddress(row, row, i, i), BorderStyle.THIN);
  587. }
  588. addProperty(cell, CellUtil.TOP_BORDER_COLOR, color);
  589. }
  590. }
  591. /**
  592. * <p>
  593. * Sets the color of the bottom border for a range of cells.
  594. * </p>
  595. *
  596. * @param range range of cells on which colors are set.
  597. * @param color Color index from {@link IndexedColors} used to draw the borders.
  598. */
  599. private void drawBottomBorderColor(CellRangeAddress range, short color) {
  600. int row = range.getLastRow();
  601. int firstCol = range.getFirstColumn();
  602. int lastCol = range.getLastColumn();
  603. for (int i = firstCol; i <= lastCol; i++) {
  604. CellAddress cell = new CellAddress(row, i);
  605. // if BORDER_BOTTOM is not set on BorderPropertyTemplate, make a thin border so that there's something to color
  606. if (borderIsNotSet(cell, CellUtil.BORDER_BOTTOM)) {
  607. drawBottomBorder(new CellRangeAddress(row, row, i, i), BorderStyle.THIN);
  608. }
  609. addProperty(cell, CellUtil.BOTTOM_BORDER_COLOR, color);
  610. }
  611. }
  612. /**
  613. * <p>
  614. * Sets the color of the left border for a range of cells.
  615. * </p>
  616. *
  617. * @param range range of cells on which colors are set.
  618. * @param color Color index from {@link IndexedColors} used to draw the borders.
  619. */
  620. private void drawLeftBorderColor(CellRangeAddress range, short color) {
  621. int firstRow = range.getFirstRow();
  622. int lastRow = range.getLastRow();
  623. int col = range.getFirstColumn();
  624. for (int i = firstRow; i <= lastRow; i++) {
  625. CellAddress cell = new CellAddress(i, col);
  626. // if BORDER_LEFT is not set on BorderPropertyTemplate, make a thin border so that there's something to color
  627. if (borderIsNotSet(cell, CellUtil.BORDER_LEFT)) {
  628. drawLeftBorder(new CellRangeAddress(i, i, col, col), BorderStyle.THIN);
  629. }
  630. addProperty(cell, CellUtil.LEFT_BORDER_COLOR, color);
  631. }
  632. }
  633. /**
  634. * A helper method to set the border line style before setting the color if the
  635. * current line style is not set.
  636. */
  637. private boolean borderIsNotSet(CellAddress cell, String borderDirection) {
  638. Object borderLineStyle = getTemplateProperty(cell, borderDirection);
  639. return (borderLineStyle == null);
  640. }
  641. /**
  642. * <p>
  643. * Sets the color of the right border for a range of cells. If the border is
  644. * not drawn, it defaults to {@link BorderStyle#THIN}
  645. * </p>
  646. *
  647. * @param range range of cells on which colors are set.
  648. * @param color Color index from {@link IndexedColors} used to draw the borders.
  649. */
  650. private void drawRightBorderColor(CellRangeAddress range, short color) {
  651. int firstRow = range.getFirstRow();
  652. int lastRow = range.getLastRow();
  653. int col = range.getLastColumn();
  654. for (int i = firstRow; i <= lastRow; i++) {
  655. CellAddress cell = new CellAddress(i, col);
  656. // if BORDER_RIGHT is not set on BorderPropertyTemplate, make a thin border so that there's something to color
  657. if (borderIsNotSet(cell, CellUtil.BORDER_RIGHT)) {
  658. drawRightBorder(new CellRangeAddress(i, i, col, col), BorderStyle.THIN);
  659. }
  660. addProperty(cell, CellUtil.RIGHT_BORDER_COLOR, color);
  661. }
  662. }
  663. /**
  664. * <p>
  665. * Sets the color of the outside borders for a range of cells.
  666. * </p>
  667. *
  668. * @param range range of cells on which colors are set.
  669. * @param color Color index from {@link IndexedColors} used to draw the borders.
  670. * @param extent BorderExtent of the borders for which colors are set. Valid Values are:
  671. * <ul>
  672. * <li>Extent.ALL</li>
  673. * <li>Extent.HORIZONTAL</li>
  674. * <li>Extent.VERTICAL</li>
  675. * </ul>
  676. */
  677. private void drawOutsideBorderColors(CellRangeAddress range, short color, BorderExtent extent) {
  678. switch (extent) {
  679. case ALL:
  680. drawTopBorderColor(range, color);
  681. drawBottomBorderColor(range, color);
  682. drawLeftBorderColor(range, color);
  683. drawRightBorderColor(range, color);
  684. break;
  685. case HORIZONTAL:
  686. drawTopBorderColor(range, color);
  687. drawBottomBorderColor(range, color);
  688. break;
  689. case VERTICAL:
  690. drawLeftBorderColor(range, color);
  691. drawRightBorderColor(range, color);
  692. break;
  693. default:
  694. throw new IllegalArgumentException(
  695. "Illegal BorderExtent. Allowed: ALL, HORIZONTAL, and VERTICAL");
  696. }
  697. }
  698. /**
  699. * <p>
  700. * Sets the color of the horizontal borders for a range of cells.
  701. * </p>
  702. *
  703. * @param range range of cells on which colors are set.
  704. * @param color Color index from {@link IndexedColors} used to draw the borders.
  705. * @param extent BorderExtent of the borders for which colors are set. Valid Values are:
  706. * <ul>
  707. * <li>Extent.ALL</li>
  708. * <li>Extent.INSIDE</li>
  709. * </ul>
  710. */
  711. private void drawHorizontalBorderColors(CellRangeAddress range, short color, BorderExtent extent) {
  712. int firstRow = range.getFirstRow();
  713. int lastRow = range.getLastRow();
  714. int firstCol = range.getFirstColumn();
  715. int lastCol = range.getLastColumn();
  716. switch (extent) {
  717. case ALL:
  718. for (int i = firstRow; i <= lastRow; i++) {
  719. CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
  720. drawTopBorderColor(row, color);
  721. drawBottomBorderColor(row, color);
  722. }
  723. break;
  724. case INSIDE:
  725. for (int i = firstRow; i <= lastRow; i++) {
  726. CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
  727. if (i > firstRow) drawTopBorderColor(row, color);
  728. if (i < lastRow) drawBottomBorderColor(row, color);
  729. }
  730. break;
  731. default:
  732. throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE");
  733. }
  734. }
  735. /**
  736. * <p>
  737. * Sets the color of the vertical borders for a range of cells.
  738. * </p>
  739. *
  740. * @param range range of cells on which colors are set.
  741. * @param color Color index from {@link IndexedColors} used to draw the borders.
  742. * @param extent BorderExtent of the borders for which colors are set. Valid Values are:
  743. * <ul>
  744. * <li>Extent.ALL</li>
  745. * <li>Extent.INSIDE</li>
  746. * </ul>
  747. */
  748. private void drawVerticalBorderColors(CellRangeAddress range, short color, BorderExtent extent) {
  749. int firstRow = range.getFirstRow();
  750. int lastRow = range.getLastRow();
  751. int firstCol = range.getFirstColumn();
  752. int lastCol = range.getLastColumn();
  753. switch (extent) {
  754. case ALL:
  755. for (int i = firstCol; i <= lastCol; i++) {
  756. CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
  757. drawLeftBorderColor(row, color);
  758. drawRightBorderColor(row, color);
  759. }
  760. break;
  761. case INSIDE:
  762. for (int i = firstCol; i <= lastCol; i++) {
  763. CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
  764. if (i > firstCol) drawLeftBorderColor(row, color);
  765. if (i < lastCol) drawRightBorderColor(row, color);
  766. }
  767. break;
  768. default:
  769. throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE");
  770. }
  771. }
  772. /**
  773. * Removes all border properties from this BorderPropertyTemplate for the
  774. * specified range.
  775. *
  776. * @param range - range of cells to remove borders.
  777. */
  778. private void removeBorderColors(CellRangeAddress range) {
  779. int firstRow = range.getFirstRow();
  780. int lastRow = range.getLastRow();
  781. int firstColumn = range.getFirstColumn();
  782. int lastColumn = range.getLastColumn();
  783. for (int row = firstRow; row <= lastRow; row++) {
  784. for (int col = firstColumn; col <= lastColumn; col++) {
  785. CellAddress cell = new CellAddress(row, col);
  786. removeProperties(cell, BORDER_COLOR_PROPERTY_NAMES);
  787. }
  788. }
  789. }
  790. /**
  791. * Adds a property to this BorderPropertyTemplate for a given cell
  792. *
  793. * @param cell The cell to add the property to in the property template.
  794. * @param property The property key to add to the property template.
  795. * @param value The property value to add to the property template.
  796. */
  797. private void addProperty(CellAddress cell, String property, Object value) {
  798. Map<String, Object> cellProperties = _propertyTemplate.get(cell);
  799. if (cellProperties == null) {
  800. cellProperties = new HashMap<String, Object>();
  801. }
  802. cellProperties.put(property, value);
  803. _propertyTemplate.put(cell, cellProperties);
  804. }
  805. /**
  806. * Adds a property to this BorderPropertyTemplate for a given cell
  807. *
  808. * @param row The row number of the cell to add the property to
  809. * @param col
  810. * @param property The property key to add to the property template.
  811. * @param value The property value to add to the property template.
  812. */
  813. private void addProperty(int row, int col, String property, Object value) {
  814. CellAddress cell = new CellAddress(row, col);
  815. addProperty(cell, property, value);
  816. }
  817. /**
  818. * Removes a set of properties from this BorderPropertyTemplate for a
  819. * given cell
  820. *
  821. * @param row the row index of the cell to remove properties from
  822. * @param col the column index of the cell to remove properties from
  823. * @param properties a list of the property names to remove from the cell
  824. */
  825. private void removeProperties(CellAddress cell, Set<String> properties) {
  826. Map<String, Object> cellProperties = _propertyTemplate.get(cell);
  827. if (cellProperties != null) {
  828. cellProperties.keySet().removeAll(properties);
  829. if (cellProperties.isEmpty()) {
  830. _propertyTemplate.remove(cell);
  831. } else {
  832. _propertyTemplate.put(cell, cellProperties);
  833. }
  834. }
  835. }
  836. /**
  837. * Retrieves the number of borders assigned to a cell (a value between 0 and 4)
  838. *
  839. * @param cell the cell to count the number of borders on
  840. * @since 3.15 beta 2
  841. */
  842. public int getNumBorders(CellAddress cell) {
  843. Map<String, Object> cellProperties = _propertyTemplate.get(cell);
  844. if (cellProperties == null) {
  845. return 0;
  846. }
  847. int count = 0;
  848. if (cellProperties.containsKey(CellUtil.BORDER_TOP)) count++;
  849. if (cellProperties.containsKey(CellUtil.BORDER_LEFT)) count++;
  850. if (cellProperties.containsKey(CellUtil.BORDER_RIGHT)) count++;
  851. if (cellProperties.containsKey(CellUtil.BORDER_BOTTOM)) count++;
  852. return count;
  853. }
  854. /**
  855. * Retrieves the number of border colors assigned to a cell
  856. *
  857. * @param cell
  858. * @since 3.15 beta 2
  859. */
  860. public int getNumBorderColors(CellAddress cell) {
  861. Map<String, Object> cellProperties = _propertyTemplate.get(cell);
  862. if (cellProperties == null) {
  863. return 0;
  864. }
  865. int count = 0;
  866. if (cellProperties.containsKey(CellUtil.TOP_BORDER_COLOR)) count++;
  867. if (cellProperties.containsKey(CellUtil.LEFT_BORDER_COLOR)) count++;
  868. if (cellProperties.containsKey(CellUtil.RIGHT_BORDER_COLOR)) count++;
  869. if (cellProperties.containsKey(CellUtil.BOTTOM_BORDER_COLOR)) count++;
  870. return count;
  871. }
  872. /**
  873. * Retrieves the border style for a given cell
  874. *
  875. * @param cell The cell to get a template property from.
  876. * @param property The template property to get from the BorderPropertyTemplate. Example: {@link CellUtil#BORDER_TOP}.
  877. * @return The stored template property. If property has not be set on the BorderPropertyTemplate, returns <code>null</code>.
  878. * @since 3.15 beta 2
  879. */
  880. public Object getTemplateProperty(CellAddress cell, String property) {
  881. Map<String, Object> cellProperties = _propertyTemplate.get(cell);
  882. if (cellProperties != null) {
  883. return cellProperties.get(property);
  884. }
  885. return null;
  886. }
  887. }