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
*@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";
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";
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[];
/**
* 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);
/**
* 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);
/**
* 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);
}
/**
* @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);
}
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!
* @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);
*/
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.
* @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)));
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));
}
}
// @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;
}
/**
}
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.
* @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);
}
/**
* @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);
}
/**
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
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
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
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
\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
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
\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
}\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
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