1 2 17 18 19 package org.apache.poi.hssf.contrib.view; 20 21 import java.util.Hashtable ; 22 23 import javax.swing.*; 24 import javax.swing.table.TableCellRenderer ; 25 import javax.swing.border.*; 26 27 import java.awt.Component ; 28 import java.awt.Color ; 29 import java.awt.Rectangle ; 30 import java.awt.Font ; 31 32 import java.io.Serializable ; 33 import java.text.*; 34 35 import org.apache.poi.hssf.usermodel.*; 36 import org.apache.poi.hssf.util.HSSFColor; 37 38 39 40 46 public class SVTableCellRenderer extends JLabel 47 implements TableCellRenderer , Serializable 48 { 49 protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); 50 protected SVBorder cellBorder = new SVBorder(); 51 52 53 private HSSFWorkbook wb = null; 54 55 57 private class CellFormatter { 58 private Format[] textFormatter; 59 60 private DecimalFormat generalNumberFormat = new DecimalFormat("0"); 61 62 public CellFormatter() { 63 textFormatter = new Format[0x31]; 64 65 textFormatter[0x01] = new DecimalFormat("0"); 66 textFormatter[0x02] = new DecimalFormat("0.00"); 67 textFormatter[0x03] = new DecimalFormat("#,##0"); 68 textFormatter[0x04] = new DecimalFormat("#,##0.00"); 69 textFormatter[0x05] = new DecimalFormat("$#,##0;$#,##0"); 70 textFormatter[0x06] = new DecimalFormat("$#,##0;$#,##0"); 71 textFormatter[0x07] = new DecimalFormat("$#,##0.00;$#,##0.00"); 72 textFormatter[0x08] = new DecimalFormat("$#,##0.00;$#,##0.00"); 73 textFormatter[0x09] = new DecimalFormat("0%"); 74 textFormatter[0x0A] = new DecimalFormat("0.00%"); 75 textFormatter[0x0B] = new DecimalFormat("0.00E0"); 76 textFormatter[0x0C] = new SVFractionalFormat("# ?/?"); 77 textFormatter[0x0D] = new SVFractionalFormat("# ??/??"); 78 textFormatter[0x0E] = new SimpleDateFormat("M/d/yy"); 79 textFormatter[0x0F] = new SimpleDateFormat("d-MMM-yy"); 80 textFormatter[0x10] = new SimpleDateFormat("d-MMM"); 81 textFormatter[0x11] = new SimpleDateFormat("MMM-yy"); 82 textFormatter[0x12] = new SimpleDateFormat("h:mm a"); 83 textFormatter[0x13] = new SimpleDateFormat("h:mm:ss a"); 84 textFormatter[0x14] = new SimpleDateFormat("h:mm"); 85 textFormatter[0x15] = new SimpleDateFormat("h:mm:ss"); 86 textFormatter[0x16] = new SimpleDateFormat("M/d/yy h:mm"); 87 textFormatter[0x26] = new DecimalFormat("#,##0;#,##0"); 92 textFormatter[0x27] = new DecimalFormat("#,##0.00;#,##0.00"); 95 textFormatter[0x28] = new DecimalFormat("#,##0.00;#,##0.00"); 96 textFormatter[0x2D] = new SimpleDateFormat("mm:ss"); 101 textFormatter[0x2F] = new SimpleDateFormat("mm:ss.0"); 103 textFormatter[0x30] = new DecimalFormat("##0.0E0"); 104 } 105 106 public String format(short index, Object value) { 107 if (index == 0) 108 return value.toString(); 109 if (textFormatter[index] == null) 110 throw new RuntimeException ("Sorry. I cant handle the format code :"+Integer.toHexString(index)); 111 return textFormatter[index].format(value); 112 } 113 114 public String format(short index, double value) { 115 if (index == 0) 116 return generalNumberFormat.format(value); 117 if (textFormatter[index] == null) 118 throw new RuntimeException ("Sorry. I cant handle the format code :"+Integer.toHexString(index)); 119 if (textFormatter[index] instanceof DecimalFormat) { 120 return ((DecimalFormat)textFormatter[index]).format(value); 121 } 122 if (textFormatter[index] instanceof SVFractionalFormat) { 123 return ((SVFractionalFormat)textFormatter[index]).format(value); 124 } 125 throw new RuntimeException ("Sorry. I cant handle a non decimal formatter for a decimal value :"+Integer.toHexString(index)); 126 } 127 128 public boolean useRedColor(short index, double value) { 129 return (((index == 0x06)||(index == 0x08)||(index == 0x26) || (index == 0x27)) && (value < 0)); 130 } 131 } 132 133 private final CellFormatter cellFormatter = new CellFormatter(); 134 135 public SVTableCellRenderer(HSSFWorkbook wb) { 136 super(); 137 setOpaque(true); 138 setBorder(noFocusBorder); 139 this.wb = wb; 140 } 141 142 public Component getTableCellRendererComponent(JTable table, Object value, 143 boolean isSelected, boolean hasFocus, int row, int column) { 144 boolean isBorderSet = false; 145 146 HSSFCell c = (HSSFCell)value; 149 150 if (c != null) { 151 HSSFCellStyle s = c.getCellStyle(); 152 HSSFFont f = wb.getFontAt(s.getFontIndex()); 153 setFont(SVTableUtils.makeFont(f)); 154 155 if (s.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) { 156 setBackground(SVTableUtils.getAWTColor(s.getFillForegroundColor(), SVTableUtils.white)); 157 } else setBackground(SVTableUtils.white); 158 159 setForeground(SVTableUtils.getAWTColor(f.getColor(), SVTableUtils.black)); 160 161 cellBorder.setBorder(SVTableUtils.getAWTColor(s.getTopBorderColor(), SVTableUtils.black), 162 SVTableUtils.getAWTColor(s.getRightBorderColor(), SVTableUtils.black), 163 SVTableUtils.getAWTColor(s.getBottomBorderColor(), SVTableUtils.black), 164 SVTableUtils.getAWTColor(s.getLeftBorderColor(), SVTableUtils.black), 165 s.getBorderTop(), s.getBorderRight(), 166 s.getBorderBottom(), s.getBorderLeft(), 167 hasFocus); 168 setBorder(cellBorder); 169 isBorderSet=true; 170 171 switch (c.getCellType()) { 173 case HSSFCell.CELL_TYPE_BLANK: 174 setValue(""); 175 break; 176 case HSSFCell.CELL_TYPE_BOOLEAN: 177 if (c.getBooleanCellValue()) { 178 setValue("true"); 179 } else { 180 setValue("false"); 181 } 182 break; 183 case HSSFCell.CELL_TYPE_NUMERIC: 184 short format = s.getDataFormat(); 185 double numericValue = c.getNumericCellValue(); 186 if (cellFormatter.useRedColor(format, numericValue)) 187 setForeground(Color.red); 188 else setForeground(null); 189 setValue(cellFormatter.format(format, c.getNumericCellValue())); 190 break; 191 case HSSFCell.CELL_TYPE_STRING: 192 setValue(c.getStringCellValue()); 193 break; 194 case HSSFCell.CELL_TYPE_FORMULA: 195 default: 196 setValue("?"); 197 } 198 switch (s.getAlignment()) { 200 case HSSFCellStyle.ALIGN_LEFT: 201 case HSSFCellStyle.ALIGN_JUSTIFY: 202 case HSSFCellStyle.ALIGN_FILL: 203 setHorizontalAlignment(SwingConstants.LEFT); 204 break; 205 case HSSFCellStyle.ALIGN_CENTER: 206 case HSSFCellStyle.ALIGN_CENTER_SELECTION: 207 setHorizontalAlignment(SwingConstants.CENTER); 208 break; 209 case HSSFCellStyle.ALIGN_GENERAL: 210 case HSSFCellStyle.ALIGN_RIGHT: 211 setHorizontalAlignment(SwingConstants.RIGHT); 212 break; 213 default: 214 setHorizontalAlignment(SwingConstants.LEFT); 215 break; 216 } 217 } else { 218 setValue(""); 219 setBackground(SVTableUtils.white); 220 } 221 222 223 if (hasFocus) { 224 if (!isBorderSet) { 225 cellBorder.setBorder(SVTableUtils.black, 228 SVTableUtils.black, 229 SVTableUtils.black, 230 SVTableUtils.black, 231 HSSFCellStyle.BORDER_NONE, 232 HSSFCellStyle.BORDER_NONE, 233 HSSFCellStyle.BORDER_NONE, 234 HSSFCellStyle.BORDER_NONE, 235 isSelected); 236 setBorder(cellBorder); 237 } 238 if (table.isCellEditable(row, column)) { 239 setForeground( UIManager.getColor("Table.focusCellForeground") ); 240 setBackground( UIManager.getColor("Table.focusCellBackground") ); 241 } 242 } else if (!isBorderSet) { 243 setBorder(noFocusBorder); 244 } 245 246 Color back = getBackground(); 248 boolean colorMatch = (back != null) && ( back.equals(table.getBackground()) ) && table.isOpaque(); 249 setOpaque(!colorMatch); 250 return this; 252 } 253 254 public void validate() {} 255 256 public void revalidate() {} 257 258 public void repaint(long tm, int x, int y, int width, int height) {} 259 260 public void repaint(Rectangle r) { } 261 262 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 263 if (propertyName=="text") { 265 super.firePropertyChange(propertyName, oldValue, newValue); 266 } 267 } 268 269 public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { } 270 271 275 protected void setValue(Object value) { 276 setText((value == null) ? "" : value.toString()); 277 } 278 } 279 | Popular Tags |