From ed7dc7e8fafa4095a0efd8e38c80e10d67790065 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Fri, 8 Jul 2016 22:28:28 +0000 Subject: [PATCH] bug 59833: add getFillPatternEnum and setFillPattern(FillPatternType) to CellStyle; consolidate duplicated FillPattern constants in CellStyle to FillPatternType enum git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751972 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/usermodel/HSSFCellStyle.java | 31 +++- .../apache/poi/ss/usermodel/CellStyle.java | 166 ++++++++++++++---- .../poi/ss/usermodel/FillPatternType.java | 58 ++++-- .../poi/xssf/usermodel/XSSFCellStyle.java | 55 +++--- .../poi/xssf/usermodel/TestXSSFCellStyle.java | 24 ++- 5 files changed, 241 insertions(+), 93 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index ad8cadedb2..4000802ecb 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -28,6 +28,7 @@ import org.apache.poi.hssf.record.StyleRecord; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.Font; /** @@ -685,21 +686,45 @@ public final class HSSFCellStyle implements CellStyle { * @see #DIAMONDS * * @param fp fill pattern (set to 1 to fill w/foreground color) + * @deprecated POI 3.15 beta 3. Use {@link #setFillPattern(FillPatternType)} instead. */ @Override public void setFillPattern(short fp) { - _format.setAdtlFillPattern(fp); + setFillPattern(FillPatternType.forInt(fp)); + } + + /** + * setting to one fills the cell with the foreground color... No idea about + * other values + * + * @param fp fill pattern (set to {@link FillPatternType#SOLID_FOREGROUND} to fill w/foreground color) + */ + @Override + public void setFillPattern(FillPatternType fp) + { + _format.setAdtlFillPattern(fp.getCode()); } /** - * get the fill pattern (??) - set to 1 to fill with foreground color + * get the fill pattern * @return fill pattern + * @deprecated POI 3.15 beta 3. This method will return {@link FillPatternType} in the future. Use {@link #setFillPattern(FillPatternType)} instead. */ @Override public short getFillPattern() { - return _format.getAdtlFillPattern(); + return getFillPatternEnum().getCode(); + } + + /** + * get the fill pattern + * @return fill pattern + */ + @Override + public FillPatternType getFillPatternEnum() + { + return FillPatternType.forInt(_format.getAdtlFillPattern()); } /** diff --git a/src/java/org/apache/poi/ss/usermodel/CellStyle.java b/src/java/org/apache/poi/ss/usermodel/CellStyle.java index e0b3dad3e2..e877e69e15 100644 --- a/src/java/org/apache/poi/ss/usermodel/CellStyle.java +++ b/src/java/org/apache/poi/ss/usermodel/CellStyle.java @@ -169,62 +169,119 @@ public interface CellStyle { */ short BORDER_SLANTED_DASH_DOT = 0xD; - /** No background */ - short NO_FILL = 0; + /** + * Fill Pattern: No background + * @deprecated 3.15 beta 3. Use {@link FillPatternType#NO_FILL} instead. + */ + short NO_FILL = FillPatternType.NO_FILL.getCode(); - /** Solidly filled */ - short SOLID_FOREGROUND = 1; + /** + * Fill Pattern: Solidly filled + * @deprecated 3.15 beta 3. Use {@link FillPatternType#SOLID_FOREGROUND} instead. + */ + short SOLID_FOREGROUND = FillPatternType.SOLID_FOREGROUND.getCode(); - /** Small fine dots */ - short FINE_DOTS = 2; + /** + * Fill Pattern: Small fine dots + * @deprecated 3.15 beta 3. Use {@link FillPatternType#FINE_DOTS} instead. + */ + short FINE_DOTS = FillPatternType.FINE_DOTS.getCode(); - /** Wide dots */ - short ALT_BARS = 3; + /** + * Fill Pattern: Wide dots + * @deprecated 3.15 beta 3. Use {@link FillPatternType#ALT_BARS} instead. + */ + short ALT_BARS = FillPatternType.ALT_BARS.getCode(); - /** Sparse dots */ - short SPARSE_DOTS = 4; + /** + * Fill Pattern: Sparse dots + * @deprecated 3.15 beta 3. Use {@link FillPatternType#SPARSE_DOTS} instead. + */ + short SPARSE_DOTS = FillPatternType.SPARSE_DOTS.getCode(); - /** Thick horizontal bands */ - short THICK_HORZ_BANDS = 5; + /** + * Fill Pattern: Thick horizontal bands + * @deprecated 3.15 beta 3. Use {@link FillPatternType#THICK_HORZ_BANDS} instead. + */ + short THICK_HORZ_BANDS = FillPatternType.THICK_HORZ_BANDS.getCode(); - /** Thick vertical bands */ - short THICK_VERT_BANDS = 6; + /** + * Fill Pattern: Thick vertical bands + * @deprecated 3.15 beta 3. Use {@link FillPatternType#THICK_VERT_BANDS} instead. + */ + short THICK_VERT_BANDS = FillPatternType.THICK_VERT_BANDS.getCode(); - /** Thick backward facing diagonals */ + /** + * Fill Pattern: Thick backward facing diagonals + * @deprecated 3.15 beta 3. Use {@link FillPatternType#NO_FILL} instead. + */ short THICK_BACKWARD_DIAG = 7; - /** Thick forward facing diagonals */ - short THICK_FORWARD_DIAG = 8; + /** + * Fill Pattern: Thick forward facing diagonals + * @deprecated 3.15 beta 3. Use {@link FillPatternType#NO_FILL} instead. + */ + short THICK_FORWARD_DIAG = FillPatternType.THICK_FORWARD_DIAG.getCode(); - /** Large spots */ - short BIG_SPOTS = 9; + /** + * Fill Pattern: Large spots + * @deprecated 3.15 beta 3. Use {@link FillPatternType#BIG_SPOTS} instead. + */ + short BIG_SPOTS = FillPatternType.BIG_SPOTS.getCode(); - /** Brick-like layout */ - short BRICKS = 10; + /** + * Fill Pattern: Brick-like layout + * @deprecated 3.15 beta 3. Use {@link FillPatternType#BRICKS} instead. + */ + short BRICKS = FillPatternType.BRICKS.getCode(); - /** Thin horizontal bands */ - short THIN_HORZ_BANDS = 11; + /** + * Fill Pattern: Thin horizontal bands + * @deprecated 3.15 beta 3. Use {@link FillPatternType#THIN_HORZ_BANDS} instead. + */ + short THIN_HORZ_BANDS = FillPatternType.THIN_HORZ_BANDS.getCode(); - /** Thin vertical bands */ - short THIN_VERT_BANDS = 12; + /** + * Fill Pattern: Thin vertical bands + * @deprecated 3.15 beta 3. Use {@link FillPatternType#THIN_VERT_BANDS} instead. + */ + short THIN_VERT_BANDS = FillPatternType.THIN_VERT_BANDS.getCode(); - /** Thin backward diagonal */ - short THIN_BACKWARD_DIAG = 13; + /** + * Fill Pattern: Thin backward diagonal + * @deprecated 3.15 beta 3. Use {@link FillPatternType#THIN_BACKWARD_DIAG} instead. + */ + short THIN_BACKWARD_DIAG = FillPatternType.THIN_BACKWARD_DIAG.getCode(); - /** Thin forward diagonal */ - short THIN_FORWARD_DIAG = 14; + /** + * Fill Pattern: Thin forward diagonal + * @deprecated 3.15 beta 3. Use {@link FillPatternType#THIN_FORWARD_DIAG} instead. + */ + short THIN_FORWARD_DIAG = FillPatternType.THIN_FORWARD_DIAG.getCode(); - /** Squares */ - short SQUARES = 15; + /** + * Fill Pattern: Squares + * @deprecated 3.15 beta 3. Use {@link FillPatternType#SQUARES} instead. + */ + short SQUARES = FillPatternType.SQUARES.getCode(); - /** Diamonds */ - short DIAMONDS = 16; + /** + * Fill Pattern: Diamonds + * @deprecated 3.15 beta 3. Use {@link FillPatternType#DIAMONDS} instead. + */ + short DIAMONDS = FillPatternType.DIAMONDS.getCode(); - /** Less Dots */ - short LESS_DOTS = 17; + /** + * Fill Pattern: Less Dots + * @deprecated 3.15 beta 3. Use {@link FillPatternType#LESS_DOTS} instead. + */ + short LESS_DOTS = FillPatternType.LESS_DOTS.getCode(); - /** Least Dots */ - short LEAST_DOTS = 18; + /** + * Fill Pattern: Least Dots + * @deprecated 3.15 beta 3. Use {@link FillPatternType#LEAST_DOTS} instead. + */ + short LEAST_DOTS = FillPatternType.LEAST_DOTS.getCode(); /** * get the index within the Workbook (sequence within the collection of ExtnededFormat objects) @@ -601,15 +658,48 @@ public interface CellStyle { * @see #DIAMONDS * * @param fp fill pattern (set to 1 to fill w/foreground color) + * @deprecated POI 3.15 beta 3. Use {@link #setFillPattern(FillPatternType)} instead. */ void setFillPattern(short fp); + /** + * setting to one fills the cell with the foreground color... No idea about + * other values + * + * @see #NO_FILL + * @see #SOLID_FOREGROUND + * @see #FINE_DOTS + * @see #ALT_BARS + * @see #SPARSE_DOTS + * @see #THICK_HORZ_BANDS + * @see #THICK_VERT_BANDS + * @see #THICK_BACKWARD_DIAG + * @see #THICK_FORWARD_DIAG + * @see #BIG_SPOTS + * @see #BRICKS + * @see #THIN_HORZ_BANDS + * @see #THIN_VERT_BANDS + * @see #THIN_BACKWARD_DIAG + * @see #THIN_FORWARD_DIAG + * @see #SQUARES + * @see #DIAMONDS + * + * @param fp fill pattern (set to {@link FillPatternType#SOLID_FOREGROUND} to fill w/foreground color) + * @since POI 3.15 beta 3 + */ + void setFillPattern(FillPatternType fp); /** * get the fill pattern (??) - set to 1 to fill with foreground color * @return fill pattern + * @deprecated POI 3.15 beta 3. This method will return {@link FillPatternType} in the future. Use {@link #setFillPattern(FillPatternType)} instead. */ - short getFillPattern(); + /** + * get the fill pattern (??) - set to 1 to fill with foreground color + * @return fill pattern + * @since POI 3.15 beta 3 + */ + FillPatternType getFillPatternEnum(); /** * set the background fill color. diff --git a/src/java/org/apache/poi/ss/usermodel/FillPatternType.java b/src/java/org/apache/poi/ss/usermodel/FillPatternType.java index e34ed34b44..705fd6d4ea 100644 --- a/src/java/org/apache/poi/ss/usermodel/FillPatternType.java +++ b/src/java/org/apache/poi/ss/usermodel/FillPatternType.java @@ -24,60 +24,80 @@ package org.apache.poi.ss.usermodel; public enum FillPatternType { /** No background */ - NO_FILL, + NO_FILL(0), /** Solidly filled */ - SOLID_FOREGROUND, + SOLID_FOREGROUND(1), /** Small fine dots */ - FINE_DOTS, + FINE_DOTS(2), /** Wide dots */ - ALT_BARS, + ALT_BARS(3), /** Sparse dots */ - SPARSE_DOTS, + SPARSE_DOTS(4), /** Thick horizontal bands */ - THICK_HORZ_BANDS, + THICK_HORZ_BANDS(5), /** Thick vertical bands */ - THICK_VERT_BANDS, + THICK_VERT_BANDS(6), /** Thick backward facing diagonals */ - THICK_BACKWARD_DIAG, + THICK_BACKWARD_DIAG(7), /** Thick forward facing diagonals */ - THICK_FORWARD_DIAG, + THICK_FORWARD_DIAG(8), /** Large spots */ - BIG_SPOTS, + BIG_SPOTS(9), /** Brick-like layout */ - BRICKS, + BRICKS(10), /** Thin horizontal bands */ - THIN_HORZ_BANDS, + THIN_HORZ_BANDS(11), /** Thin vertical bands */ - THIN_VERT_BANDS, + THIN_VERT_BANDS(12), /** Thin backward diagonal */ - THIN_BACKWARD_DIAG, + THIN_BACKWARD_DIAG(13), /** Thin forward diagonal */ - THIN_FORWARD_DIAG, + THIN_FORWARD_DIAG(14), /** Squares */ - SQUARES, + SQUARES(15), /** Diamonds */ - DIAMONDS, + DIAMONDS(16), /** Less Dots */ - LESS_DOTS, + LESS_DOTS(17), /** Least Dots */ - LEAST_DOTS + LEAST_DOTS(18); + + /** Codes are used by ExtendedFormatRecord in HSSF */ + private final short code; + private FillPatternType(int code) { + this.code = (short) code; + } + + public short getCode() { + return code; + } + + private final static int length = values().length; + public static FillPatternType forInt(int code) { + if (code < 0 || code > length) { + throw new IllegalArgumentException("Invalid FillPatternType code: " + code); + } + return values()[code]; + } + // it may also make sense to have an @Internal method to convert STPatternType.Enum + // but may cause errors if poi-ooxml.jar is not on the classpath } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index 32a2a7b7be..06c436f4f1 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -512,28 +512,29 @@ public class XSSFCellStyle implements CellStyle { * @see org.apache.poi.ss.usermodel.CellStyle#THIN_FORWARD_DIAG * @see org.apache.poi.ss.usermodel.CellStyle#SQUARES * @see org.apache.poi.ss.usermodel.CellStyle#DIAMONDS + * @deprecated POI 3.15 beta 3. This method will return {@link FillPatternType} in the future. Use {@link #setFillPattern(FillPatternType)} instead. */ @Override public short getFillPattern() { - // bug 56295: handle missing applyFill attribute as "true" because Excel does as well - if(_cellXf.isSetApplyFill() && !_cellXf.getApplyFill()) return 0; - - int fillIndex = (int)_cellXf.getFillId(); - XSSFCellFill fill = _stylesSource.getFillAt(fillIndex); - - STPatternType.Enum ptrn = fill.getPatternType(); - if(ptrn == null) return CellStyle.NO_FILL; - return (short)(ptrn.intValue() - 1); + return getFillPatternEnum().getCode(); } /** * Get the fill pattern * - * @return the fill pattern, default value is {@link org.apache.poi.ss.usermodel.FillPatternType#NO_FILL} + * @return the fill pattern, default value is {@link FillPatternType#NO_FILL} */ + @Override public FillPatternType getFillPatternEnum() { - int style = getFillPattern(); - return FillPatternType.values()[style]; + // bug 56295: handle missing applyFill attribute as "true" because Excel does as well + if(_cellXf.isSetApplyFill() && !_cellXf.getApplyFill()) return FillPatternType.NO_FILL; + + int fillIndex = (int)_cellXf.getFillId(); + XSSFCellFill fill = _stylesSource.getFillAt(fillIndex); + + STPatternType.Enum ptrn = fill.getPatternType(); + if(ptrn == null) return FillPatternType.NO_FILL; + return FillPatternType.forInt(ptrn.intValue() - 1); } /** @@ -1094,7 +1095,7 @@ public class XSSFCellStyle implements CellStyle { /** * This element is used to specify cell fill information for pattern and solid color cell fills. - * For solid cell fills (no pattern), foregorund color is used. + * For solid cell fills (no pattern), foreground color is used. * For cell fills with patterns specified, then the cell fill color is specified by the background color. * * @see org.apache.poi.ss.usermodel.CellStyle#NO_FILL @@ -1120,31 +1121,35 @@ public class XSSFCellStyle implements CellStyle { */ @Override public void setFillPattern(short fp) { - CTFill ct = getCTFill(); - CTPatternFill ptrn = ct.isSetPatternFill() ? ct.getPatternFill() : ct.addNewPatternFill(); - if(fp == NO_FILL && ptrn.isSetPatternType()) ptrn.unsetPatternType(); - else ptrn.setPatternType(STPatternType.Enum.forInt(fp + 1)); - - addFill(ct); + setFillPattern(FillPatternType.forInt(fp)); } /** * This element is used to specify cell fill information for pattern and solid color cell fills. For solid cell fills (no pattern), * foreground color is used is used. For cell fills with patterns specified, then the cell fill color is specified by the background color element. * - * @param ptrn the fill pattern to use - * @see #setFillBackgroundColor(short) - * @see #setFillForegroundColor(short) + * @param pattern the fill pattern to use + * @see #setFillBackgroundColor(XSSFColor) + * @see #setFillForegroundColor(XSSFColor) * @see org.apache.poi.ss.usermodel.FillPatternType */ - public void setFillPattern(FillPatternType ptrn) { - setFillPattern((short)ptrn.ordinal()); + @Override + public void setFillPattern(FillPatternType pattern) { + CTFill ct = getCTFill(); + CTPatternFill ctptrn = ct.isSetPatternFill() ? ct.getPatternFill() : ct.addNewPatternFill(); + if (pattern == FillPatternType.NO_FILL && ctptrn.isSetPatternType()) { + ctptrn.unsetPatternType(); + } else { + ctptrn.setPatternType(STPatternType.Enum.forInt(pattern.getCode() + 1)); + } + + addFill(ct); } /** * Set the font for this style * - * @param font a font object created or retreived from the XSSFWorkbook object + * @param font a font object created or retrieved from the XSSFWorkbook object * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#createFont() * @see org.apache.poi.xssf.usermodel.XSSFWorkbook#getFontAt(short) */ diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java index 69e1191228..94e40a3a72 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java @@ -554,6 +554,7 @@ public class TestXSSFCellStyle { HSSFCellStyle style2 = wb2.createCellStyle(); assertEquals(style2.getFillBackgroundColor(), style1.getFillBackgroundColor()); assertEquals(style2.getFillForegroundColor(), style1.getFillForegroundColor()); + assertEquals(style2.getFillPatternEnum(), style1.getFillPatternEnum()); assertEquals(style2.getFillPattern(), style1.getFillPattern()); assertEquals(style2.getLeftBorderColor(), style1.getLeftBorderColor()); @@ -578,11 +579,13 @@ public class TestXSSFCellStyle { XSSFCellStyle defaultStyle = wb.getCellStyleAt((short)0); assertEquals(IndexedColors.AUTOMATIC.getIndex(), defaultStyle.getFillForegroundColor()); assertEquals(null, defaultStyle.getFillForegroundXSSFColor()); + assertEquals(FillPatternType.NO_FILL, defaultStyle.getFillPatternEnum()); assertEquals(CellStyle.NO_FILL, defaultStyle.getFillPattern()); XSSFCellStyle customStyle = wb.createCellStyle(); - customStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); + customStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + assertEquals(FillPatternType.SOLID_FOREGROUND, customStyle.getFillPatternEnum()); assertEquals(CellStyle.SOLID_FOREGROUND, customStyle.getFillPattern()); assertEquals(3, styles.getFills().size()); @@ -593,7 +596,8 @@ public class TestXSSFCellStyle { for (int i = 0; i < 3; i++) { XSSFCellStyle style = wb.createCellStyle(); - style.setFillPattern(CellStyle.SOLID_FOREGROUND); + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); + assertEquals(FillPatternType.SOLID_FOREGROUND, style.getFillPatternEnum()); assertEquals(CellStyle.SOLID_FOREGROUND, style.getFillPattern()); assertEquals(4, styles.getFills().size()); @@ -604,15 +608,17 @@ public class TestXSSFCellStyle { assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(wb)); wb.close(); - } + } - @Test + @Test public void testGetFillPattern() { + assertEquals(STPatternType.INT_DARK_GRAY-1, cellStyle.getFillPatternEnum().getCode()); assertEquals(STPatternType.INT_DARK_GRAY-1, cellStyle.getFillPattern()); int num = stylesTable.getFills().size(); - cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); + cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + assertEquals(FillPatternType.SOLID_FOREGROUND, cellStyle.getFillPatternEnum()); assertEquals(CellStyle.SOLID_FOREGROUND, cellStyle.getFillPattern()); assertEquals(num + 1, stylesTable.getFills().size()); int fillId = (int)cellStyle.getCoreXf().getFillId(); @@ -623,11 +629,12 @@ public class TestXSSFCellStyle { //setting the same fill multiple time does not update the styles table for (int i = 0; i < 3; i++) { - cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); + cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); } assertEquals(num + 1, stylesTable.getFills().size()); - cellStyle.setFillPattern(CellStyle.NO_FILL); + cellStyle.setFillPattern(FillPatternType.NO_FILL); + assertEquals(FillPatternType.NO_FILL, cellStyle.getFillPatternEnum()); assertEquals(CellStyle.NO_FILL, cellStyle.getFillPattern()); fillId = (int)cellStyle.getCoreXf().getFillId(); ctFill2 = stylesTable.getFillAt(fillId).getCTFill(); @@ -935,7 +942,7 @@ public class TestXSSFCellStyle { cellStyle2.setFillBackgroundColor(IndexedColors.DARK_BLUE.getIndex()); cellStyle2.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex()); - cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND); + cellStyle2.setFillPattern(FillPatternType.SOLID_FOREGROUND); cellStyle2.setAlignment(CellStyle.ALIGN_RIGHT); cellStyle2.setVerticalAlignment(CellStyle.VERTICAL_TOP); @@ -950,6 +957,7 @@ public class TestXSSFCellStyle { assertEquals(IndexedColors.DARK_BLUE.getIndex(), styleBack.getFillForegroundColor()); assertEquals(CellStyle.ALIGN_RIGHT, styleBack.getAlignment()); assertEquals(CellStyle.VERTICAL_TOP, styleBack.getVerticalAlignment()); + assertEquals(FillPatternType.SOLID_FOREGROUND, styleBack.getFillPatternEnum()); assertEquals(CellStyle.SOLID_FOREGROUND, styleBack.getFillPattern()); wbBack.close();