bug 59837: update CellUtil to handle VerticalAlignment and HorizontalAlignment enums
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1752077 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0f8039f3ab
commit
2ec6b12e13
@ -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,6 +108,18 @@ public final class HSSFCellUtil {
|
||||
* @see HSSFCellStyle for alignment options
|
||||
*/
|
||||
public static void setAlignment(HSSFCell cell, HSSFWorkbook workbook, short 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);
|
||||
}
|
||||
|
||||
|
@ -18,10 +18,11 @@
|
||||
*/
|
||||
package org.apache.poi.sl.usermodel;
|
||||
|
||||
// FIXME: Identical to {@link org.apache.poi.ss.usermodel.VerticalAlignment}. Should merge these.
|
||||
/**
|
||||
* Specifies a list of available anchoring types for text
|
||||
*
|
||||
* <!-- FIXME: Identical to {@link org.apache.poi.ss.usermodel.VerticalAlignment}. Should merge these. -->
|
||||
*
|
||||
* @author Yegor Kozlov
|
||||
*/
|
||||
public enum VerticalAlignment {
|
||||
|
@ -17,11 +17,11 @@
|
||||
|
||||
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,
|
||||
|
||||
|
@ -17,18 +17,28 @@
|
||||
|
||||
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
|
||||
@ -42,6 +52,8 @@ import org.apache.poi.ss.usermodel.Workbook;
|
||||
*/
|
||||
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_BOTTOM = "borderBottom";
|
||||
@ -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,14 +71,40 @@ 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[];
|
||||
|
||||
private static final class UnicodeMapping {
|
||||
@ -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);
|
||||
@ -150,28 +191,62 @@ 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.
|
||||
*
|
||||
* @param cell the cell to set the alignment for
|
||||
* @param align the column alignment to use.
|
||||
* 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.
|
||||
*
|
||||
* @see CellStyle for alignment options
|
||||
* @param cell the cell to set the alignment for
|
||||
* @param align the horizontal alignment to use.
|
||||
*
|
||||
* @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,29 +403,59 @@ 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 {
|
||||
throw new RuntimeException("Unexpected border style class. Must be BorderStyle or Short (deprecated)");
|
||||
else if (value == null) {
|
||||
border = BorderStyle.NONE;
|
||||
}
|
||||
return (border != null) ? border : BorderStyle.NONE;
|
||||
else {
|
||||
throw new RuntimeException("Unexpected border style class. Must be BorderStyle or Short (deprecated).");
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -436,14 +652,14 @@ public final class CellUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method that puts the named short value to the given map.
|
||||
* 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 putShort(Map<String, Object> properties, String name, short value) {
|
||||
properties.put(name, Short.valueOf(value));
|
||||
private static void put(Map<String, Object> properties, String name, Object value) {
|
||||
properties.put(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -453,8 +669,20 @@ public final class CellUtil {
|
||||
* @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 putShort(Map<String, Object> properties, String name, short value) {
|
||||
properties.put(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -33,9 +33,11 @@ import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.FillPatternType;
|
||||
import org.apache.poi.ss.usermodel.Font;
|
||||
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -62,19 +64,20 @@ public class BaseTestCellUtil {
|
||||
int styCnt1 = wb.getNumCellStyles();
|
||||
CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);
|
||||
int styCnt2 = wb.getNumCellStyles();
|
||||
assertEquals(styCnt2, styCnt1+1);
|
||||
assertEquals(styCnt1+1, styCnt2);
|
||||
|
||||
// Add same border to another cell, should not create another style
|
||||
c = r.createCell(1);
|
||||
CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);
|
||||
int styCnt3 = wb.getNumCellStyles();
|
||||
assertEquals(styCnt3, styCnt2);
|
||||
assertEquals(styCnt2, styCnt3);
|
||||
|
||||
wb.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setCellStyleProperties() throws IOException {
|
||||
System.out.println("setCellStyleProps start");
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
Sheet s = wb.createSheet();
|
||||
Row r = s.createRow(0);
|
||||
@ -87,6 +90,8 @@ public class BaseTestCellUtil {
|
||||
props.put(CellUtil.BORDER_BOTTOM, BorderStyle.THIN);
|
||||
props.put(CellUtil.BORDER_LEFT, BorderStyle.THIN);
|
||||
props.put(CellUtil.BORDER_RIGHT, BorderStyle.THIN);
|
||||
props.put(CellUtil.ALIGNMENT, HorizontalAlignment.CENTER.getCode()); // try it both with a Short (deprecated)
|
||||
props.put(CellUtil.VERTICAL_ALIGNMENT, VerticalAlignment.CENTER); // and with an enum
|
||||
CellUtil.setCellStyleProperties(c, props);
|
||||
int styCnt2 = wb.getNumCellStyles();
|
||||
assertEquals("Only one additional style should have been created", styCnt1 + 1, styCnt2);
|
||||
@ -95,9 +100,11 @@ public class BaseTestCellUtil {
|
||||
c = r.createCell(1);
|
||||
CellUtil.setCellStyleProperties(c, props);
|
||||
int styCnt3 = wb.getNumCellStyles();
|
||||
assertEquals(styCnt2, styCnt3);
|
||||
System.out.println("setCellStyleProps nearing end");
|
||||
assertEquals("No additional styles should have been created", styCnt2, styCnt3);
|
||||
|
||||
wb.close();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -171,6 +178,11 @@ public class BaseTestCellUtil {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated by {@link #setAlignmentEnum()}
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void setAlignment() throws IOException {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
@ -188,7 +200,7 @@ public class BaseTestCellUtil {
|
||||
assertEquals(CellStyle.ALIGN_GENERAL, B1.getCellStyle().getAlignment());
|
||||
|
||||
// get/set alignment modifies the cell's style
|
||||
CellUtil.setAlignment(A1, CellStyle.ALIGN_RIGHT);
|
||||
CellUtil.setAlignment(A1, null, CellStyle.ALIGN_RIGHT);
|
||||
assertEquals(CellStyle.ALIGN_RIGHT, A1.getCellStyle().getAlignment());
|
||||
|
||||
// get/set alignment doesn't affect the style of cells with
|
||||
@ -199,6 +211,62 @@ public class BaseTestCellUtil {
|
||||
wb.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setAlignmentEnum() throws IOException {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
Sheet sh = wb.createSheet();
|
||||
Row row = sh.createRow(0);
|
||||
Cell A1 = row.createCell(0);
|
||||
Cell B1 = row.createCell(1);
|
||||
|
||||
// Assumptions
|
||||
assertEquals(A1.getCellStyle(), B1.getCellStyle());
|
||||
// should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call.
|
||||
// HSSFCellStyle wraps an underlying style record, and the underlying
|
||||
// style record is the same between multiple getCellStyle() calls.
|
||||
assertEquals(HorizontalAlignment.GENERAL, A1.getCellStyle().getAlignmentEnum());
|
||||
assertEquals(HorizontalAlignment.GENERAL, B1.getCellStyle().getAlignmentEnum());
|
||||
|
||||
// get/set alignment modifies the cell's style
|
||||
CellUtil.setAlignment(A1, HorizontalAlignment.RIGHT);
|
||||
assertEquals(HorizontalAlignment.RIGHT, A1.getCellStyle().getAlignmentEnum());
|
||||
|
||||
// get/set alignment doesn't affect the style of cells with
|
||||
// the same style prior to modifying the style
|
||||
assertNotEquals(A1.getCellStyle(), B1.getCellStyle());
|
||||
assertEquals(HorizontalAlignment.GENERAL, B1.getCellStyle().getAlignmentEnum());
|
||||
|
||||
wb.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setVerticalAlignmentEnum() throws IOException {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
Sheet sh = wb.createSheet();
|
||||
Row row = sh.createRow(0);
|
||||
Cell A1 = row.createCell(0);
|
||||
Cell B1 = row.createCell(1);
|
||||
|
||||
// Assumptions
|
||||
assertEquals(A1.getCellStyle(), B1.getCellStyle());
|
||||
// should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call.
|
||||
// HSSFCellStyle wraps an underlying style record, and the underlying
|
||||
// style record is the same between multiple getCellStyle() calls.
|
||||
assertEquals(VerticalAlignment.BOTTOM, A1.getCellStyle().getVerticalAlignmentEnum());
|
||||
assertEquals(VerticalAlignment.BOTTOM, B1.getCellStyle().getVerticalAlignmentEnum());
|
||||
|
||||
// get/set alignment modifies the cell's style
|
||||
CellUtil.setVerticalAlignment(A1, VerticalAlignment.TOP);
|
||||
assertEquals(VerticalAlignment.TOP, A1.getCellStyle().getVerticalAlignmentEnum());
|
||||
|
||||
// get/set alignment doesn't affect the style of cells with
|
||||
// the same style prior to modifying the style
|
||||
assertNotEquals(A1.getCellStyle(), B1.getCellStyle());
|
||||
assertEquals(VerticalAlignment.BOTTOM, B1.getCellStyle().getVerticalAlignmentEnum());
|
||||
|
||||
wb.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setFont() throws IOException {
|
||||
Workbook wb = _testDataProvider.createWorkbook();
|
||||
@ -263,6 +331,11 @@ public class BaseTestCellUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* bug 55555
|
||||
* @deprecated Replaced by {@link #setFillForegroundColorBeforeFillBackgroundColorEnum()}
|
||||
* @since POI 3.15 beta 3
|
||||
*/
|
||||
// bug 55555
|
||||
@Test
|
||||
public void setFillForegroundColorBeforeFillBackgroundColor() {
|
||||
@ -281,4 +354,25 @@ public class BaseTestCellUtil {
|
||||
assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor()));
|
||||
assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor()));
|
||||
}
|
||||
/**
|
||||
* bug 55555
|
||||
* @since POI 3.15 beta 3
|
||||
*/
|
||||
@Test
|
||||
public void setFillForegroundColorBeforeFillBackgroundColorEnum() {
|
||||
Workbook wb1 = _testDataProvider.createWorkbook();
|
||||
Cell A1 = wb1.createSheet().createRow(0).createCell(0);
|
||||
Map<String, Object> properties = new HashMap<String, Object>();
|
||||
// FIXME: Use FillPatternType.BRICKS enum
|
||||
properties.put(CellUtil.FILL_PATTERN, FillPatternType.BRICKS);
|
||||
properties.put(CellUtil.FILL_FOREGROUND_COLOR, IndexedColors.BLUE.index);
|
||||
properties.put(CellUtil.FILL_BACKGROUND_COLOR, IndexedColors.RED.index);
|
||||
|
||||
CellUtil.setCellStyleProperties(A1, properties);
|
||||
CellStyle style = A1.getCellStyle();
|
||||
// FIXME: Use FillPatternType.BRICKS enum
|
||||
assertEquals("fill pattern", FillPatternType.BRICKS, style.getFillPatternEnum());
|
||||
assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor()));
|
||||
assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor()));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user