Changeset 7649
- Timestamp:
- Mar 14, 2019, 7:52:48 AM (4 years ago)
- Location:
- trunk/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/clients/web/net/sf/basedb/clients/web/plugins/XlsxTemplate.java
r7646 r7649 188 188 ev = ExcelValue.asString(ep.formatter.format(data)); 189 189 } 190 if (ev == null) ev = ExcelValue. EMPTY_CELL;190 if (ev == null) ev = ExcelValue.emptyCell(); 191 191 ev.writeTo(cell, styleCreator); 192 192 } -
trunk/src/core/net/sf/basedb/util/excel/ExcelValue.java
r7647 r7649 20 20 { 21 21 22 // Some static cells 23 private static final ExcelValue<Void> EMPTY_CELL = new BlankValue<>(); 24 private static final ExcelValue<Boolean> TRUE = new BooleanValue(true); 25 private static final ExcelValue<Boolean> FALSE = new BooleanValue(false); 26 22 27 /** 23 28 This creates a blank cell. 24 29 */ 25 public static final ExcelValue<Void> EMPTY_CELL = new BlankValue<>(); 30 @SuppressWarnings("unchecked") 31 public static final <T> ExcelValue<T> emptyCell() 32 { 33 return (ExcelValue<T>)EMPTY_CELL; 34 } 26 35 27 36 /** … … 30 39 public static ExcelValue<String> asString(String value) 31 40 { 41 if (value == null) return emptyCell(); 32 42 return new StringValue(value); 33 43 } … … 40 50 public static ExcelValue<Date> asDate(Date value, String javaFormat) 41 51 { 52 if (value == null) return emptyCell(); 42 53 if (javaFormat == null) javaFormat = "yyyy-MM-dd"; 43 54 return new DateValue(value, javaFormat); … … 53 64 public static ExcelValue<Date> asTimestamp(Date value, String javaFormat) 54 65 { 66 if (value == null) return emptyCell(); 55 67 if (javaFormat == null) javaFormat = "yyyy-MM-dd HH:mm:ss"; 56 68 return new DateValue(value, javaFormat); … … 64 76 public static final ExcelValue<Number> asInt(Number value) 65 77 { 78 if (value == null) return emptyCell(); 66 79 return new IntValue(value); 67 80 } … … 74 87 public static final ExcelValue<Number> asNumber(Number value, int numDecimals) 75 88 { 89 if (value == null) return emptyCell(); 76 90 return new NumericValue(value, numberFormat(numDecimals)); 77 91 } … … 82 96 public static final ExcelValue<Number> asNumber(Number value, String format) 83 97 { 98 if (value == null) return emptyCell(); 84 99 return new NumericValue(value, format); 100 } 101 102 /** 103 Create a numeric cell value with boolean format. 104 */ 105 public static final ExcelValue<Boolean> asBoolean(Boolean value) 106 { 107 if (value == null) return emptyCell(); 108 return value ? TRUE : FALSE; 85 109 } 86 110 … … 206 230 public void writeTo(Cell cell, CellStyleCreator styleCreator) 207 231 { 208 if (value == null) 209 { 210 EMPTY_CELL.writeTo(cell, styleCreator); 211 } 212 else 213 { 214 cell.setCellValue(value.doubleValue()); 215 } 232 cell.setCellValue(value.doubleValue()); 216 233 cell.setCellStyle(styleCreator.getIntCellStyle()); 217 } 218 219 } 220 234 } 235 } 221 236 222 237 /** … … 234 249 public void writeTo(Cell cell, CellStyleCreator styleCreator) 235 250 { 236 if (value == null) 237 { 238 EMPTY_CELL.writeTo(cell, styleCreator); 239 } 240 else 241 { 242 cell.setCellValue(value.doubleValue()); 243 } 251 cell.setCellValue(value.doubleValue()); 244 252 if (format != null) 245 253 { … … 249 257 } 250 258 259 /** 260 Represents a cell with a numeric value. 261 */ 262 static class BooleanValue 263 extends ExcelValue<Boolean> 264 { 265 BooleanValue(boolean value) 266 { 267 super(value, "BOOLEAN"); 268 } 269 270 @Override 271 public void writeTo(Cell cell, CellStyleCreator styleCreator) 272 { 273 cell.setCellValue(value ? 1 : 0); 274 cell.setCellStyle(styleCreator.getCellStyle(format)); 275 } 276 } 277 251 278 } -
trunk/src/core/net/sf/basedb/util/formatter/BooleanFormatter.java
r6127 r7649 23 23 24 24 import net.sf.basedb.util.Values; 25 import net.sf.basedb.util.excel.ExcelFormatter; 26 import net.sf.basedb.util.excel.ExcelValue; 25 27 26 28 /** 27 29 Formats a boolean value. It is possible to select between yes/no, true/false, and 1/0 28 mappings. 30 mappings. 29 31 30 32 @author nicklas … … 33 35 */ 34 36 public class BooleanFormatter 35 implements Formatter<Boolean> 37 implements Formatter<Boolean>, ExcelFormatter<Boolean, Boolean> 36 38 { 37 39 … … 63 65 // ------------------------------------------- 64 66 67 /** 68 @since 3.15 69 */ 70 @Override 71 public ExcelValue<Boolean> toExcelValue(Boolean value) 72 { 73 return ExcelValue.asBoolean(value); 74 } 75 65 76 /** 66 77 Enum for holding the various types of boolean output. … … 97 108 } 98 109 } 110 99 111 } -
trunk/src/core/net/sf/basedb/util/formatter/MultiFormatter.java
r6875 r7649 25 25 import java.util.Map; 26 26 27 import net.sf.basedb.util.excel.ExcelFormatter; 28 import net.sf.basedb.util.excel.ExcelValue; 29 27 30 28 31 /** … … 38 41 */ 39 42 public class MultiFormatter 40 implements Formatter<Object> 43 implements Formatter<Object>, ExcelFormatter<Object, Object> 41 44 { 42 45 … … 88 91 { 89 92 if (value == null) return ""; 93 Formatter<Object> f = getFormatter(value); 94 return f.format(value); 95 } 96 @Override 97 public Number parseString(String value) 98 { 99 throw new UnsupportedOperationException("parseString"); 100 } 101 // ------------------------------------------- 102 103 private Formatter<Object> getFormatter(Object value) 104 { 90 105 Class<?> c = value.getClass(); 91 106 Formatter<Object> f = formatter; … … 114 129 if (decideOnFirst) formatter = f; 115 130 } 116 return f .format(value);131 return f; 117 132 } 118 @Override 119 public Number parseString(String value) 120 { 121 throw new UnsupportedOperationException("parseString"); 122 } 123 // ------------------------------------------- 124 133 125 134 /** 126 135 Register a formatter for the specified class. … … 133 142 formatters.put((Class)clazz, (Formatter)formatter); 134 143 } 144 145 @SuppressWarnings({ "rawtypes", "unchecked" }) 146 @Override 147 public ExcelValue<Object> toExcelValue(Object value) 148 { 149 if (value == null) return null; 150 Formatter<Object> f = getFormatter(value); 151 ExcelValue ev = null; 152 if (f instanceof ExcelFormatter) 153 { 154 ExcelFormatter ef = (ExcelFormatter)f; 155 ev = ef.toExcelValue(value); 156 } 157 else 158 { 159 ev = ExcelValue.asString(f.format(value)); 160 } 161 return ev; 162 } 135 163 }
Note: See TracChangeset
for help on using the changeset viewer.