1 2 17 18 package org.apache.poi.hssf.contrib.view; 19 20 import java.awt.*; 21 import java.awt.event.*; 22 import java.text.*; 23 import java.util.*; 24 25 import javax.swing.*; 26 import javax.swing.border.*; 27 import javax.swing.table.*; 28 29 import org.apache.poi.hssf.usermodel.*; 30 import org.apache.poi.hssf.util.HSSFColor; 31 32 39 public class SVTableCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener { 40 private static final Color black = getAWTColor(new HSSFColor.BLACK()); 41 private static final Color white = getAWTColor(new HSSFColor.WHITE()); 42 private Hashtable colors = HSSFColor.getIndexHash(); 43 44 45 private HSSFWorkbook wb; 46 private JTextField editor; 47 48 private HSSFCell editorValue; 49 50 51 public SVTableCellEditor(HSSFWorkbook wb) { 52 this.wb = wb; 53 this.editor = new JTextField(); 54 } 55 56 57 62 public boolean isCellEditable(java.util.EventObject e) { 63 if (e instanceof MouseEvent) { 64 return ((MouseEvent) e).getClickCount() >= 2; 65 } 66 return false; 67 } 68 69 70 public boolean shouldSelectCell(EventObject anEvent) { 71 return true; 72 } 73 74 75 public boolean startCellEditing(EventObject anEvent) { 76 System.out.println("Start Cell Editing"); 77 return true; 78 } 79 80 81 public boolean stopCellEditing() { 82 System.out.println("Stop Cell Editing"); 83 fireEditingStopped(); 84 return true; 85 } 86 87 88 public void cancelCellEditing() { 89 System.out.println("Cancel Cell Editing"); 90 fireEditingCanceled(); 91 } 92 93 94 public void actionPerformed(ActionEvent e) { 95 System.out.println("Action performed"); 96 stopCellEditing(); 97 } 98 99 100 105 public Object getCellEditorValue() { 106 System.out.println("GetCellEditorValue"); 107 return editor.getText(); 109 } 110 111 112 117 public Component getTableCellEditorComponent(JTable table, Object value, 118 boolean isSelected, 119 int row, 120 int column) { 121 System.out.println("GetTableCellEditorComponent"); 122 HSSFCell cell = (HSSFCell) value; 123 if (cell != null) { 124 HSSFCellStyle style = cell.getCellStyle(); 125 HSSFFont f = wb.getFontAt(style.getFontIndex()); 126 boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL; 127 boolean isitalics = f.getItalic(); 128 129 int fontstyle = Font.PLAIN; 130 131 if (isbold) fontstyle = Font.BOLD; 132 if (isitalics) fontstyle = fontstyle | Font.ITALIC; 133 134 int fontheight = f.getFontHeightInPoints(); 135 if (fontheight == 9) fontheight = 10; 137 Font font = new Font(f.getFontName(),fontstyle,fontheight); 138 editor.setFont(font); 139 140 if (style.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) { 141 editor.setBackground(getAWTColor(style.getFillForegroundColor(), white)); 142 } else editor.setBackground(white); 143 144 editor.setForeground(getAWTColor(f.getColor(), black)); 145 146 147 switch (cell.getCellType()) { 149 case HSSFCell.CELL_TYPE_BLANK: 150 editor.setText(""); 151 break; 152 case HSSFCell.CELL_TYPE_BOOLEAN: 153 if (cell.getBooleanCellValue()) { 154 editor.setText("true"); 155 } else { 156 editor.setText("false"); 157 } 158 break; 159 case HSSFCell.CELL_TYPE_NUMERIC: 160 editor.setText(Double.toString(cell.getNumericCellValue())); 161 break; 162 case HSSFCell.CELL_TYPE_STRING: 163 editor.setText(cell.getStringCellValue()); 164 break; 165 case HSSFCell.CELL_TYPE_FORMULA: 166 default: 167 editor.setText("?"); 168 } 169 switch (style.getAlignment()) { 170 case HSSFCellStyle.ALIGN_LEFT: 171 case HSSFCellStyle.ALIGN_JUSTIFY: 172 case HSSFCellStyle.ALIGN_FILL: 173 editor.setHorizontalAlignment(SwingConstants.LEFT); 174 break; 175 case HSSFCellStyle.ALIGN_CENTER: 176 case HSSFCellStyle.ALIGN_CENTER_SELECTION: 177 editor.setHorizontalAlignment(SwingConstants.CENTER); 178 break; 179 case HSSFCellStyle.ALIGN_GENERAL: 180 case HSSFCellStyle.ALIGN_RIGHT: 181 editor.setHorizontalAlignment(SwingConstants.RIGHT); 182 break; 183 default: 184 editor.setHorizontalAlignment(SwingConstants.LEFT); 185 break; 186 } 187 188 } 189 return editor; 190 } 191 192 195 private final Color getAWTColor(int index, Color deflt) { 196 HSSFColor clr = (HSSFColor)colors.get(new Integer (index)); 197 if (clr == null) return deflt; 198 return getAWTColor(clr); 199 } 200 201 private static final Color getAWTColor(HSSFColor clr) { 202 short[] rgb = clr.getTriplet(); 203 return new Color(rgb[0],rgb[1],rgb[2]); 204 } 205 206 } 207 | Popular Tags |