]> source.dussan.org Git - poi.git/commitdiff
bug 59837: update CellUtil to handle VerticalAlignment and HorizontalAlignment enums
authorJaven O'Neal <onealj@apache.org>
Sun, 10 Jul 2016 11:18:55 +0000 (11:18 +0000)
committerJaven O'Neal <onealj@apache.org>
Sun, 10 Jul 2016 11:18:55 +0000 (11:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1752077 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/util/HSSFCellUtil.java
src/java/org/apache/poi/sl/usermodel/VerticalAlignment.java
src/java/org/apache/poi/ss/usermodel/VerticalAlignment.java
src/java/org/apache/poi/ss/util/CellUtil.java
src/testcases/org/apache/poi/ss/util/BaseTestCellUtil.java

index f7bded47ae167c60b397e166fffa8027bb641681..ae46a5a2e8cc19d1cedce879431bf42c066322e6 100644 (file)
@@ -23,7 +23,9 @@ import org.apache.poi.hssf.usermodel.HSSFFont;
 import org.apache.poi.hssf.usermodel.HSSFRow;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
 import org.apache.poi.ss.util.CellUtil;
+import org.apache.poi.util.Removal;
 
 /**
  * Various utility functions that make working with a cells and rows easier.  The various
@@ -33,8 +35,10 @@ import org.apache.poi.ss.util.CellUtil;
  * creating too many styles.  there is an upper limit in Excel on the number of styles that
  * can be supported.
  * 
- * @deprecated 3.15 beta2. Removed in 3.17. Use {@link org.apache.poi.ss.util.CellUtil} instead.
+ * @deprecated 3.15 beta2. Use {@link org.apache.poi.ss.util.CellUtil} instead.
  */
+@Deprecated
+@Removal(version="3.17")
 public final class HSSFCellUtil {
 
     private HSSFCellUtil() {
@@ -104,7 +108,19 @@ public final class HSSFCellUtil {
      * @see HSSFCellStyle for alignment options
      */
     public static void setAlignment(HSSFCell cell, HSSFWorkbook workbook, short align) {
-       CellUtil.setAlignment(cell, align);
+       setAlignment(cell, HorizontalAlignment.forInt(align));
+    }
+    /**
+     *  Take a cell, and align it.
+     *
+     * @param  cell     the cell to set the alignment for
+     * @param  align  the column alignment to use.
+     * @deprecated 3.15 beta2. Removed in 3.17. Use {@link org.apache.poi.ss.util.CellUtil#setAlignment} instead.
+     *
+     * @see HSSFCellStyle for alignment options
+     */
+    public static void setAlignment(HSSFCell cell, HorizontalAlignment align) {
+        CellUtil.setAlignment(cell, align);
     }
 
     /**
index 4820dc75a1579572e44ab0bf09ab847218d0b813..6a63af6b0e8f6da9df34380361d196bf2b5a83f2 100644 (file)
  */\r
 package org.apache.poi.sl.usermodel;\r
 \r
-// FIXME: Identical to {@link org.apache.poi.ss.usermodel.VerticalAlignment}. Should merge these.\r
 /**\r
  * Specifies a list of available anchoring types for text\r
  * \r
+ * <!-- FIXME: Identical to {@link org.apache.poi.ss.usermodel.VerticalAlignment}. Should merge these. -->\r
+ * \r
  * @author Yegor Kozlov\r
  */\r
 public enum VerticalAlignment {\r
index c81a0a728fa574d6101e4d9d63fb9bdcb63fff9c..32d5e64a7763e3d07bbb83c282899bf175e41522 100644 (file)
 
 package org.apache.poi.ss.usermodel;
 
-//FIXME: Identical to {@link org.apache.poi.ss.usermodel.VerticalAlignment}. Should merge these.
 /**
  * This enumeration value indicates the type of vertical alignment for a cell, i.e.,
  * whether it is aligned top, bottom, vertically centered, justified or distributed.
- * FIXME: Identical to {@link org.apache.poi.ss.usermodel.VerticalAlignment}. Should merge these.
+ * 
+ * <!-- FIXME: Identical to {@link org.apache.poi.ss.usermodel.VerticalAlignment}. Should merge these.-->
  */
 public enum VerticalAlignment {
     /**
@@ -35,7 +35,7 @@ public enum VerticalAlignment {
     CENTER,
 
     /**
-     * The vertical alignment is aligned-to-bottom.
+     * The vertical alignment is aligned-to-bottom. (typically the default value)
      */
     BOTTOM,
 
index 4f8c9cd1836c06c8d7b6c565b61eaee2f6070901..4f446c10dce116ccd618677a77c9e628dc5ee142 100644 (file)
 
 package org.apache.poi.ss.util;
 
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
 
 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.Row;
 import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.VerticalAlignment;
 import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
+import org.apache.poi.util.Removal;
 
 /**
  * Various utility functions that make working with a cells and rows easier. The various methods
@@ -41,6 +51,8 @@ import org.apache.poi.ss.usermodel.Workbook;
  *@author (secondary) Avinash Kewalramani akewalramani@accelrys.com
  */
 public final class CellUtil {
+    
+    private static final POILogger log = POILogFactory.getLogger(CellUtil.class);
 
     // FIXME: Move these constants into an enum
     public static final String ALIGNMENT = "alignment";
@@ -49,6 +61,9 @@ public final class CellUtil {
     public static final String BORDER_RIGHT = "borderRight";
     public static final String BORDER_TOP = "borderTop";
     public static final String BOTTOM_BORDER_COLOR = "bottomBorderColor";
+    public static final String LEFT_BORDER_COLOR = "leftBorderColor";
+    public static final String RIGHT_BORDER_COLOR = "rightBorderColor";
+    public static final String TOP_BORDER_COLOR = "topBorderColor";
     public static final String DATA_FORMAT = "dataFormat";
     public static final String FILL_BACKGROUND_COLOR = "fillBackgroundColor";
     public static final String FILL_FOREGROUND_COLOR = "fillForegroundColor";
@@ -56,13 +71,39 @@ public final class CellUtil {
     public static final String FONT = "font";
     public static final String HIDDEN = "hidden";
     public static final String INDENTION = "indention";
-    public static final String LEFT_BORDER_COLOR = "leftBorderColor";
     public static final String LOCKED = "locked";
-    public static final String RIGHT_BORDER_COLOR = "rightBorderColor";
     public static final String ROTATION = "rotation";
-    public static final String TOP_BORDER_COLOR = "topBorderColor";
     public static final String VERTICAL_ALIGNMENT = "verticalAlignment";
     public static final String WRAP_TEXT = "wrapText";
+    
+    private static final Set<String> shortValues = Collections.unmodifiableSet(
+            new HashSet<String>(Arrays.asList(
+                    BOTTOM_BORDER_COLOR,
+                    LEFT_BORDER_COLOR,
+                    RIGHT_BORDER_COLOR,
+                    TOP_BORDER_COLOR,
+                    FILL_FOREGROUND_COLOR,
+                    FILL_BACKGROUND_COLOR,
+                    INDENTION,
+                    DATA_FORMAT,
+                    FONT,
+                    ROTATION
+    )));
+    private static final Set<String> booleanValues = Collections.unmodifiableSet(
+            new HashSet<String>(Arrays.asList(
+                    LOCKED,
+                    HIDDEN,
+                    WRAP_TEXT
+    )));
+    private static final Set<String> borderTypeValues = Collections.unmodifiableSet(
+            new HashSet<String>(Arrays.asList(
+                    BORDER_BOTTOM,
+                    BORDER_LEFT,
+                    BORDER_RIGHT,
+                    BORDER_TOP
+    )));
+    
+
 
     private static UnicodeMapping unicodeMappings[];
 
@@ -84,9 +125,9 @@ public final class CellUtil {
     /**
      * Get a row from the spreadsheet, and create it if it doesn't exist.
      *
-     *@param rowIndex The 0 based row number
-     *@param sheet The sheet that the row is part of.
-     *@return The row indicated by the rowCounter
+     * @param rowIndex The 0 based row number
+     * @param sheet The sheet that the row is part of.
+     * @return The row indicated by the rowCounter
      */
     public static Row getRow(int rowIndex, Sheet sheet) {
         Row row = sheet.getRow(rowIndex);
@@ -100,9 +141,9 @@ public final class CellUtil {
     /**
      * Get a specific cell from a row. If the cell doesn't exist, then create it.
      *
-     *@param row The row that the cell is part of
-     *@param columnIndex The column index that the cell is in.
-     *@return The cell indicated by the column.
+     * @param row The row that the cell is part of
+     * @param columnIndex The column index that the cell is in.
+     * @return The cell indicated by the column.
      */
     public static Cell getCell(Row row, int columnIndex) {
         Cell cell = row.getCell(columnIndex);
@@ -149,29 +190,63 @@ 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 workbook The workbook that is being worked with.
      * @param align the column alignment to use.
      *
-     * @deprecated 3.15-beta2. Removed in 3.17. Use {@link #setAlignment(Cell, short)} instead.
+     * @deprecated 3.15-beta2. Use {@link #setAlignment(Cell, HorizontalAlignment)} instead.
      *
      * @see CellStyle for alignment options
      */
+    @Deprecated
+    @Removal(version="3.17")
     public static void setAlignment(Cell cell, Workbook workbook, short align) {
-        setAlignment(cell, align);
+        setAlignment(cell, HorizontalAlignment.forInt(align));
     }
 
     /**
      * 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 column alignment to use.
+     * @param align the horizontal alignment to use.
      *
-     * @see CellStyle for alignment options
+     * @see HorizontalAlignment for alignment options
+     * @since POI 3.15 beta 3
      */
-    public static void setAlignment(Cell cell, short align) {
-        setCellStyleProperty(cell, ALIGNMENT, Short.valueOf(align));
+    public static void setAlignment(Cell cell, HorizontalAlignment align) {
+        setCellStyleProperty(cell, ALIGNMENT, align);
+    }
+    
+    /**
+     * Take a cell, and vertically align it.
+     * 
+     * This is superior to cell.getCellStyle().setVerticalAlignment(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 vertical alignment to use.
+     *
+     * @see VerticalAlignment for alignment options
+     * @since POI 3.15 beta 3
+     */
+    public static void setVerticalAlignment(Cell cell, VerticalAlignment align) {
+        setCellStyleProperty(cell, VERTICAL_ALIGNMENT, align);
     }
     
     /**
@@ -182,8 +257,10 @@ public final class CellUtil {
      * @param font The Font that you want to set.
      * @throws IllegalArgumentException if <tt>font</tt> and <tt>cell</tt> do not belong to the same workbook
      *
-     * @deprecated 3.15-beta2. Removed in 3.17. Use {@link #setFont(Cell, Font)} instead.
+     * @deprecated 3.15-beta2. Use {@link #setFont(Cell, Font)} instead.
      */
+    @Deprecated
+    @Removal(version="3.17")
     public static void setFont(Cell cell, Workbook workbook, Font font) {
         setFont(cell, font);
     }
@@ -239,7 +316,7 @@ public final class CellUtil {
         CellStyle originalStyle = cell.getCellStyle();
         CellStyle newStyle = null;
         Map<String, Object> values = getFormatProperties(originalStyle);
-        values.putAll(properties);
+        putAll(properties, values);
 
         // index seems like what index the cellstyle is in the list of styles for a workbook.
         // not good to compare on!
@@ -283,8 +360,10 @@ public final class CellUtil {
      * @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 3.15-beta2. Removed in 3.17. Use {@link #setCellStyleProperty(Cell, String, Object)} instead.
+     * @deprecated 3.15-beta2. Use {@link #setCellStyleProperty(Cell, String, Object)} instead.
      */
+    @Deprecated
+    @Removal(version="3.17")
     public static void setCellStyleProperty(Cell cell, Workbook workbook, String propertyName,
             Object propertyValue) {
         setCellStyleProperty(cell, propertyName, propertyValue);
@@ -324,28 +403,58 @@ public final class CellUtil {
      */
     private static Map<String, Object> getFormatProperties(CellStyle style) {
         Map<String, Object> properties = new HashMap<String, Object>();
-        putShort(properties, ALIGNMENT, style.getAlignment());
-        putBorderStyle(properties, BORDER_BOTTOM, style.getBorderBottom());
-        putBorderStyle(properties, BORDER_LEFT, style.getBorderLeft());
-        putBorderStyle(properties, BORDER_RIGHT, style.getBorderRight());
-        putBorderStyle(properties, BORDER_TOP, style.getBorderTop());
-        putShort(properties, BOTTOM_BORDER_COLOR, style.getBottomBorderColor());
-        putShort(properties, DATA_FORMAT, style.getDataFormat());
-        putShort(properties, FILL_PATTERN, style.getFillPattern());
-        putShort(properties, FILL_FOREGROUND_COLOR, style.getFillForegroundColor());
-        putShort(properties, FILL_BACKGROUND_COLOR, style.getFillBackgroundColor());
-        putShort(properties, FONT, style.getFontIndex());
-        putBoolean(properties, HIDDEN, style.getHidden());
-        putShort(properties, INDENTION, style.getIndention());
-        putShort(properties, LEFT_BORDER_COLOR, style.getLeftBorderColor());
-        putBoolean(properties, LOCKED, style.getLocked());
-        putShort(properties, RIGHT_BORDER_COLOR, style.getRightBorderColor());
-        putShort(properties, ROTATION, style.getRotation());
-        putShort(properties, TOP_BORDER_COLOR, style.getTopBorderColor());
-        putShort(properties, VERTICAL_ALIGNMENT, style.getVerticalAlignment());
-        putBoolean(properties, WRAP_TEXT, style.getWrapText());
+        put(properties, ALIGNMENT, style.getAlignmentEnum());
+        put(properties, VERTICAL_ALIGNMENT, style.getVerticalAlignmentEnum());
+        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.getFillPatternEnum());
+        put(properties, FILL_FOREGROUND_COLOR, style.getFillForegroundColor());
+        put(properties, FILL_BACKGROUND_COLOR, style.getFillBackgroundColor());
+        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());
         return properties;
     }
+    
+    /**
+     * Copies the entries in src to dest, using the preferential data type
+     * so that maps can be compared for equality
+     *
+     * @param src the property map to copy from (read-only)
+     * @param dest the property map to copy into
+     * @since POI 3.15 beta 3
+     */
+    private static void putAll(final Map<String, Object> src, Map<String, Object> dest) {
+        for (final String key : src.keySet()) {
+            if (shortValues.contains(key)) {
+                dest.put(key, getShort(src, key));
+            } else if (booleanValues.contains(key)) {
+                dest.put(key, getBoolean(src, key));
+            } else if (borderTypeValues.contains(key)) {
+                dest.put(key, getBorderStyle(src, key));
+            } else if (ALIGNMENT.equals(key)) {
+                dest.put(key, getHorizontalAlignment(src, key));
+            } else if (VERTICAL_ALIGNMENT.equals(key)) {
+                dest.put(key, getVerticalAlignment(src, key));
+            } else if (FILL_PATTERN.equals(key)) {
+                dest.put(key, getFillPattern(src, key));
+            } else {
+                if (log.check(POILogger.INFO)) {
+                    log.log(POILogger.INFO, "Ignoring unrecognized CellUtil format properties key: " + key);
+                }
+            }
+        }
+    }
 
     /**
      * Sets the format properties of the given style based on the given map.
@@ -356,14 +465,15 @@ public final class CellUtil {
      * @see #getFormatProperties(CellStyle)
      */
     private static void setFormatProperties(CellStyle style, Workbook workbook, Map<String, Object> properties) {
-        style.setAlignment(getShort(properties, ALIGNMENT));
+        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(getShort(properties, FILL_PATTERN));
+        style.setFillPattern(getFillPattern(properties, FILL_PATTERN));
         style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR));
         style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR));
         style.setFont(workbook.getFontAt(getShort(properties, FONT)));
@@ -374,7 +484,6 @@ public final class CellUtil {
         style.setRightBorderColor(getShort(properties, RIGHT_BORDER_COLOR));
         style.setRotation(getShort(properties, ROTATION));
         style.setTopBorderColor(getShort(properties, TOP_BORDER_COLOR));
-        style.setVerticalAlignment(getShort(properties, VERTICAL_ALIGNMENT));
         style.setWrapText(getBoolean(properties, WRAP_TEXT));
     }
 
@@ -409,13 +518,120 @@ public final class CellUtil {
         }
         // @deprecated 3.15 beta 2. getBorderStyle will only work on BorderStyle enums instead of codes in the future.
         else if (value instanceof Short) {
+            if (log.check(POILogger.WARN)) {
+                log.log(POILogger.WARN, "Deprecation warning: CellUtil properties map uses Short values for "
+                        + name + ". Should use BorderStyle enums instead.");
+            }
+            System.out.println("BorderStyle short usage");
             short code = ((Short) value).shortValue();
             border = BorderStyle.valueOf(code);
         }
+        else if (value == null) {
+            border = BorderStyle.NONE;
+        }
         else {
-            throw new RuntimeException("Unexpected border style class. Must be BorderStyle or Short (deprecated)");
+            throw new RuntimeException("Unexpected border style class. Must be BorderStyle or Short (deprecated).");
         }
-        return (border != null) ? border : BorderStyle.NONE;
+        return border;
+    }
+    
+    /**
+     * Utility method that returns the named FillPatternType value form the given map.
+     *
+     * @param properties map of named properties (String -> Object)
+     * @param name property name
+     * @return FillPatternType style if set, otherwise {@link FillPatternType#NO_FILL}
+     * @since POI 3.15 beta 3
+     */
+    private static FillPatternType getFillPattern(Map<String, Object> properties, String name) {
+        Object value = properties.get(name);
+        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) {
+            if (log.check(POILogger.WARN)) {
+                log.log(POILogger.WARN, "Deprecation warning: CellUtil properties map uses Short values for "
+                        + name + ". Should use FillPatternType enums instead.");
+            }
+            System.out.println("FillPatternType short usage");
+            short code = ((Short) value).shortValue();
+            pattern = FillPatternType.forInt(code);
+        }
+        else if (value == null) {
+            pattern = FillPatternType.NO_FILL;
+        }
+        else {
+            throw new RuntimeException("Unexpected fill pattern style class. Must be FillPatternType or Short (deprecated).");
+        }
+        return pattern;
+    }
+    
+    /**
+     * Utility method that returns the named HorizontalAlignment value form the given map.
+     *
+     * @param properties map of named properties (String -> Object)
+     * @param name property name
+     * @return HorizontalAlignment style if set, otherwise {@link HorizontalAlignment#GENERAL}
+     * @since POI 3.15 beta 3
+     */
+    private static HorizontalAlignment getHorizontalAlignment(Map<String, Object> properties, String name) {
+        Object value = properties.get(name);
+        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) {
+            if (log.check(POILogger.WARN)) {
+                log.log(POILogger.WARN, "Deprecation warning: CellUtil properties map used a Short value for "
+                        + name + ". Should use HorizontalAlignment enums instead.");
+            }
+            System.out.println("HorizontalAlignment short usage");
+            short code = ((Short) value).shortValue();
+            align = HorizontalAlignment.forInt(code);
+        }
+        else if (value == null) {
+            align = HorizontalAlignment.GENERAL;
+        }
+        else {
+            throw new RuntimeException("Unexpected horizontal alignment style class. Must be HorizontalAlignment or Short (deprecated).");
+        }
+        return align;
+    }
+    
+    /**
+     * Utility method that returns the named VerticalAlignment value form the given map.
+     *
+     * @param properties map of named properties (String -> Object)
+     * @param name property name
+     * @return VerticalAlignment style if set, otherwise {@link VerticalAlignment#BOTTOM}
+     * @since POI 3.15 beta 3
+     */
+    private static VerticalAlignment getVerticalAlignment(Map<String, Object> properties, String name) {
+        Object value = properties.get(name);
+        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) {
+            if (log.check(POILogger.WARN)) {
+                log.log(POILogger.WARN, "Deprecation warning: CellUtil properties map used a Short value for "
+                        + name + ". Should use VerticalAlignment enums instead.");
+            }
+            System.out.println("VerticalAlignment usage " + name + " " + value);
+            short code = ((Short) value).shortValue();
+            align = VerticalAlignment.forInt(code);
+        }
+        else if (value == null) {
+            align = VerticalAlignment.BOTTOM;
+        }
+        else {
+            throw new RuntimeException("Unexpected vertical alignment style class. Must be VerticalAlignment or Short (deprecated).");
+        }
+        return align;
     }
 
     /**
@@ -434,6 +650,17 @@ public final class CellUtil {
         }
         return false;
     }
+    
+    /**
+     * Utility method that puts the given value to the given map.
+     *
+     * @param properties map of properties (String -> Object)
+     * @param name property name
+     * @param value property value
+     */
+    private static void put(Map<String, Object> properties, String name, Object value) {
+        properties.put(name, value);
+    }
 
     /**
      * Utility method that puts the named short value to the given map.
@@ -443,18 +670,19 @@ public final class CellUtil {
      * @param value property value
      */
     private static void putShort(Map<String, Object> properties, String name, short value) {
-        properties.put(name, Short.valueOf(value));
+        properties.put(name, value);
     }
     
-       /**
-     * Utility method that puts the named short value to the given map.
+    /**
+     * Utility method that puts the named BorderStyle, HorizontalAlignment, VerticalAlignment, etc
+     * value to the given map.
      *
      * @param properties map of properties (String -> Object)
      * @param name property name
      * @param value property value
      */
-    private static void putBorderStyle(Map<String, Object> properties, String name, BorderStyle border) {
-        properties.put(name, border);
+    private static void putEnum(Map<String, Object> properties, String name, Enum<?> value) {
+        properties.put(name, value);
     }
 
     /**
@@ -465,7 +693,7 @@ public final class CellUtil {
      * @param value property value
      */
     private static void putBoolean(Map<String, Object> properties, String name, boolean value) {
-        properties.put(name, Boolean.valueOf(value));
+        properties.put(name, value);
     }
 
     /**
index 5caf980b07fd219f6d8bd69a3c9df65d0fd308db..cc299eb166424c773eb47db9ab839dba3ca333c7 100644 (file)
@@ -33,9 +33,11 @@ import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellStyle;\r
 import org.apache.poi.ss.usermodel.FillPatternType;\r
 import org.apache.poi.ss.usermodel.Font;\r
+import org.apache.poi.ss.usermodel.HorizontalAlignment;\r
 import org.apache.poi.ss.usermodel.IndexedColors;\r
 import org.apache.poi.ss.usermodel.Row;\r
 import org.apache.poi.ss.usermodel.Sheet;\r
+import org.apache.poi.ss.usermodel.VerticalAlignment;\r
 import org.apache.poi.ss.usermodel.Workbook;\r
 import org.junit.Test;\r
 \r
@@ -62,19 +64,20 @@ public class BaseTestCellUtil {
         int styCnt1 = wb.getNumCellStyles();\r
         CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
         int styCnt2 = wb.getNumCellStyles();\r
-        assertEquals(styCnt2, styCnt1+1);\r
+        assertEquals(styCnt1+1, styCnt2);\r
 \r
         // Add same border to another cell, should not create another style\r
         c = r.createCell(1);\r
         CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
         int styCnt3 = wb.getNumCellStyles();\r
-        assertEquals(styCnt3, styCnt2);\r
+        assertEquals(styCnt2, styCnt3);\r
 \r
         wb.close();\r
     }\r
 \r
     @Test\r
     public void setCellStyleProperties() throws IOException {\r
+        System.out.println("setCellStyleProps start");\r
         Workbook wb = _testDataProvider.createWorkbook();\r
         Sheet s = wb.createSheet();\r
         Row r = s.createRow(0);\r
@@ -87,6 +90,8 @@ public class BaseTestCellUtil {
         props.put(CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
         props.put(CellUtil.BORDER_LEFT, BorderStyle.THIN);\r
         props.put(CellUtil.BORDER_RIGHT, BorderStyle.THIN);\r
+        props.put(CellUtil.ALIGNMENT, HorizontalAlignment.CENTER.getCode()); // try it both with a Short (deprecated)\r
+        props.put(CellUtil.VERTICAL_ALIGNMENT, VerticalAlignment.CENTER); // and with an enum\r
         CellUtil.setCellStyleProperties(c, props);\r
         int styCnt2 = wb.getNumCellStyles();\r
         assertEquals("Only one additional style should have been created", styCnt1 + 1, styCnt2);\r
@@ -95,9 +100,11 @@ public class BaseTestCellUtil {
         c = r.createCell(1);\r
         CellUtil.setCellStyleProperties(c, props);\r
         int styCnt3 = wb.getNumCellStyles();\r
-        assertEquals(styCnt2, styCnt3);\r
+        System.out.println("setCellStyleProps nearing end");\r
+        assertEquals("No additional styles should have been created", styCnt2, styCnt3);\r
 \r
         wb.close();\r
+        \r
     }\r
 \r
     @Test\r
@@ -171,6 +178,11 @@ public class BaseTestCellUtil {
 \r
     }\r
 \r
+    /**\r
+     * @deprecated by {@link #setAlignmentEnum()}\r
+     *\r
+     * @throws IOException\r
+     */\r
     @Test\r
     public void setAlignment() throws IOException {\r
         Workbook wb = _testDataProvider.createWorkbook();\r
@@ -188,7 +200,7 @@ public class BaseTestCellUtil {
         assertEquals(CellStyle.ALIGN_GENERAL, B1.getCellStyle().getAlignment());\r
 \r
         // get/set alignment modifies the cell's style\r
-        CellUtil.setAlignment(A1, CellStyle.ALIGN_RIGHT);\r
+        CellUtil.setAlignment(A1, null, CellStyle.ALIGN_RIGHT);\r
         assertEquals(CellStyle.ALIGN_RIGHT, A1.getCellStyle().getAlignment());\r
 \r
         // get/set alignment doesn't affect the style of cells with\r
@@ -198,6 +210,62 @@ public class BaseTestCellUtil {
 \r
         wb.close();\r
     }\r
+    \r
+    @Test\r
+    public void setAlignmentEnum() throws IOException {\r
+        Workbook wb = _testDataProvider.createWorkbook();\r
+        Sheet sh = wb.createSheet();\r
+        Row row = sh.createRow(0);\r
+        Cell A1 = row.createCell(0);\r
+        Cell B1 = row.createCell(1);\r
+\r
+        // Assumptions\r
+        assertEquals(A1.getCellStyle(), B1.getCellStyle());\r
+        // should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call. \r
+        // HSSFCellStyle wraps an underlying style record, and the underlying\r
+        // style record is the same between multiple getCellStyle() calls.\r
+        assertEquals(HorizontalAlignment.GENERAL, A1.getCellStyle().getAlignmentEnum());\r
+        assertEquals(HorizontalAlignment.GENERAL, B1.getCellStyle().getAlignmentEnum());\r
+\r
+        // get/set alignment modifies the cell's style\r
+        CellUtil.setAlignment(A1, HorizontalAlignment.RIGHT);\r
+        assertEquals(HorizontalAlignment.RIGHT, A1.getCellStyle().getAlignmentEnum());\r
+\r
+        // get/set alignment doesn't affect the style of cells with\r
+        // the same style prior to modifying the style\r
+        assertNotEquals(A1.getCellStyle(), B1.getCellStyle());\r
+        assertEquals(HorizontalAlignment.GENERAL, B1.getCellStyle().getAlignmentEnum());\r
+\r
+        wb.close();\r
+    }\r
+    \r
+    @Test\r
+    public void setVerticalAlignmentEnum() throws IOException {\r
+        Workbook wb = _testDataProvider.createWorkbook();\r
+        Sheet sh = wb.createSheet();\r
+        Row row = sh.createRow(0);\r
+        Cell A1 = row.createCell(0);\r
+        Cell B1 = row.createCell(1);\r
+\r
+        // Assumptions\r
+        assertEquals(A1.getCellStyle(), B1.getCellStyle());\r
+        // should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call. \r
+        // HSSFCellStyle wraps an underlying style record, and the underlying\r
+        // style record is the same between multiple getCellStyle() calls.\r
+        assertEquals(VerticalAlignment.BOTTOM, A1.getCellStyle().getVerticalAlignmentEnum());\r
+        assertEquals(VerticalAlignment.BOTTOM, B1.getCellStyle().getVerticalAlignmentEnum());\r
+\r
+        // get/set alignment modifies the cell's style\r
+        CellUtil.setVerticalAlignment(A1, VerticalAlignment.TOP);\r
+        assertEquals(VerticalAlignment.TOP, A1.getCellStyle().getVerticalAlignmentEnum());\r
+\r
+        // get/set alignment doesn't affect the style of cells with\r
+        // the same style prior to modifying the style\r
+        assertNotEquals(A1.getCellStyle(), B1.getCellStyle());\r
+        assertEquals(VerticalAlignment.BOTTOM, B1.getCellStyle().getVerticalAlignmentEnum());\r
+\r
+        wb.close();\r
+    }\r
 \r
     @Test\r
     public void setFont() throws IOException {\r
@@ -263,6 +331,11 @@ public class BaseTestCellUtil {
         }\r
     }\r
     \r
+    /**\r
+     * bug 55555\r
+     * @deprecated Replaced by {@link #setFillForegroundColorBeforeFillBackgroundColorEnum()}\r
+     * @since POI 3.15 beta 3\r
+     */\r
     // bug 55555\r
     @Test\r
     public void setFillForegroundColorBeforeFillBackgroundColor() {\r
@@ -281,4 +354,25 @@ public class BaseTestCellUtil {
         assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor()));\r
         assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor()));\r
     }\r
+    /**\r
+     * bug 55555\r
+     * @since POI 3.15 beta 3\r
+     */\r
+    @Test\r
+    public void setFillForegroundColorBeforeFillBackgroundColorEnum() {\r
+        Workbook wb1 = _testDataProvider.createWorkbook();\r
+        Cell A1 = wb1.createSheet().createRow(0).createCell(0);\r
+        Map<String, Object> properties = new HashMap<String, Object>();\r
+        // FIXME: Use FillPatternType.BRICKS enum\r
+        properties.put(CellUtil.FILL_PATTERN, FillPatternType.BRICKS);\r
+        properties.put(CellUtil.FILL_FOREGROUND_COLOR, IndexedColors.BLUE.index);\r
+        properties.put(CellUtil.FILL_BACKGROUND_COLOR, IndexedColors.RED.index);\r
+        \r
+        CellUtil.setCellStyleProperties(A1, properties);\r
+        CellStyle style = A1.getCellStyle();\r
+        // FIXME: Use FillPatternType.BRICKS enum\r
+        assertEquals("fill pattern", FillPatternType.BRICKS, style.getFillPatternEnum());\r
+        assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor()));\r
+        assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor()));\r
+    }\r
 }\r