]> source.dussan.org Git - poi.git/commitdiff
bug 58787: add PropertyTemplate, unit test, and example from Mark Murphy
authorJaven O'Neal <onealj@apache.org>
Sat, 11 Jun 2016 06:47:25 +0000 (06:47 +0000)
committerJaven O'Neal <onealj@apache.org>
Sat, 11 Jun 2016 06:47:25 +0000 (06:47 +0000)
Attachment 33684 plus changes to TestPropertyTemplate#applyBorders: replaced assertEquals(CellStyle.BORDER_*, cs.getBorder*()) with assertEquals(BorderStyle.*, cs.getBorder*())

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ss_border_property_template@1747851 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java [new file with mode: 0644]
src/java/org/apache/poi/ss/util/PropertyTemplate.java [new file with mode: 0644]
src/testcases/org/apache/poi/ss/util/TestPropertyTemplate.java [new file with mode: 0644]

diff --git a/src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java b/src/examples/src/org/apache/poi/ss/examples/DrawingBorders.java
new file mode 100644 (file)
index 0000000..5add842
--- /dev/null
@@ -0,0 +1,111 @@
+/*\r
+ *  ====================================================================\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
+\r
+package org.apache.poi.ss.examples;\r
+\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+\r
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;\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;\r
+import org.apache.poi.ss.util.CellRangeAddress;\r
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+\r
+/**\r
+ * Excel Border Drawing - examples\r
+ * \r
+ * <p>\r
+ * Partly based on the code snippets from\r
+ * org.apache.poi.ss.examples.ConditionalFormats\r
+ * </p>\r
+ */\r
+public class DrawingBorders {\r
+\r
+    public static void main(String[] args) throws IOException {\r
+        Workbook wb;\r
+\r
+        if (args.length > 0 && args[0].equals("-xls")) {\r
+            wb = new HSSFWorkbook();\r
+        } else {\r
+            wb = new XSSFWorkbook();\r
+        }\r
+\r
+        // add a sheet, and put some values into it\r
+        Sheet sh1 = wb.createSheet("Sheet1");\r
+        Row r = sh1.createRow(0);\r
+        Cell c = r.createCell(1);\r
+        c.setCellValue("All Borders Medium Width");\r
+        r = sh1.createRow(4);\r
+        c = r.createCell(1);\r
+        c.setCellValue("Medium Outside / Thin Inside Borders");\r
+        r = sh1.createRow(8);\r
+        c = r.createCell(1);\r
+        c.setCellValue("Colored Borders");\r
+\r
+        // draw borders (three 3x3 grids)\r
+        PropertyTemplate pt = new PropertyTemplate();\r
+        // #1) these borders will all be medium in default color\r
+        pt.drawBorders(new CellRangeAddress(1, 3, 1, 3),\r
+                CellStyle.BORDER_MEDIUM, PropertyTemplate.Extent.ALL);\r
+        // #2) these cells will have medium outside borders and thin inside borders\r
+        pt.drawBorders(new CellRangeAddress(5, 7, 1, 3),\r
+                CellStyle.BORDER_MEDIUM, PropertyTemplate.Extent.OUTSIDE);\r
+        pt.drawBorders(new CellRangeAddress(5, 7, 1, 3), CellStyle.BORDER_THIN,\r
+                PropertyTemplate.Extent.INSIDE);\r
+        // #3) these cells will all be medium weight with different colors for the\r
+        //     outside, inside horizontal, and inside vertical borders. The center\r
+        //     cell will have no borders.\r
+        pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),\r
+                CellStyle.BORDER_MEDIUM, IndexedColors.RED.getIndex(),\r
+                PropertyTemplate.Extent.OUTSIDE);\r
+        pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),\r
+                CellStyle.BORDER_MEDIUM, IndexedColors.BLUE.getIndex(),\r
+                PropertyTemplate.Extent.INSIDE_VERTICAL);\r
+        pt.drawBorders(new CellRangeAddress(9, 11, 1, 3),\r
+                CellStyle.BORDER_MEDIUM, IndexedColors.GREEN.getIndex(),\r
+                PropertyTemplate.Extent.INSIDE_HORIZONTAL);\r
+        pt.drawBorders(new CellRangeAddress(10, 10, 2, 2),\r
+                CellStyle.BORDER_NONE, \r
+                PropertyTemplate.Extent.ALL);\r
+\r
+        // apply borders to sheet\r
+        pt.applyBorders(sh1);\r
+        \r
+        // add another sheet and apply the borders to it\r
+        Sheet sh2 = wb.createSheet("Sheet2");\r
+        pt.applyBorders(sh2);\r
+\r
+        // Write the output to a file\r
+        String file = "db-poi.xls";\r
+        if (wb instanceof XSSFWorkbook)\r
+            file += "x";\r
+        FileOutputStream out = new FileOutputStream(file);\r
+        wb.write(out);\r
+        out.close();\r
+        wb.close();\r
+        System.out.println("Generated: " + file);\r
+    }\r
+\r
+}\r
diff --git a/src/java/org/apache/poi/ss/util/PropertyTemplate.java b/src/java/org/apache/poi/ss/util/PropertyTemplate.java
new file mode 100644 (file)
index 0000000..3f9d388
--- /dev/null
@@ -0,0 +1,989 @@
+/* ====================================================================\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
diff --git a/src/testcases/org/apache/poi/ss/util/TestPropertyTemplate.java b/src/testcases/org/apache/poi/ss/util/TestPropertyTemplate.java
new file mode 100644 (file)
index 0000000..6c3f0c7
--- /dev/null
@@ -0,0 +1,910 @@
+/* ====================================================================\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