import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.ss.util.PropertyTemplate;
-import org.apache.poi.ss.util.PropertyTemplate.Extent;
+import org.apache.poi.ss.util.BorderPropertyTemplate;
+import org.apache.poi.ss.util.BorderPropertyTemplate.BorderExtent;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
c.setCellValue("Colored Borders");
// draw borders (three 3x3 grids)
- PropertyTemplate pt = new PropertyTemplate();
+ BorderPropertyTemplate pt = new BorderPropertyTemplate();
// #1) these borders will all be medium in default color
- pt.drawBorders(new CellRangeAddress(1, 3, 1, 3), BorderStyle.MEDIUM, Extent.ALL);
+ pt.drawBorders(new CellRangeAddress(1, 3, 1, 3), BorderStyle.MEDIUM, BorderExtent.ALL);
// #2) these cells will have medium outside borders and thin inside borders
- pt.drawBorders(new CellRangeAddress(5, 7, 1, 3), BorderStyle.MEDIUM, Extent.OUTSIDE);
- pt.drawBorders(new CellRangeAddress(5, 7, 1, 3), BorderStyle.THIN, Extent.INSIDE);
+ pt.drawBorders(new CellRangeAddress(5, 7, 1, 3), BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
+ pt.drawBorders(new CellRangeAddress(5, 7, 1, 3), BorderStyle.THIN, BorderExtent.INSIDE);
// #3) these cells will all be medium weight with different colors for the
// outside, inside horizontal, and inside vertical borders. The center
// cell will have no borders.
- pt.drawBorders(new CellRangeAddress(9, 11, 1, 3), BorderStyle.MEDIUM, IndexedColors.RED.getIndex(), Extent.OUTSIDE);
- pt.drawBorders(new CellRangeAddress(9, 11, 1, 3), BorderStyle.MEDIUM, IndexedColors.BLUE.getIndex(), Extent.INSIDE_VERTICAL);
- pt.drawBorders(new CellRangeAddress(9, 11, 1, 3), BorderStyle.MEDIUM, IndexedColors.GREEN.getIndex(), Extent.INSIDE_HORIZONTAL);
- pt.drawBorders(new CellRangeAddress(10, 10, 2, 2), BorderStyle.NONE, Extent.ALL);
+ pt.drawBorders(new CellRangeAddress(9, 11, 1, 3), BorderStyle.MEDIUM, IndexedColors.RED.getIndex(), BorderExtent.OUTSIDE);
+ pt.drawBorders(new CellRangeAddress(9, 11, 1, 3), BorderStyle.MEDIUM, IndexedColors.BLUE.getIndex(), BorderExtent.INSIDE_VERTICAL);
+ pt.drawBorders(new CellRangeAddress(9, 11, 1, 3), BorderStyle.MEDIUM, IndexedColors.GREEN.getIndex(), BorderExtent.INSIDE_HORIZONTAL);
+ pt.drawBorders(new CellRangeAddress(10, 10, 2, 2), BorderStyle.NONE, BorderExtent.ALL);
// apply borders to sheet
pt.applyBorders(sh1);
--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.util;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.SpreadsheetVersion;
+import org.apache.poi.ss.usermodel.BorderStyle;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.util.Beta;
+
+/**
+ * <p>
+ * A BorderPropertyTemplate is a template that can be applied to any sheet in
+ * a project. It contains all the border type and color attributes needed to
+ * draw all the borders for a single sheet. That template can be applied to any
+ * sheet in any workbook.
+ *
+ * This class requires the full spreadsheet to be in memory so
+ * {@link SWorkbook} Spreadsheets are not supported. The same
+ * BorderPropertyTemplate can, however, be applied to both
+ * {@link org.apache.poi.hssf.usermodel.HSSFWorkbook}, and Workbook objects
+ * if necessary. Portions of the border that fall outside the max range of the
+ * {@link HSSFWorkbook} sheet are ignored.
+ * </p>
+ *
+ * <p>
+ * This may be merged with {@link RegionUtil} in the future.
+ * </p>
+ */
+@Beta
+public final class BorderPropertyTemplate {
+
+ /**
+ * Provides various extents of the properties being added to the template
+ * Note that the Border BorderExtent differs from a BorderStyle. A BorderStyle
+ * refers to the border around a single cell while a BorderExtent refers to
+ * borders around and through an area of cells.
+ */
+ public enum BorderExtent {
+ /**
+ * No properties defined. This can be used to remove existing
+ * properties.
+ */
+ NONE,
+
+ /**
+ * All borders, that is top, bottom, left and right, including interior
+ * borders for the range. Does not include diagonals which are different
+ * and not implemented here.
+ */
+ ALL,
+
+ /**
+ * All inside borders. This is top, bottom, left, and right borders, but
+ * restricted to the interior borders for the range. For a range of one
+ * cell, this will produce no borders.
+ */
+ INSIDE,
+
+ /**
+ * All outside borders. That is top, bottom, left and right borders that
+ * bound the range only.
+ */
+ OUTSIDE,
+
+ /**
+ * This is just the top border for the range. No interior borders will
+ * be produced.
+ */
+ TOP,
+
+ /**
+ * This is just the bottom border for the range. No interior borders
+ * will be produced.
+ */
+ BOTTOM,
+
+ /**
+ * This is just the left border for the range, no interior borders will
+ * be produced.
+ */
+ LEFT,
+
+ /**
+ * This is just the right border for the range, no interior borders will
+ * be produced.
+ */
+ RIGHT,
+
+ /**
+ * This is all horizontal borders for the range, including interior and
+ * outside borders.
+ */
+ HORIZONTAL,
+
+ /**
+ * This is just the interior horizontal borders for the range.
+ */
+ INSIDE_HORIZONTAL,
+
+ /**
+ * This is just the outside horizontal borders for the range.
+ */
+ OUTSIDE_HORIZONTAL,
+
+ /**
+ * This is all vertical borders for the range, including interior and
+ * outside borders.
+ */
+ VERTICAL,
+
+ /**
+ * This is just the interior vertical borders for the range.
+ */
+ INSIDE_VERTICAL,
+
+ /**
+ * This is just the outside vertical borders for the range.
+ */
+ OUTSIDE_VERTICAL
+ }
+
+ /**
+ * A set of the border color property names
+ */
+ private static final Set<String> BORDER_COLOR_PROPERTY_NAMES;
+ static {
+ Set<String> properties = new HashSet<String>();
+ properties.add(CellUtil.TOP_BORDER_COLOR);
+ properties.add(CellUtil.BOTTOM_BORDER_COLOR);
+ properties.add(CellUtil.LEFT_BORDER_COLOR);
+ properties.add(CellUtil.RIGHT_BORDER_COLOR);
+ BORDER_COLOR_PROPERTY_NAMES = Collections.unmodifiableSet(properties);
+ }
+
+ /**
+ * A set of the border direction property names
+ */
+ private static final Set<String> BORDER_DIRECTION_PROPERTY_NAMES;
+ static {
+ Set<String> properties = new HashSet<String>();
+ properties.add(CellUtil.BORDER_TOP);
+ properties.add(CellUtil.BORDER_BOTTOM);
+ properties.add(CellUtil.BORDER_LEFT);
+ properties.add(CellUtil.BORDER_RIGHT);
+ BORDER_DIRECTION_PROPERTY_NAMES = Collections.unmodifiableSet(properties);
+ }
+
+ // this must be greater than or equal to the workbooks used in #applyBorders
+ private static final SpreadsheetVersion _ss = SpreadsheetVersion.EXCEL2007;
+
+ /**
+ * This is a list of cell properties for one shot application to a range of
+ * cells at a later time.
+ */
+ private final Map<CellAddress, Map<String, Object>> _propertyTemplate;
+
+ /**
+ *
+ */
+ public BorderPropertyTemplate() {
+ _propertyTemplate = new HashMap<CellAddress, Map<String, Object>>();
+ }
+
+ /**
+ * Draws a group of cell borders for a cell range. The borders are not
+ * applied to the cells at this time, just the template is drawn. To apply
+ * the drawn borders to a sheet, use {@link #applyBorders}.
+ *
+ * @param range range of cells on which borders are drawn.
+ * @param borderType Type of border to draw.
+ * @param extent BorderExtent of the borders to be applied.
+ */
+ public void drawBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) {
+ switch (extent) {
+ case NONE:
+ removeBorders(range);
+ break;
+ case ALL:
+ drawHorizontalBorders(range, borderType, BorderExtent.ALL);
+ drawVerticalBorders(range, borderType, BorderExtent.ALL);
+ break;
+ case INSIDE:
+ drawHorizontalBorders(range, borderType, BorderExtent.INSIDE);
+ drawVerticalBorders(range, borderType, BorderExtent.INSIDE);
+ break;
+ case OUTSIDE:
+ drawOutsideBorders(range, borderType, BorderExtent.ALL);
+ break;
+ case TOP:
+ drawTopBorder(range, borderType);
+ break;
+ case BOTTOM:
+ drawBottomBorder(range, borderType);
+ break;
+ case LEFT:
+ drawLeftBorder(range, borderType);
+ break;
+ case RIGHT:
+ drawRightBorder(range, borderType);
+ break;
+ case HORIZONTAL:
+ drawHorizontalBorders(range, borderType, BorderExtent.ALL);
+ break;
+ case INSIDE_HORIZONTAL:
+ drawHorizontalBorders(range, borderType, BorderExtent.INSIDE);
+ break;
+ case OUTSIDE_HORIZONTAL:
+ drawOutsideBorders(range, borderType, BorderExtent.HORIZONTAL);
+ break;
+ case VERTICAL:
+ drawVerticalBorders(range, borderType, BorderExtent.ALL);
+ break;
+ case INSIDE_VERTICAL:
+ drawVerticalBorders(range, borderType, BorderExtent.INSIDE);
+ break;
+ case OUTSIDE_VERTICAL:
+ drawOutsideBorders(range, borderType, BorderExtent.VERTICAL);
+ break;
+ }
+ }
+
+ /**
+ * Draws a group of cell borders for a cell range. The borders are not
+ * applied to the cells at this time, just the template is drawn. To apply
+ * the drawn borders to a sheet, use {@link #applyBorders}.
+ *
+ * @param range range of cells on which borders are drawn.
+ * @param borderType Type of border to draw.
+ * @param color Color index from {@link IndexedColors} used to draw the borders.
+ * @param extent BorderExtent of the borders to be applied.
+ */
+ public void drawBorders(CellRangeAddress range, BorderStyle borderType, short color, BorderExtent extent) {
+ drawBorders(range, borderType, extent);
+ if (borderType != BorderStyle.NONE) {
+ drawBorderColors(range, color, extent);
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the top border for a range of cells
+ * </p>
+ *
+ * @param range range of cells on which borders are drawn.
+ * @param borderType Type of border to draw.
+ */
+ private void drawTopBorder(CellRangeAddress range, BorderStyle borderType) {
+ int row = range.getFirstRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ boolean addBottom = (borderType == BorderStyle.NONE && row > 0);
+ for (int i = firstCol; i <= lastCol; i++) {
+ addProperty(row, i, CellUtil.BORDER_TOP, borderType);
+ if (addBottom) {
+ addProperty(row - 1, i, CellUtil.BORDER_BOTTOM, borderType);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the bottom border for a range of cells
+ * </p>
+ *
+ * @param range range of cells on which borders are drawn.
+ * @param borderType Type of border to draw.
+ */
+ private void drawBottomBorder(CellRangeAddress range, BorderStyle borderType) {
+ int row = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ boolean addTop = (borderType == BorderStyle.NONE && row < _ss.getLastRowIndex());
+ for (int i = firstCol; i <= lastCol; i++) {
+ addProperty(row, i, CellUtil.BORDER_BOTTOM, borderType);
+ if (addTop) {
+ addProperty(row + 1, i, CellUtil.BORDER_TOP, borderType);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the left border for a range of cells
+ * </p>
+ *
+ * @param range range of cells on which borders are drawn.
+ * @param borderType Type of border to draw.
+ */
+ private void drawLeftBorder(CellRangeAddress range, BorderStyle borderType) {
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int col = range.getFirstColumn();
+ boolean addRight = (borderType == BorderStyle.NONE && col > 0);
+ for (int i = firstRow; i <= lastRow; i++) {
+ addProperty(i, col, CellUtil.BORDER_LEFT, borderType);
+ if (addRight) {
+ addProperty(i, col - 1, CellUtil.BORDER_RIGHT, borderType);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the right border for a range of cells
+ * </p>
+ *
+ * @param range range of cells on which borders are drawn.
+ * @param borderType Type of border to draw.
+ */
+ private void drawRightBorder(CellRangeAddress range, BorderStyle borderType) {
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int col = range.getLastColumn();
+ boolean addLeft = (borderType == BorderStyle.NONE && col < _ss.getLastColumnIndex());
+ for (int i = firstRow; i <= lastRow; i++) {
+ addProperty(i, col, CellUtil.BORDER_RIGHT, borderType);
+ if (addLeft) {
+ addProperty(i, col + 1, CellUtil.BORDER_LEFT, borderType);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the outside borders for a range of cells.
+ * </p>
+ *
+ * @param range range of cells on which borders are drawn.
+ * @param borderType Type of border to draw.
+ * @param extent BorderExtent of the borders to be applied. Valid Values are:
+ * <ul>
+ * <li>Extent.ALL</li>
+ * <li>Extent.HORIZONTAL</li>
+ * <li>Extent.VERTICAL</li>
+ * </ul>
+ */
+ private void drawOutsideBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) {
+ switch (extent) {
+ case ALL:
+ case HORIZONTAL:
+ case VERTICAL:
+ if (extent == BorderExtent.ALL || extent == BorderExtent.HORIZONTAL) {
+ drawTopBorder(range, borderType);
+ drawBottomBorder(range, borderType);
+ }
+ if (extent == BorderExtent.ALL || extent == BorderExtent.VERTICAL) {
+ drawLeftBorder(range, borderType);
+ drawRightBorder(range, borderType);
+ }
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "Illegal BorderExtent. Allowed: ALL, HORIZONTAL, and VERTICAL");
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the horizontal borders for a range of cells.
+ * </p>
+ *
+ * @param range range of cells on which borders are drawn.
+ * @param borderType Type of border to draw.
+ * @param extent BorderExtent of the borders to be applied. Valid Values are:
+ * <ul>
+ * <li>Extent.ALL</li>
+ * <li>Extent.INSIDE</li>
+ * </ul>
+ */
+ private void drawHorizontalBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) {
+ switch (extent) {
+ case ALL:
+ case INSIDE:
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstRow; i <= lastRow; i++) {
+ CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
+ if (extent == BorderExtent.ALL || i > firstRow) {
+ drawTopBorder(row, borderType);
+ }
+ if (extent == BorderExtent.ALL || i < lastRow) {
+ drawBottomBorder(row, borderType);
+ }
+ }
+ break;
+ default:
+ throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE");
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the vertical borders for a range of cells.
+ * </p>
+ *
+ * @param range range of cells on which borders are drawn.
+ * @param borderType Type of border to draw.
+ * @param extent BorderExtent of the borders to be applied. Valid Values are:
+ * <ul>
+ * <li>Extent.ALL</li>
+ * <li>Extent.INSIDE</li>
+ * </ul>
+ */
+ private void drawVerticalBorders(CellRangeAddress range, BorderStyle borderType, BorderExtent extent) {
+ switch (extent) {
+ case ALL:
+ case INSIDE:
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstCol; i <= lastCol; i++) {
+ CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
+ if (extent == BorderExtent.ALL || i > firstCol) {
+ drawLeftBorder(row, borderType);
+ }
+ if (extent == BorderExtent.ALL || i < lastCol) {
+ drawRightBorder(row, borderType);
+ }
+ }
+ break;
+ default:
+ throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE");
+ }
+ }
+
+ /**
+ * Removes all border properties from this BorderPropertyTemplate for the
+ * specified range.
+ *
+ * @param range - range of cells to remove borders.
+ */
+ private void removeBorders(CellRangeAddress range) {
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int row = firstRow; row <= lastRow; row++) {
+ for (int col = firstCol; col <= lastCol; col++) {
+ removeProperties(row, col, BORDER_DIRECTION_PROPERTY_NAMES);
+ }
+ }
+ removeBorderColors(range);
+ }
+
+ /**
+ * Applies the drawn borders to a Sheet. The borders that are applied are
+ * the ones that have been drawn by the {@link #drawBorders} and
+ * {@link #drawBorderColors} methods.
+ *
+ * @param sheet Sheet on which to apply borders
+ */
+ public void applyBorders(Sheet sheet) {
+ SpreadsheetVersion ss = sheet.getWorkbook().getSpreadsheetVersion();
+ int lastValidRow = ss.getLastRowIndex();
+ int lastValidCol = ss.getLastColumnIndex();
+ for (Entry<CellAddress, Map<String, Object>> entry : _propertyTemplate.entrySet()) {
+ CellAddress cellAddress = entry.getKey();
+ int r = cellAddress.getRow();
+ int c = cellAddress.getColumn();
+ if (r <= lastValidRow && c <= lastValidCol) {
+ Map<String, Object> properties = entry.getValue();
+ Row row = CellUtil.getRow(r, sheet);
+ Cell cell = CellUtil.getCell(row, c);
+ CellUtil.setCellStyleProperties(cell, properties);
+ }
+ }
+ }
+
+ /**
+ * Sets the color for a group of cell borders for a cell range. The borders
+ * are not applied to the cells at this time, just the template is drawn. If
+ * the borders do not exist, a {@link BorderStyle#THIN} border is used. To apply the
+ * drawn borders to a sheet, use {@link #applyBorders}.
+ *
+ * @param range range of cells on which colors are set.
+ * @param color Color index from {@link IndexedColors} used to draw the borders.
+ * @param extent BorderExtent of the borders for which colors are set.
+ */
+ public void drawBorderColors(CellRangeAddress range, short color, BorderExtent extent) {
+ switch (extent) {
+ case NONE:
+ removeBorderColors(range);
+ break;
+ case ALL:
+ drawHorizontalBorderColors(range, color, BorderExtent.ALL);
+ drawVerticalBorderColors(range, color, BorderExtent.ALL);
+ break;
+ case INSIDE:
+ drawHorizontalBorderColors(range, color, BorderExtent.INSIDE);
+ drawVerticalBorderColors(range, color, BorderExtent.INSIDE);
+ break;
+ case OUTSIDE:
+ drawOutsideBorderColors(range, color, BorderExtent.ALL);
+ break;
+ case TOP:
+ drawTopBorderColor(range, color);
+ break;
+ case BOTTOM:
+ drawBottomBorderColor(range, color);
+ break;
+ case LEFT:
+ drawLeftBorderColor(range, color);
+ break;
+ case RIGHT:
+ drawRightBorderColor(range, color);
+ break;
+ case HORIZONTAL:
+ drawHorizontalBorderColors(range, color, BorderExtent.ALL);
+ break;
+ case INSIDE_HORIZONTAL:
+ drawHorizontalBorderColors(range, color, BorderExtent.INSIDE);
+ break;
+ case OUTSIDE_HORIZONTAL:
+ drawOutsideBorderColors(range, color, BorderExtent.HORIZONTAL);
+ break;
+ case VERTICAL:
+ drawVerticalBorderColors(range, color, BorderExtent.ALL);
+ break;
+ case INSIDE_VERTICAL:
+ drawVerticalBorderColors(range, color, BorderExtent.INSIDE);
+ break;
+ case OUTSIDE_VERTICAL:
+ drawOutsideBorderColors(range, color, BorderExtent.VERTICAL);
+ break;
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the top border for a range of cells.
+ * </p>
+ *
+ * @param range range of cells on which colors are set.
+ * @param color Color index from {@link IndexedColors} used to draw the borders.
+ */
+ private void drawTopBorderColor(CellRangeAddress range, short color) {
+ int row = range.getFirstRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstCol; i <= lastCol; i++) {
+ // if BORDER_TOP is not set on BorderPropertyTemplate, make a thin border so that there's something to color
+ if (getTemplateProperty(row, i, CellUtil.BORDER_TOP) == null) {
+ drawTopBorder(new CellRangeAddress(row, row, i, i), BorderStyle.THIN);
+ }
+ addProperty(row, i, CellUtil.TOP_BORDER_COLOR, color);
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the bottom border for a range of cells.
+ * </p>
+ *
+ * @param range range of cells on which colors are set.
+ * @param color Color index from {@link IndexedColors} used to draw the borders.
+ */
+ private void drawBottomBorderColor(CellRangeAddress range, short color) {
+ int row = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstCol; i <= lastCol; i++) {
+ // if BORDER_BOTTOM is not set on BorderPropertyTemplate, make a thin border so that there's something to color
+ if (getTemplateProperty(row, i, CellUtil.BORDER_BOTTOM) == null) {
+ drawBottomBorder(new CellRangeAddress(row, row, i, i), BorderStyle.THIN);
+ }
+ addProperty(row, i, CellUtil.BOTTOM_BORDER_COLOR, color);
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the left border for a range of cells.
+ * </p>
+ *
+ * @param range range of cells on which colors are set.
+ * @param color Color index from {@link IndexedColors} used to draw the borders.
+ */
+ private void drawLeftBorderColor(CellRangeAddress range, short color) {
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int col = range.getFirstColumn();
+ for (int i = firstRow; i <= lastRow; i++) {
+ // if BORDER_LEFT is not set on BorderPropertyTemplate, make a thin border so that there's something to color
+ if (getTemplateProperty(i, col, CellUtil.BORDER_LEFT) == null) {
+ drawLeftBorder(new CellRangeAddress(i, i, col, col), BorderStyle.THIN);
+ }
+ addProperty(i, col, CellUtil.LEFT_BORDER_COLOR, color);
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the right border for a range of cells. If the border is
+ * not drawn, it defaults to {@link BorderStyle#THIN}
+ * </p>
+ *
+ * @param range range of cells on which colors are set.
+ * @param color Color index from {@link IndexedColors} used to draw the borders.
+ */
+ private void drawRightBorderColor(CellRangeAddress range, short color) {
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int col = range.getLastColumn();
+ for (int i = firstRow; i <= lastRow; i++) {
+ // if BORDER_RIGHT is not set on BorderPropertyTemplate, make a thin border so that there's something to color
+ if (getTemplateProperty(i, col, CellUtil.BORDER_RIGHT) == null) {
+ drawRightBorder(new CellRangeAddress(i, i, col, col), BorderStyle.THIN);
+ }
+ addProperty(i, col, CellUtil.RIGHT_BORDER_COLOR, color);
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the outside borders for a range of cells.
+ * </p>
+ *
+ * @param range range of cells on which colors are set.
+ * @param color Color index from {@link IndexedColors} used to draw the borders.
+ * @param extent BorderExtent of the borders for which colors are set. Valid Values are:
+ * <ul>
+ * <li>Extent.ALL</li>
+ * <li>Extent.HORIZONTAL</li>
+ * <li>Extent.VERTICAL</li>
+ * </ul>
+ */
+ private void drawOutsideBorderColors(CellRangeAddress range, short color, BorderExtent extent) {
+ switch (extent) {
+ case ALL:
+ case HORIZONTAL:
+ case VERTICAL:
+ if (extent == BorderExtent.ALL || extent == BorderExtent.HORIZONTAL) {
+ drawTopBorderColor(range, color);
+ drawBottomBorderColor(range, color);
+ }
+ if (extent == BorderExtent.ALL || extent == BorderExtent.VERTICAL) {
+ drawLeftBorderColor(range, color);
+ drawRightBorderColor(range, color);
+ }
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "Illegal BorderExtent. Allowed: ALL, HORIZONTAL, and VERTICAL");
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the horizontal borders for a range of cells.
+ * </p>
+ *
+ * @param range range of cells on which colors are set.
+ * @param color Color index from {@link IndexedColors} used to draw the borders.
+ * @param extent BorderExtent of the borders for which colors are set. Valid Values are:
+ * <ul>
+ * <li>Extent.ALL</li>
+ * <li>Extent.INSIDE</li>
+ * </ul>
+ */
+ private void drawHorizontalBorderColors(CellRangeAddress range, short color, BorderExtent extent) {
+ switch (extent) {
+ case ALL:
+ case INSIDE:
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstRow; i <= lastRow; i++) {
+ CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
+ if (extent == BorderExtent.ALL || i > firstRow) {
+ drawTopBorderColor(row, color);
+ }
+ if (extent == BorderExtent.ALL || i < lastRow) {
+ drawBottomBorderColor(row, color);
+ }
+ }
+ break;
+ default:
+ throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE");
+ }
+ }
+
+ /**
+ * <p>
+ * Sets the color of the vertical borders for a range of cells.
+ * </p>
+ *
+ * @param range range of cells on which colors are set.
+ * @param color Color index from {@link IndexedColors} used to draw the borders.
+ * @param extent BorderExtent of the borders for which colors are set. Valid Values are:
+ * <ul>
+ * <li>Extent.ALL</li>
+ * <li>Extent.INSIDE</li>
+ * </ul>
+ */
+ private void drawVerticalBorderColors(CellRangeAddress range, short color, BorderExtent extent) {
+ switch (extent) {
+ case ALL:
+ case INSIDE:
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstCol; i <= lastCol; i++) {
+ CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
+ if (extent == BorderExtent.ALL || i > firstCol) {
+ drawLeftBorderColor(row, color);
+ }
+ if (extent == BorderExtent.ALL || i < lastCol) {
+ drawRightBorderColor(row, color);
+ }
+ }
+ break;
+ default:
+ throw new IllegalArgumentException("Illegal BorderExtent. Allowed: ALL and INSIDE");
+ }
+ }
+
+ /**
+ * Removes all border properties from this BorderPropertyTemplate for the
+ * specified range.
+ *
+ * @param range - range of cells to remove borders.
+ */
+ private void removeBorderColors(CellRangeAddress range) {
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int firstColumn = range.getFirstColumn();
+ int lastColumn = range.getLastColumn();
+ for (int row = firstRow; row <= lastRow; row++) {
+ for (int col = firstColumn; col <= lastColumn; col++) {
+ removeProperties(row, col, BORDER_COLOR_PROPERTY_NAMES);
+ }
+ }
+ }
+
+ /**
+ * Adds a property to this BorderPropertyTemplate for a given cell
+ *
+ * @param row
+ * @param col
+ * @param property
+ * @param value
+ */
+ private void addProperty(int row, int col, String property, Object value) {
+ CellAddress cell = new CellAddress(row, col);
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties == null) {
+ cellProperties = new HashMap<String, Object>();
+ }
+ cellProperties.put(property, value);
+ _propertyTemplate.put(cell, cellProperties);
+ }
+
+ /**
+ * Removes a set of properties from this BorderPropertyTemplate for a
+ * given cell
+ *
+ * @param row the row index of the cell to remove properties from
+ * @param col the column index of the cell to remove properties from
+ * @param properties a list of the property names to remove from the cell
+ */
+ private void removeProperties(int row, int col, Set<String> properties) {
+ CellAddress cell = new CellAddress(row, col);
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties != null) {
+ cellProperties.keySet().removeAll(properties);
+ if (cellProperties.isEmpty()) {
+ _propertyTemplate.remove(cell);
+ } else {
+ _propertyTemplate.put(cell, cellProperties);
+ }
+ }
+ }
+
+ /**
+ * Retrieves the number of borders assigned to a cell (a value between 0 and 4)
+ *
+ * @param cell the cell to count the number of borders on
+ */
+ public int getNumBorders(CellAddress cell) {
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties == null) {
+ return 0;
+ }
+
+ int count = 0;
+ if (cellProperties.containsKey(CellUtil.BORDER_TOP)) count++;
+ if (cellProperties.containsKey(CellUtil.BORDER_LEFT)) count++;
+ if (cellProperties.containsKey(CellUtil.BORDER_RIGHT)) count++;
+ if (cellProperties.containsKey(CellUtil.BORDER_BOTTOM)) count++;
+ return count;
+ }
+
+ /**
+ * Retrieves the number of borders assigned to a cell
+ *
+ * @param row
+ * @param col
+ */
+ public int getNumBorders(int row, int col) {
+ return getNumBorders(new CellAddress(row, col));
+ }
+
+ /**
+ * Retrieves the number of border colors assigned to a cell
+ *
+ * @param cell
+ */
+ public int getNumBorderColors(CellAddress cell) {
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties == null) {
+ return 0;
+ }
+
+ int count = 0;
+ if (cellProperties.containsKey(CellUtil.TOP_BORDER_COLOR)) count++;
+ if (cellProperties.containsKey(CellUtil.LEFT_BORDER_COLOR)) count++;
+ if (cellProperties.containsKey(CellUtil.RIGHT_BORDER_COLOR)) count++;
+ if (cellProperties.containsKey(CellUtil.BOTTOM_BORDER_COLOR)) count++;
+ return count;
+ }
+
+ /**
+ * Retrieves the number of border colors assigned to a cell
+ *
+ * @param row
+ * @param col
+ */
+ public int getNumBorderColors(int row, int col) {
+ return getNumBorderColors(new CellAddress(row, col));
+ }
+
+ /**
+ * Retrieves the border style for a given cell
+ *
+ * @param cell
+ * @param property
+ */
+ public Object getTemplateProperty(CellAddress cell, String property) {
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties != null) {
+ return cellProperties.get(property);
+ }
+ return null;
+ }
+
+ /**
+ * Retrieves the border style for a given cell
+ *
+ * @param row
+ * @param col
+ * @param property
+ */
+ public Object getTemplateProperty(int row, int col, String property) {
+ return getTemplateProperty(new CellAddress(row, col), property);
+ }
+}
+++ /dev/null
-/* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-==================================================================== */
-
-package org.apache.poi.ss.util;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.SpreadsheetVersion;
-import org.apache.poi.ss.usermodel.BorderStyle;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.IndexedColors;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-
-/**
- * <p>
- * A PropertyTemplate is a template that can be applied to any sheet in
- * a project. It contains all the border type and color attributes needed to
- * draw all the borders for a single sheet. That template can be applied to any
- * sheet in any workbook.
- *
- * This class requires the full spreadsheet to be in memory so
- * {@link SWorkbook} Spreadsheets are not supported. The same
- * PropertyTemplate can, however, be applied to both
- * {@link org.apache.poi.hssf.usermodel.HSSFWorkbook}, and Workbook objects
- * if necessary. Portions of the border that fall outside the max range of the
- * {@link HSSFWorkbook} sheet are ignored.
- * </p>
- *
- * <p>
- * This would replace {@link RegionUtil}.
- * </p>
- */
-public final class PropertyTemplate {
-
- /**
- * Provides various extents of the properties being added to the template
- * Note that the Border Extent differs from a BorderStyle. A BorderStyle
- * refers to the border around a single cell while a BorderExtent refers to
- * borders around and through an area of cells.
- */
- public enum Extent {
- /**
- * No properties defined. This can be used to remove existing
- * properties.
- */
- NONE,
-
- /**
- * All borders, that is top, bottom, left and right, including interior
- * borders for the range. Does not include diagonals which are different
- * and not implemented here.
- */
- ALL,
-
- /**
- * All inside borders. This is top, bottom, left, and right borders, but
- * restricted to the interior borders for the range. For a range of one
- * cell, this will produce no borders.
- */
- INSIDE,
-
- /**
- * All outside borders. That is top, bottom, left and right borders that
- * bound the range only.
- */
- OUTSIDE,
-
- /**
- * This is just the top border for the range. No interior borders will
- * be produced.
- */
- TOP,
-
- /**
- * This is just the bottom border for the range. No interior borders
- * will be produced.
- */
- BOTTOM,
-
- /**
- * This is just the left border for the range, no interior borders will
- * be produced.
- */
- LEFT,
-
- /**
- * This is just the right border for the range, no interior borders will
- * be produced.
- */
- RIGHT,
-
- /**
- * This is all horizontal borders for the range, including interior and
- * outside borders.
- */
- HORIZONTAL,
-
- /**
- * This is just the interior horizontal borders for the range.
- */
- INSIDE_HORIZONTAL,
-
- /**
- * This is just the outside horizontal borders for the range.
- */
- OUTSIDE_HORIZONTAL,
-
- /**
- * This is all vertical borders for the range, including interior and
- * outside borders.
- */
- VERTICAL,
-
- /**
- * This is just the interior vertical borders for the range.
- */
- INSIDE_VERTICAL,
-
- /**
- * This is just the outside vertical borders for the range.
- */
- OUTSIDE_VERTICAL
- }
-
- /**
- * A set of the border color property names
- */
- private static final Set<String> BORDER_COLOR_PROPERTY_NAMES;
- static {
- Set<String> properties = new HashSet<String>();
- properties.add(CellUtil.TOP_BORDER_COLOR);
- properties.add(CellUtil.BOTTOM_BORDER_COLOR);
- properties.add(CellUtil.LEFT_BORDER_COLOR);
- properties.add(CellUtil.RIGHT_BORDER_COLOR);
- BORDER_COLOR_PROPERTY_NAMES = Collections.unmodifiableSet(properties);
- }
-
- /**
- * A set of the border direction property names
- */
- private static final Set<String> BORDER_DIRECTION_PROPERTY_NAMES;
- static {
- Set<String> properties = new HashSet<String>();
- properties.add(CellUtil.BORDER_TOP);
- properties.add(CellUtil.BORDER_BOTTOM);
- properties.add(CellUtil.BORDER_LEFT);
- properties.add(CellUtil.BORDER_RIGHT);
- BORDER_DIRECTION_PROPERTY_NAMES = Collections.unmodifiableSet(properties);
- }
-
- /**
- * This is a list of cell properties for one shot application to a range of
- * cells at a later time.
- */
- private final Map<CellAddress, Map<String, Object>> _propertyTemplate;
- private final SpreadsheetVersion _ss;
-
- /**
- *
- */
- public PropertyTemplate() {
- _propertyTemplate = new HashMap<CellAddress, Map<String, Object>>();
- _ss = SpreadsheetVersion.EXCEL2007;
- }
-
- /**
- * Draws a group of cell borders for a cell range. The borders are not
- * applied to the cells at this time, just the template is drawn. To apply
- * the drawn borders to a sheet, use {@link #applyBorders}.
- *
- * @param range range of cells on which borders are drawn.
- * @param borderType Type of border to draw.
- * @param extent Extent of the borders to be applied.
- */
- public void drawBorders(CellRangeAddress range, BorderStyle borderType, Extent extent) {
- switch (extent) {
- case NONE:
- removeBorders(range);
- break;
- case ALL:
- drawHorizontalBorders(range, borderType, Extent.ALL);
- drawVerticalBorders(range, borderType, Extent.ALL);
- break;
- case INSIDE:
- drawHorizontalBorders(range, borderType, Extent.INSIDE);
- drawVerticalBorders(range, borderType, Extent.INSIDE);
- break;
- case OUTSIDE:
- drawOutsideBorders(range, borderType, Extent.ALL);
- break;
- case TOP:
- drawTopBorder(range, borderType);
- break;
- case BOTTOM:
- drawBottomBorder(range, borderType);
- break;
- case LEFT:
- drawLeftBorder(range, borderType);
- break;
- case RIGHT:
- drawRightBorder(range, borderType);
- break;
- case HORIZONTAL:
- drawHorizontalBorders(range, borderType, Extent.ALL);
- break;
- case INSIDE_HORIZONTAL:
- drawHorizontalBorders(range, borderType, Extent.INSIDE);
- break;
- case OUTSIDE_HORIZONTAL:
- drawOutsideBorders(range, borderType, Extent.HORIZONTAL);
- break;
- case VERTICAL:
- drawVerticalBorders(range, borderType, Extent.ALL);
- break;
- case INSIDE_VERTICAL:
- drawVerticalBorders(range, borderType, Extent.INSIDE);
- break;
- case OUTSIDE_VERTICAL:
- drawOutsideBorders(range, borderType, Extent.VERTICAL);
- break;
- }
- }
-
- /**
- * Draws a group of cell borders for a cell range. The borders are not
- * applied to the cells at this time, just the template is drawn. To apply
- * the drawn borders to a sheet, use {@link #applyBorders}.
- *
- * @param range range of cells on which borders are drawn.
- * @param borderType Type of border to draw.
- * @param color Color index from {@link IndexedColors} used to draw the borders.
- * @param extent Extent of the borders to be applied.
- */
- public void drawBorders(CellRangeAddress range, BorderStyle borderType, short color, Extent extent) {
- drawBorders(range, borderType, extent);
- if (borderType != BorderStyle.NONE) {
- drawBorderColors(range, color, extent);
- }
- }
-
- /**
- * <p>
- * Draws the top border for a range of cells
- * </p>
- *
- * @param range range of cells on which borders are drawn.
- * @param borderType Type of border to draw.
- */
- private void drawTopBorder(CellRangeAddress range, BorderStyle borderType) {
- int row = range.getFirstRow();
- int firstCol = range.getFirstColumn();
- int lastCol = range.getLastColumn();
- boolean addBottom = (borderType == BorderStyle.NONE && row > 0);
- for (int i = firstCol; i <= lastCol; i++) {
- addProperty(row, i, CellUtil.BORDER_TOP, borderType);
- if (addBottom) {
- addProperty(row - 1, i, CellUtil.BORDER_BOTTOM, borderType);
- }
- }
- }
-
- /**
- * <p>
- * Draws the bottom border for a range of cells
- * </p>
- *
- * @param range range of cells on which borders are drawn.
- * @param borderType Type of border to draw.
- */
- private void drawBottomBorder(CellRangeAddress range, BorderStyle borderType) {
- int row = range.getLastRow();
- int firstCol = range.getFirstColumn();
- int lastCol = range.getLastColumn();
- boolean addTop = (borderType == BorderStyle.NONE && row < _ss.getLastRowIndex());
- for (int i = firstCol; i <= lastCol; i++) {
- addProperty(row, i, CellUtil.BORDER_BOTTOM, borderType);
- if (addTop) {
- addProperty(row + 1, i, CellUtil.BORDER_TOP, borderType);
- }
- }
- }
-
- /**
- * <p>
- * Draws the left border for a range of cells
- * </p>
- *
- * @param range range of cells on which borders are drawn.
- * @param borderType Type of border to draw.
- */
- private void drawLeftBorder(CellRangeAddress range, BorderStyle borderType) {
- int firstRow = range.getFirstRow();
- int lastRow = range.getLastRow();
- int col = range.getFirstColumn();
- boolean addRight = (borderType == BorderStyle.NONE && col > 0);
- for (int i = firstRow; i <= lastRow; i++) {
- addProperty(i, col, CellUtil.BORDER_LEFT, borderType);
- if (addRight) {
- addProperty(i, col - 1, CellUtil.BORDER_RIGHT, borderType);
- }
- }
- }
-
- /**
- * <p>
- * Draws the right border for a range of cells
- * </p>
- *
- * @param range range of cells on which borders are drawn.
- * @param borderType Type of border to draw.
- */
- private void drawRightBorder(CellRangeAddress range, BorderStyle borderType) {
- int firstRow = range.getFirstRow();
- int lastRow = range.getLastRow();
- int col = range.getLastColumn();
- boolean addLeft = (borderType == BorderStyle.NONE && col < _ss.getLastColumnIndex());
- for (int i = firstRow; i <= lastRow; i++) {
- addProperty(i, col, CellUtil.BORDER_RIGHT, borderType);
- if (addLeft) {
- addProperty(i, col + 1, CellUtil.BORDER_LEFT, borderType);
- }
- }
- }
-
- /**
- * <p>
- * Draws the outside borders for a range of cells.
- * </p>
- *
- * @param range range of cells on which borders are drawn.
- * @param borderType Type of border to draw.
- * @param extent Extent of the borders to be applied. Valid Values are:
- * <ul>
- * <li>Extent.ALL</li>
- * <li>Extent.HORIZONTAL</li>
- * <li>Extent.VERTICAL</li>
- * </ul>
- */
- private void drawOutsideBorders(CellRangeAddress range, BorderStyle borderType, Extent extent) {
- switch (extent) {
- case ALL:
- case HORIZONTAL:
- case VERTICAL:
- if (extent == Extent.ALL || extent == Extent.HORIZONTAL) {
- drawTopBorder(range, borderType);
- drawBottomBorder(range, borderType);
- }
- if (extent == Extent.ALL || extent == Extent.VERTICAL) {
- drawLeftBorder(range, borderType);
- drawRightBorder(range, borderType);
- }
- break;
- default:
- throw new IllegalArgumentException(
- "Unsupported PropertyTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL");
- }
- }
-
- /**
- * <p>
- * Draws the horizontal borders for a range of cells.
- * </p>
- *
- * @param range range of cells on which borders are drawn.
- * @param borderType Type of border to draw.
- * @param extent Extent of the borders to be applied. Valid Values are:
- * <ul>
- * <li>Extent.ALL</li>
- * <li>Extent.INSIDE</li>
- * </ul>
- */
- private void drawHorizontalBorders(CellRangeAddress range, BorderStyle borderType, Extent extent) {
- switch (extent) {
- case ALL:
- case INSIDE:
- int firstRow = range.getFirstRow();
- int lastRow = range.getLastRow();
- int firstCol = range.getFirstColumn();
- int lastCol = range.getLastColumn();
- for (int i = firstRow; i <= lastRow; i++) {
- CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
- if (extent == Extent.ALL || i > firstRow) {
- drawTopBorder(row, borderType);
- }
- if (extent == Extent.ALL || i < lastRow) {
- drawBottomBorder(row, borderType);
- }
- }
- break;
- default:
- throw new IllegalArgumentException(
- "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");
- }
- }
-
- /**
- * <p>
- * Draws the vertical borders for a range of cells.
- * </p>
- *
- * @param range range of cells on which borders are drawn.
- * @param borderType Type of border to draw.
- * @param extent Extent of the borders to be applied. Valid Values are:
- * <ul>
- * <li>Extent.ALL</li>
- * <li>Extent.INSIDE</li>
- * </ul>
- */
- private void drawVerticalBorders(CellRangeAddress range, BorderStyle borderType, Extent extent) {
- switch (extent) {
- case ALL:
- case INSIDE:
- int firstRow = range.getFirstRow();
- int lastRow = range.getLastRow();
- int firstCol = range.getFirstColumn();
- int lastCol = range.getLastColumn();
- for (int i = firstCol; i <= lastCol; i++) {
- CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
- if (extent == Extent.ALL || i > firstCol) {
- drawLeftBorder(row, borderType);
- }
- if (extent == Extent.ALL || i < lastCol) {
- drawRightBorder(row, borderType);
- }
- }
- break;
- default:
- throw new IllegalArgumentException(
- "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");
- }
- }
-
- /**
- * Removes all border properties from this PropertyTemplate for the
- * specified range.
- *
- * @param range - range of cells to remove borders.
- */
- private void removeBorders(CellRangeAddress range) {
- int firstRow = range.getFirstRow();
- int lastRow = range.getLastRow();
- int firstCol = range.getFirstColumn();
- int lastCol = range.getLastColumn();
- for (int row = firstRow; row <= lastRow; row++) {
- for (int col = firstCol; col <= lastCol; col++) {
- removeProperties(row, col, BORDER_DIRECTION_PROPERTY_NAMES);
- }
- }
- removeBorderColors(range);
- }
-
- /**
- * Applies the drawn borders to a Sheet. The borders that are applied are
- * the ones that have been drawn by the {@link #drawBorders} and
- * {@link #drawBorderColors} methods.
- *
- * @param sheet Sheet on which to apply borders
- */
- public void applyBorders(Sheet sheet) {
- SpreadsheetVersion ss = sheet.getWorkbook().getSpreadsheetVersion();
- int lastValidRow = ss.getLastRowIndex();
- int lastValidCol = ss.getLastColumnIndex();
- for (Entry<CellAddress, Map<String, Object>> entry : _propertyTemplate.entrySet()) {
- CellAddress cellAddress = entry.getKey();
- int r = cellAddress.getRow();
- int c = cellAddress.getColumn();
- if (r <= lastValidRow && c <= lastValidCol) {
- Map<String, Object> properties = entry.getValue();
- Row row = CellUtil.getRow(r, sheet);
- Cell cell = CellUtil.getCell(row, c);
- CellUtil.setCellStyleProperties(cell, properties);
- }
- }
- }
-
- /**
- * Sets the color for a group of cell borders for a cell range. The borders
- * are not applied to the cells at this time, just the template is drawn. If
- * the borders do not exist, a {@link BorderStyle#THIN} border is used. To apply the
- * drawn borders to a sheet, use {@link #applyBorders}.
- *
- * @param range range of cells on which colors are set.
- * @param color Color index from {@link IndexedColors} used to draw the borders.
- * @param extent Extent of the borders for which colors are set.
- */
- public void drawBorderColors(CellRangeAddress range, short color, Extent extent) {
- switch (extent) {
- case NONE:
- removeBorderColors(range);
- break;
- case ALL:
- drawHorizontalBorderColors(range, color, Extent.ALL);
- drawVerticalBorderColors(range, color, Extent.ALL);
- break;
- case INSIDE:
- drawHorizontalBorderColors(range, color, Extent.INSIDE);
- drawVerticalBorderColors(range, color, Extent.INSIDE);
- break;
- case OUTSIDE:
- drawOutsideBorderColors(range, color, Extent.ALL);
- break;
- case TOP:
- drawTopBorderColor(range, color);
- break;
- case BOTTOM:
- drawBottomBorderColor(range, color);
- break;
- case LEFT:
- drawLeftBorderColor(range, color);
- break;
- case RIGHT:
- drawRightBorderColor(range, color);
- break;
- case HORIZONTAL:
- drawHorizontalBorderColors(range, color, Extent.ALL);
- break;
- case INSIDE_HORIZONTAL:
- drawHorizontalBorderColors(range, color, Extent.INSIDE);
- break;
- case OUTSIDE_HORIZONTAL:
- drawOutsideBorderColors(range, color, Extent.HORIZONTAL);
- break;
- case VERTICAL:
- drawVerticalBorderColors(range, color, Extent.ALL);
- break;
- case INSIDE_VERTICAL:
- drawVerticalBorderColors(range, color, Extent.INSIDE);
- break;
- case OUTSIDE_VERTICAL:
- drawOutsideBorderColors(range, color, Extent.VERTICAL);
- break;
- }
- }
-
- /**
- * <p>
- * Sets the color of the top border for a range of cells.
- * </p>
- *
- * @param range range of cells on which colors are set.
- * @param color Color index from {@link IndexedColors} used to draw the borders.
- */
- private void drawTopBorderColor(CellRangeAddress range, short color) {
- int row = range.getFirstRow();
- int firstCol = range.getFirstColumn();
- int lastCol = range.getLastColumn();
- for (int i = firstCol; i <= lastCol; i++) {
- // if BORDER_TOP is not set on PropertyTemplate, make a thin border so that there's something to color
- if (getTemplateProperty(row, i, CellUtil.BORDER_TOP) == null) {
- drawTopBorder(new CellRangeAddress(row, row, i, i), BorderStyle.THIN);
- }
- addProperty(row, i, CellUtil.TOP_BORDER_COLOR, color);
- }
- }
-
- /**
- * <p>
- * Sets the color of the bottom border for a range of cells.
- * </p>
- *
- * @param range range of cells on which colors are set.
- * @param color Color index from {@link IndexedColors} used to draw the borders.
- */
- private void drawBottomBorderColor(CellRangeAddress range, short color) {
- int row = range.getLastRow();
- int firstCol = range.getFirstColumn();
- int lastCol = range.getLastColumn();
- for (int i = firstCol; i <= lastCol; i++) {
- // if BORDER_BOTTOM is not set on PropertyTemplate, make a thin border so that there's something to color
- if (getTemplateProperty(row, i, CellUtil.BORDER_BOTTOM) == null) {
- drawBottomBorder(new CellRangeAddress(row, row, i, i), BorderStyle.THIN);
- }
- addProperty(row, i, CellUtil.BOTTOM_BORDER_COLOR, color);
- }
- }
-
- /**
- * <p>
- * Sets the color of the left border for a range of cells.
- * </p>
- *
- * @param range range of cells on which colors are set.
- * @param color Color index from {@link IndexedColors} used to draw the borders.
- */
- private void drawLeftBorderColor(CellRangeAddress range, short color) {
- int firstRow = range.getFirstRow();
- int lastRow = range.getLastRow();
- int col = range.getFirstColumn();
- for (int i = firstRow; i <= lastRow; i++) {
- // if BORDER_LEFT is not set on PropertyTemplate, make a thin border so that there's something to color
- if (getTemplateProperty(i, col, CellUtil.BORDER_LEFT) == null) {
- drawLeftBorder(new CellRangeAddress(i, i, col, col), BorderStyle.THIN);
- }
- addProperty(i, col, CellUtil.LEFT_BORDER_COLOR, color);
- }
- }
-
- /**
- * <p>
- * Sets the color of the right border for a range of cells. If the border is
- * not drawn, it defaults to {@link BorderStyle#THIN}
- * </p>
- *
- * @param range range of cells on which colors are set.
- * @param color Color index from {@link IndexedColors} used to draw the borders.
- */
- private void drawRightBorderColor(CellRangeAddress range, short color) {
- int firstRow = range.getFirstRow();
- int lastRow = range.getLastRow();
- int col = range.getLastColumn();
- for (int i = firstRow; i <= lastRow; i++) {
- // if BORDER_RIGHT is not set on PropertyTemplate, make a thin border so that there's something to color
- if (getTemplateProperty(i, col, CellUtil.BORDER_RIGHT) == null) {
- drawRightBorder(new CellRangeAddress(i, i, col, col), BorderStyle.THIN);
- }
- addProperty(i, col, CellUtil.RIGHT_BORDER_COLOR, color);
- }
- }
-
- /**
- * <p>
- * Sets the color of the outside borders for a range of cells.
- * </p>
- *
- * @param range range of cells on which colors are set.
- * @param color Color index from {@link IndexedColors} used to draw the borders.
- * @param extent Extent of the borders for which colors are set. Valid Values are:
- * <ul>
- * <li>Extent.ALL</li>
- * <li>Extent.HORIZONTAL</li>
- * <li>Extent.VERTICAL</li>
- * </ul>
- */
- private void drawOutsideBorderColors(CellRangeAddress range, short color, Extent extent) {
- switch (extent) {
- case ALL:
- case HORIZONTAL:
- case VERTICAL:
- if (extent == Extent.ALL || extent == Extent.HORIZONTAL) {
- drawTopBorderColor(range, color);
- drawBottomBorderColor(range, color);
- }
- if (extent == Extent.ALL || extent == Extent.VERTICAL) {
- drawLeftBorderColor(range, color);
- drawRightBorderColor(range, color);
- }
- break;
- default:
- throw new IllegalArgumentException(
- "Unsupported PropertyTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL");
- }
- }
-
- /**
- * <p>
- * Sets the color of the horizontal borders for a range of cells.
- * </p>
- *
- * @param range range of cells on which colors are set.
- * @param color Color index from {@link IndexedColors} used to draw the borders.
- * @param extent Extent of the borders for which colors are set. Valid Values are:
- * <ul>
- * <li>Extent.ALL</li>
- * <li>Extent.INSIDE</li>
- * </ul>
- */
- private void drawHorizontalBorderColors(CellRangeAddress range, short color, Extent extent) {
- switch (extent) {
- case ALL:
- case INSIDE:
- int firstRow = range.getFirstRow();
- int lastRow = range.getLastRow();
- int firstCol = range.getFirstColumn();
- int lastCol = range.getLastColumn();
- for (int i = firstRow; i <= lastRow; i++) {
- CellRangeAddress row = new CellRangeAddress(i, i, firstCol, lastCol);
- if (extent == Extent.ALL || i > firstRow) {
- drawTopBorderColor(row, color);
- }
- if (extent == Extent.ALL || i < lastRow) {
- drawBottomBorderColor(row, color);
- }
- }
- break;
- default:
- throw new IllegalArgumentException(
- "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");
- }
- }
-
- /**
- * <p>
- * Sets the color of the vertical borders for a range of cells.
- * </p>
- *
- * @param range range of cells on which colors are set.
- * @param color Color index from {@link IndexedColors} used to draw the borders.
- * @param extent Extent of the borders for which colors are set. Valid Values are:
- * <ul>
- * <li>Extent.ALL</li>
- * <li>Extent.INSIDE</li>
- * </ul>
- */
- private void drawVerticalBorderColors(CellRangeAddress range, short color, Extent extent) {
- switch (extent) {
- case ALL:
- case INSIDE:
- int firstRow = range.getFirstRow();
- int lastRow = range.getLastRow();
- int firstCol = range.getFirstColumn();
- int lastCol = range.getLastColumn();
- for (int i = firstCol; i <= lastCol; i++) {
- CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, i, i);
- if (extent == Extent.ALL || i > firstCol) {
- drawLeftBorderColor(row, color);
- }
- if (extent == Extent.ALL || i < lastCol) {
- drawRightBorderColor(row, color);
- }
- }
- break;
- default:
- throw new IllegalArgumentException(
- "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");
- }
- }
-
- /**
- * Removes all border properties from this PropertyTemplate for the
- * specified range.
- *
- * @param range - range of cells to remove borders.
- */
- private void removeBorderColors(CellRangeAddress range) {
- int firstRow = range.getFirstRow();
- int lastRow = range.getLastRow();
- int firstColumn = range.getFirstColumn();
- int lastColumn = range.getLastColumn();
- for (int row = firstRow; row <= lastRow; row++) {
- for (int col = firstColumn; col <= lastColumn; col++) {
- removeProperties(row, col, BORDER_COLOR_PROPERTY_NAMES);
- }
- }
- }
-
- /**
- * Adds a property to this PropertyTemplate for a given cell
- *
- * @param row
- * @param col
- * @param property
- * @param value
- */
- private void addProperty(int row, int col, String property, Object value) {
- CellAddress cell = new CellAddress(row, col);
- Map<String, Object> cellProperties = _propertyTemplate.get(cell);
- if (cellProperties == null) {
- cellProperties = new HashMap<String, Object>();
- }
- cellProperties.put(property, value);
- _propertyTemplate.put(cell, cellProperties);
- }
-
- /**
- * Removes a set of properties from this PropertyTemplate for a
- * given cell
- *
- * @param row the row index of the cell to remove properties from
- * @param col the column index of the cell to remove properties from
- * @param properties a list of the property names to remove from the cell
- */
- private void removeProperties(int row, int col, Set<String> properties) {
- CellAddress cell = new CellAddress(row, col);
- Map<String, Object> cellProperties = _propertyTemplate.get(cell);
- if (cellProperties != null) {
- cellProperties.keySet().removeAll(properties);
- if (cellProperties.isEmpty()) {
- _propertyTemplate.remove(cell);
- } else {
- _propertyTemplate.put(cell, cellProperties);
- }
- }
- }
-
- /**
- * Retrieves the number of borders assigned to a cell (a value between 0 and 4)
- *
- * @param cell the cell to count the number of borders on
- */
- public int getNumBorders(CellAddress cell) {
- Map<String, Object> cellProperties = _propertyTemplate.get(cell);
- if (cellProperties == null) {
- return 0;
- }
-
- int count = 0;
- if (cellProperties.containsKey(CellUtil.BORDER_TOP)) count++;
- if (cellProperties.containsKey(CellUtil.BORDER_LEFT)) count++;
- if (cellProperties.containsKey(CellUtil.BORDER_RIGHT)) count++;
- if (cellProperties.containsKey(CellUtil.BORDER_BOTTOM)) count++;
- return count;
- }
-
- /**
- * Retrieves the number of borders assigned to a cell
- *
- * @param row
- * @param col
- */
- public int getNumBorders(int row, int col) {
- return getNumBorders(new CellAddress(row, col));
- }
-
- /**
- * Retrieves the number of border colors assigned to a cell
- *
- * @param cell
- */
- public int getNumBorderColors(CellAddress cell) {
- Map<String, Object> cellProperties = _propertyTemplate.get(cell);
- if (cellProperties == null) {
- return 0;
- }
-
- int count = 0;
- if (cellProperties.containsKey(CellUtil.TOP_BORDER_COLOR)) count++;
- if (cellProperties.containsKey(CellUtil.LEFT_BORDER_COLOR)) count++;
- if (cellProperties.containsKey(CellUtil.RIGHT_BORDER_COLOR)) count++;
- if (cellProperties.containsKey(CellUtil.BOTTOM_BORDER_COLOR)) count++;
- return count;
- }
-
- /**
- * Retrieves the number of border colors assigned to a cell
- *
- * @param row
- * @param col
- */
- public int getNumBorderColors(int row, int col) {
- return getNumBorderColors(new CellAddress(row, col));
- }
-
- /**
- * Retrieves the border style for a given cell
- *
- * @param cell
- * @param property
- */
- public Object getTemplateProperty(CellAddress cell, String property) {
- Map<String, Object> cellProperties = _propertyTemplate.get(cell);
- if (cellProperties != null) {
- return cellProperties.get(property);
- }
- return null;
- }
-
- /**
- * Retrieves the border style for a given cell
- *
- * @param row
- * @param col
- * @param property
- */
- public Object getTemplateProperty(int row, int col, String property) {
- return getTemplateProperty(new CellAddress(row, col), property);
- }
-}
--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.util;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.BorderStyle;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.BorderPropertyTemplate.BorderExtent;
+import org.junit.Test;
+
+/**
+ * Tests Spreadsheet BorderPropertyTemplate
+ *
+ * @see org.apache.poi.ss.util.BorderPropertyTemplate
+ */
+public final class TestBorderPropertyTemplate {
+ @Test
+ public void getNumBorders() throws IOException {
+ CellRangeAddress a1 = new CellRangeAddress(0, 0, 0, 0);
+ BorderPropertyTemplate pt = new BorderPropertyTemplate();
+
+ pt.drawBorders(a1, BorderStyle.THIN, BorderExtent.TOP);
+ assertEquals(1, pt.getNumBorders(0, 0));
+
+ pt.drawBorders(a1, BorderStyle.MEDIUM, BorderExtent.BOTTOM);
+ assertEquals(2, pt.getNumBorders(0, 0));
+
+ pt.drawBorders(a1, BorderStyle.MEDIUM, BorderExtent.NONE);
+ assertEquals(0, pt.getNumBorders(0, 0));
+ }
+
+ @Test
+ public void getNumBorderColors() throws IOException {
+ CellRangeAddress a1 = new CellRangeAddress(0, 0, 0, 0);
+ BorderPropertyTemplate pt = new BorderPropertyTemplate();
+
+ pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), BorderExtent.TOP);
+ assertEquals(1, pt.getNumBorderColors(0, 0));
+
+ pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), BorderExtent.BOTTOM);
+ assertEquals(2, pt.getNumBorderColors(0, 0));
+
+ pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), BorderExtent.NONE);
+ assertEquals(0, pt.getNumBorderColors(0, 0));
+ }
+
+ @Test
+ public void getTemplateProperties() throws IOException {
+ CellRangeAddress a1 = new CellRangeAddress(0, 0, 0, 0);
+ BorderPropertyTemplate pt = new BorderPropertyTemplate();
+
+ pt.drawBorders(a1, BorderStyle.THIN, BorderExtent.TOP);
+ assertThin(pt.getTemplateProperty(0, 0, CellUtil.BORDER_TOP));
+
+ pt.drawBorders(a1, BorderStyle.MEDIUM, BorderExtent.BOTTOM);
+ assertMedium(pt.getTemplateProperty(0, 0, CellUtil.BORDER_BOTTOM));
+
+ pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), BorderExtent.TOP);
+ assertRed(pt.getTemplateProperty(0, 0, CellUtil.TOP_BORDER_COLOR));
+
+ pt.drawBorderColors(a1, IndexedColors.BLUE.getIndex(), BorderExtent.BOTTOM);
+ assertBlue(pt.getTemplateProperty(0, 0, CellUtil.BOTTOM_BORDER_COLOR));
+ }
+
+ @Test
+ public void drawBorders() throws IOException {
+ CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);
+ BorderPropertyTemplate pt = new BorderPropertyTemplate();
+
+ pt.drawBorders(a1c3, BorderStyle.THIN, BorderExtent.ALL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(4, pt.getNumBorders(i, j));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, BorderExtent.OUTSIDE);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(4, pt.getNumBorders(i, j));
+ if (i == 0) {
+ if (j == 0) {
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ } else if (j == 2) {
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ } else {
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ }
+ } else if (i == 2) {
+ if (j == 0) {
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ } else if (j == 2) {
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ } else {
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ }
+ } else {
+ if (j == 0) {
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ } else if (j == 2) {
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ } else {
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ }
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(0, pt.getNumBorders(i, j));
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, BorderExtent.TOP);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (i == 0) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, BorderExtent.BOTTOM);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (i == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, BorderExtent.LEFT);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (j == 0) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, BorderExtent.RIGHT);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (j == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, BorderExtent.HORIZONTAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, BorderExtent.INSIDE_HORIZONTAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (i == 0) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ } else if (i == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ } else {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, BorderExtent.OUTSIDE_HORIZONTAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (i == 0) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ } else if (i == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, BorderExtent.VERTICAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, BorderExtent.INSIDE_VERTICAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (j == 0) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ } else if (j == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ } else {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, BorderExtent.OUTSIDE_VERTICAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (j == 0) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ } else if (j == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertMedium(pt.getTemplateProperty(i ,j, CellUtil.BORDER_RIGHT));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ }
+ }
+ }
+ }
+
+ @Test
+ public void drawBorderColors() throws IOException {
+ CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);
+ BorderPropertyTemplate pt = new BorderPropertyTemplate();
+
+ pt.drawBorderColors(a1c3, IndexedColors.RED.getIndex(), BorderExtent.ALL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ CellAddress addr = new CellAddress(i, j);
+ String msg = addr.formatAsString();
+ assertEquals(msg, 4, pt.getNumBorders(i, j));
+ assertEquals(msg, 4, pt.getNumBorderColors(i, j));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ }
+ }
+
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.OUTSIDE);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(4, pt.getNumBorders(i, j));
+ assertEquals(4, pt.getNumBorderColors(i, j));
+ if (i == 0) {
+ if (j == 0) {
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ } else if (j == 2) {
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ } else {
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ }
+ } else if (i == 2) {
+ if (j == 0) {
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ } else if (j == 2) {
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ } else {
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ }
+ } else {
+ if (j == 0) {
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ } else if (j == 2) {
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ } else {
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ }
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), BorderExtent.NONE);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(0, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ }
+ }
+
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.TOP);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (i == 0) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.BOTTOM);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (i == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.LEFT);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (j == 0) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.RIGHT);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (j == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.HORIZONTAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertEquals(2, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.INSIDE_HORIZONTAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (i == 0) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ } else if (i == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ } else {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertEquals(2, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.OUTSIDE_HORIZONTAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (i == 0) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ } else if (i == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.VERTICAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertEquals(2, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.INSIDE_VERTICAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (j == 0) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ } else if (j == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ } else {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertEquals(2, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ }
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), BorderExtent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.OUTSIDE_VERTICAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ if (j == 0) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ } else if (j == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ }
+ }
+ }
+ }
+
+ @Test
+ public void drawBordersWithColors() throws IOException {
+ CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);
+ BorderPropertyTemplate pt = new BorderPropertyTemplate();
+
+ pt.drawBorders(a1c3, BorderStyle.MEDIUM, IndexedColors.RED.getIndex(), BorderExtent.ALL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(4, pt.getNumBorders(i, j));
+ assertEquals(4, pt.getNumBorderColors(i, j));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
+ }
+ }
+
+ pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE);
+ pt.drawBorders(a1c3, BorderStyle.NONE, IndexedColors.RED.getIndex(), BorderExtent.ALL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(4, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ assertNone(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertNone(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertNone(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertNone(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ }
+ }
+ }
+
+ @Test
+ public void applyBorders() throws IOException {
+ CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);
+ CellRangeAddress b2 = new CellRangeAddress(1, 1, 1, 1);
+ BorderPropertyTemplate pt = new BorderPropertyTemplate();
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = wb.createSheet();
+
+ pt.drawBorders(a1c3, BorderStyle.THIN, IndexedColors.RED.getIndex(), BorderExtent.ALL);
+ pt.applyBorders(sheet);
+
+ for (Row row: sheet) {
+ for (Cell cell: row) {
+ CellStyle cs = cell.getCellStyle();
+
+ assertThin(cs.getBorderTop());
+ assertRed(cs.getTopBorderColor());
+
+ assertThin(cs.getBorderBottom());
+ assertRed(cs.getBottomBorderColor());
+
+ assertThin(cs.getBorderLeft());
+ assertRed(cs.getLeftBorderColor());
+
+ assertThin(cs.getBorderRight());
+ assertRed(cs.getRightBorderColor());
+ }
+ }
+
+ pt.drawBorders(b2, BorderStyle.NONE, BorderExtent.ALL);
+ pt.applyBorders(sheet);
+
+ for (Row row: sheet) {
+ for (Cell cell: row) {
+ CellStyle cs = cell.getCellStyle();
+ if (cell.getColumnIndex() != 1 || row.getRowNum() == 0) {
+ assertThin(cs.getBorderTop());
+ assertRed(cs.getTopBorderColor());
+ } else {
+ assertNone(cs.getBorderTop());
+ }
+ if (cell.getColumnIndex() != 1 || row.getRowNum() == 2) {
+ assertThin(cs.getBorderBottom());
+ assertRed(cs.getBottomBorderColor());
+ } else {
+ assertNone(cs.getBorderBottom());
+ }
+ if (cell.getColumnIndex() == 0 || row.getRowNum() != 1) {
+ assertThin(cs.getBorderLeft());
+ assertRed(cs.getLeftBorderColor());
+ } else {
+ assertNone(cs.getBorderLeft());
+ }
+ if (cell.getColumnIndex() == 2 || row.getRowNum() != 1) {
+ assertThin(cs.getBorderRight());
+ assertRed(cs.getRightBorderColor());
+ } else {
+ assertNone(cs.getBorderRight());
+ }
+ }
+ }
+
+ wb.close();
+ }
+
+ // helper functions to make sure template properties were set correctly
+
+ //////// Border Styles ///////////
+ private static void assertNone(Object actual) {
+ assertNone((BorderStyle) actual);
+ }
+ private static void assertNone(BorderStyle actual) {
+ assertEquals(BorderStyle.NONE, actual);
+ }
+
+ private static void assertThin(Object actual) {
+ assertThin((BorderStyle) actual);
+ }
+ private static void assertThin(BorderStyle actual) {
+ assertEquals(BorderStyle.THIN, actual);
+ }
+
+ private static void assertMedium(Object actual) {
+ assertMedium((BorderStyle) actual);
+ }
+ private static void assertMedium(BorderStyle actual) {
+ assertEquals(BorderStyle.MEDIUM, actual);
+ }
+
+ //////// Border Colors (use IndexedColor codes, for now ///////////
+ private static void assertRed(Object actual) {
+ IndexedColors actualColor = IndexedColors.fromInt((Short) actual);
+ assertRed(actualColor);
+ }
+ private static void assertRed(IndexedColors actual) {
+ assertEquals(IndexedColors.RED, actual);
+ }
+
+ private static void assertBlue(Object actual) {
+ IndexedColors actualColor = IndexedColors.fromInt((Short) actual);
+ assertBlue(actualColor);
+ }
+ private static void assertBlue(IndexedColors actual) {
+ assertEquals(IndexedColors.BLUE, actual);
+ }
+}
+++ /dev/null
-/* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-==================================================================== */
-
-package org.apache.poi.ss.util;
-
-import static org.junit.Assert.assertEquals;
-
-import java.io.IOException;
-
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.BorderStyle;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellStyle;
-import org.apache.poi.ss.usermodel.IndexedColors;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.ss.util.PropertyTemplate.Extent;
-import org.junit.Test;
-
-/**
- * Tests Spreadsheet PropertyTemplate
- *
- * @see org.apache.poi.ss.util.PropertyTemplate
- */
-public final class TestPropertyTemplate {
- @Test
- public void getNumBorders() throws IOException {
- CellRangeAddress a1 = new CellRangeAddress(0, 0, 0, 0);
- PropertyTemplate pt = new PropertyTemplate();
-
- pt.drawBorders(a1, BorderStyle.THIN, Extent.TOP);
- assertEquals(1, pt.getNumBorders(0, 0));
-
- pt.drawBorders(a1, BorderStyle.MEDIUM, Extent.BOTTOM);
- assertEquals(2, pt.getNumBorders(0, 0));
-
- pt.drawBorders(a1, BorderStyle.MEDIUM, Extent.NONE);
- assertEquals(0, pt.getNumBorders(0, 0));
- }
-
- @Test
- public void getNumBorderColors() throws IOException {
- CellRangeAddress a1 = new CellRangeAddress(0, 0, 0, 0);
- PropertyTemplate pt = new PropertyTemplate();
-
- pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), Extent.TOP);
- assertEquals(1, pt.getNumBorderColors(0, 0));
-
- pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), Extent.BOTTOM);
- assertEquals(2, pt.getNumBorderColors(0, 0));
-
- pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), Extent.NONE);
- assertEquals(0, pt.getNumBorderColors(0, 0));
- }
-
- @Test
- public void getTemplateProperties() throws IOException {
- CellRangeAddress a1 = new CellRangeAddress(0, 0, 0, 0);
- PropertyTemplate pt = new PropertyTemplate();
-
- pt.drawBorders(a1, BorderStyle.THIN, Extent.TOP);
- assertThin(pt.getTemplateProperty(0, 0, CellUtil.BORDER_TOP));
-
- pt.drawBorders(a1, BorderStyle.MEDIUM, Extent.BOTTOM);
- assertMedium(pt.getTemplateProperty(0, 0, CellUtil.BORDER_BOTTOM));
-
- pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), Extent.TOP);
- assertRed(pt.getTemplateProperty(0, 0, CellUtil.TOP_BORDER_COLOR));
-
- pt.drawBorderColors(a1, IndexedColors.BLUE.getIndex(), Extent.BOTTOM);
- assertBlue(pt.getTemplateProperty(0, 0, CellUtil.BOTTOM_BORDER_COLOR));
- }
-
- @Test
- public void drawBorders() throws IOException {
- CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);
- PropertyTemplate pt = new PropertyTemplate();
-
- pt.drawBorders(a1c3, BorderStyle.THIN, Extent.ALL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- assertEquals(4, pt.getNumBorders(i, j));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.MEDIUM, Extent.OUTSIDE);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- assertEquals(4, pt.getNumBorders(i, j));
- if (i == 0) {
- if (j == 0) {
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- } else if (j == 2) {
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- } else {
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- }
- } else if (i == 2) {
- if (j == 0) {
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- } else if (j == 2) {
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- } else {
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- }
- } else {
- if (j == 0) {
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- } else if (j == 2) {
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- } else {
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertThin(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- }
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- assertEquals(0, pt.getNumBorders(i, j));
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.MEDIUM, Extent.TOP);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (i == 0) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- } else {
- assertEquals(0, pt.getNumBorders(i, j));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorders(a1c3, BorderStyle.MEDIUM, Extent.BOTTOM);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (i == 2) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- } else {
- assertEquals(0, pt.getNumBorders(i, j));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorders(a1c3, BorderStyle.MEDIUM, Extent.LEFT);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (j == 0) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- } else {
- assertEquals(0, pt.getNumBorders(i, j));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorders(a1c3, BorderStyle.MEDIUM, Extent.RIGHT);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (j == 2) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- } else {
- assertEquals(0, pt.getNumBorders(i, j));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorders(a1c3, BorderStyle.MEDIUM, Extent.HORIZONTAL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- assertEquals(2, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorders(a1c3, BorderStyle.MEDIUM, Extent.INSIDE_HORIZONTAL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (i == 0) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- } else if (i == 2) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- } else {
- assertEquals(2, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorders(a1c3, BorderStyle.MEDIUM, Extent.OUTSIDE_HORIZONTAL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (i == 0) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- } else if (i == 2) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- } else {
- assertEquals(0, pt.getNumBorders(i, j));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorders(a1c3, BorderStyle.MEDIUM, Extent.VERTICAL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- assertEquals(2, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorders(a1c3, BorderStyle.MEDIUM, Extent.INSIDE_VERTICAL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (j == 0) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- } else if (j == 2) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- } else {
- assertEquals(2, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorders(a1c3, BorderStyle.MEDIUM, Extent.OUTSIDE_VERTICAL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (j == 0) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- } else if (j == 2) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertMedium(pt.getTemplateProperty(i ,j, CellUtil.BORDER_RIGHT));
- } else {
- assertEquals(0, pt.getNumBorders(i, j));
- }
- }
- }
- }
-
- @Test
- public void drawBorderColors() throws IOException {
- CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);
- PropertyTemplate pt = new PropertyTemplate();
-
- pt.drawBorderColors(a1c3, IndexedColors.RED.getIndex(), Extent.ALL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- CellAddress addr = new CellAddress(i, j);
- String msg = addr.formatAsString();
- assertEquals(msg, 4, pt.getNumBorders(i, j));
- assertEquals(msg, 4, pt.getNumBorderColors(i, j));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- }
- }
-
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), Extent.OUTSIDE);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- assertEquals(4, pt.getNumBorders(i, j));
- assertEquals(4, pt.getNumBorderColors(i, j));
- if (i == 0) {
- if (j == 0) {
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- } else if (j == 2) {
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- } else {
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- }
- } else if (i == 2) {
- if (j == 0) {
- assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- } else if (j == 2) {
- assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- } else {
- assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- }
- } else {
- if (j == 0) {
- assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- } else if (j == 2) {
- assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- } else {
- assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- }
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), Extent.NONE);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- assertEquals(0, pt.getNumBorders(i, j));
- assertEquals(0, pt.getNumBorderColors(i, j));
- }
- }
-
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), Extent.TOP);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (i == 0) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertEquals(1, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- } else {
- assertEquals(0, pt.getNumBorders(i, j));
- assertEquals(0, pt.getNumBorderColors(i, j));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), Extent.BOTTOM);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (i == 2) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertEquals(1, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- } else {
- assertEquals(0, pt.getNumBorders(i, j));
- assertEquals(0, pt.getNumBorderColors(i, j));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), Extent.LEFT);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (j == 0) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertEquals(1, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- } else {
- assertEquals(0, pt.getNumBorders(i, j));
- assertEquals(0, pt.getNumBorderColors(i, j));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), Extent.RIGHT);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (j == 2) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertEquals(1, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- } else {
- assertEquals(0, pt.getNumBorders(i, j));
- assertEquals(0, pt.getNumBorderColors(i, j));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), Extent.HORIZONTAL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- assertEquals(2, pt.getNumBorders(i, j));
- assertEquals(2, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), Extent.INSIDE_HORIZONTAL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (i == 0) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertEquals(1, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- } else if (i == 2) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertEquals(1, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- } else {
- assertEquals(2, pt.getNumBorders(i, j));
- assertEquals(2, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), Extent.OUTSIDE_HORIZONTAL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (i == 0) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertEquals(1, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- } else if (i == 2) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertEquals(1, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- } else {
- assertEquals(0, pt.getNumBorders(i, j));
- assertEquals(0, pt.getNumBorderColors(i, j));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), Extent.VERTICAL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- assertEquals(2, pt.getNumBorders(i, j));
- assertEquals(2, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), Extent.INSIDE_VERTICAL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (j == 0) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertEquals(1, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- } else if (j == 2) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertEquals(1, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- } else {
- assertEquals(2, pt.getNumBorders(i, j));
- assertEquals(2, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- }
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), Extent.NONE);
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), Extent.OUTSIDE_VERTICAL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- if (j == 0) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertEquals(1, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- } else if (j == 2) {
- assertEquals(1, pt.getNumBorders(i, j));
- assertEquals(1, pt.getNumBorderColors(i, j));
- assertBlue(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- } else {
- assertEquals(0, pt.getNumBorders(i, j));
- assertEquals(0, pt.getNumBorderColors(i, j));
- }
- }
- }
- }
-
- @Test
- public void drawBordersWithColors() throws IOException {
- CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);
- PropertyTemplate pt = new PropertyTemplate();
-
- pt.drawBorders(a1c3, BorderStyle.MEDIUM, IndexedColors.RED.getIndex(), Extent.ALL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- assertEquals(4, pt.getNumBorders(i, j));
- assertEquals(4, pt.getNumBorderColors(i, j));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertMedium(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.BOTTOM_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
- assertRed(pt.getTemplateProperty(i, j, CellUtil.RIGHT_BORDER_COLOR));
- }
- }
-
- pt.drawBorders(a1c3, BorderStyle.NONE, Extent.NONE);
- pt.drawBorders(a1c3, BorderStyle.NONE, IndexedColors.RED.getIndex(), Extent.ALL);
- for (int i = 0; i <= 2; i++) {
- for (int j = 0; j <= 2; j++) {
- assertEquals(4, pt.getNumBorders(i, j));
- assertEquals(0, pt.getNumBorderColors(i, j));
- assertNone(pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
- assertNone(pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
- assertNone(pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
- assertNone(pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
- }
- }
- }
-
- @Test
- public void applyBorders() throws IOException {
- CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);
- CellRangeAddress b2 = new CellRangeAddress(1, 1, 1, 1);
- PropertyTemplate pt = new PropertyTemplate();
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet();
-
- pt.drawBorders(a1c3, BorderStyle.THIN, IndexedColors.RED.getIndex(), Extent.ALL);
- pt.applyBorders(sheet);
-
- for (Row row: sheet) {
- for (Cell cell: row) {
- CellStyle cs = cell.getCellStyle();
-
- assertThin(cs.getBorderTop());
- assertRed(cs.getTopBorderColor());
-
- assertThin(cs.getBorderBottom());
- assertRed(cs.getBottomBorderColor());
-
- assertThin(cs.getBorderLeft());
- assertRed(cs.getLeftBorderColor());
-
- assertThin(cs.getBorderRight());
- assertRed(cs.getRightBorderColor());
- }
- }
-
- pt.drawBorders(b2, BorderStyle.NONE, Extent.ALL);
- pt.applyBorders(sheet);
-
- for (Row row: sheet) {
- for (Cell cell: row) {
- CellStyle cs = cell.getCellStyle();
- if (cell.getColumnIndex() != 1 || row.getRowNum() == 0) {
- assertThin(cs.getBorderTop());
- assertRed(cs.getTopBorderColor());
- } else {
- assertNone(cs.getBorderTop());
- }
- if (cell.getColumnIndex() != 1 || row.getRowNum() == 2) {
- assertThin(cs.getBorderBottom());
- assertRed(cs.getBottomBorderColor());
- } else {
- assertNone(cs.getBorderBottom());
- }
- if (cell.getColumnIndex() == 0 || row.getRowNum() != 1) {
- assertThin(cs.getBorderLeft());
- assertRed(cs.getLeftBorderColor());
- } else {
- assertNone(cs.getBorderLeft());
- }
- if (cell.getColumnIndex() == 2 || row.getRowNum() != 1) {
- assertThin(cs.getBorderRight());
- assertRed(cs.getRightBorderColor());
- } else {
- assertNone(cs.getBorderRight());
- }
- }
- }
-
- wb.close();
- }
-
- // helper functions to make sure template properties were set correctly
-
- //////// Border Styles ///////////
- private static void assertNone(Object actual) {
- assertNone((BorderStyle) actual);
- }
- private static void assertNone(BorderStyle actual) {
- assertEquals(BorderStyle.NONE, actual);
- }
-
- private static void assertThin(Object actual) {
- assertThin((BorderStyle) actual);
- }
- private static void assertThin(BorderStyle actual) {
- assertEquals(BorderStyle.THIN, actual);
- }
-
- private static void assertMedium(Object actual) {
- assertMedium((BorderStyle) actual);
- }
- private static void assertMedium(BorderStyle actual) {
- assertEquals(BorderStyle.MEDIUM, actual);
- }
-
- //////// Border Colors (use IndexedColor codes, for now ///////////
- private static void assertRed(Object actual) {
- IndexedColors actualColor = IndexedColors.fromInt((Short) actual);
- assertRed(actualColor);
- }
- private static void assertRed(IndexedColors actual) {
- assertEquals(IndexedColors.RED, actual);
- }
-
- private static void assertBlue(Object actual) {
- IndexedColors actualColor = IndexedColors.fromInt((Short) actual);
- assertBlue(actualColor);
- }
- private static void assertBlue(IndexedColors actual) {
- assertEquals(IndexedColors.BLUE, actual);
- }
-}