final Column[] columns = getColumns();
for (int i = 0; i < columns.length; i++) {
Object value = values[i];
if (value != null) {
int columnNumber = columns[i].getColumnNumber();
Cell cell = row.createCell(columnNumber);
// use a lazyref and the isFetched method to only create style
// if nescesary
LazyRef<CellStyle> cellStyle = new LazyRef<CellStyle>() {
@Override
protected CellStyle fetch() {
return getUpdateCallback().createCellStyle();
}
};
if (value instanceof Number) {
cell.setCellValue(((Number) value).doubleValue());
} else if (value instanceof Boolean) {
cell.setCellValue((Boolean) value);
} else if (value instanceof Date) {
cell.setCellValue((Date) value);
} else {
cell.setCellValue(value.toString());
}
Style style = styles[i];
if (style != null && !Style.NO_STYLE.equals(style)) {
LazyRef<Font> font = new LazyRef<Font>() {
@Override
protected Font fetch() {
return getUpdateCallback().createFont();
}
};
if (style.isBold()) {
font.get().setBoldweight(Font.BOLDWEIGHT_BOLD);
}
if (style.isItalic()) {
font.get().setItalic(true);
}
if (style.isUnderline()) {
font.get().setUnderline(Font.U_SINGLE);
}
if (style.getFontSize() != null) {
Integer fontSize = style.getFontSize();
SizeUnit sizeUnit = style.getFontSizeUnit();
if (sizeUnit == SizeUnit.PERCENT) {
fontSize = convertFontPercentageToPt(fontSize);
}
font.get().setFontHeightInPoints(fontSize.shortValue());
}
Color foregroundColor = style.getForegroundColor();
if (foregroundColor != null) {
short index = getUpdateCallback().getColorIndex(
foregroundColor);
font.get().setColor(index);
}
if (font.isFetched()) {
cellStyle.get().setFont(font.get());
}
if (style.getAlignment() != null) {
cellStyle.get().setAlignment(
getAlignment(style.getAlignment()));
}
final Color backgroundColor = style.getBackgroundColor();
if (backgroundColor != null) {
cellStyle.get().setFillPattern(
CellStyle.SOLID_FOREGROUND);
cellStyle.get().setFillForegroundColor(
getUpdateCallback().getColorIndex(
backgroundColor));
}
}
if (value instanceof Date) {
if (cellStyle.isFetched()) {
cellStyle.get().setDataFormat(
getUpdateCallback().getDateCellFormat());
} else {
cellStyle = new LazyRef<CellStyle>() {
@Override
protected CellStyle fetch() {
return getUpdateCallback().getDateCellStyle();
}
};
// trigger the fetch
cellStyle.get();
}
}
if (cellStyle.isFetched()) {
cell.setCellStyle(cellStyle.get());
}
}
}
}