diff --git a/src/java/org/apache/poi/ss/util/RegionUtil.java b/src/java/org/apache/poi/ss/util/RegionUtil.java index e18712986..96b4707f1 100644 --- a/src/java/org/apache/poi/ss/util/RegionUtil.java +++ b/src/java/org/apache/poi/ss/util/RegionUtil.java @@ -17,10 +17,12 @@ 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.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.util.Removal; /** * Various utility functions that make working with a region of cells easier. @@ -47,32 +49,37 @@ public final class RegionUtil { 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); } } /** - * Sets the left border for a region of cells by manipulating the cell style of the individual + * 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 workbook The workbook that the region is on. * @param sheet The sheet that the region is on. - * @deprecated 3.15 beta 2. Use {@link #setBorderLeft(int, CellRangeAddress, Sheet)}. + * @deprecated 3.15 beta 2. Use {@link #setBorderLeft(BorderStyle, CellRangeAddress, Sheet)}. */ + @Removal(version="3.17") public static void setBorderLeft(int border, CellRangeAddress region, Sheet sheet, Workbook workbook) { setBorderLeft(border, region, sheet); } /** - * Sets the left border for a region of cells by manipulating the cell style of the individual + * 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. + * @since POI 3.15 beta 2 + * @deprecated 3.16 beta 1. Use {@link #setBorderLeft(BorderStyle, CellRangeAddress, Sheet)}. */ + @Removal(version="3.18") public static void setBorderLeft(int border, CellRangeAddress region, Sheet sheet) { int rowStart = region.getFirstRow(); int rowEnd = region.getLastRow(); @@ -83,6 +90,18 @@ public final class RegionUtil { cps.setProperty(CellUtil.getRow(i, sheet), column); } } + /** + * 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. + * @since POI 3.16 beta 1 + */ + public static void setBorderLeft(BorderStyle border, CellRangeAddress region, Sheet sheet) { + setBorderLeft(border.getCode(), region, sheet); + } /** * Sets the left border color for a region of cells by manipulating the cell style of the individual @@ -94,6 +113,7 @@ public final class RegionUtil { * @param sheet The sheet that the region is on. * @deprecated 3.15 beta 2. Use {@link #setLeftBorderColor(int, CellRangeAddress, Sheet)}. */ + @Removal(version="3.17") public static void setLeftBorderColor(int color, CellRangeAddress region, Sheet sheet, Workbook workbook) { setLeftBorderColor(color, region, sheet); } @@ -104,6 +124,7 @@ public final class RegionUtil { * @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. + * @since POI 3.15 beta 2 */ public static void setLeftBorderColor(int color, CellRangeAddress region, Sheet sheet) { int rowStart = region.getFirstRow(); @@ -117,26 +138,30 @@ public final class RegionUtil { } /** - * Sets the right border for a region of cells by manipulating the cell style of the individual + * 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 workbook The workbook that the region is on. * @param sheet The sheet that the region is on. - * @deprecated 3.15 beta 2. Use {@link #setBorderRight(int, CellRangeAddress, Sheet)}. + * @deprecated 3.15 beta 2. Use {@link #setBorderRight(BorderStyle, CellRangeAddress, Sheet)}. */ + @Removal(version="3.17") public static void setBorderRight(int border, CellRangeAddress region, Sheet sheet, Workbook workbook) { setBorderRight(border, region, sheet); } /** - * Sets the right border for a region of cells by manipulating the cell style of the individual + * 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. + * @since POI 3.15 beta 2 + * @deprecated POI 3.16 beta 1. Use {@link #setBorderRight(BorderStyle, CellRangeAddress, Sheet)}. */ + @Removal(version="3.18") public static void setBorderRight(int border, CellRangeAddress region, Sheet sheet) { int rowStart = region.getFirstRow(); int rowEnd = region.getLastRow(); @@ -147,6 +172,18 @@ public final class RegionUtil { cps.setProperty(CellUtil.getRow(i, sheet), column); } } + /** + * 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. + * @since POI 3.16 beta 1 + */ + public static void setBorderRight(BorderStyle border, CellRangeAddress region, Sheet sheet) { + setBorderRight(border.getCode(), region, sheet); + } /** * Sets the right border color for a region of cells by manipulating the cell style of the individual @@ -158,6 +195,7 @@ public final class RegionUtil { * @param sheet The sheet that the region is on. * @deprecated 3.15 beta 2. Use {@link #setRightBorderColor(int, CellRangeAddress, Sheet)}. */ + @Removal(version="3.17") public static void setRightBorderColor(int color, CellRangeAddress region, Sheet sheet, Workbook workbook) { setRightBorderColor(color, region, sheet); } @@ -168,6 +206,7 @@ public final class RegionUtil { * @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. + * @since POI 3.15 beta 2 */ public static void setRightBorderColor(int color, CellRangeAddress region, Sheet sheet) { int rowStart = region.getFirstRow(); @@ -181,26 +220,30 @@ public final class RegionUtil { } /** - * Sets the bottom border for a region of cells by manipulating the cell style of the individual + * 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 workbook The workbook that the region is on. * @param sheet The sheet that the region is on. - * @deprecated 3.15 beta 2. Use {@link #setBorderBottom(int, CellRangeAddress, Sheet)}. + * @deprecated 3.15 beta 2. Use {@link #setBorderBottom(BorderStyle, CellRangeAddress, Sheet)}. */ + @Removal(version="3.17") public static void setBorderBottom(int border, CellRangeAddress region, Sheet sheet, Workbook workbook) { setBorderBottom(border, region, sheet); } /** - * Sets the bottom border for a region of cells by manipulating the cell style of the individual + * 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. + * @since POI 3.15 beta 2 + * @deprecated POI 3.16 beta 1. Use {@link #setBorderBottom(BorderStyle, CellRangeAddress, Sheet)}. */ + @Removal(version="3.18") public static void setBorderBottom(int border, CellRangeAddress region, Sheet sheet) { int colStart = region.getFirstColumn(); int colEnd = region.getLastColumn(); @@ -211,6 +254,18 @@ public final class RegionUtil { cps.setProperty(row, i); } } + /** + * 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. + * @since POI 3.16 beta 1 + */ + public static void setBorderBottom(BorderStyle border, CellRangeAddress region, Sheet sheet) { + setBorderBottom(border.getCode(), region, sheet); + } /** * Sets the bottom border color for a region of cells by manipulating the cell style of the individual @@ -222,6 +277,7 @@ public final class RegionUtil { * @param sheet The sheet that the region is on. * @deprecated 3.15 beta 2. Use {@link #setBottomBorderColor(int, CellRangeAddress, Sheet)}. */ + @Removal(version="3.17") public static void setBottomBorderColor(int color, CellRangeAddress region, Sheet sheet, Workbook workbook) { setBottomBorderColor(color, region, sheet); } @@ -232,6 +288,7 @@ public final class RegionUtil { * @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. + * @since POI 3.15 beta 2 */ public static void setBottomBorderColor(int color, CellRangeAddress region, Sheet sheet) { int colStart = region.getFirstColumn(); @@ -245,26 +302,30 @@ public final class RegionUtil { } /** - * Sets the top border for a region of cells by manipulating the cell style of the individual + * 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 workbook The workbook that the region is on. * @param sheet The sheet that the region is on. - * @deprecated 3.15 beta 2. Use {@link #setBorderTop(int, CellRangeAddress, Sheet)}. + * @deprecated 3.15 beta 2. Use {@link #setBorderTop(BorderStyle, CellRangeAddress, Sheet)}. */ + @Removal(version="3.17") public static void setBorderTop(int border, CellRangeAddress region, Sheet sheet, Workbook workbook) { setBorderTop(border, region, sheet); } /** - * Sets the top border for a region of cells by manipulating the cell style of the individual + * 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. + * @since POI 3.15 beta 2 + * @deprecated 3.16 beta 1. Use {@link #setBorderTop(BorderStyle, CellRangeAddress, Sheet)}. */ + @Removal(version="3.18") public static void setBorderTop(int border, CellRangeAddress region, Sheet sheet) { int colStart = region.getFirstColumn(); int colEnd = region.getLastColumn(); @@ -275,6 +336,18 @@ public final class RegionUtil { cps.setProperty(row, i); } } + /** + * 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. + * @since POI 3.16 beta 1 + */ + public static void setBorderTop(BorderStyle border, CellRangeAddress region, Sheet sheet) { + setBorderTop(border.getCode(), region, sheet); + } /** * Sets the top border color for a region of cells by manipulating the cell style of the individual @@ -286,6 +359,7 @@ public final class RegionUtil { * @param sheet The sheet that the region is on. * @deprecated 3.15 beta 2. Use {@link #setTopBorderColor(int, CellRangeAddress, Sheet)}. */ + @Removal(version="3.17") public static void setTopBorderColor(int color, CellRangeAddress region, Sheet sheet, Workbook workbook) { setTopBorderColor(color, region, sheet); } @@ -296,6 +370,7 @@ public final class RegionUtil { * @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. + * @since POI 3.15 beta 2 */ public static void setTopBorderColor(int color, CellRangeAddress region, Sheet sheet) { int colStart = region.getFirstColumn(); diff --git a/src/testcases/org/apache/poi/ss/util/TestRegionUtil.java b/src/testcases/org/apache/poi/ss/util/TestRegionUtil.java index 567907326..13fad63bf 100644 --- a/src/testcases/org/apache/poi/ss/util/TestRegionUtil.java +++ b/src/testcases/org/apache/poi/ss/util/TestRegionUtil.java @@ -17,8 +17,14 @@ package org.apache.poi.ss.util; +import static org.junit.Assert.*; + import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.BorderStyle; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.junit.After; @@ -32,7 +38,10 @@ import java.io.IOException; */ public final class TestRegionUtil { private static final CellRangeAddress A1C3 = new CellRangeAddress(0, 2, 0, 2); - private static short THIN = BorderStyle.THIN.getCode(); + private static final BorderStyle NONE = BorderStyle.NONE; + private static final BorderStyle THIN = BorderStyle.THIN; + private static final int RED = IndexedColors.RED.getIndex(); + private static final int DEFAULT_COLOR = 0; private Workbook wb; private Sheet sheet; @@ -47,42 +56,108 @@ public final class TestRegionUtil { wb.close(); } - // TODO: fill this in with meaningful unit tests - // Right now this just makes sure that RegionUtil is compiled into poi schemas - // and that the code doesn't run in an infinite loop. - // Don't spend too much time getting this unit test to work as this class - // will likely be replaced by CellStyleTemplate soon. + private CellStyle getCellStyle(int rowIndex, int columnIndex) { + Row row = sheet.getRow(rowIndex); + if (row == null) row = sheet.createRow(rowIndex); + Cell cell = row.getCell(columnIndex); + if (cell == null) cell = row.createCell(columnIndex); + return cell.getCellStyle(); + } + @Test public void setBorderTop() { + assertEquals(NONE, getCellStyle(0, 0).getBorderTopEnum()); + assertEquals(NONE, getCellStyle(0, 1).getBorderTopEnum()); + assertEquals(NONE, getCellStyle(0, 2).getBorderTopEnum()); RegionUtil.setBorderTop(THIN, A1C3, sheet); + assertEquals(THIN, getCellStyle(0, 0).getBorderTopEnum()); + assertEquals(THIN, getCellStyle(0, 1).getBorderTopEnum()); + assertEquals(THIN, getCellStyle(0, 2).getBorderTopEnum()); } @Test public void setBorderBottom() { + assertEquals(NONE, getCellStyle(2, 0).getBorderBottomEnum()); + assertEquals(NONE, getCellStyle(2, 1).getBorderBottomEnum()); + assertEquals(NONE, getCellStyle(2, 2).getBorderBottomEnum()); RegionUtil.setBorderBottom(THIN, A1C3, sheet); + assertEquals(THIN, getCellStyle(2, 0).getBorderBottomEnum()); + assertEquals(THIN, getCellStyle(2, 1).getBorderBottomEnum()); + assertEquals(THIN, getCellStyle(2, 2).getBorderBottomEnum()); } @Test public void setBorderRight() { + assertEquals(NONE, getCellStyle(0, 2).getBorderRightEnum()); + assertEquals(NONE, getCellStyle(1, 2).getBorderRightEnum()); + assertEquals(NONE, getCellStyle(2, 2).getBorderRightEnum()); RegionUtil.setBorderRight(THIN, A1C3, sheet); + assertEquals(THIN, getCellStyle(0, 2).getBorderRightEnum()); + assertEquals(THIN, getCellStyle(1, 2).getBorderRightEnum()); + assertEquals(THIN, getCellStyle(2, 2).getBorderRightEnum()); } @Test public void setBorderLeft() { + assertEquals(NONE, getCellStyle(0, 0).getBorderLeftEnum()); + assertEquals(NONE, getCellStyle(1, 0).getBorderLeftEnum()); + assertEquals(NONE, getCellStyle(2, 0).getBorderLeftEnum()); RegionUtil.setBorderLeft(THIN, A1C3, sheet); + assertEquals(THIN, getCellStyle(0, 0).getBorderLeftEnum()); + assertEquals(THIN, getCellStyle(1, 0).getBorderLeftEnum()); + assertEquals(THIN, getCellStyle(2, 0).getBorderLeftEnum()); } @Test public void setTopBorderColor() { - RegionUtil.setTopBorderColor(THIN, A1C3, sheet); + assertEquals(DEFAULT_COLOR, getCellStyle(0, 0).getTopBorderColor()); + assertEquals(DEFAULT_COLOR, getCellStyle(0, 1).getTopBorderColor()); + assertEquals(DEFAULT_COLOR, getCellStyle(0, 2).getTopBorderColor()); + RegionUtil.setTopBorderColor(RED, A1C3, sheet); + assertEquals(RED, getCellStyle(0, 0).getTopBorderColor()); + assertEquals(RED, getCellStyle(0, 1).getTopBorderColor()); + assertEquals(RED, getCellStyle(0, 2).getTopBorderColor()); } @Test public void setBottomBorderColor() { - RegionUtil.setBottomBorderColor(THIN, A1C3, sheet); + assertEquals(DEFAULT_COLOR, getCellStyle(2, 0).getBottomBorderColor()); + assertEquals(DEFAULT_COLOR, getCellStyle(2, 1).getBottomBorderColor()); + assertEquals(DEFAULT_COLOR, getCellStyle(2, 2).getBottomBorderColor()); + RegionUtil.setBottomBorderColor(RED, A1C3, sheet); + assertEquals(RED, getCellStyle(2, 0).getBottomBorderColor()); + assertEquals(RED, getCellStyle(2, 1).getBottomBorderColor()); + assertEquals(RED, getCellStyle(2, 2).getBottomBorderColor()); } @Test public void setRightBorderColor() { - RegionUtil.setRightBorderColor(THIN, A1C3, sheet); + assertEquals(DEFAULT_COLOR, getCellStyle(0, 2).getRightBorderColor()); + assertEquals(DEFAULT_COLOR, getCellStyle(1, 2).getRightBorderColor()); + assertEquals(DEFAULT_COLOR, getCellStyle(2, 2).getRightBorderColor()); + RegionUtil.setRightBorderColor(RED, A1C3, sheet); + assertEquals(RED, getCellStyle(0, 2).getRightBorderColor()); + assertEquals(RED, getCellStyle(1, 2).getRightBorderColor()); + assertEquals(RED, getCellStyle(2, 2).getRightBorderColor()); } @Test public void setLeftBorderColor() { - RegionUtil.setLeftBorderColor(THIN, A1C3, sheet); + assertEquals(DEFAULT_COLOR, getCellStyle(0, 0).getLeftBorderColor()); + assertEquals(DEFAULT_COLOR, getCellStyle(1, 0).getLeftBorderColor()); + assertEquals(DEFAULT_COLOR, getCellStyle(2, 0).getLeftBorderColor()); + RegionUtil.setLeftBorderColor(RED, A1C3, sheet); + assertEquals(RED, getCellStyle(0, 0).getLeftBorderColor()); + assertEquals(RED, getCellStyle(1, 0).getLeftBorderColor()); + assertEquals(RED, getCellStyle(2, 0).getLeftBorderColor()); + } + + @Test + public void bordersCanBeAddedToNonExistantCells() { + RegionUtil.setBorderTop(THIN, A1C3, sheet); + assertEquals(THIN, getCellStyle(0, 0).getBorderTopEnum()); + assertEquals(THIN, getCellStyle(0, 1).getBorderTopEnum()); + assertEquals(THIN, getCellStyle(0, 2).getBorderTopEnum()); + } + @Test + public void borderColorsCanBeAddedToNonExistantCells() { + RegionUtil.setTopBorderColor(RED, A1C3, sheet); + assertEquals(RED, getCellStyle(0, 0).getTopBorderColor()); + assertEquals(RED, getCellStyle(0, 1).getTopBorderColor()); + assertEquals(RED, getCellStyle(0, 2).getTopBorderColor()); } }