-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.ss.util;\r
-\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.Map;\r
-import java.util.Set;\r
-\r
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
-import org.apache.poi.ss.SpreadsheetVersion;\r
-import org.apache.poi.ss.usermodel.Cell;\r
-import org.apache.poi.ss.usermodel.CellStyle;\r
-import org.apache.poi.ss.usermodel.IndexedColors;\r
-import org.apache.poi.ss.usermodel.Row;\r
-import org.apache.poi.ss.usermodel.Sheet;\r
-import org.apache.poi.ss.usermodel.Workbook;\r
-\r
-/**\r
- * <p>\r
- * A {@link PropertyTemplate} is a template that can be applied to any sheet in\r
- * a project. It contains all the border type and color attributes needed to\r
- * draw all the borders for a single sheet. That template can be applied to any\r
- * sheet in any workbook.\r
- * \r
- * This class requires the full spreadsheet to be in memory so\r
- * {@link SWorkbook} Spreadsheets are not supported. The same\r
- * {@link PropertyTemplate} can, however, be applied to both\r
- * {@link org.apache.poi.hssf.usermodel.HSSFWorkbook}, and Workbook objects\r
- * if necessary. Portions of the border that fall outside the max range of the\r
- * {@link HSSFWorkbook} sheet are ignored.\r
- * </p>\r
- * \r
- * <p>\r
- * This would replace {@link RegionUtil}.\r
- * </p>\r
- */\r
-public final class PropertyTemplate {\r
-\r
- /**\r
- * Provides various extents of the properties being added to the template\r
- */\r
- public enum Extent {\r
- /**\r
- * No properties defined. This can be used to remove existing\r
- * properties.\r
- */\r
- NONE,\r
-\r
- /**\r
- * All borders, that is top, bottom, left and right, including interior\r
- * borders for the range. Does not include diagonals which are different\r
- * and not implemented here.\r
- */\r
- ALL,\r
-\r
- /**\r
- * All inside borders. This is top, bottom, left, and right borders, but\r
- * restricted to the interior borders for the range. For a range of one\r
- * cell, this will produce no borders.\r
- */\r
- INSIDE,\r
-\r
- /**\r
- * All outside borders. That is top, bottom, left and right borders that\r
- * bound the range only.\r
- */\r
- OUTSIDE,\r
-\r
- /**\r
- * This is just the top border for the range. No interior borders will\r
- * be produced.\r
- */\r
- TOP,\r
-\r
- /**\r
- * This is just the bottom border for the range. No interior borders\r
- * will be produced.\r
- */\r
- BOTTOM,\r
-\r
- /**\r
- * This is just the left border for the range, no interior borders will\r
- * be produced.\r
- */\r
- LEFT,\r
-\r
- /**\r
- * This is just the right border for the range, no interior borders will\r
- * be produced.\r
- */\r
- RIGHT,\r
-\r
- /**\r
- * This is all horizontal borders for the range, including interior and\r
- * outside borders.\r
- */\r
- HORIZONTAL,\r
-\r
- /**\r
- * This is just the interior horizontal borders for the range.\r
- */\r
- INSIDE_HORIZONTAL,\r
-\r
- /**\r
- * This is just the outside horizontal borders for the range.\r
- */\r
- OUTSIDE_HORIZONTAL,\r
-\r
- /**\r
- * This is all vertical borders for the range, including interior and\r
- * outside borders.\r
- */\r
- VERTICAL,\r
-\r
- /**\r
- * This is just the interior vertical borders for the range.\r
- */\r
- INSIDE_VERTICAL,\r
-\r
- /**\r
- * This is just the outside vertical borders for the range.\r
- */\r
- OUTSIDE_VERTICAL\r
- }\r
-\r
- /**\r
- * This is a list of cell properties for one shot application to a range of\r
- * cells at a later time.\r
- */\r
- private Map<CellAddress, Map<String, Object>> _propertyTemplate;\r
-\r
- /**\r
- *\r
- */\r
- public PropertyTemplate() {\r
- _propertyTemplate = new HashMap<CellAddress, Map<String, Object>>();\r
- }\r
-\r
- /**\r
- * Draws a group of cell borders for a cell range. The borders are not\r
- * applied to the cells at this time, just the template is drawn. To apply\r
- * the drawn borders to a sheet, use {@link #applyBorders}.\r
- * \r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which borders are\r
- * drawn.\r
- * @param borderType\r
- * - Type of border to draw. Use BORDER_XXX constants in\r
- * {@link CellStyle}.\r
- * @param extent\r
- * - {@link PropertyTemplate.Extent} of the borders to be\r
- * applied.\r
- */\r
- public void drawBorders(CellRangeAddress range, short borderType,\r
- Extent extent) {\r
- switch (extent) {\r
- case NONE:\r
- removeBorders(range);\r
- break;\r
- case ALL:\r
- drawHorizontalBorders(range, borderType, Extent.ALL);\r
- drawVerticalBorders(range, borderType, Extent.ALL);\r
- break;\r
- case INSIDE:\r
- drawHorizontalBorders(range, borderType, Extent.INSIDE);\r
- drawVerticalBorders(range, borderType, Extent.INSIDE);\r
- break;\r
- case OUTSIDE:\r
- drawOutsideBorders(range, borderType, Extent.ALL);\r
- break;\r
- case TOP:\r
- drawTopBorder(range, borderType);\r
- break;\r
- case BOTTOM:\r
- drawBottomBorder(range, borderType);\r
- break;\r
- case LEFT:\r
- drawLeftBorder(range, borderType);\r
- break;\r
- case RIGHT:\r
- drawRightBorder(range, borderType);\r
- break;\r
- case HORIZONTAL:\r
- drawHorizontalBorders(range, borderType, Extent.ALL);\r
- break;\r
- case INSIDE_HORIZONTAL:\r
- drawHorizontalBorders(range, borderType, Extent.INSIDE);\r
- break;\r
- case OUTSIDE_HORIZONTAL:\r
- drawOutsideBorders(range, borderType, Extent.HORIZONTAL);\r
- break;\r
- case VERTICAL:\r
- drawVerticalBorders(range, borderType, Extent.ALL);\r
- break;\r
- case INSIDE_VERTICAL:\r
- drawVerticalBorders(range, borderType, Extent.INSIDE);\r
- break;\r
- case OUTSIDE_VERTICAL:\r
- drawOutsideBorders(range, borderType, Extent.VERTICAL);\r
- break;\r
- }\r
- }\r
-\r
- /**\r
- * Draws a group of cell borders for a cell range. The borders are not\r
- * applied to the cells at this time, just the template is drawn. To apply\r
- * the drawn borders to a sheet, use {@link #applyBorders}.\r
- * \r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which borders are\r
- * drawn.\r
- * @param borderType\r
- * - Type of border to draw. Use BORDER_XXX constants in\r
- * {@link CellStyle}.\r
- * @param color\r
- * - Color index from {@link IndexedColors} used to draw the\r
- * borders.\r
- * @param extent\r
- * - {@link PropertyTemplate.Extent} of the borders to be\r
- * applied.\r
- */\r
- public void drawBorders(CellRangeAddress range, short borderType,\r
- short color, Extent extent) {\r
- drawBorders(range, borderType, extent);\r
- if (borderType != CellStyle.BORDER_NONE) {\r
- drawBorderColors(range, color, extent);\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Draws the top border for a range of cells\r
- * </p>\r
- * \r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which borders are\r
- * drawn.\r
- * @param borderType\r
- * - Type of border to draw. Use BORDER_XXX constants in\r
- * {@link CellStyle}.\r
- */\r
- private void drawTopBorder(CellRangeAddress range, short borderType) {\r
- int row = range.getFirstRow();\r
- int firstCol = range.getFirstColumn();\r
- int lastCol = range.getLastColumn();\r
- for (int i = firstCol; i <= lastCol; i++) {\r
- addProperty(row, i, CellUtil.BORDER_TOP, borderType);\r
- if (borderType == CellStyle.BORDER_NONE && row > 0) {\r
- addProperty(row - 1, i, CellUtil.BORDER_BOTTOM, borderType);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Draws the bottom border for a range of cells\r
- * </p>\r
- * \r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which borders are\r
- * drawn.\r
- * @param borderType\r
- * - Type of border to draw. Use BORDER_XXX constants in\r
- * {@link CellStyle}.\r
- */\r
- private void drawBottomBorder(CellRangeAddress range, short borderType) {\r
- int row = range.getLastRow();\r
- int firstCol = range.getFirstColumn();\r
- int lastCol = range.getLastColumn();\r
- for (int i = firstCol; i <= lastCol; i++) {\r
- addProperty(row, i, CellUtil.BORDER_BOTTOM, borderType);\r
- if (borderType == CellStyle.BORDER_NONE\r
- && row < SpreadsheetVersion.EXCEL2007.getMaxRows() - 1) {\r
- addProperty(row + 1, i, CellUtil.BORDER_TOP, borderType);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Draws the left border for a range of cells\r
- * </p>\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which borders are\r
- * drawn.\r
- * @param borderType\r
- * - Type of border to draw. Use BORDER_XXX constants in\r
- * {@link CellStyle}.\r
- */\r
- private void drawLeftBorder(CellRangeAddress range, short borderType) {\r
- int firstRow = range.getFirstRow();\r
- int lastRow = range.getLastRow();\r
- int col = range.getFirstColumn();\r
- for (int i = firstRow; i <= lastRow; i++) {\r
- addProperty(i, col, CellUtil.BORDER_LEFT, borderType);\r
- if (borderType == CellStyle.BORDER_NONE && col > 0) {\r
- addProperty(i, col - 1, CellUtil.BORDER_RIGHT, borderType);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Draws the right border for a range of cells\r
- * </p>\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which borders are\r
- * drawn.\r
- * @param borderType\r
- * - Type of border to draw. Use BORDER_XXX constants in\r
- * {@link CellStyle}.\r
- */\r
- private void drawRightBorder(CellRangeAddress range, short borderType) {\r
- int firstRow = range.getFirstRow();\r
- int lastRow = range.getLastRow();\r
- int col = range.getLastColumn();\r
- for (int i = firstRow; i <= lastRow; i++) {\r
- addProperty(i, col, CellUtil.BORDER_RIGHT, borderType);\r
- if (borderType == CellStyle.BORDER_NONE\r
- && col < SpreadsheetVersion.EXCEL2007.getMaxColumns() - 1) {\r
- addProperty(i, col + 1, CellUtil.BORDER_LEFT, borderType);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Draws the outside borders for a range of cells.\r
- * </p>\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which borders are\r
- * drawn.\r
- * @param borderType\r
- * - Type of border to draw. Use BORDER_XXX constants in\r
- * {@link CellStyle}.\r
- * @param extent\r
- * - {@link PropertyTemplate.Extent} of the borders to be\r
- * applied. Valid Values are:\r
- * <ul>\r
- * <li>CellBorder.Extent.ALL</li>\r
- * <li>CellBorder.Extent.HORIZONTAL</li>\r
- * <li>CellBorder.Extent.VERTICAL</li>\r
- * </ul>\r
- */\r
- private void drawOutsideBorders(CellRangeAddress range, short borderType,\r
- Extent extent) {\r
- switch (extent) {\r
- case ALL:\r
- case HORIZONTAL:\r
- case VERTICAL:\r
- if (extent == Extent.ALL || extent == Extent.HORIZONTAL) {\r
- drawTopBorder(range, borderType);\r
- drawBottomBorder(range, borderType);\r
- }\r
- if (extent == Extent.ALL || extent == Extent.VERTICAL) {\r
- drawLeftBorder(range, borderType);\r
- drawRightBorder(range, borderType);\r
- }\r
- break;\r
- default:\r
- throw new IllegalArgumentException(\r
- "Unsupported PropertyTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL");\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Draws the horizontal borders for a range of cells.\r
- * </p>\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which borders are\r
- * drawn.\r
- * @param borderType\r
- * - Type of border to draw. Use BORDER_XXX constants in\r
- * {@link CellStyle}.\r
- * @param extent\r
- * - {@link PropertyTemplate.Extent} of the borders to be\r
- * applied. Valid Values are:\r
- * <ul>\r
- * <li>CellBorder.Extent.ALL</li>\r
- * <li>CellBorder.Extent.INSIDE</li>\r
- * </ul>\r
- */\r
- private void drawHorizontalBorders(CellRangeAddress range, short borderType,\r
- Extent extent) {\r
- switch (extent) {\r
- case ALL:\r
- case INSIDE:\r
- int firstRow = range.getFirstRow();\r
- int lastRow = range.getLastRow();\r
- int firstCol = range.getFirstColumn();\r
- int lastCol = range.getLastColumn();\r
- for (int i = firstRow; i <= lastRow; i++) {\r
- CellRangeAddress row = new CellRangeAddress(i, i, firstCol,\r
- lastCol);\r
- if (extent == Extent.ALL || i > firstRow) {\r
- drawTopBorder(row, borderType);\r
- }\r
- if (extent == Extent.ALL || i < lastRow) {\r
- drawBottomBorder(row, borderType);\r
- }\r
- }\r
- break;\r
- default:\r
- throw new IllegalArgumentException(\r
- "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Draws the vertical borders for a range of cells.\r
- * </p>\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which borders are\r
- * drawn.\r
- * @param borderType\r
- * - Type of border to draw. Use BORDER_XXX constants in\r
- * {@link CellStyle}.\r
- * @param extent\r
- * - {@link PropertyTemplate.Extent} of the borders to be\r
- * applied. Valid Values are:\r
- * <ul>\r
- * <li>CellBorder.Extent.ALL</li>\r
- * <li>CellBorder.Extent.INSIDE</li>\r
- * </ul>\r
- */\r
- private void drawVerticalBorders(CellRangeAddress range, short borderType,\r
- Extent extent) {\r
- switch (extent) {\r
- case ALL:\r
- case INSIDE:\r
- int firstRow = range.getFirstRow();\r
- int lastRow = range.getLastRow();\r
- int firstCol = range.getFirstColumn();\r
- int lastCol = range.getLastColumn();\r
- for (int i = firstCol; i <= lastCol; i++) {\r
- CellRangeAddress row = new CellRangeAddress(firstRow, lastRow,\r
- i, i);\r
- if (extent == Extent.ALL || i > firstCol) {\r
- drawLeftBorder(row, borderType);\r
- }\r
- if (extent == Extent.ALL || i < lastCol) {\r
- drawRightBorder(row, borderType);\r
- }\r
- }\r
- break;\r
- default:\r
- throw new IllegalArgumentException(\r
- "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");\r
- }\r
- }\r
-\r
- /**\r
- * Removes all border properties from this {@link PropertyTemplate} for the\r
- * specified range.\r
- * \r
- * @parm range - {@link CellRangeAddress} range of cells to remove borders.\r
- */\r
- private void removeBorders(CellRangeAddress range) {\r
- Set<String> properties = new HashSet<String>();\r
- properties.add(CellUtil.BORDER_TOP);\r
- properties.add(CellUtil.BORDER_BOTTOM);\r
- properties.add(CellUtil.BORDER_LEFT);\r
- properties.add(CellUtil.BORDER_RIGHT);\r
- for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) {\r
- for (int col = range.getFirstColumn(); col <= range\r
- .getLastColumn(); col++) {\r
- removeProperties(row, col, properties);\r
- }\r
- }\r
- removeBorderColors(range);\r
- }\r
-\r
- /**\r
- * Applies the drawn borders to a Sheet. The borders that are applied are\r
- * the ones that have been drawn by the {@link #drawBorders} and\r
- * {@link #drawBorderColors} methods.\r
- *\r
- * @param sheet\r
- * - {@link Sheet} on which to apply borders\r
- */\r
- public void applyBorders(Sheet sheet) {\r
- Workbook wb = sheet.getWorkbook();\r
- for (Map.Entry<CellAddress, Map<String, Object>> entry : _propertyTemplate\r
- .entrySet()) {\r
- CellAddress cellAddress = entry.getKey();\r
- if (cellAddress.getRow() < wb.getSpreadsheetVersion().getMaxRows()\r
- && cellAddress.getColumn() < wb.getSpreadsheetVersion()\r
- .getMaxColumns()) {\r
- Map<String, Object> properties = entry.getValue();\r
- Row row = CellUtil.getRow(cellAddress.getRow(), sheet);\r
- Cell cell = CellUtil.getCell(row, cellAddress.getColumn());\r
- CellUtil.setCellStyleProperties(cell, properties);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Sets the color for a group of cell borders for a cell range. The borders\r
- * are not applied to the cells at this time, just the template is drawn. If\r
- * the borders do not exist, a BORDER_THIN border is used. To apply the\r
- * drawn borders to a sheet, use {@link #applyBorders}.\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which colors are\r
- * set.\r
- * @param color\r
- * - Color index from {@link IndexedColors} used to draw the\r
- * borders.\r
- * @param extent\r
- * - {@link PropertyTemplate.Extent} of the borders for which\r
- * colors are set.\r
- */\r
- public void drawBorderColors(CellRangeAddress range, short color,\r
- Extent extent) {\r
- switch (extent) {\r
- case NONE:\r
- removeBorderColors(range);\r
- break;\r
- case ALL:\r
- drawHorizontalBorderColors(range, color, Extent.ALL);\r
- drawVerticalBorderColors(range, color, Extent.ALL);\r
- break;\r
- case INSIDE:\r
- drawHorizontalBorderColors(range, color, Extent.INSIDE);\r
- drawVerticalBorderColors(range, color, Extent.INSIDE);\r
- break;\r
- case OUTSIDE:\r
- drawOutsideBorderColors(range, color, Extent.ALL);\r
- break;\r
- case TOP:\r
- drawTopBorderColor(range, color);\r
- break;\r
- case BOTTOM:\r
- drawBottomBorderColor(range, color);\r
- break;\r
- case LEFT:\r
- drawLeftBorderColor(range, color);\r
- break;\r
- case RIGHT:\r
- drawRightBorderColor(range, color);\r
- break;\r
- case HORIZONTAL:\r
- drawHorizontalBorderColors(range, color, Extent.ALL);\r
- break;\r
- case INSIDE_HORIZONTAL:\r
- drawHorizontalBorderColors(range, color, Extent.INSIDE);\r
- break;\r
- case OUTSIDE_HORIZONTAL:\r
- drawOutsideBorderColors(range, color, Extent.HORIZONTAL);\r
- break;\r
- case VERTICAL:\r
- drawVerticalBorderColors(range, color, Extent.ALL);\r
- break;\r
- case INSIDE_VERTICAL:\r
- drawVerticalBorderColors(range, color, Extent.INSIDE);\r
- break;\r
- case OUTSIDE_VERTICAL:\r
- drawOutsideBorderColors(range, color, Extent.VERTICAL);\r
- break;\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Sets the color of the top border for a range of cells.\r
- * </p>\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which colors are\r
- * set.\r
- * @param color\r
- * - Color index from {@link IndexedColors} used to draw the\r
- * borders.\r
- */\r
- private void drawTopBorderColor(CellRangeAddress range, short color) {\r
- int row = range.getFirstRow();\r
- int firstCol = range.getFirstColumn();\r
- int lastCol = range.getLastColumn();\r
- for (int i = firstCol; i <= lastCol; i++) {\r
- if (getTemplateProperty(row, i, CellUtil.BORDER_TOP) == 0) {\r
- drawTopBorder(new CellRangeAddress(row, row, i, i),\r
- CellStyle.BORDER_THIN);\r
- }\r
- addProperty(row, i, CellUtil.TOP_BORDER_COLOR, color);\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Sets the color of the bottom border for a range of cells.\r
- * </p>\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which colors are\r
- * set.\r
- * @param color\r
- * - Color index from {@link IndexedColors} used to draw the\r
- * borders.\r
- */\r
- private void drawBottomBorderColor(CellRangeAddress range, short color) {\r
- int row = range.getLastRow();\r
- int firstCol = range.getFirstColumn();\r
- int lastCol = range.getLastColumn();\r
- for (int i = firstCol; i <= lastCol; i++) {\r
- if (getTemplateProperty(row, i, CellUtil.BORDER_BOTTOM) == 0) {\r
- drawBottomBorder(new CellRangeAddress(row, row, i, i),\r
- CellStyle.BORDER_THIN);\r
- }\r
- addProperty(row, i, CellUtil.BOTTOM_BORDER_COLOR, color);\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Sets the color of the left border for a range of cells.\r
- * </p>\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which colors are\r
- * set.\r
- * @param color\r
- * - Color index from {@link IndexedColors} used to draw the\r
- * borders.\r
- */\r
- private void drawLeftBorderColor(CellRangeAddress range, short color) {\r
- int firstRow = range.getFirstRow();\r
- int lastRow = range.getLastRow();\r
- int col = range.getFirstColumn();\r
- for (int i = firstRow; i <= lastRow; i++) {\r
- if (getTemplateProperty(i, col, CellUtil.BORDER_LEFT) == 0) {\r
- drawLeftBorder(new CellRangeAddress(i, i, col, col),\r
- CellStyle.BORDER_THIN);\r
- }\r
- addProperty(i, col, CellUtil.LEFT_BORDER_COLOR, color);\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Sets the color of the right border for a range of cells. If the border is\r
- * not drawn, it defaults to BORDER_THIN\r
- * </p>\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which colors are\r
- * set.\r
- * @param color\r
- * - Color index from {@link IndexedColors} used to draw the\r
- * borders.\r
- */\r
- private void drawRightBorderColor(CellRangeAddress range, short color) {\r
- int firstRow = range.getFirstRow();\r
- int lastRow = range.getLastRow();\r
- int col = range.getLastColumn();\r
- for (int i = firstRow; i <= lastRow; i++) {\r
- if (getTemplateProperty(i, col, CellUtil.BORDER_RIGHT) == 0) {\r
- drawRightBorder(new CellRangeAddress(i, i, col, col),\r
- CellStyle.BORDER_THIN);\r
- }\r
- addProperty(i, col, CellUtil.RIGHT_BORDER_COLOR, color);\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Sets the color of the outside borders for a range of cells.\r
- * </p>\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which colors are\r
- * set.\r
- * @param color\r
- * - Color index from {@link IndexedColors} used to draw the\r
- * borders.\r
- * @param extent\r
- * - {@link PropertyTemplate.Extent} of the borders for which\r
- * colors are set. Valid Values are:\r
- * <ul>\r
- * <li>CellBorder.Extent.ALL</li>\r
- * <li>CellBorder.Extent.HORIZONTAL</li>\r
- * <li>CellBorder.Extent.VERTICAL</li>\r
- * </ul>\r
- */\r
- private void drawOutsideBorderColors(CellRangeAddress range, short color,\r
- Extent extent) {\r
- switch (extent) {\r
- case ALL:\r
- case HORIZONTAL:\r
- case VERTICAL:\r
- if (extent == Extent.ALL || extent == Extent.HORIZONTAL) {\r
- drawTopBorderColor(range, color);\r
- drawBottomBorderColor(range, color);\r
- }\r
- if (extent == Extent.ALL || extent == Extent.VERTICAL) {\r
- drawLeftBorderColor(range, color);\r
- drawRightBorderColor(range, color);\r
- }\r
- break;\r
- default:\r
- throw new IllegalArgumentException(\r
- "Unsupported PropertyTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL");\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Sets the color of the horizontal borders for a range of cells.\r
- * </p>\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which colors are\r
- * set.\r
- * @param color\r
- * - Color index from {@link IndexedColors} used to draw the\r
- * borders.\r
- * @param extent\r
- * - {@link PropertyTemplate.Extent} of the borders for which\r
- * colors are set. Valid Values are:\r
- * <ul>\r
- * <li>CellBorder.Extent.ALL</li>\r
- * <li>CellBorder.Extent.INSIDE</li>\r
- * </ul>\r
- */\r
- private void drawHorizontalBorderColors(CellRangeAddress range, short color,\r
- Extent extent) {\r
- switch (extent) {\r
- case ALL:\r
- case INSIDE:\r
- int firstRow = range.getFirstRow();\r
- int lastRow = range.getLastRow();\r
- int firstCol = range.getFirstColumn();\r
- int lastCol = range.getLastColumn();\r
- for (int i = firstRow; i <= lastRow; i++) {\r
- CellRangeAddress row = new CellRangeAddress(i, i, firstCol,\r
- lastCol);\r
- if (extent == Extent.ALL || i > firstRow) {\r
- drawTopBorderColor(row, color);\r
- }\r
- if (extent == Extent.ALL || i < lastRow) {\r
- drawBottomBorderColor(row, color);\r
- }\r
- }\r
- break;\r
- default:\r
- throw new IllegalArgumentException(\r
- "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Sets the color of the vertical borders for a range of cells.\r
- * </p>\r
- *\r
- * @param range\r
- * - {@link CellRangeAddress} range of cells on which colors are\r
- * set.\r
- * @param color\r
- * - Color index from {@link IndexedColors} used to draw the\r
- * borders.\r
- * @param extent\r
- * - {@link PropertyTemplate.Extent} of the borders for which\r
- * colors are set. Valid Values are:\r
- * <ul>\r
- * <li>CellBorder.Extent.ALL</li>\r
- * <li>CellBorder.Extent.INSIDE</li>\r
- * </ul>\r
- */\r
- private void drawVerticalBorderColors(CellRangeAddress range, short color,\r
- Extent extent) {\r
- switch (extent) {\r
- case ALL:\r
- case INSIDE:\r
- int firstRow = range.getFirstRow();\r
- int lastRow = range.getLastRow();\r
- int firstCol = range.getFirstColumn();\r
- int lastCol = range.getLastColumn();\r
- for (int i = firstCol; i <= lastCol; i++) {\r
- CellRangeAddress row = new CellRangeAddress(firstRow, lastRow,\r
- i, i);\r
- if (extent == Extent.ALL || i > firstCol) {\r
- drawLeftBorderColor(row, color);\r
- }\r
- if (extent == Extent.ALL || i < lastCol) {\r
- drawRightBorderColor(row, color);\r
- }\r
- }\r
- break;\r
- default:\r
- throw new IllegalArgumentException(\r
- "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE");\r
- }\r
- }\r
-\r
- /**\r
- * Removes all border properties from this {@link PropertyTemplate} for the\r
- * specified range.\r
- * \r
- * @parm range - {@link CellRangeAddress} range of cells to remove borders.\r
- */\r
- private void removeBorderColors(CellRangeAddress range) {\r
- Set<String> properties = new HashSet<String>();\r
- properties.add(CellUtil.TOP_BORDER_COLOR);\r
- properties.add(CellUtil.BOTTOM_BORDER_COLOR);\r
- properties.add(CellUtil.LEFT_BORDER_COLOR);\r
- properties.add(CellUtil.RIGHT_BORDER_COLOR);\r
- for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) {\r
- for (int col = range.getFirstColumn(); col <= range\r
- .getLastColumn(); col++) {\r
- removeProperties(row, col, properties);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Adds a property to this {@link PropertyTemplate} for a given cell\r
- *\r
- * @param row\r
- * @param col\r
- * @param property\r
- * @param value\r
- */\r
- private void addProperty(int row, int col, String property, short value) {\r
- CellAddress cell = new CellAddress(row, col);\r
- Map<String, Object> cellProperties = _propertyTemplate.get(cell);\r
- if (cellProperties == null) {\r
- cellProperties = new HashMap<String, Object>();\r
- }\r
- cellProperties.put(property, Short.valueOf(value));\r
- _propertyTemplate.put(cell, cellProperties);\r
- }\r
-\r
- /**\r
- * Removes a set of properties from this {@link PropertyTemplate} for a\r
- * given cell\r
- *\r
- * @param row\r
- * @param col\r
- * @param properties\r
- */\r
- private void removeProperties(int row, int col, Set<String> properties) {\r
- CellAddress cell = new CellAddress(row, col);\r
- Map<String, Object> cellProperties = _propertyTemplate.get(cell);\r
- if (cellProperties != null) {\r
- cellProperties.keySet().removeAll(properties);\r
- if (cellProperties.isEmpty()) {\r
- _propertyTemplate.remove(cell);\r
- } else {\r
- _propertyTemplate.put(cell, cellProperties);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Retrieves the number of borders assigned to a cell\r
- *\r
- * @param cell\r
- */\r
- public int getNumBorders(CellAddress cell) {\r
- Map<String, Object> cellProperties = _propertyTemplate.get(cell);\r
- if (cellProperties == null) {\r
- return 0;\r
- }\r
-\r
- int count = 0;\r
- for (String property : cellProperties.keySet()) {\r
- if (property.equals(CellUtil.BORDER_TOP))\r
- count += 1;\r
- if (property.equals(CellUtil.BORDER_BOTTOM))\r
- count += 1;\r
- if (property.equals(CellUtil.BORDER_LEFT))\r
- count += 1;\r
- if (property.equals(CellUtil.BORDER_RIGHT))\r
- count += 1;\r
- }\r
- return count;\r
- }\r
-\r
- /**\r
- * Retrieves the number of borders assigned to a cell\r
- *\r
- * @param row\r
- * @param col\r
- */\r
- public int getNumBorders(int row, int col) {\r
- return getNumBorders(new CellAddress(row, col));\r
- }\r
-\r
- /**\r
- * Retrieves the number of border colors assigned to a cell\r
- *\r
- * @param cell\r
- */\r
- public int getNumBorderColors(CellAddress cell) {\r
- Map<String, Object> cellProperties = _propertyTemplate.get(cell);\r
- if (cellProperties == null) {\r
- return 0;\r
- }\r
-\r
- int count = 0;\r
- for (String property : cellProperties.keySet()) {\r
- if (property.equals(CellUtil.TOP_BORDER_COLOR))\r
- count += 1;\r
- if (property.equals(CellUtil.BOTTOM_BORDER_COLOR))\r
- count += 1;\r
- if (property.equals(CellUtil.LEFT_BORDER_COLOR))\r
- count += 1;\r
- if (property.equals(CellUtil.RIGHT_BORDER_COLOR))\r
- count += 1;\r
- }\r
- return count;\r
- }\r
-\r
- /**\r
- * Retrieves the number of border colors assigned to a cell\r
- *\r
- * @param row\r
- * @param col\r
- */\r
- public int getNumBorderColors(int row, int col) {\r
- return getNumBorderColors(new CellAddress(row, col));\r
- }\r
-\r
- /**\r
- * Retrieves the border style for a given cell\r
- * \r
- * @param cell\r
- * @param property\r
- */\r
- public short getTemplateProperty(CellAddress cell, String property) {\r
- short value = 0;\r
- Map<String, Object> cellProperties = _propertyTemplate.get(cell);\r
- if (cellProperties != null) {\r
- Object obj = cellProperties.get(property);\r
- if (obj != null) {\r
- value = getShort(obj);\r
- }\r
- }\r
- return value;\r
- }\r
-\r
- /**\r
- * Retrieves the border style for a given cell\r
- * \r
- * @param row\r
- * @param col\r
- * @param property\r
- */\r
- public short getTemplateProperty(int row, int col, String property) {\r
- return getTemplateProperty(new CellAddress(row, col), property);\r
- }\r
-\r
- /**\r
- * Converts a Short object to a short value or 0 if the object is not a\r
- * Short\r
- *\r
- * @param value\r
- * @return\r
- */\r
- private static short getShort(Object value) {\r
- if (value instanceof Short) {\r
- return ((Short) value).shortValue();\r
- }\r
- return 0;\r
- }\r
-}\r
+/* ====================================================================
+ 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.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.SpreadsheetVersion;
+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;
+
+/**
+ * <p>
+ * A {@link 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
+ * {@link 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
+ */
+ 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
+ }
+
+ /**
+ * This is a list of cell properties for one shot application to a range of
+ * cells at a later time.
+ */
+ private Map<CellAddress, Map<String, Object>> _propertyTemplate;
+
+ /**
+ *
+ */
+ public PropertyTemplate() {
+ _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
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. Use BORDER_XXX constants in
+ * {@link CellStyle}.
+ * @param extent
+ * - {@link PropertyTemplate.Extent} of the borders to be
+ * applied.
+ */
+ public void drawBorders(CellRangeAddress range, short 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
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. Use BORDER_XXX constants in
+ * {@link CellStyle}.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ * @param extent
+ * - {@link PropertyTemplate.Extent} of the borders to be
+ * applied.
+ */
+ public void drawBorders(CellRangeAddress range, short borderType,
+ short color, Extent extent) {
+ drawBorders(range, borderType, extent);
+ if (borderType != CellStyle.BORDER_NONE) {
+ drawBorderColors(range, color, extent);
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the top border for a range of cells
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. Use BORDER_XXX constants in
+ * {@link CellStyle}.
+ */
+ private void drawTopBorder(CellRangeAddress range, short borderType) {
+ int row = range.getFirstRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstCol; i <= lastCol; i++) {
+ addProperty(row, i, CellUtil.BORDER_TOP, borderType);
+ if (borderType == CellStyle.BORDER_NONE && row > 0) {
+ addProperty(row - 1, i, CellUtil.BORDER_BOTTOM, borderType);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the bottom border for a range of cells
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. Use BORDER_XXX constants in
+ * {@link CellStyle}.
+ */
+ private void drawBottomBorder(CellRangeAddress range, short borderType) {
+ int row = range.getLastRow();
+ int firstCol = range.getFirstColumn();
+ int lastCol = range.getLastColumn();
+ for (int i = firstCol; i <= lastCol; i++) {
+ addProperty(row, i, CellUtil.BORDER_BOTTOM, borderType);
+ if (borderType == CellStyle.BORDER_NONE
+ && row < SpreadsheetVersion.EXCEL2007.getMaxRows() - 1) {
+ addProperty(row + 1, i, CellUtil.BORDER_TOP, borderType);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the left border for a range of cells
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. Use BORDER_XXX constants in
+ * {@link CellStyle}.
+ */
+ private void drawLeftBorder(CellRangeAddress range, short borderType) {
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int col = range.getFirstColumn();
+ for (int i = firstRow; i <= lastRow; i++) {
+ addProperty(i, col, CellUtil.BORDER_LEFT, borderType);
+ if (borderType == CellStyle.BORDER_NONE && col > 0) {
+ addProperty(i, col - 1, CellUtil.BORDER_RIGHT, borderType);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the right border for a range of cells
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. Use BORDER_XXX constants in
+ * {@link CellStyle}.
+ */
+ private void drawRightBorder(CellRangeAddress range, short borderType) {
+ int firstRow = range.getFirstRow();
+ int lastRow = range.getLastRow();
+ int col = range.getLastColumn();
+ for (int i = firstRow; i <= lastRow; i++) {
+ addProperty(i, col, CellUtil.BORDER_RIGHT, borderType);
+ if (borderType == CellStyle.BORDER_NONE
+ && col < SpreadsheetVersion.EXCEL2007.getMaxColumns() - 1) {
+ addProperty(i, col + 1, CellUtil.BORDER_LEFT, borderType);
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Draws the outside borders for a range of cells.
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. Use BORDER_XXX constants in
+ * {@link CellStyle}.
+ * @param extent
+ * - {@link PropertyTemplate.Extent} of the borders to be
+ * applied. Valid Values are:
+ * <ul>
+ * <li>CellBorder.Extent.ALL</li>
+ * <li>CellBorder.Extent.HORIZONTAL</li>
+ * <li>CellBorder.Extent.VERTICAL</li>
+ * </ul>
+ */
+ private void drawOutsideBorders(CellRangeAddress range, short 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
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. Use BORDER_XXX constants in
+ * {@link CellStyle}.
+ * @param extent
+ * - {@link PropertyTemplate.Extent} of the borders to be
+ * applied. Valid Values are:
+ * <ul>
+ * <li>CellBorder.Extent.ALL</li>
+ * <li>CellBorder.Extent.INSIDE</li>
+ * </ul>
+ */
+ private void drawHorizontalBorders(CellRangeAddress range, short 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
+ * - {@link CellRangeAddress} range of cells on which borders are
+ * drawn.
+ * @param borderType
+ * - Type of border to draw. Use BORDER_XXX constants in
+ * {@link CellStyle}.
+ * @param extent
+ * - {@link PropertyTemplate.Extent} of the borders to be
+ * applied. Valid Values are:
+ * <ul>
+ * <li>CellBorder.Extent.ALL</li>
+ * <li>CellBorder.Extent.INSIDE</li>
+ * </ul>
+ */
+ private void drawVerticalBorders(CellRangeAddress range, short 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 {@link PropertyTemplate} for the
+ * specified range.
+ *
+ * @parm range - {@link CellRangeAddress} range of cells to remove borders.
+ */
+ private void removeBorders(CellRangeAddress range) {
+ 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);
+ for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) {
+ for (int col = range.getFirstColumn(); col <= range
+ .getLastColumn(); col++) {
+ removeProperties(row, col, properties);
+ }
+ }
+ 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
+ * - {@link Sheet} on which to apply borders
+ */
+ public void applyBorders(Sheet sheet) {
+ Workbook wb = sheet.getWorkbook();
+ for (Map.Entry<CellAddress, Map<String, Object>> entry : _propertyTemplate
+ .entrySet()) {
+ CellAddress cellAddress = entry.getKey();
+ if (cellAddress.getRow() < wb.getSpreadsheetVersion().getMaxRows()
+ && cellAddress.getColumn() < wb.getSpreadsheetVersion()
+ .getMaxColumns()) {
+ Map<String, Object> properties = entry.getValue();
+ Row row = CellUtil.getRow(cellAddress.getRow(), sheet);
+ Cell cell = CellUtil.getCell(row, cellAddress.getColumn());
+ 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 BORDER_THIN border is used. To apply the
+ * drawn borders to a sheet, use {@link #applyBorders}.
+ *
+ * @param range
+ * - {@link CellRangeAddress} range of cells on which colors are
+ * set.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ * @param extent
+ * - {@link PropertyTemplate.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
+ * - {@link CellRangeAddress} 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 (getTemplateProperty(row, i, CellUtil.BORDER_TOP) == 0) {
+ drawTopBorder(new CellRangeAddress(row, row, i, i),
+ CellStyle.BORDER_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
+ * - {@link CellRangeAddress} 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 (getTemplateProperty(row, i, CellUtil.BORDER_BOTTOM) == 0) {
+ drawBottomBorder(new CellRangeAddress(row, row, i, i),
+ CellStyle.BORDER_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
+ * - {@link CellRangeAddress} 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 (getTemplateProperty(i, col, CellUtil.BORDER_LEFT) == 0) {
+ drawLeftBorder(new CellRangeAddress(i, i, col, col),
+ CellStyle.BORDER_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 BORDER_THIN
+ * </p>
+ *
+ * @param range
+ * - {@link CellRangeAddress} 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 (getTemplateProperty(i, col, CellUtil.BORDER_RIGHT) == 0) {
+ drawRightBorder(new CellRangeAddress(i, i, col, col),
+ CellStyle.BORDER_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
+ * - {@link CellRangeAddress} range of cells on which colors are
+ * set.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ * @param extent
+ * - {@link PropertyTemplate.Extent} of the borders for which
+ * colors are set. Valid Values are:
+ * <ul>
+ * <li>CellBorder.Extent.ALL</li>
+ * <li>CellBorder.Extent.HORIZONTAL</li>
+ * <li>CellBorder.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
+ * - {@link CellRangeAddress} range of cells on which colors are
+ * set.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ * @param extent
+ * - {@link PropertyTemplate.Extent} of the borders for which
+ * colors are set. Valid Values are:
+ * <ul>
+ * <li>CellBorder.Extent.ALL</li>
+ * <li>CellBorder.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
+ * - {@link CellRangeAddress} range of cells on which colors are
+ * set.
+ * @param color
+ * - Color index from {@link IndexedColors} used to draw the
+ * borders.
+ * @param extent
+ * - {@link PropertyTemplate.Extent} of the borders for which
+ * colors are set. Valid Values are:
+ * <ul>
+ * <li>CellBorder.Extent.ALL</li>
+ * <li>CellBorder.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 {@link PropertyTemplate} for the
+ * specified range.
+ *
+ * @parm range - {@link CellRangeAddress} range of cells to remove borders.
+ */
+ private void removeBorderColors(CellRangeAddress range) {
+ 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);
+ for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) {
+ for (int col = range.getFirstColumn(); col <= range
+ .getLastColumn(); col++) {
+ removeProperties(row, col, properties);
+ }
+ }
+ }
+
+ /**
+ * Adds a property to this {@link PropertyTemplate} for a given cell
+ *
+ * @param row
+ * @param col
+ * @param property
+ * @param value
+ */
+ private void addProperty(int row, int col, String property, short 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, Short.valueOf(value));
+ _propertyTemplate.put(cell, cellProperties);
+ }
+
+ /**
+ * Removes a set of properties from this {@link PropertyTemplate} for a
+ * given cell
+ *
+ * @param row
+ * @param col
+ * @param properties
+ */
+ 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
+ *
+ * @param cell
+ */
+ public int getNumBorders(CellAddress cell) {
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties == null) {
+ return 0;
+ }
+
+ int count = 0;
+ for (String property : cellProperties.keySet()) {
+ if (property.equals(CellUtil.BORDER_TOP))
+ count += 1;
+ if (property.equals(CellUtil.BORDER_BOTTOM))
+ count += 1;
+ if (property.equals(CellUtil.BORDER_LEFT))
+ count += 1;
+ if (property.equals(CellUtil.BORDER_RIGHT))
+ count += 1;
+ }
+ 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;
+ for (String property : cellProperties.keySet()) {
+ if (property.equals(CellUtil.TOP_BORDER_COLOR))
+ count += 1;
+ if (property.equals(CellUtil.BOTTOM_BORDER_COLOR))
+ count += 1;
+ if (property.equals(CellUtil.LEFT_BORDER_COLOR))
+ count += 1;
+ if (property.equals(CellUtil.RIGHT_BORDER_COLOR))
+ count += 1;
+ }
+ 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 short getTemplateProperty(CellAddress cell, String property) {
+ short value = 0;
+ Map<String, Object> cellProperties = _propertyTemplate.get(cell);
+ if (cellProperties != null) {
+ Object obj = cellProperties.get(property);
+ if (obj != null) {
+ value = getShort(obj);
+ }
+ }
+ return value;
+ }
+
+ /**
+ * Retrieves the border style for a given cell
+ *
+ * @param row
+ * @param col
+ * @param property
+ */
+ public short getTemplateProperty(int row, int col, String property) {
+ return getTemplateProperty(new CellAddress(row, col), property);
+ }
+
+ /**
+ * Converts a Short object to a short value or 0 if the object is not a
+ * Short
+ *
+ * @param value
+ * @return
+ */
+ private static short getShort(Object value) {
+ if (value instanceof Short) {
+ return ((Short) value).shortValue();
+ }
+ return 0;
+ }
+}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.ss.util;\r
-\r
-import static org.junit.Assert.assertEquals;\r
-\r
-import java.io.IOException;\r
-\r
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
-import org.apache.poi.ss.usermodel.BorderStyle;\r
-import org.apache.poi.ss.usermodel.Cell;\r
-import org.apache.poi.ss.usermodel.CellStyle;\r
-import org.apache.poi.ss.usermodel.IndexedColors;\r
-import org.apache.poi.ss.usermodel.Row;\r
-import org.apache.poi.ss.usermodel.Sheet;\r
-import org.apache.poi.ss.usermodel.Workbook;\r
-import org.apache.poi.ss.util.PropertyTemplate.Extent;\r
-import org.junit.Test;\r
-\r
-/**\r
- * Tests Spreadsheet PropertyTemplate\r
- *\r
- * @see org.apache.poi.ss.util.PropertyTemplate\r
- */\r
-public final class TestPropertyTemplate {\r
- @Test\r
- public void getNumBorders() throws IOException {\r
- CellRangeAddress a1 = new CellRangeAddress(0, 0, 0, 0);\r
- PropertyTemplate pt = new PropertyTemplate();\r
- pt.drawBorders(a1, CellStyle.BORDER_THIN, Extent.TOP);\r
- assertEquals(1, pt.getNumBorders(0, 0));\r
- pt.drawBorders(a1, CellStyle.BORDER_MEDIUM, Extent.BOTTOM);\r
- assertEquals(2, pt.getNumBorders(0, 0));\r
- pt.drawBorders(a1, CellStyle.BORDER_MEDIUM, Extent.NONE);\r
- assertEquals(0, pt.getNumBorders(0, 0));\r
- }\r
-\r
- @Test\r
- public void getNumBorderColors() throws IOException {\r
- CellRangeAddress a1 = new CellRangeAddress(0, 0, 0, 0);\r
- PropertyTemplate pt = new PropertyTemplate();\r
- pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), Extent.TOP);\r
- assertEquals(1, pt.getNumBorderColors(0, 0));\r
- pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), Extent.BOTTOM);\r
- assertEquals(2, pt.getNumBorderColors(0, 0));\r
- pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), Extent.NONE);\r
- assertEquals(0, pt.getNumBorderColors(0, 0));\r
- }\r
-\r
- @Test\r
- public void getTemplateProperties() throws IOException {\r
- CellRangeAddress a1 = new CellRangeAddress(0, 0, 0, 0);\r
- PropertyTemplate pt = new PropertyTemplate();\r
- pt.drawBorders(a1, CellStyle.BORDER_THIN, Extent.TOP);\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(0, 0, CellUtil.BORDER_TOP));\r
- pt.drawBorders(a1, CellStyle.BORDER_MEDIUM, Extent.BOTTOM);\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(0, 0, CellUtil.BORDER_BOTTOM));\r
- pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), Extent.TOP);\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(0, 0, CellUtil.TOP_BORDER_COLOR));\r
- pt.drawBorderColors(a1, IndexedColors.BLUE.getIndex(), Extent.BOTTOM);\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(0, 0, CellUtil.BOTTOM_BORDER_COLOR));\r
- }\r
-\r
- @Test\r
- public void drawBorders() throws IOException {\r
- CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);\r
- PropertyTemplate pt = new PropertyTemplate();\r
- pt.drawBorders(a1c3, CellStyle.BORDER_THIN,\r
- PropertyTemplate.Extent.ALL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- assertEquals(4, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,\r
- PropertyTemplate.Extent.OUTSIDE);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- assertEquals(4, pt.getNumBorders(i, j));\r
- if (i == 0) {\r
- if (j == 0) {\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_BOTTOM));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_RIGHT));\r
- } else if (j == 2) {\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_BOTTOM));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_RIGHT));\r
- } else {\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_BOTTOM));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_RIGHT));\r
- }\r
- } else if (i == 2) {\r
- if (j == 0) {\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_BOTTOM));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_RIGHT));\r
- } else if (j == 2) {\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_BOTTOM));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_RIGHT));\r
- } else {\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_BOTTOM));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_RIGHT));\r
- }\r
- } else {\r
- if (j == 0) {\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_BOTTOM));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_RIGHT));\r
- } else if (j == 2) {\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_BOTTOM));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_RIGHT));\r
- } else {\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_BOTTOM));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_THIN,\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BORDER_RIGHT));\r
- }\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,\r
- PropertyTemplate.Extent.TOP);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (i == 0) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));\r
- } else {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,\r
- PropertyTemplate.Extent.BOTTOM);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (i == 2) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM, pt\r
- .getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));\r
- } else {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,\r
- PropertyTemplate.Extent.LEFT);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (j == 0) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));\r
- } else {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,\r
- PropertyTemplate.Extent.RIGHT);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (j == 2) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM, pt\r
- .getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));\r
- } else {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,\r
- PropertyTemplate.Extent.HORIZONTAL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- assertEquals(2, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,\r
- PropertyTemplate.Extent.INSIDE_HORIZONTAL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (i == 0) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM, pt\r
- .getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));\r
- } else if (i == 2) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));\r
- } else {\r
- assertEquals(2, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_MEDIUM, pt\r
- .getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,\r
- PropertyTemplate.Extent.OUTSIDE_HORIZONTAL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (i == 0) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));\r
- } else if (i == 2) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM, pt\r
- .getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));\r
- } else {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,\r
- PropertyTemplate.Extent.VERTICAL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- assertEquals(2, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,\r
- PropertyTemplate.Extent.INSIDE_VERTICAL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (j == 0) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM, pt\r
- .getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));\r
- } else if (j == 2) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));\r
- } else {\r
- assertEquals(2, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_MEDIUM, pt\r
- .getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,\r
- PropertyTemplate.Extent.OUTSIDE_VERTICAL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (j == 0) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));\r
- } else if (j == 2) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM, pt\r
- .getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));\r
- } else {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- }\r
- }\r
- }\r
- }\r
-\r
- @Test\r
- public void drawBorderColors() throws IOException {\r
- CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);\r
- PropertyTemplate pt = new PropertyTemplate();\r
- pt.drawBorderColors(a1c3, IndexedColors.RED.getIndex(),\r
- PropertyTemplate.Extent.ALL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- assertEquals(4, pt.getNumBorders(i, j));\r
- assertEquals(4, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.RED.getIndex(), pt\r
- .getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(), pt\r
- .getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- }\r
- }\r
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),\r
- PropertyTemplate.Extent.OUTSIDE);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- assertEquals(4, pt.getNumBorders(i, j));\r
- assertEquals(4, pt.getNumBorderColors(i, j));\r
- if (i == 0) {\r
- if (j == 0) {\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- } else if (j == 2) {\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- } else {\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- }\r
- } else if (i == 2) {\r
- if (j == 0) {\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- } else if (j == 2) {\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- } else {\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- }\r
- } else {\r
- if (j == 0) {\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- } else if (j == 2) {\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- } else {\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- }\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),\r
- PropertyTemplate.Extent.NONE);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- assertEquals(0, pt.getNumBorderColors(i, j));\r
- }\r
- }\r
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),\r
- PropertyTemplate.Extent.TOP);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (i == 0) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(1, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- } else {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- assertEquals(0, pt.getNumBorderColors(i, j));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),\r
- PropertyTemplate.Extent.BOTTOM);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (i == 2) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(1, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- } else {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- assertEquals(0, pt.getNumBorderColors(i, j));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),\r
- PropertyTemplate.Extent.LEFT);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (j == 0) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(1, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- } else {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- assertEquals(0, pt.getNumBorderColors(i, j));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),\r
- PropertyTemplate.Extent.RIGHT);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (j == 2) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(1, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- } else {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- assertEquals(0, pt.getNumBorderColors(i, j));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),\r
- PropertyTemplate.Extent.HORIZONTAL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- assertEquals(2, pt.getNumBorders(i, j));\r
- assertEquals(2, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(), pt\r
- .getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),\r
- PropertyTemplate.Extent.INSIDE_HORIZONTAL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (i == 0) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(1, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- } else if (i == 2) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(1, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- } else {\r
- assertEquals(2, pt.getNumBorders(i, j));\r
- assertEquals(2, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),\r
- PropertyTemplate.Extent.OUTSIDE_HORIZONTAL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (i == 0) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(1, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.TOP_BORDER_COLOR));\r
- } else if (i == 2) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(1, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- } else {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- assertEquals(0, pt.getNumBorderColors(i, j));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),\r
- PropertyTemplate.Extent.VERTICAL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- assertEquals(2, pt.getNumBorders(i, j));\r
- assertEquals(2, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(), pt\r
- .getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),\r
- PropertyTemplate.Extent.INSIDE_VERTICAL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (j == 0) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(1, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- } else if (j == 2) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(1, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- } else {\r
- assertEquals(2, pt.getNumBorders(i, j));\r
- assertEquals(2, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- }\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE,\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),\r
- PropertyTemplate.Extent.NONE);\r
- pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),\r
- PropertyTemplate.Extent.OUTSIDE_VERTICAL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- if (j == 0) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(1, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.LEFT_BORDER_COLOR));\r
- } else if (j == 2) {\r
- assertEquals(1, pt.getNumBorders(i, j));\r
- assertEquals(1, pt.getNumBorderColors(i, j));\r
- assertEquals(IndexedColors.BLUE.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- } else {\r
- assertEquals(0, pt.getNumBorders(i, j));\r
- assertEquals(0, pt.getNumBorderColors(i, j));\r
- }\r
- }\r
- }\r
- }\r
- \r
- @Test\r
- public void drawBordersWithColors() throws IOException {\r
- CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);\r
- PropertyTemplate pt = new PropertyTemplate();\r
- \r
- pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM, IndexedColors.RED.getIndex(), Extent.ALL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- assertEquals(4, pt.getNumBorders(i, j));\r
- assertEquals(4, pt.getNumBorderColors(i, j));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_MEDIUM,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));\r
- assertEquals(IndexedColors.RED.getIndex(), pt\r
- .getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.BOTTOM_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(), pt\r
- .getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));\r
- assertEquals(IndexedColors.RED.getIndex(),\r
- pt.getTemplateProperty(i, j,\r
- CellUtil.RIGHT_BORDER_COLOR));\r
- }\r
- }\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE, Extent.NONE);\r
- pt.drawBorders(a1c3, CellStyle.BORDER_NONE, IndexedColors.RED.getIndex(), Extent.ALL);\r
- for (int i = 0; i <= 2; i++) {\r
- for (int j = 0; j <= 2; j++) {\r
- assertEquals(4, pt.getNumBorders(i, j));\r
- assertEquals(0, pt.getNumBorderColors(i, j));\r
- assertEquals(CellStyle.BORDER_NONE,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));\r
- assertEquals(CellStyle.BORDER_NONE,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));\r
- assertEquals(CellStyle.BORDER_NONE,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));\r
- assertEquals(CellStyle.BORDER_NONE,\r
- pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));\r
- }\r
- }\r
- }\r
-\r
- @Test\r
- public void applyBorders() throws IOException {\r
- CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2);\r
- CellRangeAddress b2 = new CellRangeAddress(1, 1, 1, 1);\r
- PropertyTemplate pt = new PropertyTemplate();\r
- Workbook wb = new HSSFWorkbook();\r
- Sheet sheet = wb.createSheet();\r
- \r
- pt.drawBorders(a1c3, CellStyle.BORDER_THIN, IndexedColors.RED.getIndex(), Extent.ALL);\r
- pt.applyBorders(sheet);\r
- \r
- for (Row row: sheet) {\r
- for (Cell cell: row) {\r
- CellStyle cs = cell.getCellStyle();\r
- assertEquals(BorderStyle.THIN, cs.getBorderTop());\r
- assertEquals(IndexedColors.RED.getIndex(), cs.getTopBorderColor());\r
- assertEquals(BorderStyle.THIN, cs.getBorderBottom());\r
- assertEquals(IndexedColors.RED.getIndex(), cs.getBottomBorderColor());\r
- assertEquals(BorderStyle.THIN, cs.getBorderLeft());\r
- assertEquals(IndexedColors.RED.getIndex(), cs.getLeftBorderColor());\r
- assertEquals(BorderStyle.THIN, cs.getBorderRight());\r
- assertEquals(IndexedColors.RED.getIndex(), cs.getRightBorderColor());\r
- }\r
- }\r
- \r
- pt.drawBorders(b2, CellStyle.BORDER_NONE, Extent.ALL);\r
- pt.applyBorders(sheet);\r
- \r
- for (Row row: sheet) {\r
- for (Cell cell: row) {\r
- CellStyle cs = cell.getCellStyle();\r
- if (cell.getColumnIndex() != 1 || row.getRowNum() == 0) {\r
- assertEquals(BorderStyle.THIN, cs.getBorderTop());\r
- assertEquals(IndexedColors.RED.getIndex(), cs.getTopBorderColor());\r
- } else {\r
- assertEquals(BorderStyle.NONE, cs.getBorderTop());\r
- }\r
- if (cell.getColumnIndex() != 1 || row.getRowNum() == 2) {\r
- assertEquals(BorderStyle.THIN, cs.getBorderBottom());\r
- assertEquals(IndexedColors.RED.getIndex(), cs.getBottomBorderColor());\r
- } else {\r
- assertEquals(BorderStyle.NONE, cs.getBorderBottom());\r
- }\r
- if (cell.getColumnIndex() == 0 || row.getRowNum() != 1) {\r
- assertEquals(BorderStyle.THIN, cs.getBorderLeft());\r
- assertEquals(IndexedColors.RED.getIndex(), cs.getLeftBorderColor());\r
- } else {\r
- assertEquals(BorderStyle.NONE, cs.getBorderLeft());\r
- }\r
- if (cell.getColumnIndex() == 2 || row.getRowNum() != 1) {\r
- assertEquals(BorderStyle.THIN, cs.getBorderRight());\r
- assertEquals(IndexedColors.RED.getIndex(), cs.getRightBorderColor());\r
- } else {\r
- assertEquals(BorderStyle.NONE, cs.getBorderRight());\r
- }\r
- }\r
- }\r
- \r
- wb.close();\r
- }\r
-}\r
+/* ====================================================================
+ 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, CellStyle.BORDER_THIN, Extent.TOP);
+ assertEquals(1, pt.getNumBorders(0, 0));
+ pt.drawBorders(a1, CellStyle.BORDER_MEDIUM, Extent.BOTTOM);
+ assertEquals(2, pt.getNumBorders(0, 0));
+ pt.drawBorders(a1, CellStyle.BORDER_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, CellStyle.BORDER_THIN, Extent.TOP);
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(0, 0, CellUtil.BORDER_TOP));
+ pt.drawBorders(a1, CellStyle.BORDER_MEDIUM, Extent.BOTTOM);
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(0, 0, CellUtil.BORDER_BOTTOM));
+ pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), Extent.TOP);
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(0, 0, CellUtil.TOP_BORDER_COLOR));
+ pt.drawBorderColors(a1, IndexedColors.BLUE.getIndex(), Extent.BOTTOM);
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ 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, CellStyle.BORDER_THIN,
+ PropertyTemplate.Extent.ALL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(4, pt.getNumBorders(i, j));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,
+ PropertyTemplate.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) {
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_BOTTOM));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_RIGHT));
+ } else if (j == 2) {
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_BOTTOM));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_RIGHT));
+ } else {
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_BOTTOM));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_RIGHT));
+ }
+ } else if (i == 2) {
+ if (j == 0) {
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_BOTTOM));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_RIGHT));
+ } else if (j == 2) {
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_BOTTOM));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_RIGHT));
+ } else {
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_BOTTOM));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_RIGHT));
+ }
+ } else {
+ if (j == 0) {
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_BOTTOM));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_RIGHT));
+ } else if (j == 2) {
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_BOTTOM));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_RIGHT));
+ } else {
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_BOTTOM));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_THIN,
+ pt.getTemplateProperty(i, j,
+ CellUtil.BORDER_RIGHT));
+ }
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.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, CellStyle.BORDER_MEDIUM,
+ PropertyTemplate.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(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,
+ PropertyTemplate.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(CellStyle.BORDER_MEDIUM, pt
+ .getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,
+ PropertyTemplate.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(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,
+ PropertyTemplate.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(CellStyle.BORDER_MEDIUM, pt
+ .getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,
+ PropertyTemplate.Extent.HORIZONTAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,
+ PropertyTemplate.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(CellStyle.BORDER_MEDIUM, pt
+ .getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ } else if (i == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ } else {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_MEDIUM, pt
+ .getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,
+ PropertyTemplate.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(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ } else if (i == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(CellStyle.BORDER_MEDIUM, pt
+ .getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,
+ PropertyTemplate.Extent.VERTICAL);
+ for (int i = 0; i <= 2; i++) {
+ for (int j = 0; j <= 2; j++) {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,
+ PropertyTemplate.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(CellStyle.BORDER_MEDIUM, pt
+ .getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ } else if (j == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ } else {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_MEDIUM, pt
+ .getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorders(a1c3, CellStyle.BORDER_MEDIUM,
+ PropertyTemplate.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(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ } else if (j == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(CellStyle.BORDER_MEDIUM, 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(),
+ PropertyTemplate.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));
+ assertEquals(IndexedColors.RED.getIndex(), pt
+ .getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(), pt
+ .getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ }
+ }
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+ PropertyTemplate.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) {
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ } else if (j == 2) {
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ } else {
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ }
+ } else if (i == 2) {
+ if (j == 0) {
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ } else if (j == 2) {
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ } else {
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ }
+ } else {
+ if (j == 0) {
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ } else if (j == 2) {
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ } else {
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ }
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+ PropertyTemplate.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(),
+ PropertyTemplate.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));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+ PropertyTemplate.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));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+ PropertyTemplate.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));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+ PropertyTemplate.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));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+ PropertyTemplate.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));
+ assertEquals(IndexedColors.BLUE.getIndex(), pt
+ .getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+ PropertyTemplate.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));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ } else if (i == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ } else {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertEquals(2, pt.getNumBorderColors(i, j));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+ PropertyTemplate.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));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.TOP_BORDER_COLOR));
+ } else if (i == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ } else {
+ assertEquals(0, pt.getNumBorders(i, j));
+ assertEquals(0, pt.getNumBorderColors(i, j));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+ PropertyTemplate.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));
+ assertEquals(IndexedColors.BLUE.getIndex(), pt
+ .getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+ PropertyTemplate.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));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ } else if (j == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ } else {
+ assertEquals(2, pt.getNumBorders(i, j));
+ assertEquals(2, pt.getNumBorderColors(i, j));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ }
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE,
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(),
+ PropertyTemplate.Extent.NONE);
+ pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(),
+ PropertyTemplate.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));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.LEFT_BORDER_COLOR));
+ } else if (j == 2) {
+ assertEquals(1, pt.getNumBorders(i, j));
+ assertEquals(1, pt.getNumBorderColors(i, j));
+ assertEquals(IndexedColors.BLUE.getIndex(),
+ 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, CellStyle.BORDER_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));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_MEDIUM,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_RIGHT));
+ assertEquals(IndexedColors.RED.getIndex(), pt
+ .getTemplateProperty(i, j, CellUtil.TOP_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.BOTTOM_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(), pt
+ .getTemplateProperty(i, j, CellUtil.LEFT_BORDER_COLOR));
+ assertEquals(IndexedColors.RED.getIndex(),
+ pt.getTemplateProperty(i, j,
+ CellUtil.RIGHT_BORDER_COLOR));
+ }
+ }
+ pt.drawBorders(a1c3, CellStyle.BORDER_NONE, Extent.NONE);
+ pt.drawBorders(a1c3, CellStyle.BORDER_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));
+ assertEquals(CellStyle.BORDER_NONE,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_TOP));
+ assertEquals(CellStyle.BORDER_NONE,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_BOTTOM));
+ assertEquals(CellStyle.BORDER_NONE,
+ pt.getTemplateProperty(i, j, CellUtil.BORDER_LEFT));
+ assertEquals(CellStyle.BORDER_NONE,
+ 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, CellStyle.BORDER_THIN, IndexedColors.RED.getIndex(), Extent.ALL);
+ pt.applyBorders(sheet);
+
+ for (Row row: sheet) {
+ for (Cell cell: row) {
+ CellStyle cs = cell.getCellStyle();
+ assertEquals(BorderStyle.THIN, cs.getBorderTop());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getTopBorderColor());
+ assertEquals(BorderStyle.THIN, cs.getBorderBottom());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getBottomBorderColor());
+ assertEquals(BorderStyle.THIN, cs.getBorderLeft());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getLeftBorderColor());
+ assertEquals(BorderStyle.THIN, cs.getBorderRight());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getRightBorderColor());
+ }
+ }
+
+ pt.drawBorders(b2, CellStyle.BORDER_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) {
+ assertEquals(BorderStyle.THIN, cs.getBorderTop());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getTopBorderColor());
+ } else {
+ assertEquals(BorderStyle.NONE, cs.getBorderTop());
+ }
+ if (cell.getColumnIndex() != 1 || row.getRowNum() == 2) {
+ assertEquals(BorderStyle.THIN, cs.getBorderBottom());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getBottomBorderColor());
+ } else {
+ assertEquals(BorderStyle.NONE, cs.getBorderBottom());
+ }
+ if (cell.getColumnIndex() == 0 || row.getRowNum() != 1) {
+ assertEquals(BorderStyle.THIN, cs.getBorderLeft());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getLeftBorderColor());
+ } else {
+ assertEquals(BorderStyle.NONE, cs.getBorderLeft());
+ }
+ if (cell.getColumnIndex() == 2 || row.getRowNum() != 1) {
+ assertEquals(BorderStyle.THIN, cs.getBorderRight());
+ assertEquals(IndexedColors.RED.getIndex(), cs.getRightBorderColor());
+ } else {
+ assertEquals(BorderStyle.NONE, cs.getBorderRight());
+ }
+ }
+ }
+
+ wb.close();
+ }
+}