Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/util/PropertyTemplate.java URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/util/PropertyTemplate.java?rev=1920230&r1=1920229&r2=1920230&view=diff ============================================================================== --- poi/trunk/poi/src/main/java/org/apache/poi/ss/util/PropertyTemplate.java (original) +++ poi/trunk/poi/src/main/java/org/apache/poi/ss/util/PropertyTemplate.java Tue Aug 27 18:36:59 2024 @@ -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; + /** * <p> * 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<CellAddress, Map<String, Object>> _propertyTemplate; + private final Map<CellAddress, Map<CellPropertyType, Object>> _propertyTemplate; /** * Create a PropertyTemplate object @@ -73,16 +75,16 @@ public final class PropertyTemplate { */ public PropertyTemplate(PropertyTemplate template) { this(); - for(Map.Entry<CellAddress, Map<String, Object>> entry : template.getTemplate().entrySet()) { + for (Map.Entry<CellAddress, Map<CellPropertyType, Object>> entry : template.getTemplate().entrySet()) { _propertyTemplate.put(new CellAddress(entry.getKey()), cloneCellProperties(entry.getValue())); } } - private Map<CellAddress,Map<String, Object>> getTemplate() { + private Map<CellAddress, Map<CellPropertyType, Object>> getTemplate() { return _propertyTemplate; } - private static Map<String, Object> cloneCellProperties(Map<String, Object> properties) { + private static Map<CellPropertyType, Object> cloneCellProperties(Map<CellPropertyType, Object> 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 { * </ul> */ 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 { * </ul> */ 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 { * </ul> */ 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<String> properties = new HashSet<>(); - properties.add(CellUtil.BORDER_TOP); - properties.add(CellUtil.BORDER_BOTTOM); - properties.add(CellUtil.BORDER_LEFT); - properties.add(CellUtil.BORDER_RIGHT); + Set<CellPropertyType> 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<CellAddress, Map<String, Object>> entry : _propertyTemplate + for (Map.Entry<CellAddress, Map<CellPropertyType, Object>> entry : _propertyTemplate .entrySet()) { CellAddress cellAddress = entry.getKey(); if (cellAddress.getRow() < wb.getSpreadsheetVersion().getMaxRows() && cellAddress.getColumn() < wb.getSpreadsheetVersion() - .getMaxColumns()) { - Map<String, Object> properties = entry.getValue(); + .getMaxColumns()) { + Map<CellPropertyType, Object> 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 { * </ul> */ 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 { * </ul> */ 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 { * </ul> */ 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<String> 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<CellPropertyType> 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<String, Object> cellProperties = _propertyTemplate.get(cell); + Map<CellPropertyType, Object> 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<String> properties) { + private void removeProperties(int row, int col, Set<CellPropertyType> properties) { CellAddress cell = new CellAddress(row, col); - Map<String, Object> cellProperties = _propertyTemplate.get(cell); + Map<CellPropertyType, Object> 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<String, Object> cellProperties = _propertyTemplate.get(cell); + Map<CellPropertyType, Object> 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<String, Object> cellProperties = _propertyTemplate.get(cell); + Map<CellPropertyType, Object> cellProperties = _propertyTemplate.get(cell); if (cellProperties == null) { return 0; } int count = 0; - for (String property : cellProperties.keySet()) { - if (property.equals(CellUtil.TOP_BORDER_COLOR)) + 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<String, Object> cellProperties = _propertyTemplate.get(cell); + Map<CellPropertyType, Object> cellProperties = _propertyTemplate.get(cell); if (cellProperties != null) { Object obj = cellProperties.get(property); if (obj instanceof BorderStyle) { @@ -881,17 +883,37 @@ public final class PropertyTemplate { /** * 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<String, Object> cellProperties = _propertyTemplate.get(cell); + Map<CellPropertyType, Object> cellProperties = _propertyTemplate.get(cell); if (cellProperties != null) { Object obj = cellProperties.get(property); if (obj != null) { @@ -903,12 +925,32 @@ public final class PropertyTemplate { /** * 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 *
Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/util/RegionUtil.java URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/util/RegionUtil.java?rev=1920230&r1=1920229&r2=1920230&view=diff ============================================================================== --- poi/trunk/poi/src/main/java/org/apache/poi/ss/util/RegionUtil.java (original) +++ poi/trunk/poi/src/main/java/org/apache/poi/ss/util/RegionUtil.java Tue Aug 27 18:36:59 2024 @@ -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); Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java?rev=1920230&r1=1920229&r2=1920230&view=diff ============================================================================== --- poi/trunk/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java (original) +++ poi/trunk/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java Tue Aug 27 18:36:59 2024 @@ -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 @@ -56,6 +46,27 @@ public abstract class BaseTestCellUtil { } @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()) { Sheet s = wb.createSheet(); @@ -77,6 +88,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()) { Sheet s = wb.createSheet(); @@ -106,6 +130,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()) { Sheet s = wb.createSheet(); @@ -122,6 +179,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()) { Sheet s = wb.createSheet(); @@ -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()); @@ -254,6 +331,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<CellPropertyType, Object> 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()) { Sheet sh = wb.createSheet(); @@ -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<CellPropertyType, Object> 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 --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org For additional commands, e-mail: commits-h...@poi.apache.org