From: PJ Fanning Date: Tue, 27 Aug 2024 18:36:59 +0000 (+0000) Subject: [github-682] Add CellPropertyType and CellPropertyCategory enums. Thanks to Danila... X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=abf153d6a429a061870cc23636daa3740bd95cc8;p=poi.git [github-682] Add CellPropertyType and CellPropertyCategory enums. Thanks to Danila Avdeyenko. This closes #682 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1920230 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java index 6c468f3c8a..4861a09b5d 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java @@ -572,7 +572,7 @@ public class SXSSFCell extends CellBase { * the Workbook.

* *

To change the style of a cell without affecting other cells that use the same style, - * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, Map)}

+ * use {@link org.apache.poi.ss.util.CellUtil#setCellStylePropertiesEnum(Cell, Map)}

* * @param style reference contained in the workbook. * If the value is null then the style information is removed causing the cell to used the default workbook style. diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 03110ec00b..20b7a2cbf4 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -606,7 +606,7 @@ public final class XSSFCell extends CellBase { * the XSSFWorkbook.

* *

To change the style of a cell without affecting other cells that use the same style, - * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, java.util.Map)}

+ * use {@link org.apache.poi.ss.util.CellUtil#setCellStylePropertiesEnum(Cell, java.util.Map)}

* * @param style reference contained in the workbook. * If the value is null then the style information is removed causing the cell to use the default workbook style. diff --git a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java index 58b93c6047..1597524183 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java @@ -20,6 +20,7 @@ package org.apache.poi.ss.tests.util; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellPropertyType; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; @@ -32,19 +33,38 @@ import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.jupiter.api.Test; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; import java.util.LinkedHashMap; import java.util.Map; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; class TestXSSFCellUtil extends BaseTestCellUtil { public TestXSSFCellUtil() { super(XSSFITestDataProvider.instance); } + @Test + public void testSetForegroundColorCellStylePropertyByEnum() throws IOException, DecoderException { + try (Workbook workbook = new XSSFWorkbook()) { + + final Sheet sheet = workbook.createSheet("Sheet"); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); + final XSSFColor color = new XSSFColor(Hex.decodeHex("AAAAAA")); + + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + + CellUtil.setCellStyleProperty( + cell, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, color); + + assertEquals(color, cell.getCellStyle().getFillForegroundColorColor()); + } + } + @Test public void testSetForegroundColorCellStyleProperty() throws IOException, DecoderException { try (Workbook workbook = new XSSFWorkbook()) { @@ -64,8 +84,32 @@ class TestXSSFCellUtil extends BaseTestCellUtil { } @Test - public void testSetForegroundColorCellStylePropertyToNull() throws IOException, DecoderException { + public void testSetForegroundColorCellStylePropertyToNullByEnum() throws IOException, DecoderException { + try (Workbook workbook = new XSSFWorkbook()) { + + final Sheet sheet = workbook.createSheet("Sheet"); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); + final XSSFColor color = new XSSFColor(Hex.decodeHex("AAAAAA")); + + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + + CellUtil.setCellStyleProperty( + cell, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, color); + + assertEquals(color, cell.getCellStyle().getFillForegroundColorColor()); + + CellUtil.setCellStyleProperty( + cell, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, null); + + assertNotEquals(color, cell.getCellStyle().getFillForegroundColorColor()); + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + assertEquals(IndexedColors.AUTOMATIC.getIndex(), cell.getCellStyle().getFillForegroundColor()); + } + } + @Test + public void testSetForegroundColorCellStylePropertyToNull() throws IOException, DecoderException { try (Workbook workbook = new XSSFWorkbook()) { final Sheet sheet = workbook.createSheet("Sheet"); @@ -89,6 +133,41 @@ class TestXSSFCellUtil extends BaseTestCellUtil { } } + @Test + public void testSetForegroundColorCellStylePropertiesToNullByEnum() throws IOException, DecoderException { + + try (Workbook workbook = new XSSFWorkbook()) { + + final Sheet sheet = workbook.createSheet("Sheet"); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); + final XSSFColor color = new XSSFColor(Hex.decodeHex("FF0000")); + + { + final Map properties = new LinkedHashMap<>(); + + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, color); + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.SOLID_FOREGROUND); + + CellUtil.setCellStylePropertiesEnum(cell, properties); + } + assertEquals(color, cell.getCellStyle().getFillForegroundColorColor()); + assertEquals(FillPatternType.SOLID_FOREGROUND, cell.getCellStyle().getFillPattern()); + + { + final Map properties = new LinkedHashMap<>(); + + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, null); + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.NO_FILL); + + CellUtil.setCellStylePropertiesEnum(cell, properties); + } + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + assertEquals(IndexedColors.AUTOMATIC.getIndex(), cell.getCellStyle().getFillForegroundColor()); + assertEquals(FillPatternType.NO_FILL, cell.getCellStyle().getFillPattern()); + } + } + @Test public void testSetForegroundColorCellStylePropertiesToNull() throws IOException, DecoderException { @@ -124,9 +203,49 @@ class TestXSSFCellUtil extends BaseTestCellUtil { } } + @Test - public void testBug66052WithWorkaround() throws IOException, DecoderException { + public void testBug66052WithWorkaroundByEnum() throws IOException, DecoderException { + try (Workbook workbook = new XSSFWorkbook()) { + + final Sheet sheet = workbook.createSheet("Sheet"); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); + final XSSFColor color = new XSSFColor(Hex.decodeHex("FFAAAA")); + + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + assertNull(cell.getCellStyle().getFillBackgroundColorColor()); + + { + Map properties = new LinkedHashMap<>(); + + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, color); + properties.put(CellPropertyType.FILL_BACKGROUND_COLOR_COLOR, null); // WORKAROUND + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.SOLID_FOREGROUND); + + CellUtil.setCellStylePropertiesEnum(cell, properties); + } + + assertNotNull(cell.getCellStyle().getFillForegroundColorColor()); + assertNull(cell.getCellStyle().getFillBackgroundColorColor()); + + { + Map properties = new LinkedHashMap<>(); + + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, null); + properties.put(CellPropertyType.FILL_BACKGROUND_COLOR_COLOR, null); // WORKAROUND + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.NO_FILL); + + CellUtil.setCellStylePropertiesEnum(cell, properties); + } + + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + assertNull(cell.getCellStyle().getFillBackgroundColorColor()); + } + } + @Test + public void testBug66052WithWorkaround() throws IOException, DecoderException { try (Workbook workbook = new XSSFWorkbook()) { final Sheet sheet = workbook.createSheet("Sheet"); @@ -165,6 +284,47 @@ class TestXSSFCellUtil extends BaseTestCellUtil { } } + @Test + public void testBug66052WithoutWorkaroundByEnum() throws IOException, DecoderException { + + try (Workbook workbook = new XSSFWorkbook()) { + + final Sheet sheet = workbook.createSheet("Sheet"); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); + final XSSFColor color = new XSSFColor(Hex.decodeHex("FFAAAA")); + + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + assertNull(cell.getCellStyle().getFillBackgroundColorColor()); + + { + Map properties = new LinkedHashMap<>(); + + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, color); + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.SOLID_FOREGROUND); + + CellUtil.setCellStylePropertiesEnum(cell, properties); + } + + assertEquals(color, cell.getCellStyle().getFillForegroundColorColor()); + assertEquals(IndexedColors.AUTOMATIC.getIndex(), + ((XSSFColor) cell.getCellStyle().getFillBackgroundColorColor()).getIndex()); + + { + Map properties = new LinkedHashMap<>(); + + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, null); + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.NO_FILL); + + CellUtil.setCellStylePropertiesEnum(cell, properties); + } + + assertNull(cell.getCellStyle().getFillForegroundColorColor()); + assertEquals(IndexedColors.AUTOMATIC.getIndex(), + ((XSSFColor) cell.getCellStyle().getFillBackgroundColorColor()).getIndex()); + } + } + @Test public void testBug66052WithoutWorkaround() throws IOException, DecoderException { @@ -205,4 +365,4 @@ class TestXSSFCellUtil extends BaseTestCellUtil { ((XSSFColor) cell.getCellStyle().getFillBackgroundColorColor()).getIndex()); } } -} \ No newline at end of file +} diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 76eadaabe4..ac67a459a4 100644 --- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -946,7 +946,7 @@ public class HSSFCell extends CellBase { * the HSSFWorkbook.

* *

To change the style of a cell without affecting other cells that use the same style, - * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(org.apache.poi.ss.usermodel.Cell, java.util.Map)}

+ * use {@link org.apache.poi.ss.util.CellUtil#setCellStylePropertiesEnum(org.apache.poi.ss.usermodel.Cell, java.util.Map)}

* * @param style reference contained in the workbook * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle() diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/Cell.java b/poi/src/main/java/org/apache/poi/ss/usermodel/Cell.java index bca5856903..362358ba6a 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/Cell.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/Cell.java @@ -369,7 +369,7 @@ public interface Cell { * the Workbook.

* *

To change the style of a cell without affecting other cells that use the same style, - * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, Map)}

+ * use {@link org.apache.poi.ss.util.CellUtil#setCellStylePropertiesEnum(Cell, Map)}

* * @param style reference contained in the workbook. * If the value is null then the style information is removed causing the cell to used the default workbook style. diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/CellPropertyCategory.java b/poi/src/main/java/org/apache/poi/ss/usermodel/CellPropertyCategory.java new file mode 100644 index 0000000000..79eeb8edc5 --- /dev/null +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/CellPropertyCategory.java @@ -0,0 +1,36 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + + +package org.apache.poi.ss.usermodel; + +/** + * The CellPropertyCategory enum represents the different categories of cell properties. + * Each category is used to classify and organize the cell properties based on their characteristics. + * + * @since POI 5.3.1 + */ +public enum CellPropertyCategory { + + SHORT, + COLOR, + INT, + BOOL, + BORDER_TYPE, + OTHER + +} diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/CellPropertyType.java b/poi/src/main/java/org/apache/poi/ss/usermodel/CellPropertyType.java new file mode 100644 index 0000000000..74259b5ba3 --- /dev/null +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/CellPropertyType.java @@ -0,0 +1,69 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.usermodel; + +/** + * The CellPropertyType enum represents the different types of cell properties that can be applied to a cell. + * Each type is associated with a specific category {@link CellPropertyCategory}, which classifies and organizes + * the properties based on their characteristics. + * + * @since POI 5.3.1 + */ +public enum CellPropertyType { + + BORDER_BOTTOM(CellPropertyCategory.BORDER_TYPE), + BORDER_LEFT(CellPropertyCategory.BORDER_TYPE), + BORDER_RIGHT(CellPropertyCategory.BORDER_TYPE), + BORDER_TOP(CellPropertyCategory.BORDER_TYPE), + + BOTTOM_BORDER_COLOR(CellPropertyCategory.SHORT), + LEFT_BORDER_COLOR(CellPropertyCategory.SHORT), + RIGHT_BORDER_COLOR(CellPropertyCategory.SHORT), + TOP_BORDER_COLOR(CellPropertyCategory.SHORT), + DATA_FORMAT(CellPropertyCategory.SHORT), + FILL_BACKGROUND_COLOR(CellPropertyCategory.SHORT), + FILL_FOREGROUND_COLOR(CellPropertyCategory.SHORT), + INDENTION(CellPropertyCategory.SHORT), + ROTATION(CellPropertyCategory.SHORT), + + FILL_BACKGROUND_COLOR_COLOR(CellPropertyCategory.COLOR), + FILL_FOREGROUND_COLOR_COLOR(CellPropertyCategory.COLOR), + + FONT(CellPropertyCategory.INT), + + HIDDEN(CellPropertyCategory.BOOL), + LOCKED(CellPropertyCategory.BOOL), + WRAP_TEXT(CellPropertyCategory.BOOL), + SHRINK_TO_FIT(CellPropertyCategory.BOOL), + QUOTE_PREFIXED(CellPropertyCategory.BOOL), + + ALIGNMENT(CellPropertyCategory.OTHER), + FILL_PATTERN(CellPropertyCategory.OTHER), + VERTICAL_ALIGNMENT(CellPropertyCategory.OTHER); + + CellPropertyType(CellPropertyCategory category) { + this.category = category; + } + + private final CellPropertyCategory category; + + public CellPropertyCategory getCategory() { + return this.category; + } + +} diff --git a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java index 3f1a30ac81..593dd6eef9 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java +++ b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java @@ -17,10 +17,9 @@ package org.apache.poi.ss.util; -import java.util.Arrays; import java.util.Collections; +import java.util.EnumSet; import java.util.HashMap; -import java.util.HashSet; import java.util.Locale; import java.util.Map; import java.util.Objects; @@ -29,8 +28,25 @@ import java.util.Set; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.poi.common.Duplicatable; -import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.usermodel.BorderStyle; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellCopyContext; +import org.apache.poi.ss.usermodel.CellCopyPolicy; +import org.apache.poi.ss.usermodel.CellPropertyType; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.Color; +import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.FillPatternType; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.HorizontalAlignment; +import org.apache.poi.ss.usermodel.Hyperlink; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.Beta; +import org.apache.poi.util.Removal; /** * Various utility functions that make working with a cells and rows easier. The various methods @@ -43,73 +59,238 @@ public final class CellUtil { private static final Logger LOGGER = LogManager.getLogger(CellUtil.class); - // FIXME: Move these constants into an enum + private static final Set SHORT_VALUES = EnumSet.of( + CellPropertyType.BOTTOM_BORDER_COLOR, + CellPropertyType.LEFT_BORDER_COLOR, + CellPropertyType.RIGHT_BORDER_COLOR, + CellPropertyType.TOP_BORDER_COLOR, + CellPropertyType.FILL_FOREGROUND_COLOR, + CellPropertyType.FILL_BACKGROUND_COLOR, + CellPropertyType.INDENTION, + CellPropertyType.DATA_FORMAT, + CellPropertyType.ROTATION + ); + + private static final Set COLOR_VALUES = EnumSet.of( + CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, + CellPropertyType.FILL_BACKGROUND_COLOR_COLOR + ); + + private static final Set INT_VALUES = EnumSet.of( + CellPropertyType.FONT + ); + + private static final Set BOOLEAN_VALUES = EnumSet.of( + CellPropertyType.LOCKED, + CellPropertyType.HIDDEN, + CellPropertyType.WRAP_TEXT, + CellPropertyType.SHRINK_TO_FIT, + CellPropertyType.QUOTE_PREFIXED + ); + + private static final Set BORDER_TYPE_VALUES = EnumSet.of( + CellPropertyType.BORDER_BOTTOM, + CellPropertyType.BORDER_LEFT, + CellPropertyType.BORDER_RIGHT, + CellPropertyType.BORDER_TOP + ); + + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#ALIGNMENT} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String ALIGNMENT = "alignment"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#BORDER_BOTTOM} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String BORDER_BOTTOM = "borderBottom"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#BORDER_LEFT} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String BORDER_LEFT = "borderLeft"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#BORDER_RIGHT} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String BORDER_RIGHT = "borderRight"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#BORDER_TOP} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String BORDER_TOP = "borderTop"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#BORDER_BOTTOM} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String BOTTOM_BORDER_COLOR = "bottomBorderColor"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#BOTTOM_BORDER_COLOR} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String LEFT_BORDER_COLOR = "leftBorderColor"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#RIGHT_BORDER_COLOR} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String RIGHT_BORDER_COLOR = "rightBorderColor"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#TOP_BORDER_COLOR} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String TOP_BORDER_COLOR = "topBorderColor"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#DATA_FORMAT} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String DATA_FORMAT = "dataFormat"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#FILL_BACKGROUND_COLOR} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String FILL_BACKGROUND_COLOR = "fillBackgroundColor"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#FILL_FOREGROUND_COLOR} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String FILL_FOREGROUND_COLOR = "fillForegroundColor"; - + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#FILL_BACKGROUND_COLOR_COLOR} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String FILL_BACKGROUND_COLOR_COLOR = "fillBackgroundColorColor"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#FILL_FOREGROUND_COLOR_COLOR} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String FILL_FOREGROUND_COLOR_COLOR = "fillForegroundColorColor"; + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#FILL_PATTERN} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String FILL_PATTERN = "fillPattern"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#FONT} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String FONT = "font"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#HIDDEN} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String HIDDEN = "hidden"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#INDENTION} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String INDENTION = "indention"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#LOCKED} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String LOCKED = "locked"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#ROTATION} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String ROTATION = "rotation"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#VERTICAL_ALIGNMENT} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String VERTICAL_ALIGNMENT = "verticalAlignment"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#WRAP_TEXT} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String WRAP_TEXT = "wrapText"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#SHRINK_TO_FIT} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String SHRINK_TO_FIT = "shrinkToFit"; + + /** + * @deprecated as of POI 5.3.1. Use {@link CellPropertyType#QUOTE_PREFIXED} instead. + */ + @Deprecated + @Removal(version = "7.0.0") public static final String QUOTE_PREFIXED = "quotePrefixed"; - private static final Set shortValues = Collections.unmodifiableSet( - new HashSet<>(Arrays.asList( - BOTTOM_BORDER_COLOR, - LEFT_BORDER_COLOR, - RIGHT_BORDER_COLOR, - TOP_BORDER_COLOR, - FILL_FOREGROUND_COLOR, - FILL_BACKGROUND_COLOR, - INDENTION, - DATA_FORMAT, - ROTATION - ))); - - private static final Set colorValues = Collections.unmodifiableSet( - new HashSet<>(Arrays.asList( - FILL_FOREGROUND_COLOR_COLOR, - FILL_BACKGROUND_COLOR_COLOR - ))); - - private static final Set intValues = Collections.unmodifiableSet( - new HashSet<>(Collections.singletonList( - FONT - ))); - private static final Set booleanValues = Collections.unmodifiableSet( - new HashSet<>(Arrays.asList( - LOCKED, - HIDDEN, - WRAP_TEXT, - SHRINK_TO_FIT, - QUOTE_PREFIXED - ))); - private static final Set borderTypeValues = Collections.unmodifiableSet( - new HashSet<>(Arrays.asList( - BORDER_BOTTOM, - BORDER_LEFT, - BORDER_RIGHT, - BORDER_TOP - ))); + // FIXME Must be deleted along with string constants + static final Map namePropertyMap = new HashMap<>(); + static { + namePropertyMap.put(ALIGNMENT, CellPropertyType.ALIGNMENT); + namePropertyMap.put(BORDER_BOTTOM, CellPropertyType.BORDER_BOTTOM); + namePropertyMap.put(BORDER_LEFT, CellPropertyType.BORDER_LEFT); + namePropertyMap.put(BORDER_RIGHT, CellPropertyType.BORDER_RIGHT); + namePropertyMap.put(BORDER_TOP, CellPropertyType.BORDER_TOP); + namePropertyMap.put(BOTTOM_BORDER_COLOR, CellPropertyType.BOTTOM_BORDER_COLOR); + namePropertyMap.put(LEFT_BORDER_COLOR, CellPropertyType.LEFT_BORDER_COLOR); + namePropertyMap.put(RIGHT_BORDER_COLOR, CellPropertyType.RIGHT_BORDER_COLOR); + namePropertyMap.put(TOP_BORDER_COLOR, CellPropertyType.TOP_BORDER_COLOR); + namePropertyMap.put(FILL_BACKGROUND_COLOR, CellPropertyType.FILL_BACKGROUND_COLOR); + namePropertyMap.put(FILL_FOREGROUND_COLOR, CellPropertyType.FILL_FOREGROUND_COLOR); + namePropertyMap.put(FILL_BACKGROUND_COLOR_COLOR, CellPropertyType.FILL_BACKGROUND_COLOR_COLOR); + namePropertyMap.put(FILL_FOREGROUND_COLOR_COLOR, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR); + namePropertyMap.put(FILL_PATTERN, CellPropertyType.FILL_PATTERN); + namePropertyMap.put(FONT, CellPropertyType.FONT); + namePropertyMap.put(HIDDEN, CellPropertyType.HIDDEN); + namePropertyMap.put(INDENTION, CellPropertyType.INDENTION); + namePropertyMap.put(LOCKED, CellPropertyType.LOCKED); + namePropertyMap.put(ROTATION, CellPropertyType.ROTATION); + namePropertyMap.put(VERTICAL_ALIGNMENT, CellPropertyType.VERTICAL_ALIGNMENT); + namePropertyMap.put(SHRINK_TO_FIT, CellPropertyType.SHRINK_TO_FIT); + namePropertyMap.put(QUOTE_PREFIXED, CellPropertyType.QUOTE_PREFIXED); + } private static final UnicodeMapping[] unicodeMappings; @@ -228,8 +409,7 @@ public final class CellUtil { // DataFormat is not copied unless policy.isCopyCellStyle is true if (DateUtil.isCellDateFormatted(srcCell)) { destCell.setCellValue(srcCell.getDateCellValue()); - } - else { + } else { destCell.setCellValue(srcCell.getNumericCellValue()); } break; @@ -280,7 +460,7 @@ public final class CellUtil { // if srcCell doesn't have a hyperlink and destCell has a hyperlink, don't clear destCell's hyperlink if (srcHyperlink != null) { if (srcHyperlink instanceof Duplicatable) { - Hyperlink newHyperlink = (Hyperlink)((Duplicatable)srcHyperlink).copy(); + Hyperlink newHyperlink = (Hyperlink) ((Duplicatable) srcHyperlink).copy(); destCell.setHyperlink(newHyperlink); } else { throw new IllegalStateException("srcCell hyperlink is not an instance of Duplicatable"); @@ -292,7 +472,7 @@ public final class CellUtil { if (srcHyperlink == null) { destCell.setHyperlink(null); } else if (srcHyperlink instanceof Duplicatable) { - Hyperlink newHyperlink = (Hyperlink)((Duplicatable)srcHyperlink).copy(); + Hyperlink newHyperlink = (Hyperlink) ((Duplicatable) srcHyperlink).copy(); destCell.setHyperlink(newHyperlink); } else { throw new IllegalStateException("srcCell hyperlink is not an instance of Duplicatable"); @@ -302,13 +482,13 @@ public final class CellUtil { /** * Take a cell, and align it. - * + *

* This is superior to cell.getCellStyle().setAlignment(align) because * this method will not modify the CellStyle object that may be referenced * by multiple cells. Instead, this method will search for existing CellStyles * that match the desired CellStyle, creating a new CellStyle with the desired * style if no match exists. - * + *

* @param cell the cell to set the alignment for * @param align the horizontal alignment to use. * @@ -316,7 +496,7 @@ public final class CellUtil { * @since POI 3.15 beta 3 */ public static void setAlignment(Cell cell, HorizontalAlignment align) { - setCellStyleProperty(cell, ALIGNMENT, align); + setCellStyleProperty(cell, CellPropertyType.ALIGNMENT, align); } /** @@ -335,7 +515,7 @@ public final class CellUtil { * @since POI 3.15 beta 3 */ public static void setVerticalAlignment(Cell cell, VerticalAlignment align) { - setCellStyleProperty(cell, VERTICAL_ALIGNMENT, align); + setCellStyleProperty(cell, CellPropertyType.VERTICAL_ALIGNMENT, align); } /** @@ -356,7 +536,7 @@ public final class CellUtil { // Check if cell belongs to workbook // (checked in setCellStyleProperty) - setCellStyleProperty(cell, FONT, fontIndex); + setCellStyleProperty(cell, CellPropertyType.FONT, fontIndex); } /** @@ -370,7 +550,7 @@ public final class CellUtil { *

This is necessary because Excel has an upper limit on the number of styles that it supports.

* *

This function is more efficient than multiple calls to - * {@link #setCellStyleProperty(Cell, String, Object)} + * {@link #setCellStyleProperty(Cell, CellPropertyType, Object)} * if adding multiple cell styles.

* *

For performance reasons, if this is the only cell in a workbook that uses a cell style, @@ -381,31 +561,65 @@ public final class CellUtil { *

* * @param cell The cell to change the style of - * @param properties The properties to be added to a cell style, as {propertyName: propertyValue}. + * @param properties The properties to be added to a cell style, as {property: propertyValue}. * @since POI 3.14 beta 2 + * @deprecated as of POI 5.3.1. See {@link #setCellStylePropertiesEnum(Cell, Map)} */ + @Deprecated + @Removal(version = "7.0.0") public static void setCellStyleProperties(Cell cell, Map properties) { + Map strPropMap = new HashMap<>(properties.size()); + properties.forEach((k, v) -> strPropMap.put(namePropertyMap.get(k), v)); + setCellStyleProperties(cell, strPropMap, false); + } + + /** + *

This method attempts to find an existing CellStyle that matches the {@code cell}'s + * current style plus styles properties in {@code properties}. A new style is created if the + * workbook does not contain a matching style.

+ * + *

Modifies the cell style of {@code cell} without affecting other cells that use the + * same style.

+ * + *

This is necessary because Excel has an upper limit on the number of styles that it supports.

+ * + *

This function is more efficient than multiple calls to + * {@link #setCellStyleProperty(Cell, CellPropertyType, Object)} + * if adding multiple cell styles.

+ * + *

For performance reasons, if this is the only cell in a workbook that uses a cell style, + * this method does NOT remove the old style from the workbook. + * + *

+ * + * @param cell The cell to change the style of + * @param properties The properties to be added to a cell style, as {property: propertyValue}. + * @since POI 5.3.1 + */ + public static void setCellStylePropertiesEnum(Cell cell, Map properties) { setCellStyleProperties(cell, properties, false); } - private static void setCellStyleProperties(final Cell cell, final Map properties, + private static void setCellStyleProperties(final Cell cell, final Map properties, final boolean disableNullColorCheck) { Workbook workbook = cell.getSheet().getWorkbook(); CellStyle originalStyle = cell.getCellStyle(); CellStyle newStyle = null; - Map values = getFormatProperties(originalStyle); - if (properties.containsKey(FILL_FOREGROUND_COLOR_COLOR) && properties.get(FILL_FOREGROUND_COLOR_COLOR) == null) { - values.remove(FILL_FOREGROUND_COLOR); + Map values = getFormatProperties(originalStyle); + if (properties.containsKey(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR) && properties.get(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR) == null) { + values.remove(CellPropertyType.FILL_FOREGROUND_COLOR); } - if (properties.containsKey(FILL_FOREGROUND_COLOR) && !properties.containsKey(FILL_FOREGROUND_COLOR_COLOR)) { - values.remove(FILL_FOREGROUND_COLOR_COLOR); + if (properties.containsKey(CellPropertyType.FILL_FOREGROUND_COLOR) && !properties.containsKey(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR)) { + values.remove(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR); } - if (properties.containsKey(FILL_BACKGROUND_COLOR_COLOR) && properties.get(FILL_BACKGROUND_COLOR_COLOR) == null) { - values.remove(FILL_BACKGROUND_COLOR); + if (properties.containsKey(CellPropertyType.FILL_BACKGROUND_COLOR_COLOR) && properties.get(CellPropertyType.FILL_BACKGROUND_COLOR_COLOR) == null) { + values.remove(CellPropertyType.FILL_BACKGROUND_COLOR); } - if (properties.containsKey(FILL_BACKGROUND_COLOR) && !properties.containsKey(FILL_BACKGROUND_COLOR_COLOR)) { - values.remove(FILL_BACKGROUND_COLOR_COLOR); + if (properties.containsKey(CellPropertyType.FILL_BACKGROUND_COLOR) && !properties.containsKey(CellPropertyType.FILL_BACKGROUND_COLOR_COLOR)) { + values.remove(CellPropertyType.FILL_BACKGROUND_COLOR_COLOR); } putAll(properties, values); @@ -415,7 +629,7 @@ public final class CellUtil { for (int i = 0; i < numberCellStyles; i++) { CellStyle wbStyle = workbook.getCellStyleAt(i); - Map wbStyleMap = getFormatProperties(wbStyle); + Map wbStyleMap = getFormatProperties(wbStyle); // the desired style already exists in the workbook. Use the existing style. if (styleMapsMatch(wbStyleMap, values, disableNullColorCheck)) { @@ -433,14 +647,14 @@ public final class CellUtil { cell.setCellStyle(newStyle); } - private static boolean styleMapsMatch(final Map newProps, - final Map storedProps, final boolean disableNullColorCheck) { - final Map map1Copy = new HashMap<>(newProps); - final Map map2Copy = new HashMap<>(storedProps); - final Object backColor1 = map1Copy.remove(FILL_BACKGROUND_COLOR_COLOR); - final Object backColor2 = map2Copy.remove(FILL_BACKGROUND_COLOR_COLOR); - final Object foreColor1 = map1Copy.remove(FILL_FOREGROUND_COLOR_COLOR); - final Object foreColor2 = map2Copy.remove(FILL_FOREGROUND_COLOR_COLOR); + private static boolean styleMapsMatch(final Map newProps, + final Map storedProps, final boolean disableNullColorCheck) { + final Map map1Copy = new HashMap<>(newProps); + final Map map2Copy = new HashMap<>(storedProps); + final Object backColor1 = map1Copy.remove(CellPropertyType.FILL_BACKGROUND_COLOR_COLOR); + final Object backColor2 = map2Copy.remove(CellPropertyType.FILL_BACKGROUND_COLOR_COLOR); + final Object foreColor1 = map1Copy.remove(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR); + final Object foreColor2 = map2Copy.remove(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR); if (map1Copy.equals(map2Copy)) { final boolean backColorsMatch = (!disableNullColorCheck && backColor2 == null) || Objects.equals(backColor1, backColor2); @@ -461,32 +675,58 @@ public final class CellUtil { * same style.

* *

If setting more than one cell style property on a cell, use - * {@link #setCellStyleProperties(Cell, Map)}, + * {@link #setCellStylePropertiesEnum(Cell, Map)}, * which is faster and does not add unnecessary intermediate CellStyles to the workbook.

* * @param cell The cell that is to be changed. - * @param propertyName The name of the property that is to be changed. + * @param property The name of the property that is to be changed. * @param propertyValue The value of the property that is to be changed. + * + * @since POI 5.3.1 */ - public static void setCellStyleProperty(Cell cell, String propertyName, Object propertyValue) { + public static void setCellStyleProperty(Cell cell, CellPropertyType property, Object propertyValue) { boolean disableNullColorCheck = false; - final Map propMap; - if (CellUtil.FILL_FOREGROUND_COLOR_COLOR.equals(propertyName) && propertyValue == null) { + final Map propMap; + if (CellPropertyType.FILL_FOREGROUND_COLOR_COLOR.equals(property) && propertyValue == null) { disableNullColorCheck = true; propMap = new HashMap<>(); - propMap.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, null); - propMap.put(CellUtil.FILL_FOREGROUND_COLOR, null); - } else if (CellUtil.FILL_BACKGROUND_COLOR_COLOR.equals(propertyName) && propertyValue == null) { + propMap.put(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, null); + propMap.put(CellPropertyType.FILL_FOREGROUND_COLOR, null); + } else if (CellPropertyType.FILL_BACKGROUND_COLOR_COLOR.equals(property) && propertyValue == null) { disableNullColorCheck = true; propMap = new HashMap<>(); - propMap.put(CellUtil.FILL_BACKGROUND_COLOR_COLOR, null); - propMap.put(CellUtil.FILL_BACKGROUND_COLOR, null); + propMap.put(CellPropertyType.FILL_BACKGROUND_COLOR_COLOR, null); + propMap.put(CellPropertyType.FILL_BACKGROUND_COLOR, null); } else { - propMap = Collections.singletonMap(propertyName, propertyValue); + propMap = Collections.singletonMap(property, propertyValue); } setCellStyleProperties(cell, propMap, disableNullColorCheck); } + /** + *

This method attempts to find an existing CellStyle that matches the {@code cell}'s + * current style plus a single style property {@code propertyName} with value + * {@code propertyValue}. + * A new style is created if the workbook does not contain a matching style.

+ * + *

Modifies the cell style of {@code cell} without affecting other cells that use the + * same style.

+ * + *

If setting more than one cell style property on a cell, use + * {@link #setCellStylePropertiesEnum(Cell, Map)}, + * which is faster and does not add unnecessary intermediate CellStyles to the workbook.

+ * + * @param cell The cell that is to be changed. + * @param propertyName The name of the property that is to be changed. + * @param propertyValue The value of the property that is to be changed. + * @deprecated as of POI 5.3.1. See {@link #setCellStyleProperty(Cell, CellPropertyType, Object)} + */ + @Deprecated + @Removal(version = "7.0.0") + public static void setCellStyleProperty(Cell cell, String propertyName, Object propertyValue) { + setCellStyleProperty(cell, namePropertyMap.get(propertyName), propertyValue); + } + /** * Returns a map containing the format properties of the given cell style. * The returned map is not tied to {@code style}, so subsequent changes @@ -494,37 +734,37 @@ public final class CellUtil { * map will not modify the cell style. The returned map is mutable. * * @param style cell style - * @return map of format properties (String -> Object) + * @return map of format properties (CellPropertyType -> Object) * @see #setFormatProperties(CellStyle, Workbook, Map) */ - private static Map getFormatProperties(CellStyle style) { - Map properties = new HashMap<>(); - put(properties, ALIGNMENT, style.getAlignment()); - put(properties, VERTICAL_ALIGNMENT, style.getVerticalAlignment()); - put(properties, BORDER_BOTTOM, style.getBorderBottom()); - put(properties, BORDER_LEFT, style.getBorderLeft()); - put(properties, BORDER_RIGHT, style.getBorderRight()); - put(properties, BORDER_TOP, style.getBorderTop()); - put(properties, BOTTOM_BORDER_COLOR, style.getBottomBorderColor()); - put(properties, DATA_FORMAT, style.getDataFormat()); - put(properties, FILL_PATTERN, style.getFillPattern()); - - put(properties, FILL_FOREGROUND_COLOR, style.getFillForegroundColor()); - put(properties, FILL_BACKGROUND_COLOR, style.getFillBackgroundColor()); - put(properties, FILL_FOREGROUND_COLOR_COLOR, style.getFillForegroundColorColor()); - put(properties, FILL_BACKGROUND_COLOR_COLOR, style.getFillBackgroundColorColor()); - - put(properties, FONT, style.getFontIndex()); - put(properties, HIDDEN, style.getHidden()); - put(properties, INDENTION, style.getIndention()); - put(properties, LEFT_BORDER_COLOR, style.getLeftBorderColor()); - put(properties, LOCKED, style.getLocked()); - put(properties, RIGHT_BORDER_COLOR, style.getRightBorderColor()); - put(properties, ROTATION, style.getRotation()); - put(properties, TOP_BORDER_COLOR, style.getTopBorderColor()); - put(properties, WRAP_TEXT, style.getWrapText()); - put(properties, SHRINK_TO_FIT, style.getShrinkToFit()); - put(properties, QUOTE_PREFIXED, style.getQuotePrefixed()); + private static Map getFormatProperties(CellStyle style) { + Map properties = new HashMap<>(); + put(properties, CellPropertyType.ALIGNMENT, style.getAlignment()); + put(properties, CellPropertyType.VERTICAL_ALIGNMENT, style.getVerticalAlignment()); + put(properties, CellPropertyType.BORDER_BOTTOM, style.getBorderBottom()); + put(properties, CellPropertyType.BORDER_LEFT, style.getBorderLeft()); + put(properties, CellPropertyType.BORDER_RIGHT, style.getBorderRight()); + put(properties, CellPropertyType.BORDER_TOP, style.getBorderTop()); + put(properties, CellPropertyType.BOTTOM_BORDER_COLOR, style.getBottomBorderColor()); + put(properties, CellPropertyType.DATA_FORMAT, style.getDataFormat()); + put(properties, CellPropertyType.FILL_PATTERN, style.getFillPattern()); + + put(properties, CellPropertyType.FILL_FOREGROUND_COLOR, style.getFillForegroundColor()); + put(properties, CellPropertyType.FILL_BACKGROUND_COLOR, style.getFillBackgroundColor()); + put(properties, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR, style.getFillForegroundColorColor()); + put(properties, CellPropertyType.FILL_BACKGROUND_COLOR_COLOR, style.getFillBackgroundColorColor()); + + put(properties, CellPropertyType.FONT, style.getFontIndex()); + put(properties, CellPropertyType.HIDDEN, style.getHidden()); + put(properties, CellPropertyType.INDENTION, style.getIndention()); + put(properties, CellPropertyType.LEFT_BORDER_COLOR, style.getLeftBorderColor()); + put(properties, CellPropertyType.LOCKED, style.getLocked()); + put(properties, CellPropertyType.RIGHT_BORDER_COLOR, style.getRightBorderColor()); + put(properties, CellPropertyType.ROTATION, style.getRotation()); + put(properties, CellPropertyType.TOP_BORDER_COLOR, style.getTopBorderColor()); + put(properties, CellPropertyType.WRAP_TEXT, style.getWrapText()); + put(properties, CellPropertyType.SHRINK_TO_FIT, style.getShrinkToFit()); + put(properties, CellPropertyType.QUOTE_PREFIXED, style.getQuotePrefixed()); return properties; } @@ -536,23 +776,23 @@ public final class CellUtil { * @param dest the property map to copy into * @since POI 3.15 beta 3 */ - private static void putAll(final Map src, Map dest) { - for (final String key : src.keySet()) { - if (shortValues.contains(key)) { + private static void putAll(final Map src, Map dest) { + for (final CellPropertyType key : src.keySet()) { + if (SHORT_VALUES.contains(key)) { dest.put(key, nullableShort(src, key)); - } else if (colorValues.contains(key)) { + } else if (COLOR_VALUES.contains(key)) { dest.put(key, getColor(src, key)); - } else if (intValues.contains(key)) { + } else if (INT_VALUES.contains(key)) { dest.put(key, getInt(src, key)); - } else if (booleanValues.contains(key)) { + } else if (BOOLEAN_VALUES.contains(key)) { dest.put(key, getBoolean(src, key)); - } else if (borderTypeValues.contains(key)) { + } else if (BORDER_TYPE_VALUES.contains(key)) { dest.put(key, getBorderStyle(src, key)); - } else if (ALIGNMENT.equals(key)) { + } else if (CellPropertyType.ALIGNMENT.equals(key)) { dest.put(key, getHorizontalAlignment(src, key)); - } else if (VERTICAL_ALIGNMENT.equals(key)) { + } else if (CellPropertyType.VERTICAL_ALIGNMENT.equals(key)) { dest.put(key, getVerticalAlignment(src, key)); - } else if (FILL_PATTERN.equals(key)) { + } else if (CellPropertyType.FILL_PATTERN.equals(key)) { dest.put(key, getFillPattern(src, key)); } else { LOGGER.atInfo().log("Ignoring unrecognized CellUtil format properties key: {}", key); @@ -565,31 +805,31 @@ public final class CellUtil { * * @param style cell style * @param workbook parent workbook - * @param properties map of format properties (String -> Object) + * @param properties map of format properties (CellPropertyType -> Object) * @see #getFormatProperties(CellStyle) */ - private static void setFormatProperties(CellStyle style, Workbook workbook, Map properties) { - style.setAlignment(getHorizontalAlignment(properties, ALIGNMENT)); - style.setVerticalAlignment(getVerticalAlignment(properties, VERTICAL_ALIGNMENT)); - style.setBorderBottom(getBorderStyle(properties, BORDER_BOTTOM)); - style.setBorderLeft(getBorderStyle(properties, BORDER_LEFT)); - style.setBorderRight(getBorderStyle(properties, BORDER_RIGHT)); - style.setBorderTop(getBorderStyle(properties, BORDER_TOP)); - style.setBottomBorderColor(getShort(properties, BOTTOM_BORDER_COLOR)); - style.setDataFormat(getShort(properties, DATA_FORMAT)); - style.setFillPattern(getFillPattern(properties, FILL_PATTERN)); - - Short fillForeColorShort = nullableShort(properties, FILL_FOREGROUND_COLOR); + private static void setFormatProperties(CellStyle style, Workbook workbook, Map properties) { + style.setAlignment(getHorizontalAlignment(properties, CellPropertyType.ALIGNMENT)); + style.setVerticalAlignment(getVerticalAlignment(properties, CellPropertyType.VERTICAL_ALIGNMENT)); + style.setBorderBottom(getBorderStyle(properties, CellPropertyType.BORDER_BOTTOM)); + style.setBorderLeft(getBorderStyle(properties, CellPropertyType.BORDER_LEFT)); + style.setBorderRight(getBorderStyle(properties, CellPropertyType.BORDER_RIGHT)); + style.setBorderTop(getBorderStyle(properties, CellPropertyType.BORDER_TOP)); + style.setBottomBorderColor(getShort(properties, CellPropertyType.BOTTOM_BORDER_COLOR)); + style.setDataFormat(getShort(properties, CellPropertyType.DATA_FORMAT)); + style.setFillPattern(getFillPattern(properties, CellPropertyType.FILL_PATTERN)); + + Short fillForeColorShort = nullableShort(properties, CellPropertyType.FILL_FOREGROUND_COLOR); if (fillForeColorShort != null) { style.setFillForegroundColor(fillForeColorShort); } - Short fillBackColorShort = nullableShort(properties, FILL_BACKGROUND_COLOR); + Short fillBackColorShort = nullableShort(properties, CellPropertyType.FILL_BACKGROUND_COLOR); if (fillBackColorShort != null) { style.setFillBackgroundColor(fillBackColorShort); } - Color foregroundFillColor = getColor(properties, FILL_FOREGROUND_COLOR_COLOR); - Color backgroundFillColor = getColor(properties, FILL_BACKGROUND_COLOR_COLOR); + Color foregroundFillColor = getColor(properties, CellPropertyType.FILL_FOREGROUND_COLOR_COLOR); + Color backgroundFillColor = getColor(properties, CellPropertyType.FILL_BACKGROUND_COLOR_COLOR); if (foregroundFillColor != null) { try { @@ -606,37 +846,37 @@ public final class CellUtil { } } - style.setFont(workbook.getFontAt(getInt(properties, FONT))); - style.setHidden(getBoolean(properties, HIDDEN)); - style.setIndention(getShort(properties, INDENTION)); - style.setLeftBorderColor(getShort(properties, LEFT_BORDER_COLOR)); - style.setLocked(getBoolean(properties, LOCKED)); - style.setRightBorderColor(getShort(properties, RIGHT_BORDER_COLOR)); - style.setRotation(getShort(properties, ROTATION)); - style.setTopBorderColor(getShort(properties, TOP_BORDER_COLOR)); - style.setWrapText(getBoolean(properties, WRAP_TEXT)); - style.setShrinkToFit(getBoolean(properties, SHRINK_TO_FIT)); - style.setQuotePrefixed(getBoolean(properties, QUOTE_PREFIXED)); + style.setFont(workbook.getFontAt(getInt(properties, CellPropertyType.FONT))); + style.setHidden(getBoolean(properties, CellPropertyType.HIDDEN)); + style.setIndention(getShort(properties, CellPropertyType.INDENTION)); + style.setLeftBorderColor(getShort(properties, CellPropertyType.LEFT_BORDER_COLOR)); + style.setLocked(getBoolean(properties, CellPropertyType.LOCKED)); + style.setRightBorderColor(getShort(properties, CellPropertyType.RIGHT_BORDER_COLOR)); + style.setRotation(getShort(properties, CellPropertyType.ROTATION)); + style.setTopBorderColor(getShort(properties, CellPropertyType.TOP_BORDER_COLOR)); + style.setWrapText(getBoolean(properties, CellPropertyType.WRAP_TEXT)); + style.setShrinkToFit(getBoolean(properties, CellPropertyType.SHRINK_TO_FIT)); + style.setQuotePrefixed(getBoolean(properties, CellPropertyType.QUOTE_PREFIXED)); } /** * Utility method that returns the named short value from the given map. * - * @param properties map of named properties (String -> Object) - * @param name property name + * @param properties map of named properties (CellPropertyType -> Object) + * @param property property * @return zero if the property does not exist, or is not a {@link Short} * otherwise the property value */ - private static short getShort(Map properties, String name) { - Object value = properties.get(name); + private static short getShort(Map properties, CellPropertyType property) { + Object value = properties.get(property); if (value instanceof Number) { return ((Number) value).shortValue(); } return 0; } - private static Short nullableShort(Map properties, String name) { - Object value = properties.get(name); + private static Short nullableShort(Map properties, CellPropertyType property) { + Object value = properties.get(property); if (value instanceof Short) { return (Short) value; } @@ -645,17 +885,17 @@ public final class CellUtil { } return null; } - + /** * Utility method that returns the named Color value from the given map. * - * @param properties map of named properties (String -> Object) - * @param name property name + * @param properties map of named properties (CellPropertyType -> Object) + * @param property property * @return null if the property does not exist, or is not a {@link Color} * otherwise the property value */ - private static Color getColor(Map properties, String name) { - Object value = properties.get(name); + private static Color getColor(Map properties, CellPropertyType property) { + Object value = properties.get(property); if (value instanceof Color) { return (Color) value; } @@ -666,13 +906,13 @@ public final class CellUtil { /** * Utility method that returns the named int value from the given map. * - * @param properties map of named properties (String -> Object) - * @param name property name + * @param properties map of named properties (CellPropertyType -> Object) + * @param property property * @return zero if the property does not exist, or is not a {@link Integer} * otherwise the property value */ - private static int getInt(Map properties, String name) { - Object value = properties.get(name); + private static int getInt(Map properties, CellPropertyType property) { + Object value = properties.get(property); if (value instanceof Number) { return ((Number) value).intValue(); } @@ -682,26 +922,24 @@ public final class CellUtil { /** * Utility method that returns the named BorderStyle value from the given map. * - * @param properties map of named properties (String -> Object) - * @param name property name + * @param properties map of named properties (CellPropertyType -> Object) + * @param property property * @return Border style if set, otherwise {@link BorderStyle#NONE} */ - private static BorderStyle getBorderStyle(Map properties, String name) { - Object value = properties.get(name); + private static BorderStyle getBorderStyle(Map properties, CellPropertyType property) { + Object value = properties.get(property); BorderStyle border; if (value instanceof BorderStyle) { border = (BorderStyle) value; } // @deprecated 3.15 beta 2. getBorderStyle will only work on BorderStyle enums instead of codes in the future. else if (value instanceof Short) { - LOGGER.atWarn().log("Deprecation warning: CellUtil properties map uses Short values for {}. Should use BorderStyle enums instead.", name); + LOGGER.atWarn().log("Deprecation warning: CellUtil properties map uses Short values for {}. Should use BorderStyle enums instead.", property); short code = (Short) value; border = BorderStyle.valueOf(code); - } - else if (value == null) { + } else if (value == null) { border = BorderStyle.NONE; - } - else { + } else { throw new IllegalStateException("Unexpected border style class. Must be BorderStyle or Short (deprecated)."); } return border; @@ -710,27 +948,25 @@ public final class CellUtil { /** * Utility method that returns the named FillPatternType value from the given map. * - * @param properties map of named properties (String -> Object) - * @param name property name + * @param properties map of named properties (CellPropertyType -> Object) + * @param property property * @return FillPatternType style if set, otherwise {@link FillPatternType#NO_FILL} * @since POI 3.15 beta 3 */ - private static FillPatternType getFillPattern(Map properties, String name) { - Object value = properties.get(name); + private static FillPatternType getFillPattern(Map properties, CellPropertyType property) { + Object value = properties.get(property); FillPatternType pattern; if (value instanceof FillPatternType) { pattern = (FillPatternType) value; } // @deprecated 3.15 beta 2. getFillPattern will only work on FillPatternType enums instead of codes in the future. else if (value instanceof Short) { - LOGGER.atWarn().log("Deprecation warning: CellUtil properties map uses Short values for {}. Should use FillPatternType enums instead.", name); + LOGGER.atWarn().log("Deprecation warning: CellUtil properties map uses Short values for {}. Should use FillPatternType enums instead.", property); short code = (Short) value; pattern = FillPatternType.forInt(code); - } - else if (value == null) { + } else if (value == null) { pattern = FillPatternType.NO_FILL; - } - else { + } else { throw new IllegalStateException("Unexpected fill pattern style class. Must be FillPatternType or Short (deprecated)."); } return pattern; @@ -739,27 +975,25 @@ public final class CellUtil { /** * Utility method that returns the named HorizontalAlignment value from the given map. * - * @param properties map of named properties (String -> Object) - * @param name property name + * @param properties map of named properties (CellPropertyType -> Object) + * @param property property * @return HorizontalAlignment style if set, otherwise {@link HorizontalAlignment#GENERAL} * @since POI 3.15 beta 3 */ - private static HorizontalAlignment getHorizontalAlignment(Map properties, String name) { - Object value = properties.get(name); + private static HorizontalAlignment getHorizontalAlignment(Map properties, CellPropertyType property) { + Object value = properties.get(property); HorizontalAlignment align; if (value instanceof HorizontalAlignment) { align = (HorizontalAlignment) value; } // @deprecated 3.15 beta 2. getHorizontalAlignment will only work on HorizontalAlignment enums instead of codes in the future. else if (value instanceof Short) { - LOGGER.atWarn().log("Deprecation warning: CellUtil properties map used a Short value for {}. Should use HorizontalAlignment enums instead.", name); + LOGGER.atWarn().log("Deprecation warning: CellUtil properties map used a Short value for {}. Should use HorizontalAlignment enums instead.", property); short code = (Short) value; align = HorizontalAlignment.forInt(code); - } - else if (value == null) { + } else if (value == null) { align = HorizontalAlignment.GENERAL; - } - else { + } else { throw new IllegalStateException("Unexpected horizontal alignment style class. Must be HorizontalAlignment or Short (deprecated)."); } return align; @@ -768,27 +1002,25 @@ public final class CellUtil { /** * Utility method that returns the named VerticalAlignment value from the given map. * - * @param properties map of named properties (String -> Object) - * @param name property name + * @param properties map of named properties (CellPropertyType -> Object) + * @param property property * @return VerticalAlignment style if set, otherwise {@link VerticalAlignment#BOTTOM} * @since POI 3.15 beta 3 */ - private static VerticalAlignment getVerticalAlignment(Map properties, String name) { - Object value = properties.get(name); + private static VerticalAlignment getVerticalAlignment(Map properties, CellPropertyType property) { + Object value = properties.get(property); VerticalAlignment align; if (value instanceof VerticalAlignment) { align = (VerticalAlignment) value; } // @deprecated 3.15 beta 2. getVerticalAlignment will only work on VerticalAlignment enums instead of codes in the future. else if (value instanceof Short) { - LOGGER.atWarn().log("Deprecation warning: CellUtil properties map used a Short value for {}. Should use VerticalAlignment enums instead.", name); + LOGGER.atWarn().log("Deprecation warning: CellUtil properties map used a Short value for {}. Should use VerticalAlignment enums instead.", property); short code = (Short) value; align = VerticalAlignment.forInt(code); - } - else if (value == null) { + } else if (value == null) { align = VerticalAlignment.BOTTOM; - } - else { + } else { throw new IllegalStateException("Unexpected vertical alignment style class. Must be VerticalAlignment or Short (deprecated)."); } return align; @@ -797,13 +1029,13 @@ public final class CellUtil { /** * Utility method that returns the named boolean value from the given map. * - * @param properties map of properties (String -> Object) - * @param name property name + * @param properties map of properties (CellPropertyType -> Object) + * @param property property * @return false if the property does not exist, or is not a {@link Boolean}, * true otherwise */ - private static boolean getBoolean(Map properties, String name) { - Object value = properties.get(name); + private static boolean getBoolean(Map properties, CellPropertyType property) { + Object value = properties.get(property); //noinspection SimplifiableIfStatement if (value instanceof Boolean) { return (Boolean) value; @@ -814,12 +1046,12 @@ public final class CellUtil { /** * Utility method that puts the given value to the given map. * - * @param properties map of properties (String -> Object) - * @param name property name + * @param properties map of properties (CellPropertyType -> Object) + * @param property property * @param value property value */ - private static void put(Map properties, String name, Object value) { - properties.put(name, value); + private static void put(Map properties, CellPropertyType property, Object value) { + properties.put(property, value); } /** @@ -851,24 +1083,25 @@ public final class CellUtil { static { unicodeMappings = new UnicodeMapping[] { - um("alpha", "\u03B1" ), - um("beta", "\u03B2" ), - um("gamma", "\u03B3" ), - um("delta", "\u03B4" ), - um("epsilon", "\u03B5" ), - um("zeta", "\u03B6" ), - um("eta", "\u03B7" ), - um("theta", "\u03B8" ), - um("iota", "\u03B9" ), - um("kappa", "\u03BA" ), - um("lambda", "\u03BB" ), - um("mu", "\u03BC" ), - um("nu", "\u03BD" ), - um("xi", "\u03BE" ), - um("omicron", "\u03BF" ), + um("alpha", "\u03B1" ), + um("beta", "\u03B2" ), + um("gamma", "\u03B3" ), + um("delta", "\u03B4" ), + um("epsilon", "\u03B5" ), + um("zeta", "\u03B6" ), + um("eta", "\u03B7" ), + um("theta", "\u03B8" ), + um("iota", "\u03B9" ), + um("kappa", "\u03BA" ), + um("lambda", "\u03BB" ), + um("mu", "\u03BC" ), + um("nu", "\u03BD" ), + um("xi", "\u03BE" ), + um("omicron", "\u03BF" ), }; } + private static UnicodeMapping um(String entityName, String resolvedValue) { return new UnicodeMapping(entityName, resolvedValue); } diff --git a/poi/src/main/java/org/apache/poi/ss/util/PropertyTemplate.java b/poi/src/main/java/org/apache/poi/ss/util/PropertyTemplate.java index c9aa9e835e..2f74af55d7 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/PropertyTemplate.java +++ b/poi/src/main/java/org/apache/poi/ss/util/PropertyTemplate.java @@ -17,21 +17,23 @@ package org.apache.poi.ss.util; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.SpreadsheetVersion; + import org.apache.poi.ss.usermodel.BorderExtent; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellPropertyType; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + /** *

* A PropertyTemplate is a template that can be applied to any sheet in @@ -57,7 +59,7 @@ public final class PropertyTemplate { * This is a list of cell properties for one shot application to a range of * cells at a later time. */ - private final Map> _propertyTemplate; + private final Map> _propertyTemplate; /** * Create a PropertyTemplate object @@ -73,16 +75,16 @@ public final class PropertyTemplate { */ public PropertyTemplate(PropertyTemplate template) { this(); - for(Map.Entry> entry : template.getTemplate().entrySet()) { + for (Map.Entry> entry : template.getTemplate().entrySet()) { _propertyTemplate.put(new CellAddress(entry.getKey()), cloneCellProperties(entry.getValue())); } } - private Map> getTemplate() { + private Map> getTemplate() { return _propertyTemplate; } - private static Map cloneCellProperties(Map properties) { + private static Map cloneCellProperties(Map properties) { return new HashMap<>(properties); } @@ -101,52 +103,52 @@ public final class PropertyTemplate { * applied. */ public void drawBorders(CellRangeAddress range, BorderStyle borderType, - BorderExtent extent) { + BorderExtent extent) { switch (extent) { - case NONE: - removeBorders(range); - break; - case ALL: - drawHorizontalBorders(range, borderType, BorderExtent.ALL); - drawVerticalBorders(range, borderType, BorderExtent.ALL); - break; - case INSIDE: - drawHorizontalBorders(range, borderType, BorderExtent.INSIDE); - drawVerticalBorders(range, borderType, BorderExtent.INSIDE); - break; - case OUTSIDE: - drawOutsideBorders(range, borderType, BorderExtent.ALL); - break; - case TOP: - drawTopBorder(range, borderType); - break; - case BOTTOM: - drawBottomBorder(range, borderType); - break; - case LEFT: - drawLeftBorder(range, borderType); - break; - case RIGHT: - drawRightBorder(range, borderType); - break; - case HORIZONTAL: - drawHorizontalBorders(range, borderType, BorderExtent.ALL); - break; - case INSIDE_HORIZONTAL: - drawHorizontalBorders(range, borderType, BorderExtent.INSIDE); - break; - case OUTSIDE_HORIZONTAL: - drawOutsideBorders(range, borderType, BorderExtent.HORIZONTAL); - break; - case VERTICAL: - drawVerticalBorders(range, borderType, BorderExtent.ALL); - break; - case INSIDE_VERTICAL: - drawVerticalBorders(range, borderType, BorderExtent.INSIDE); - break; - case OUTSIDE_VERTICAL: - drawOutsideBorders(range, borderType, BorderExtent.VERTICAL); - break; + case NONE: + removeBorders(range); + break; + case ALL: + drawHorizontalBorders(range, borderType, BorderExtent.ALL); + drawVerticalBorders(range, borderType, BorderExtent.ALL); + break; + case INSIDE: + drawHorizontalBorders(range, borderType, BorderExtent.INSIDE); + drawVerticalBorders(range, borderType, BorderExtent.INSIDE); + break; + case OUTSIDE: + drawOutsideBorders(range, borderType, BorderExtent.ALL); + break; + case TOP: + drawTopBorder(range, borderType); + break; + case BOTTOM: + drawBottomBorder(range, borderType); + break; + case LEFT: + drawLeftBorder(range, borderType); + break; + case RIGHT: + drawRightBorder(range, borderType); + break; + case HORIZONTAL: + drawHorizontalBorders(range, borderType, BorderExtent.ALL); + break; + case INSIDE_HORIZONTAL: + drawHorizontalBorders(range, borderType, BorderExtent.INSIDE); + break; + case OUTSIDE_HORIZONTAL: + drawOutsideBorders(range, borderType, BorderExtent.HORIZONTAL); + break; + case VERTICAL: + drawVerticalBorders(range, borderType, BorderExtent.ALL); + break; + case INSIDE_VERTICAL: + drawVerticalBorders(range, borderType, BorderExtent.INSIDE); + break; + case OUTSIDE_VERTICAL: + drawOutsideBorders(range, borderType, BorderExtent.VERTICAL); + break; } } @@ -168,7 +170,7 @@ public final class PropertyTemplate { * applied. */ public void drawBorders(CellRangeAddress range, BorderStyle borderType, - short color, BorderExtent extent) { + short color, BorderExtent extent) { drawBorders(range, borderType, extent); if (borderType != BorderStyle.NONE) { drawBorderColors(range, color, extent); @@ -191,9 +193,9 @@ public final class PropertyTemplate { int firstCol = range.getFirstColumn(); int lastCol = range.getLastColumn(); for (int i = firstCol; i <= lastCol; i++) { - addProperty(row, i, CellUtil.BORDER_TOP, borderType); + addProperty(row, i, CellPropertyType.BORDER_TOP, borderType); if (borderType == BorderStyle.NONE && row > 0) { - addProperty(row - 1, i, CellUtil.BORDER_BOTTOM, borderType); + addProperty(row - 1, i, CellPropertyType.BORDER_BOTTOM, borderType); } } } @@ -210,15 +212,15 @@ public final class PropertyTemplate { * - Type of border to draw. {@link BorderStyle}. */ private void drawBottomBorder(CellRangeAddress range, - BorderStyle borderType) { + BorderStyle borderType) { int row = range.getLastRow(); int firstCol = range.getFirstColumn(); int lastCol = range.getLastColumn(); for (int i = firstCol; i <= lastCol; i++) { - addProperty(row, i, CellUtil.BORDER_BOTTOM, borderType); + addProperty(row, i, CellPropertyType.BORDER_BOTTOM, borderType); if (borderType == BorderStyle.NONE && row < SpreadsheetVersion.EXCEL2007.getMaxRows() - 1) { - addProperty(row + 1, i, CellUtil.BORDER_TOP, borderType); + addProperty(row + 1, i, CellPropertyType.BORDER_TOP, borderType); } } } @@ -235,14 +237,14 @@ public final class PropertyTemplate { * - Type of border to draw. {@link BorderStyle}. */ private void drawLeftBorder(CellRangeAddress range, - BorderStyle borderType) { + BorderStyle borderType) { int firstRow = range.getFirstRow(); int lastRow = range.getLastRow(); int col = range.getFirstColumn(); for (int i = firstRow; i <= lastRow; i++) { - addProperty(i, col, CellUtil.BORDER_LEFT, borderType); + addProperty(i, col, CellPropertyType.BORDER_LEFT, borderType); if (borderType == BorderStyle.NONE && col > 0) { - addProperty(i, col - 1, CellUtil.BORDER_RIGHT, borderType); + addProperty(i, col - 1, CellPropertyType.BORDER_RIGHT, borderType); } } } @@ -259,15 +261,15 @@ public final class PropertyTemplate { * - Type of border to draw. {@link BorderStyle}. */ private void drawRightBorder(CellRangeAddress range, - BorderStyle borderType) { + BorderStyle borderType) { int firstRow = range.getFirstRow(); int lastRow = range.getLastRow(); int col = range.getLastColumn(); for (int i = firstRow; i <= lastRow; i++) { - addProperty(i, col, CellUtil.BORDER_RIGHT, borderType); + addProperty(i, col, CellPropertyType.BORDER_RIGHT, borderType); if (borderType == BorderStyle.NONE && col < SpreadsheetVersion.EXCEL2007.getMaxColumns() - 1) { - addProperty(i, col + 1, CellUtil.BORDER_LEFT, borderType); + addProperty(i, col + 1, CellPropertyType.BORDER_LEFT, borderType); } } } @@ -292,23 +294,23 @@ public final class PropertyTemplate { * */ private void drawOutsideBorders(CellRangeAddress range, - BorderStyle borderType, BorderExtent extent) { + BorderStyle borderType, BorderExtent extent) { switch (extent) { - case ALL: - case HORIZONTAL: - case VERTICAL: - if (extent == BorderExtent.ALL || extent == BorderExtent.HORIZONTAL) { - drawTopBorder(range, borderType); - drawBottomBorder(range, borderType); - } - if (extent == BorderExtent.ALL || extent == BorderExtent.VERTICAL) { - drawLeftBorder(range, borderType); - drawRightBorder(range, borderType); - } - break; - default: - throw new IllegalArgumentException( - "Unsupported PropertyTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL"); + case ALL: + case HORIZONTAL: + case VERTICAL: + if (extent == BorderExtent.ALL || extent == BorderExtent.HORIZONTAL) { + drawTopBorder(range, borderType); + drawBottomBorder(range, borderType); + } + if (extent == BorderExtent.ALL || extent == BorderExtent.VERTICAL) { + drawLeftBorder(range, borderType); + drawRightBorder(range, borderType); + } + break; + default: + throw new IllegalArgumentException( + "Unsupported PropertyTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL"); } } @@ -331,28 +333,28 @@ public final class PropertyTemplate { * */ private void drawHorizontalBorders(CellRangeAddress range, - BorderStyle borderType, BorderExtent extent) { + BorderStyle borderType, BorderExtent extent) { switch (extent) { - case ALL: - case INSIDE: - int firstRow = range.getFirstRow(); - int lastRow = range.getLastRow(); - int firstCol = range.getFirstColumn(); - int lastCol = range.getLastColumn(); - for (int i = firstRow; i <= lastRow; i++) { - CellRangeAddress row = new CellRangeAddress(i, i, firstCol, - lastCol); - if (extent == BorderExtent.ALL || i > firstRow) { - drawTopBorder(row, borderType); - } - if (extent == BorderExtent.ALL || i < lastRow) { - drawBottomBorder(row, borderType); + case ALL: + case INSIDE: + int firstRow = range.getFirstRow(); + int lastRow = range.getLastRow(); + int firstCol = range.getFirstColumn(); + int lastCol = range.getLastColumn(); + for (int i = firstRow; i <= lastRow; i++) { + CellRangeAddress row = new CellRangeAddress(i, i, firstCol, + lastCol); + if (extent == BorderExtent.ALL || i > firstRow) { + drawTopBorder(row, borderType); + } + if (extent == BorderExtent.ALL || i < lastRow) { + drawBottomBorder(row, borderType); + } } - } - break; - default: - throw new IllegalArgumentException( - "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE"); + break; + default: + throw new IllegalArgumentException( + "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE"); } } @@ -375,28 +377,28 @@ public final class PropertyTemplate { * */ private void drawVerticalBorders(CellRangeAddress range, - BorderStyle borderType, BorderExtent extent) { + BorderStyle borderType, BorderExtent extent) { switch (extent) { - case ALL: - case INSIDE: - int firstRow = range.getFirstRow(); - int lastRow = range.getLastRow(); - int firstCol = range.getFirstColumn(); - int lastCol = range.getLastColumn(); - for (int i = firstCol; i <= lastCol; i++) { - CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, - i, i); - if (extent == BorderExtent.ALL || i > firstCol) { - drawLeftBorder(row, borderType); - } - if (extent == BorderExtent.ALL || i < lastCol) { - drawRightBorder(row, borderType); + case ALL: + case INSIDE: + int firstRow = range.getFirstRow(); + int lastRow = range.getLastRow(); + int firstCol = range.getFirstColumn(); + int lastCol = range.getLastColumn(); + for (int i = firstCol; i <= lastCol; i++) { + CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, + i, i); + if (extent == BorderExtent.ALL || i > firstCol) { + drawLeftBorder(row, borderType); + } + if (extent == BorderExtent.ALL || i < lastCol) { + drawRightBorder(row, borderType); + } } - } - break; - default: - throw new IllegalArgumentException( - "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE"); + break; + default: + throw new IllegalArgumentException( + "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE"); } } @@ -407,11 +409,11 @@ public final class PropertyTemplate { * @param range - {@link CellRangeAddress} range of cells to remove borders. */ private void removeBorders(CellRangeAddress range) { - Set properties = new HashSet<>(); - properties.add(CellUtil.BORDER_TOP); - properties.add(CellUtil.BORDER_BOTTOM); - properties.add(CellUtil.BORDER_LEFT); - properties.add(CellUtil.BORDER_RIGHT); + Set properties = new HashSet<>(); + properties.add(CellPropertyType.BORDER_TOP); + properties.add(CellPropertyType.BORDER_BOTTOM); + properties.add(CellPropertyType.BORDER_LEFT); + properties.add(CellPropertyType.BORDER_RIGHT); for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) { for (int col = range.getFirstColumn(); col <= range .getLastColumn(); col++) { @@ -431,16 +433,16 @@ public final class PropertyTemplate { */ public void applyBorders(Sheet sheet) { Workbook wb = sheet.getWorkbook(); - for (Map.Entry> entry : _propertyTemplate + for (Map.Entry> entry : _propertyTemplate .entrySet()) { CellAddress cellAddress = entry.getKey(); if (cellAddress.getRow() < wb.getSpreadsheetVersion().getMaxRows() && cellAddress.getColumn() < wb.getSpreadsheetVersion() - .getMaxColumns()) { - Map properties = entry.getValue(); + .getMaxColumns()) { + Map properties = entry.getValue(); Row row = CellUtil.getRow(cellAddress.getRow(), sheet); Cell cell = CellUtil.getCell(row, cellAddress.getColumn()); - CellUtil.setCellStyleProperties(cell, properties); + CellUtil.setCellStylePropertiesEnum(cell, properties); } } } @@ -462,52 +464,52 @@ public final class PropertyTemplate { * colors are set. */ public void drawBorderColors(CellRangeAddress range, short color, - BorderExtent extent) { + BorderExtent extent) { switch (extent) { - case NONE: - removeBorderColors(range); - break; - case ALL: - drawHorizontalBorderColors(range, color, BorderExtent.ALL); - drawVerticalBorderColors(range, color, BorderExtent.ALL); - break; - case INSIDE: - drawHorizontalBorderColors(range, color, BorderExtent.INSIDE); - drawVerticalBorderColors(range, color, BorderExtent.INSIDE); - break; - case OUTSIDE: - drawOutsideBorderColors(range, color, BorderExtent.ALL); - break; - case TOP: - drawTopBorderColor(range, color); - break; - case BOTTOM: - drawBottomBorderColor(range, color); - break; - case LEFT: - drawLeftBorderColor(range, color); - break; - case RIGHT: - drawRightBorderColor(range, color); - break; - case HORIZONTAL: - drawHorizontalBorderColors(range, color, BorderExtent.ALL); - break; - case INSIDE_HORIZONTAL: - drawHorizontalBorderColors(range, color, BorderExtent.INSIDE); - break; - case OUTSIDE_HORIZONTAL: - drawOutsideBorderColors(range, color, BorderExtent.HORIZONTAL); - break; - case VERTICAL: - drawVerticalBorderColors(range, color, BorderExtent.ALL); - break; - case INSIDE_VERTICAL: - drawVerticalBorderColors(range, color, BorderExtent.INSIDE); - break; - case OUTSIDE_VERTICAL: - drawOutsideBorderColors(range, color, BorderExtent.VERTICAL); - break; + case NONE: + removeBorderColors(range); + break; + case ALL: + drawHorizontalBorderColors(range, color, BorderExtent.ALL); + drawVerticalBorderColors(range, color, BorderExtent.ALL); + break; + case INSIDE: + drawHorizontalBorderColors(range, color, BorderExtent.INSIDE); + drawVerticalBorderColors(range, color, BorderExtent.INSIDE); + break; + case OUTSIDE: + drawOutsideBorderColors(range, color, BorderExtent.ALL); + break; + case TOP: + drawTopBorderColor(range, color); + break; + case BOTTOM: + drawBottomBorderColor(range, color); + break; + case LEFT: + drawLeftBorderColor(range, color); + break; + case RIGHT: + drawRightBorderColor(range, color); + break; + case HORIZONTAL: + drawHorizontalBorderColors(range, color, BorderExtent.ALL); + break; + case INSIDE_HORIZONTAL: + drawHorizontalBorderColors(range, color, BorderExtent.INSIDE); + break; + case OUTSIDE_HORIZONTAL: + drawOutsideBorderColors(range, color, BorderExtent.HORIZONTAL); + break; + case VERTICAL: + drawVerticalBorderColors(range, color, BorderExtent.ALL); + break; + case INSIDE_VERTICAL: + drawVerticalBorderColors(range, color, BorderExtent.INSIDE); + break; + case OUTSIDE_VERTICAL: + drawOutsideBorderColors(range, color, BorderExtent.VERTICAL); + break; } } @@ -529,11 +531,11 @@ public final class PropertyTemplate { int lastCol = range.getLastColumn(); for (int i = firstCol; i <= lastCol; i++) { if (getBorderStyle(row, i, - CellUtil.BORDER_TOP) == BorderStyle.NONE) { + CellPropertyType.BORDER_TOP) == BorderStyle.NONE) { drawTopBorder(new CellRangeAddress(row, row, i, i), BorderStyle.THIN); } - addProperty(row, i, CellUtil.TOP_BORDER_COLOR, color); + addProperty(row, i, CellPropertyType.TOP_BORDER_COLOR, color); } } @@ -555,11 +557,11 @@ public final class PropertyTemplate { int lastCol = range.getLastColumn(); for (int i = firstCol; i <= lastCol; i++) { if (getBorderStyle(row, i, - CellUtil.BORDER_BOTTOM) == BorderStyle.NONE) { + CellPropertyType.BORDER_BOTTOM) == BorderStyle.NONE) { drawBottomBorder(new CellRangeAddress(row, row, i, i), BorderStyle.THIN); } - addProperty(row, i, CellUtil.BOTTOM_BORDER_COLOR, color); + addProperty(row, i, CellPropertyType.BOTTOM_BORDER_COLOR, color); } } @@ -581,11 +583,11 @@ public final class PropertyTemplate { int col = range.getFirstColumn(); for (int i = firstRow; i <= lastRow; i++) { if (getBorderStyle(i, col, - CellUtil.BORDER_LEFT) == BorderStyle.NONE) { + CellPropertyType.BORDER_LEFT) == BorderStyle.NONE) { drawLeftBorder(new CellRangeAddress(i, i, col, col), BorderStyle.THIN); } - addProperty(i, col, CellUtil.LEFT_BORDER_COLOR, color); + addProperty(i, col, CellPropertyType.LEFT_BORDER_COLOR, color); } } @@ -608,11 +610,11 @@ public final class PropertyTemplate { int col = range.getLastColumn(); for (int i = firstRow; i <= lastRow; i++) { if (getBorderStyle(i, col, - CellUtil.BORDER_RIGHT) == BorderStyle.NONE) { + CellPropertyType.BORDER_RIGHT) == BorderStyle.NONE) { drawRightBorder(new CellRangeAddress(i, i, col, col), BorderStyle.THIN); } - addProperty(i, col, CellUtil.RIGHT_BORDER_COLOR, color); + addProperty(i, col, CellPropertyType.RIGHT_BORDER_COLOR, color); } } @@ -637,23 +639,23 @@ public final class PropertyTemplate { * */ private void drawOutsideBorderColors(CellRangeAddress range, short color, - BorderExtent extent) { + BorderExtent extent) { switch (extent) { - case ALL: - case HORIZONTAL: - case VERTICAL: - if (extent == BorderExtent.ALL || extent == BorderExtent.HORIZONTAL) { - drawTopBorderColor(range, color); - drawBottomBorderColor(range, color); - } - if (extent == BorderExtent.ALL || extent == BorderExtent.VERTICAL) { - drawLeftBorderColor(range, color); - drawRightBorderColor(range, color); - } - break; - default: - throw new IllegalArgumentException( - "Unsupported PropertyTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL"); + case ALL: + case HORIZONTAL: + case VERTICAL: + if (extent == BorderExtent.ALL || extent == BorderExtent.HORIZONTAL) { + drawTopBorderColor(range, color); + drawBottomBorderColor(range, color); + } + if (extent == BorderExtent.ALL || extent == BorderExtent.VERTICAL) { + drawLeftBorderColor(range, color); + drawRightBorderColor(range, color); + } + break; + default: + throw new IllegalArgumentException( + "Unsupported PropertyTemplate.Extent, valid Extents are ALL, HORIZONTAL, and VERTICAL"); } } @@ -677,28 +679,28 @@ public final class PropertyTemplate { * */ private void drawHorizontalBorderColors(CellRangeAddress range, short color, - BorderExtent extent) { + BorderExtent extent) { switch (extent) { - case ALL: - case INSIDE: - int firstRow = range.getFirstRow(); - int lastRow = range.getLastRow(); - int firstCol = range.getFirstColumn(); - int lastCol = range.getLastColumn(); - for (int i = firstRow; i <= lastRow; i++) { - CellRangeAddress row = new CellRangeAddress(i, i, firstCol, - lastCol); - if (extent == BorderExtent.ALL || i > firstRow) { - drawTopBorderColor(row, color); + case ALL: + case INSIDE: + int firstRow = range.getFirstRow(); + int lastRow = range.getLastRow(); + int firstCol = range.getFirstColumn(); + int lastCol = range.getLastColumn(); + for (int i = firstRow; i <= lastRow; i++) { + CellRangeAddress row = new CellRangeAddress(i, i, firstCol, + lastCol); + if (extent == BorderExtent.ALL || i > firstRow) { + drawTopBorderColor(row, color); + } + if (extent == BorderExtent.ALL || i < lastRow) { + drawBottomBorderColor(row, color); + } } - if (extent == BorderExtent.ALL || i < lastRow) { - drawBottomBorderColor(row, color); - } - } - break; - default: - throw new IllegalArgumentException( - "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE"); + break; + default: + throw new IllegalArgumentException( + "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE"); } } @@ -722,28 +724,28 @@ public final class PropertyTemplate { * */ private void drawVerticalBorderColors(CellRangeAddress range, short color, - BorderExtent extent) { + BorderExtent extent) { switch (extent) { - case ALL: - case INSIDE: - int firstRow = range.getFirstRow(); - int lastRow = range.getLastRow(); - int firstCol = range.getFirstColumn(); - int lastCol = range.getLastColumn(); - for (int i = firstCol; i <= lastCol; i++) { - CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, - i, i); - if (extent == BorderExtent.ALL || i > firstCol) { - drawLeftBorderColor(row, color); + case ALL: + case INSIDE: + int firstRow = range.getFirstRow(); + int lastRow = range.getLastRow(); + int firstCol = range.getFirstColumn(); + int lastCol = range.getLastColumn(); + for (int i = firstCol; i <= lastCol; i++) { + CellRangeAddress row = new CellRangeAddress(firstRow, lastRow, + i, i); + if (extent == BorderExtent.ALL || i > firstCol) { + drawLeftBorderColor(row, color); + } + if (extent == BorderExtent.ALL || i < lastCol) { + drawRightBorderColor(row, color); + } } - if (extent == BorderExtent.ALL || i < lastCol) { - drawRightBorderColor(row, color); - } - } - break; - default: - throw new IllegalArgumentException( - "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE"); + break; + default: + throw new IllegalArgumentException( + "Unsupported PropertyTemplate.Extent, valid Extents are ALL and INSIDE"); } } @@ -754,11 +756,11 @@ public final class PropertyTemplate { * @param range - {@link CellRangeAddress} range of cells to remove borders. */ private void removeBorderColors(CellRangeAddress range) { - Set properties = new HashSet<>(); - properties.add(CellUtil.TOP_BORDER_COLOR); - properties.add(CellUtil.BOTTOM_BORDER_COLOR); - properties.add(CellUtil.LEFT_BORDER_COLOR); - properties.add(CellUtil.RIGHT_BORDER_COLOR); + Set properties = new HashSet<>(); + properties.add(CellPropertyType.TOP_BORDER_COLOR); + properties.add(CellPropertyType.BOTTOM_BORDER_COLOR); + properties.add(CellPropertyType.LEFT_BORDER_COLOR); + properties.add(CellPropertyType.RIGHT_BORDER_COLOR); for (int row = range.getFirstRow(); row <= range.getLastRow(); row++) { for (int col = range.getFirstColumn(); col <= range .getLastColumn(); col++) { @@ -770,16 +772,16 @@ public final class PropertyTemplate { /** * Adds a property to this PropertyTemplate for a given cell */ - private void addProperty(int row, int col, String property, short value) { + private void addProperty(int row, int col, CellPropertyType property, short value) { addProperty(row, col, property, Short.valueOf(value)); } /** * Adds a property to this PropertyTemplate for a given cell */ - private void addProperty(int row, int col, String property, Object value) { + private void addProperty(int row, int col, CellPropertyType property, Object value) { CellAddress cell = new CellAddress(row, col); - Map cellProperties = _propertyTemplate.get(cell); + Map cellProperties = _propertyTemplate.get(cell); if (cellProperties == null) { cellProperties = new HashMap<>(); } @@ -791,9 +793,9 @@ public final class PropertyTemplate { * Removes a set of properties from this PropertyTemplate for a * given cell */ - private void removeProperties(int row, int col, Set properties) { + private void removeProperties(int row, int col, Set properties) { CellAddress cell = new CellAddress(row, col); - Map cellProperties = _propertyTemplate.get(cell); + Map cellProperties = _propertyTemplate.get(cell); if (cellProperties != null) { cellProperties.keySet().removeAll(properties); if (cellProperties.isEmpty()) { @@ -808,20 +810,20 @@ public final class PropertyTemplate { * Retrieves the number of borders assigned to a cell */ public int getNumBorders(CellAddress cell) { - Map cellProperties = _propertyTemplate.get(cell); + Map cellProperties = _propertyTemplate.get(cell); if (cellProperties == null) { return 0; } int count = 0; - for (String property : cellProperties.keySet()) { - if (property.equals(CellUtil.BORDER_TOP)) + for (CellPropertyType property : cellProperties.keySet()) { + if (property.equals(CellPropertyType.BORDER_TOP)) count += 1; - if (property.equals(CellUtil.BORDER_BOTTOM)) + if (property.equals(CellPropertyType.BORDER_BOTTOM)) count += 1; - if (property.equals(CellUtil.BORDER_LEFT)) + if (property.equals(CellPropertyType.BORDER_LEFT)) count += 1; - if (property.equals(CellUtil.BORDER_RIGHT)) + if (property.equals(CellPropertyType.BORDER_RIGHT)) count += 1; } return count; @@ -838,20 +840,20 @@ public final class PropertyTemplate { * Retrieves the number of border colors assigned to a cell */ public int getNumBorderColors(CellAddress cell) { - Map cellProperties = _propertyTemplate.get(cell); + Map cellProperties = _propertyTemplate.get(cell); if (cellProperties == null) { return 0; } int count = 0; - for (String property : cellProperties.keySet()) { - if (property.equals(CellUtil.TOP_BORDER_COLOR)) + for (CellPropertyType property : cellProperties.keySet()) { + if (property.equals(CellPropertyType.TOP_BORDER_COLOR)) count += 1; - if (property.equals(CellUtil.BOTTOM_BORDER_COLOR)) + if (property.equals(CellPropertyType.BOTTOM_BORDER_COLOR)) count += 1; - if (property.equals(CellUtil.LEFT_BORDER_COLOR)) + if (property.equals(CellPropertyType.LEFT_BORDER_COLOR)) count += 1; - if (property.equals(CellUtil.RIGHT_BORDER_COLOR)) + if (property.equals(CellPropertyType.RIGHT_BORDER_COLOR)) count += 1; } return count; @@ -867,9 +869,9 @@ public final class PropertyTemplate { /** * Retrieves the border style for a given cell */ - public BorderStyle getBorderStyle(CellAddress cell, String property) { + public BorderStyle getBorderStyle(CellAddress cell, CellPropertyType property) { BorderStyle value = BorderStyle.NONE; - Map cellProperties = _propertyTemplate.get(cell); + Map cellProperties = _propertyTemplate.get(cell); if (cellProperties != null) { Object obj = cellProperties.get(property); if (obj instanceof BorderStyle) { @@ -879,19 +881,39 @@ public final class PropertyTemplate { return value; } + /** + * Retrieves the border style for a given cell + * + * @deprecated See {@link #getBorderStyle(CellAddress, CellPropertyType)} + */ + @Deprecated + public BorderStyle getBorderStyle(CellAddress cell, String propertyName) { + return getBorderStyle(cell, CellUtil.namePropertyMap.get(propertyName)); + } + /** * Retrieves the border style for a given cell */ - public BorderStyle getBorderStyle(int row, int col, String property) { + public BorderStyle getBorderStyle(int row, int col, CellPropertyType property) { return getBorderStyle(new CellAddress(row, col), property); } /** * Retrieves the border style for a given cell + * + * @deprecated See {@link #getBorderStyle(int, int, CellPropertyType)} */ - public short getTemplateProperty(CellAddress cell, String property) { + @Deprecated + public BorderStyle getBorderStyle(int row, int col, String propertyName) { + return getBorderStyle(new CellAddress(row, col), CellUtil.namePropertyMap.get(propertyName)); + } + + /** + * Retrieves the border style for a given cell + */ + public short getTemplateProperty(CellAddress cell, CellPropertyType property) { short value = 0; - Map cellProperties = _propertyTemplate.get(cell); + Map cellProperties = _propertyTemplate.get(cell); if (cellProperties != null) { Object obj = cellProperties.get(property); if (obj != null) { @@ -901,13 +923,33 @@ public final class PropertyTemplate { return value; } + /** + * Retrieves the border style for a given cell + * + * @deprecated See {@link #getTemplateProperty(CellAddress, CellPropertyType)} + */ + @Deprecated + public short getTemplateProperty(CellAddress cell, String propertyName) { + return getTemplateProperty(cell, CellUtil.namePropertyMap.get(propertyName)); + } + /** * Retrieves the border style for a given cell */ - public short getTemplateProperty(int row, int col, String property) { + public short getTemplateProperty(int row, int col, CellPropertyType property) { return getTemplateProperty(new CellAddress(row, col), property); } + /** + * Retrieves the border style for a given cell + * + * @deprecated See {@link #getTemplateProperty(int, int, CellPropertyType)} + */ + @Deprecated + public short getTemplateProperty(int row, int col, String propertyName) { + return getTemplateProperty(new CellAddress(row, col), CellUtil.namePropertyMap.get(propertyName)); + } + /** * Converts a Short object to a short value or 0 if the object is not a * Short diff --git a/poi/src/main/java/org/apache/poi/ss/util/RegionUtil.java b/poi/src/main/java/org/apache/poi/ss/util/RegionUtil.java index 7846d015a8..65a552d1ee 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/RegionUtil.java +++ b/poi/src/main/java/org/apache/poi/ss/util/RegionUtil.java @@ -19,8 +19,10 @@ package org.apache.poi.ss.util; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellPropertyType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.util.Removal; /** * Various utility functions that make working with a region of cells easier. @@ -36,30 +38,46 @@ public final class RegionUtil { */ private static final class CellPropertySetter { - private final String _propertyName; + private final CellPropertyType property; private final Object _propertyValue; - + @Deprecated public CellPropertySetter(String propertyName, int value) { - _propertyName = propertyName; - _propertyValue = Integer.valueOf(value); + this(CellUtil.namePropertyMap.get(propertyName), value); } + + @Deprecated + @Removal(version = "7.0.0") public CellPropertySetter(String propertyName, BorderStyle value) { - _propertyName = propertyName; + this(CellUtil.namePropertyMap.get(propertyName), value); + } + + /** + * @param property The property to set + * @param value The value to set the property to + * @since POI 5.3.1 + */ + public CellPropertySetter(CellPropertyType property, int value) { + this.property = property; + _propertyValue = Integer.valueOf(value); + } + + public CellPropertySetter(CellPropertyType property, BorderStyle value) { + this.property = property; _propertyValue = value; } public void setProperty(Row row, int column) { // create cell if it does not exist Cell cell = CellUtil.getCell(row, column); - CellUtil.setCellStyleProperty(cell, _propertyName, _propertyValue); + CellUtil.setCellStyleProperty(cell, property, _propertyValue); } } /** * Sets the left border style for a region of cells by manipulating the cell style of the individual * cells on the left - * + * * @param border The new border * @param region The region that should have the border * @param sheet The sheet that the region is on. @@ -70,7 +88,7 @@ public final class RegionUtil { int rowEnd = region.getLastRow(); int column = region.getFirstColumn(); - CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_LEFT, border); + CellPropertySetter cps = new CellPropertySetter(CellPropertyType.BORDER_LEFT, border); for (int i = rowStart; i <= rowEnd; i++) { cps.setProperty(CellUtil.getRow(i, sheet), column); } @@ -79,7 +97,7 @@ public final class RegionUtil { /** * Sets the left border color for a region of cells by manipulating the cell style of the individual * cells on the left - * + * * @param color The color of the border * @param region The region that should have the border * @param sheet The sheet that the region is on. @@ -90,7 +108,7 @@ public final class RegionUtil { int rowEnd = region.getLastRow(); int column = region.getFirstColumn(); - CellPropertySetter cps = new CellPropertySetter(CellUtil.LEFT_BORDER_COLOR, color); + CellPropertySetter cps = new CellPropertySetter(CellPropertyType.LEFT_BORDER_COLOR, color); for (int i = rowStart; i <= rowEnd; i++) { cps.setProperty(CellUtil.getRow(i, sheet), column); } @@ -99,7 +117,7 @@ public final class RegionUtil { /** * Sets the right border style for a region of cells by manipulating the cell style of the individual * cells on the right - * + * * @param border The new border * @param region The region that should have the border * @param sheet The sheet that the region is on. @@ -110,7 +128,7 @@ public final class RegionUtil { int rowEnd = region.getLastRow(); int column = region.getLastColumn(); - CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_RIGHT, border); + CellPropertySetter cps = new CellPropertySetter(CellPropertyType.BORDER_RIGHT, border); for (int i = rowStart; i <= rowEnd; i++) { cps.setProperty(CellUtil.getRow(i, sheet), column); } @@ -119,7 +137,7 @@ public final class RegionUtil { /** * Sets the right border color for a region of cells by manipulating the cell style of the individual * cells on the right - * + * * @param color The color of the border * @param region The region that should have the border * @param sheet The sheet that the region is on. @@ -130,7 +148,7 @@ public final class RegionUtil { int rowEnd = region.getLastRow(); int column = region.getLastColumn(); - CellPropertySetter cps = new CellPropertySetter(CellUtil.RIGHT_BORDER_COLOR, color); + CellPropertySetter cps = new CellPropertySetter(CellPropertyType.RIGHT_BORDER_COLOR, color); for (int i = rowStart; i <= rowEnd; i++) { cps.setProperty(CellUtil.getRow(i, sheet), column); } @@ -139,7 +157,7 @@ public final class RegionUtil { /** * Sets the bottom border style for a region of cells by manipulating the cell style of the individual * cells on the bottom - * + * * @param border The new border * @param region The region that should have the border * @param sheet The sheet that the region is on. @@ -149,7 +167,7 @@ public final class RegionUtil { int colStart = region.getFirstColumn(); int colEnd = region.getLastColumn(); int rowIndex = region.getLastRow(); - CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_BOTTOM, border); + CellPropertySetter cps = new CellPropertySetter(CellPropertyType.BORDER_BOTTOM, border); Row row = CellUtil.getRow(rowIndex, sheet); for (int i = colStart; i <= colEnd; i++) { cps.setProperty(row, i); @@ -159,7 +177,7 @@ public final class RegionUtil { /** * Sets the bottom border color for a region of cells by manipulating the cell style of the individual * cells on the bottom - * + * * @param color The color of the border * @param region The region that should have the border * @param sheet The sheet that the region is on. @@ -169,7 +187,7 @@ public final class RegionUtil { int colStart = region.getFirstColumn(); int colEnd = region.getLastColumn(); int rowIndex = region.getLastRow(); - CellPropertySetter cps = new CellPropertySetter(CellUtil.BOTTOM_BORDER_COLOR, color); + CellPropertySetter cps = new CellPropertySetter(CellPropertyType.BOTTOM_BORDER_COLOR, color); Row row = CellUtil.getRow(rowIndex, sheet); for (int i = colStart; i <= colEnd; i++) { cps.setProperty(row, i); @@ -179,7 +197,7 @@ public final class RegionUtil { /** * Sets the top border style for a region of cells by manipulating the cell style of the individual * cells on the top - * + * * @param border The new border * @param region The region that should have the border * @param sheet The sheet that the region is on. @@ -189,7 +207,7 @@ public final class RegionUtil { int colStart = region.getFirstColumn(); int colEnd = region.getLastColumn(); int rowIndex = region.getFirstRow(); - CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_TOP, border); + CellPropertySetter cps = new CellPropertySetter(CellPropertyType.BORDER_TOP, border); Row row = CellUtil.getRow(rowIndex, sheet); for (int i = colStart; i <= colEnd; i++) { cps.setProperty(row, i); @@ -199,7 +217,7 @@ public final class RegionUtil { /** * Sets the top border color for a region of cells by manipulating the cell style of the individual * cells on the top - * + * * @param color The color of the border * @param region The region that should have the border * @param sheet The sheet that the region is on. @@ -209,7 +227,7 @@ public final class RegionUtil { int colStart = region.getFirstColumn(); int colEnd = region.getLastColumn(); int rowIndex = region.getFirstRow(); - CellPropertySetter cps = new CellPropertySetter(CellUtil.TOP_BORDER_COLOR, color); + CellPropertySetter cps = new CellPropertySetter(CellPropertyType.TOP_BORDER_COLOR, color); Row row = CellUtil.getRow(rowIndex, sheet); for (int i = colStart; i <= colEnd; i++) { cps.setProperty(row, i); diff --git a/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java b/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java index 98927502a3..9784e080b5 100644 --- a/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java +++ b/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java @@ -17,31 +17,21 @@ package org.apache.poi.ss.util; +import org.apache.poi.ss.ITestDataProvider; +import org.apache.poi.ss.usermodel.*; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertFalse; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import org.apache.poi.ss.ITestDataProvider; -import org.apache.poi.ss.usermodel.BorderStyle; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.FillPatternType; -import org.apache.poi.ss.usermodel.Font; -import org.apache.poi.ss.usermodel.HorizontalAlignment; -import org.apache.poi.ss.usermodel.IndexedColors; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.VerticalAlignment; -import org.apache.poi.ss.usermodel.Workbook; -import org.junit.jupiter.api.Test; /** * Tests Spreadsheet CellUtil @@ -55,6 +45,27 @@ public abstract class BaseTestCellUtil { _testDataProvider = testDataProvider; } + @Test + void setCellStylePropertyByEnum() throws IOException { + try (Workbook wb = _testDataProvider.createWorkbook()) { + Sheet s = wb.createSheet(); + Row r = s.createRow(0); + Cell c = r.createCell(0); + + // Add a border should create a new style + int styCnt1 = wb.getNumCellStyles(); + CellUtil.setCellStyleProperty(c, CellPropertyType.BORDER_BOTTOM, BorderStyle.THIN); + int styCnt2 = wb.getNumCellStyles(); + assertEquals(styCnt1 + 1, styCnt2); + + // Add same border to another cell, should not create another style + c = r.createCell(1); + CellUtil.setCellStyleProperty(c, CellPropertyType.BORDER_BOTTOM, BorderStyle.THIN); + int styCnt3 = wb.getNumCellStyles(); + assertEquals(styCnt2, styCnt3); + } + } + @Test void setCellStyleProperty() throws IOException { try (Workbook wb = _testDataProvider.createWorkbook()) { @@ -76,6 +87,19 @@ public abstract class BaseTestCellUtil { } } + @Test + void setCellStylePropertyWithInvalidValueByEnum() throws IOException { + try (Workbook wb = _testDataProvider.createWorkbook()) { + Sheet s = wb.createSheet(); + Row r = s.createRow(0); + Cell c = r.createCell(0); + + // An invalid BorderStyle constant + assertThrows(RuntimeException.class, () -> CellUtil.setCellStyleProperty(c, CellPropertyType.BORDER_BOTTOM, 42)); + } + } + + @Test void setCellStylePropertyWithInvalidValue() throws IOException { try (Workbook wb = _testDataProvider.createWorkbook()) { @@ -105,6 +129,39 @@ public abstract class BaseTestCellUtil { } } + @Test() + void setCellStylePropertyBorderWithShortAndEnumByEnum() throws IOException { + try (Workbook wb = _testDataProvider.createWorkbook()) { + Sheet s = wb.createSheet(); + Row r = s.createRow(0); + Cell c = r.createCell(0); + + // A valid BorderStyle constant, as a Short + CellUtil.setCellStyleProperty(c, CellPropertyType.BORDER_BOTTOM, BorderStyle.DASH_DOT.getCode()); + assertEquals(BorderStyle.DASH_DOT, c.getCellStyle().getBorderBottom()); + + // A valid BorderStyle constant, as an Enum + CellUtil.setCellStyleProperty(c, CellPropertyType.BORDER_TOP, BorderStyle.MEDIUM_DASH_DOT); + assertEquals(BorderStyle.MEDIUM_DASH_DOT, c.getCellStyle().getBorderTop()); + } + } + + @Test() + void setCellStylePropertyWithShrinkToFitByEnum() throws IOException { + try (Workbook wb = _testDataProvider.createWorkbook()) { + Sheet s = wb.createSheet(); + Row r = s.createRow(0); + Cell c = r.createCell(0); + + // Assert that the default shrinkToFit is false + assertFalse(c.getCellStyle().getShrinkToFit()); + + // Set shrinkToFit to true + CellUtil.setCellStyleProperty(c, CellPropertyType.SHRINK_TO_FIT, true); + assertTrue(c.getCellStyle().getShrinkToFit()); + } + } + @Test() void setCellStylePropertyWithShrinkToFit() throws IOException { try (Workbook wb = _testDataProvider.createWorkbook()) { @@ -121,6 +178,22 @@ public abstract class BaseTestCellUtil { } } + @Test() + void setCellStylePropertyWithQuotePrefixedByEnum() throws IOException { + try (Workbook wb = _testDataProvider.createWorkbook()) { + Sheet s = wb.createSheet(); + Row r = s.createRow(0); + Cell c = r.createCell(0); + + // Assert that the default quotePrefixed is false + assertFalse(c.getCellStyle().getQuotePrefixed()); + + // Set quotePrefixed to true + CellUtil.setCellStyleProperty(c, CellPropertyType.QUOTE_PREFIXED, true); + assertTrue(c.getCellStyle().getQuotePrefixed()); + } + } + @Test() void setCellStylePropertyWithQuotePrefixed() throws IOException { try (Workbook wb = _testDataProvider.createWorkbook()) { @@ -143,6 +216,7 @@ public abstract class BaseTestCellUtil { Sheet s = wb.createSheet(); Row r = s.createRow(0); Cell c = r.createCell(0); + Cell c2 = r.createCell(1); Font f = wb.createFont(); f.setBold(true); @@ -195,12 +269,15 @@ public abstract class BaseTestCellUtil { cs.setShrinkToFit(true); cs.setQuotePrefixed(true); c.setCellStyle(cs); + c2.setCellStyle(cs); // Set BorderBottom from THIN to DOUBLE with setCellStyleProperty() - CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.DOUBLE); + CellUtil.setCellStyleProperty(c, CellPropertyType.BORDER_BOTTOM, BorderStyle.DOUBLE); + CellUtil.setCellStyleProperty(c2, CellUtil.BORDER_BOTTOM, BorderStyle.DOUBLE); // Assert that only BorderBottom has been changed and no others. assertEquals(BorderStyle.DOUBLE, c.getCellStyle().getBorderBottom()); + assertEquals(BorderStyle.DOUBLE, c2.getCellStyle().getBorderBottom()); assertEquals(HorizontalAlignment.CENTER, c.getCellStyle().getAlignment()); assertEquals(BorderStyle.THIN, c.getCellStyle().getBorderLeft()); assertEquals(BorderStyle.THIN, c.getCellStyle().getBorderRight()); @@ -253,6 +330,34 @@ public abstract class BaseTestCellUtil { } } + @Test + void setCellStylePropertiesEnum() throws IOException { + try (Workbook wb = _testDataProvider.createWorkbook()) { + Sheet s = wb.createSheet(); + Row r = s.createRow(0); + Cell c = r.createCell(0); + + // Add multiple border properties to cell should create a single new style + int styCnt1 = wb.getNumCellStyles(); + Map props = new HashMap<>(); + props.put(CellPropertyType.BORDER_TOP, BorderStyle.THIN); + props.put(CellPropertyType.BORDER_BOTTOM, BorderStyle.THIN); + props.put(CellPropertyType.BORDER_LEFT, BorderStyle.THIN); + props.put(CellPropertyType.BORDER_RIGHT, BorderStyle.THIN); + props.put(CellPropertyType.ALIGNMENT, HorizontalAlignment.CENTER.getCode()); // try it both with a Short (deprecated) + props.put(CellPropertyType.VERTICAL_ALIGNMENT, VerticalAlignment.CENTER); // and with an enum + CellUtil.setCellStylePropertiesEnum(c, props); + int styCnt2 = wb.getNumCellStyles(); + assertEquals(styCnt1 + 1, styCnt2, "Only one additional style should have been created"); + + // Add same border another to same cell, should not create another style + c = r.createCell(1); + CellUtil.setCellStylePropertiesEnum(c, props); + int styCnt3 = wb.getNumCellStyles(); + assertEquals(styCnt2, styCnt3, "No additional styles should have been created"); + } + } + @Test void getRow() throws IOException { try (Workbook wb = _testDataProvider.createWorkbook()) { @@ -443,7 +548,7 @@ public abstract class BaseTestCellUtil { @Test void setFontFromDifferentWorkbook() throws IOException { try (Workbook wb1 = _testDataProvider.createWorkbook(); - Workbook wb2 = _testDataProvider.createWorkbook()) { + Workbook wb2 = _testDataProvider.createWorkbook()) { Font font1 = wb1.createFont(); Font font2 = wb2.createFont(); // do something to make font1 and font2 different @@ -462,6 +567,29 @@ public abstract class BaseTestCellUtil { /** * bug 55555 + * + * @since POI 3.15 beta 3 + */ + @Test + protected void setFillForegroundColorBeforeFillBackgroundColorEnumByEnum() throws IOException { + try (Workbook wb1 = _testDataProvider.createWorkbook()) { + Cell A1 = wb1.createSheet().createRow(0).createCell(0); + Map properties = new HashMap<>(); + properties.put(CellPropertyType.FILL_PATTERN, FillPatternType.BRICKS); + properties.put(CellPropertyType.FILL_FOREGROUND_COLOR, IndexedColors.BLUE.index); + properties.put(CellPropertyType.FILL_BACKGROUND_COLOR, IndexedColors.RED.index); + + CellUtil.setCellStylePropertiesEnum(A1, properties); + CellStyle style = A1.getCellStyle(); + assertEquals(FillPatternType.BRICKS, style.getFillPattern(), "fill pattern"); + assertEquals(IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor()), "fill foreground color"); + assertEquals(IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor()), "fill background color"); + } + } + + /** + * bug 55555 + * * @since POI 3.15 beta 3 */ @Test @@ -483,6 +611,7 @@ public abstract class BaseTestCellUtil { /** * bug 63268 + * * @since POI 4.1.0 */ @Test diff --git a/poi/src/test/java/org/apache/poi/ss/util/TestPropertyTemplate.java b/poi/src/test/java/org/apache/poi/ss/util/TestPropertyTemplate.java index bd57735706..eb226466d5 100644 --- a/poi/src/test/java/org/apache/poi/ss/util/TestPropertyTemplate.java +++ b/poi/src/test/java/org/apache/poi/ss/util/TestPropertyTemplate.java @@ -17,15 +17,11 @@ package org.apache.poi.ss.util; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotSame; - -import java.io.IOException; - import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.BorderExtent; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellPropertyType; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Row; @@ -33,6 +29,11 @@ import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.junit.jupiter.api.Test; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; + /** * Tests Spreadsheet PropertyTemplate * @@ -63,6 +64,24 @@ final class TestPropertyTemplate { assertEquals(0, pt.getNumBorderColors(0, 0)); } + @Test + void getTemplatePropertiesByEnum() throws IOException { + CellRangeAddress a1 = new CellRangeAddress(0, 0, 0, 0); + PropertyTemplate pt = new PropertyTemplate(); + pt.drawBorders(a1, BorderStyle.THIN, BorderExtent.TOP); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(0, 0, CellPropertyType.BORDER_TOP)); + pt.drawBorders(a1, BorderStyle.MEDIUM, BorderExtent.BOTTOM); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(0, 0, CellPropertyType.BORDER_BOTTOM)); + pt.drawBorderColors(a1, IndexedColors.RED.getIndex(), BorderExtent.TOP); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(0, 0, CellPropertyType.TOP_BORDER_COLOR)); + pt.drawBorderColors(a1, IndexedColors.BLUE.getIndex(), BorderExtent.BOTTOM); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(0, 0, CellPropertyType.BOTTOM_BORDER_COLOR)); + } + @Test void getTemplateProperties() throws IOException { CellRangeAddress a1 = new CellRangeAddress(0, 0, 0, 0); @@ -82,7 +101,7 @@ final class TestPropertyTemplate { } @Test - void drawBorders() throws IOException { + void drawBordersByEnum() throws IOException { CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2); PropertyTemplate pt = new PropertyTemplate(); pt.drawBorders(a1c3, BorderStyle.THIN, @@ -91,13 +110,13 @@ final class TestPropertyTemplate { for (int j = 0; j <= 2; j++) { assertEquals(4, pt.getNumBorders(i, j)); assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, CellUtil.BORDER_TOP)); + pt.getBorderStyle(i, j, CellPropertyType.BORDER_TOP)); assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, CellUtil.BORDER_BOTTOM)); + pt.getBorderStyle(i, j, CellPropertyType.BORDER_BOTTOM)); assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, CellUtil.BORDER_LEFT)); + pt.getBorderStyle(i, j, CellPropertyType.BORDER_LEFT)); assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, CellUtil.BORDER_RIGHT)); + pt.getBorderStyle(i, j, CellPropertyType.BORDER_RIGHT)); } } pt.drawBorders(a1c3, BorderStyle.MEDIUM, @@ -109,300 +128,1019 @@ final class TestPropertyTemplate { if (j == 0) { assertEquals(BorderStyle.MEDIUM, pt.getBorderStyle(i, j, - CellUtil.BORDER_TOP)); + CellPropertyType.BORDER_TOP)); assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_BOTTOM)); + CellPropertyType.BORDER_BOTTOM)); assertEquals(BorderStyle.MEDIUM, pt.getBorderStyle(i, j, - CellUtil.BORDER_LEFT)); + CellPropertyType.BORDER_LEFT)); assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_RIGHT)); + CellPropertyType.BORDER_RIGHT)); } else if (j == 2) { assertEquals(BorderStyle.MEDIUM, pt.getBorderStyle(i, j, - CellUtil.BORDER_TOP)); + CellPropertyType.BORDER_TOP)); assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_BOTTOM)); + CellPropertyType.BORDER_BOTTOM)); assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_LEFT)); + CellPropertyType.BORDER_LEFT)); assertEquals(BorderStyle.MEDIUM, pt.getBorderStyle(i, j, - CellUtil.BORDER_RIGHT)); + CellPropertyType.BORDER_RIGHT)); } else { assertEquals(BorderStyle.MEDIUM, pt.getBorderStyle(i, j, - CellUtil.BORDER_TOP)); + CellPropertyType.BORDER_TOP)); assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_BOTTOM)); + CellPropertyType.BORDER_BOTTOM)); assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_LEFT)); + CellPropertyType.BORDER_LEFT)); assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_RIGHT)); + CellPropertyType.BORDER_RIGHT)); } } else if (i == 2) { if (j == 0) { assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_TOP)); + CellPropertyType.BORDER_TOP)); assertEquals(BorderStyle.MEDIUM, pt.getBorderStyle(i, j, - CellUtil.BORDER_BOTTOM)); + CellPropertyType.BORDER_BOTTOM)); assertEquals(BorderStyle.MEDIUM, pt.getBorderStyle(i, j, - CellUtil.BORDER_LEFT)); + CellPropertyType.BORDER_LEFT)); assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_RIGHT)); + CellPropertyType.BORDER_RIGHT)); } else if (j == 2) { assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_TOP)); + CellPropertyType.BORDER_TOP)); assertEquals(BorderStyle.MEDIUM, pt.getBorderStyle(i, j, - CellUtil.BORDER_BOTTOM)); + CellPropertyType.BORDER_BOTTOM)); assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_LEFT)); + CellPropertyType.BORDER_LEFT)); assertEquals(BorderStyle.MEDIUM, pt.getBorderStyle(i, j, - CellUtil.BORDER_RIGHT)); + CellPropertyType.BORDER_RIGHT)); } else { assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_TOP)); + CellPropertyType.BORDER_TOP)); assertEquals(BorderStyle.MEDIUM, pt.getBorderStyle(i, j, - CellUtil.BORDER_BOTTOM)); + CellPropertyType.BORDER_BOTTOM)); assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_LEFT)); + CellPropertyType.BORDER_LEFT)); assertEquals(BorderStyle.THIN, pt.getBorderStyle(i, j, - CellUtil.BORDER_RIGHT)); + CellPropertyType.BORDER_RIGHT)); + } + } else { + if (j == 0) { + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellPropertyType.BORDER_TOP)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellPropertyType.BORDER_BOTTOM)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellPropertyType.BORDER_LEFT)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellPropertyType.BORDER_RIGHT)); + } else if (j == 2) { + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellPropertyType.BORDER_TOP)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellPropertyType.BORDER_BOTTOM)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellPropertyType.BORDER_LEFT)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellPropertyType.BORDER_RIGHT)); + } else { + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellPropertyType.BORDER_TOP)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellPropertyType.BORDER_BOTTOM)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellPropertyType.BORDER_LEFT)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellPropertyType.BORDER_RIGHT)); + } + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.TOP); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (i == 0) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_TOP)); + } else { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.BOTTOM); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (i == 2) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellPropertyType.BORDER_BOTTOM)); + } else { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.LEFT); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (j == 0) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_LEFT)); + } else { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.RIGHT); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (j == 2) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellPropertyType.BORDER_RIGHT)); + } else { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.HORIZONTAL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + assertEquals(2, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_TOP)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_BOTTOM)); + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.INSIDE_HORIZONTAL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (i == 0) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellPropertyType.BORDER_BOTTOM)); + } else if (i == 2) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_TOP)); + } else { + assertEquals(2, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_TOP)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellPropertyType.BORDER_BOTTOM)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.OUTSIDE_HORIZONTAL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (i == 0) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_TOP)); + } else if (i == 2) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellPropertyType.BORDER_BOTTOM)); + } else { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.VERTICAL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + assertEquals(2, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_LEFT)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_RIGHT)); + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.INSIDE_VERTICAL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (j == 0) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellPropertyType.BORDER_RIGHT)); + } else if (j == 2) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_LEFT)); + } else { + assertEquals(2, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_LEFT)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellPropertyType.BORDER_RIGHT)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.OUTSIDE_VERTICAL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (j == 0) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_LEFT)); + } else if (j == 2) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellPropertyType.BORDER_RIGHT)); + } else { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + } + } + + @Test + void drawBorders() throws IOException { + CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2); + PropertyTemplate pt = new PropertyTemplate(); + pt.drawBorders(a1c3, BorderStyle.THIN, + BorderExtent.ALL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + assertEquals(4, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, CellUtil.BORDER_TOP)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, CellUtil.BORDER_BOTTOM)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, CellUtil.BORDER_LEFT)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, CellUtil.BORDER_RIGHT)); + } + } + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.OUTSIDE); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + assertEquals(4, pt.getNumBorders(i, j)); + if (i == 0) { + if (j == 0) { + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellUtil.BORDER_TOP)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_BOTTOM)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellUtil.BORDER_LEFT)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_RIGHT)); + } else if (j == 2) { + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellUtil.BORDER_TOP)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_BOTTOM)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_LEFT)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellUtil.BORDER_RIGHT)); + } else { + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellUtil.BORDER_TOP)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_BOTTOM)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_LEFT)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_RIGHT)); + } + } else if (i == 2) { + if (j == 0) { + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_TOP)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellUtil.BORDER_BOTTOM)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellUtil.BORDER_LEFT)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_RIGHT)); + } else if (j == 2) { + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_TOP)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellUtil.BORDER_BOTTOM)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_LEFT)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellUtil.BORDER_RIGHT)); + } else { + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_TOP)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellUtil.BORDER_BOTTOM)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_LEFT)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_RIGHT)); + } + } else { + if (j == 0) { + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_TOP)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_BOTTOM)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellUtil.BORDER_LEFT)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_RIGHT)); + } else if (j == 2) { + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_TOP)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_BOTTOM)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_LEFT)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, + CellUtil.BORDER_RIGHT)); + } else { + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_TOP)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_BOTTOM)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_LEFT)); + assertEquals(BorderStyle.THIN, + pt.getBorderStyle(i, j, + CellUtil.BORDER_RIGHT)); + } + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.TOP); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (i == 0) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellUtil.BORDER_TOP)); + } else { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.BOTTOM); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (i == 2) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellUtil.BORDER_BOTTOM)); + } else { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.LEFT); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (j == 0) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellUtil.BORDER_LEFT)); + } else { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.RIGHT); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (j == 2) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellUtil.BORDER_RIGHT)); + } else { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.HORIZONTAL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + assertEquals(2, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellUtil.BORDER_TOP)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellUtil.BORDER_BOTTOM)); + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.INSIDE_HORIZONTAL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (i == 0) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellUtil.BORDER_BOTTOM)); + } else if (i == 2) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellUtil.BORDER_TOP)); + } else { + assertEquals(2, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellUtil.BORDER_TOP)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellUtil.BORDER_BOTTOM)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.OUTSIDE_HORIZONTAL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (i == 0) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellUtil.BORDER_TOP)); + } else if (i == 2) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellUtil.BORDER_BOTTOM)); + } else { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.VERTICAL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + assertEquals(2, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellUtil.BORDER_LEFT)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellUtil.BORDER_RIGHT)); + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.INSIDE_VERTICAL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (j == 0) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellUtil.BORDER_RIGHT)); + } else if (j == 2) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellUtil.BORDER_LEFT)); + } else { + assertEquals(2, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellUtil.BORDER_LEFT)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellUtil.BORDER_RIGHT)); + } + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, + BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.MEDIUM, + BorderExtent.OUTSIDE_VERTICAL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + if (j == 0) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellUtil.BORDER_LEFT)); + } else if (j == 2) { + assertEquals(1, pt.getNumBorders(i, j)); + assertEquals(BorderStyle.MEDIUM, pt + .getBorderStyle(i, j, CellUtil.BORDER_RIGHT)); + } else { + assertEquals(0, pt.getNumBorders(i, j)); + } + } + } + } + + @Test + void drawBorderColorsByEnum() throws IOException { + CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2); + PropertyTemplate pt = new PropertyTemplate(); + pt.drawBorderColors(a1c3, IndexedColors.RED.getIndex(), + BorderExtent.ALL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + assertEquals(4, pt.getNumBorders(i, j)); + assertEquals(4, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.RED.getIndex(), pt + .getTemplateProperty(i, j, CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), pt + .getTemplateProperty(i, j, CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); + } + } + pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), + BorderExtent.OUTSIDE); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + assertEquals(4, pt.getNumBorders(i, j)); + assertEquals(4, pt.getNumBorderColors(i, j)); + if (i == 0) { + if (j == 0) { + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); + } else if (j == 2) { + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); + } else { + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); + } + } else if (i == 2) { + if (j == 0) { + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); + } else if (j == 2) { + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); + } else { + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); } } else { if (j == 0) { - assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, - CellUtil.BORDER_TOP)); - assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, - CellUtil.BORDER_BOTTOM)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, - CellUtil.BORDER_LEFT)); - assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, - CellUtil.BORDER_RIGHT)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); } else if (j == 2) { - assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, - CellUtil.BORDER_TOP)); - assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, - CellUtil.BORDER_BOTTOM)); - assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, - CellUtil.BORDER_LEFT)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, - CellUtil.BORDER_RIGHT)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); } else { - assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, - CellUtil.BORDER_TOP)); - assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, - CellUtil.BORDER_BOTTOM)); - assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, - CellUtil.BORDER_LEFT)); - assertEquals(BorderStyle.THIN, - pt.getBorderStyle(i, j, - CellUtil.BORDER_RIGHT)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); } } } } pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE); + pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), + BorderExtent.NONE); for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { assertEquals(0, pt.getNumBorders(i, j)); + assertEquals(0, pt.getNumBorderColors(i, j)); } } - pt.drawBorders(a1c3, BorderStyle.MEDIUM, + pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.TOP); for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { if (i == 0) { assertEquals(1, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, CellUtil.BORDER_TOP)); + assertEquals(1, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); } else { assertEquals(0, pt.getNumBorders(i, j)); + assertEquals(0, pt.getNumBorderColors(i, j)); } } } pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE); - pt.drawBorders(a1c3, BorderStyle.MEDIUM, + pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), + BorderExtent.NONE); + pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.BOTTOM); for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { if (i == 2) { assertEquals(1, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, pt - .getBorderStyle(i, j, CellUtil.BORDER_BOTTOM)); + assertEquals(1, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); } else { assertEquals(0, pt.getNumBorders(i, j)); + assertEquals(0, pt.getNumBorderColors(i, j)); } } } pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE); - pt.drawBorders(a1c3, BorderStyle.MEDIUM, + pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), + BorderExtent.NONE); + pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.LEFT); for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { if (j == 0) { assertEquals(1, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, CellUtil.BORDER_LEFT)); + assertEquals(1, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); } else { assertEquals(0, pt.getNumBorders(i, j)); + assertEquals(0, pt.getNumBorderColors(i, j)); } } } pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE); - pt.drawBorders(a1c3, BorderStyle.MEDIUM, + pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), + BorderExtent.NONE); + pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.RIGHT); for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { if (j == 2) { assertEquals(1, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, pt - .getBorderStyle(i, j, CellUtil.BORDER_RIGHT)); + assertEquals(1, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); } else { assertEquals(0, pt.getNumBorders(i, j)); + assertEquals(0, pt.getNumBorderColors(i, j)); } } } pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE); - pt.drawBorders(a1c3, BorderStyle.MEDIUM, + pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), + BorderExtent.NONE); + pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.HORIZONTAL); for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { assertEquals(2, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, CellUtil.BORDER_TOP)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, CellUtil.BORDER_BOTTOM)); + assertEquals(2, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), pt + .getTemplateProperty(i, j, CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); } } pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE); - pt.drawBorders(a1c3, BorderStyle.MEDIUM, + pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), + BorderExtent.NONE); + pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.INSIDE_HORIZONTAL); for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { if (i == 0) { assertEquals(1, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, pt - .getBorderStyle(i, j, CellUtil.BORDER_BOTTOM)); + assertEquals(1, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); } else if (i == 2) { assertEquals(1, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, CellUtil.BORDER_TOP)); + assertEquals(1, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); } else { assertEquals(2, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, CellUtil.BORDER_TOP)); - assertEquals(BorderStyle.MEDIUM, pt - .getBorderStyle(i, j, CellUtil.BORDER_BOTTOM)); + assertEquals(2, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); } } } pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE); - pt.drawBorders(a1c3, BorderStyle.MEDIUM, + pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), + BorderExtent.NONE); + pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.OUTSIDE_HORIZONTAL); for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { if (i == 0) { assertEquals(1, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, CellUtil.BORDER_TOP)); + assertEquals(1, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.TOP_BORDER_COLOR)); } else if (i == 2) { assertEquals(1, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, pt - .getBorderStyle(i, j, CellUtil.BORDER_BOTTOM)); + assertEquals(1, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); } else { assertEquals(0, pt.getNumBorders(i, j)); + assertEquals(0, pt.getNumBorderColors(i, j)); } } } pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE); - pt.drawBorders(a1c3, BorderStyle.MEDIUM, + pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), + BorderExtent.NONE); + pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.VERTICAL); for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { assertEquals(2, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, CellUtil.BORDER_LEFT)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, CellUtil.BORDER_RIGHT)); + assertEquals(2, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), pt + .getTemplateProperty(i, j, CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); } } pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE); - pt.drawBorders(a1c3, BorderStyle.MEDIUM, + pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), + BorderExtent.NONE); + pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.INSIDE_VERTICAL); for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { if (j == 0) { assertEquals(1, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, pt - .getBorderStyle(i, j, CellUtil.BORDER_RIGHT)); + assertEquals(1, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); } else if (j == 2) { assertEquals(1, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, CellUtil.BORDER_LEFT)); + assertEquals(1, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); } else { assertEquals(2, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, CellUtil.BORDER_LEFT)); - assertEquals(BorderStyle.MEDIUM, pt - .getBorderStyle(i, j, CellUtil.BORDER_RIGHT)); + assertEquals(2, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); } } } pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE); - pt.drawBorders(a1c3, BorderStyle.MEDIUM, + pt.drawBorderColors(a1c3, IndexedColors.AUTOMATIC.getIndex(), + BorderExtent.NONE); + pt.drawBorderColors(a1c3, IndexedColors.BLUE.getIndex(), BorderExtent.OUTSIDE_VERTICAL); for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 2; j++) { if (j == 0) { assertEquals(1, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, - pt.getBorderStyle(i, j, CellUtil.BORDER_LEFT)); + assertEquals(1, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.LEFT_BORDER_COLOR)); } else if (j == 2) { assertEquals(1, pt.getNumBorders(i, j)); - assertEquals(BorderStyle.MEDIUM, pt - .getBorderStyle(i, j, CellUtil.BORDER_RIGHT)); + assertEquals(1, pt.getNumBorderColors(i, j)); + assertEquals(IndexedColors.BLUE.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); } else { assertEquals(0, pt.getNumBorders(i, j)); + assertEquals(0, pt.getNumBorderColors(i, j)); } } } @@ -848,6 +1586,54 @@ final class TestPropertyTemplate { } } + @Test + void drawBordersWithColorsByEnum() throws IOException { + CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2); + PropertyTemplate pt = new PropertyTemplate(); + + pt.drawBorders(a1c3, BorderStyle.MEDIUM, IndexedColors.RED.getIndex(), BorderExtent.ALL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + assertEquals(4, pt.getNumBorders(i, j)); + assertEquals(4, pt.getNumBorderColors(i, j)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_TOP)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_BOTTOM)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_LEFT)); + assertEquals(BorderStyle.MEDIUM, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_RIGHT)); + assertEquals(IndexedColors.RED.getIndex(), pt + .getTemplateProperty(i, j, CellPropertyType.TOP_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.BOTTOM_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), pt + .getTemplateProperty(i, j, CellPropertyType.LEFT_BORDER_COLOR)); + assertEquals(IndexedColors.RED.getIndex(), + pt.getTemplateProperty(i, j, + CellPropertyType.RIGHT_BORDER_COLOR)); + } + } + pt.drawBorders(a1c3, BorderStyle.NONE, BorderExtent.NONE); + pt.drawBorders(a1c3, BorderStyle.NONE, IndexedColors.RED.getIndex(), BorderExtent.ALL); + for (int i = 0; i <= 2; i++) { + for (int j = 0; j <= 2; j++) { + assertEquals(4, pt.getNumBorders(i, j)); + assertEquals(0, pt.getNumBorderColors(i, j)); + assertEquals(BorderStyle.NONE, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_TOP)); + assertEquals(BorderStyle.NONE, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_BOTTOM)); + assertEquals(BorderStyle.NONE, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_LEFT)); + assertEquals(BorderStyle.NONE, + pt.getBorderStyle(i, j, CellPropertyType.BORDER_RIGHT)); + } + } + } + @Test void applyBorders() throws IOException { CellRangeAddress a1c3 = new CellRangeAddress(0, 2, 0, 2); @@ -859,8 +1645,8 @@ final class TestPropertyTemplate { pt.drawBorders(a1c3, BorderStyle.THIN, IndexedColors.RED.getIndex(), BorderExtent.ALL); pt.applyBorders(sheet); - for (Row row: sheet) { - for (Cell cell: row) { + for (Row row : sheet) { + for (Cell cell : row) { CellStyle cs = cell.getCellStyle(); assertEquals(BorderStyle.THIN, cs.getBorderTop()); assertEquals(IndexedColors.RED.getIndex(), cs.getTopBorderColor()); @@ -876,30 +1662,30 @@ final class TestPropertyTemplate { pt.drawBorders(b2, BorderStyle.NONE, BorderExtent.ALL); pt.applyBorders(sheet); - for (Row row: sheet) { - for (Cell cell: row) { + for (Row row : sheet) { + for (Cell cell : row) { CellStyle cs = cell.getCellStyle(); if (cell.getColumnIndex() != 1 || row.getRowNum() == 0) { - assertEquals(BorderStyle.THIN, cs.getBorderTop()); - assertEquals(IndexedColors.RED.getIndex(), cs.getTopBorderColor()); + assertEquals(BorderStyle.THIN, cs.getBorderTop()); + assertEquals(IndexedColors.RED.getIndex(), cs.getTopBorderColor()); } else { assertEquals(BorderStyle.NONE, cs.getBorderTop()); } if (cell.getColumnIndex() != 1 || row.getRowNum() == 2) { - assertEquals(BorderStyle.THIN, cs.getBorderBottom()); - assertEquals(IndexedColors.RED.getIndex(), cs.getBottomBorderColor()); + assertEquals(BorderStyle.THIN, cs.getBorderBottom()); + assertEquals(IndexedColors.RED.getIndex(), cs.getBottomBorderColor()); } else { assertEquals(BorderStyle.NONE, cs.getBorderBottom()); } if (cell.getColumnIndex() == 0 || row.getRowNum() != 1) { - assertEquals(BorderStyle.THIN, cs.getBorderLeft()); - assertEquals(IndexedColors.RED.getIndex(), cs.getLeftBorderColor()); + assertEquals(BorderStyle.THIN, cs.getBorderLeft()); + assertEquals(IndexedColors.RED.getIndex(), cs.getLeftBorderColor()); } else { assertEquals(BorderStyle.NONE, cs.getBorderLeft()); } if (cell.getColumnIndex() == 2 || row.getRowNum() != 1) { - assertEquals(BorderStyle.THIN, cs.getBorderRight()); - assertEquals(IndexedColors.RED.getIndex(), cs.getRightBorderColor()); + assertEquals(BorderStyle.THIN, cs.getBorderRight()); + assertEquals(IndexedColors.RED.getIndex(), cs.getRightBorderColor()); } else { assertEquals(BorderStyle.NONE, cs.getBorderRight()); } @@ -923,7 +1709,7 @@ final class TestPropertyTemplate { } } - CellRangeAddress b2 = new CellRangeAddress(1,1,1,1); + CellRangeAddress b2 = new CellRangeAddress(1, 1, 1, 1); pt2.drawBorders(b2, BorderStyle.THIN, BorderExtent.ALL); Workbook wb = new HSSFWorkbook();