1 30 package com.genimen.djeneric.ui; 31 32 import java.awt.Component ; 33 import java.awt.event.ActionEvent ; 34 import java.awt.event.ActionListener ; 35 import java.awt.event.ItemEvent ; 36 import java.awt.event.ItemListener ; 37 import java.awt.event.MouseEvent ; 38 import java.io.Serializable ; 39 import java.util.EventObject ; 40 41 import javax.swing.AbstractCellEditor ; 42 import javax.swing.JCheckBox ; 43 import javax.swing.JComboBox ; 44 import javax.swing.JComponent ; 45 import javax.swing.JTable ; 46 import javax.swing.JTextField ; 47 import javax.swing.JTree ; 48 import javax.swing.table.TableCellEditor ; 49 import javax.swing.tree.TreeCellEditor ; 50 51 import com.genimen.djeneric.repository.DjDomain; 52 import com.genimen.djeneric.repository.DjDomainValue; 53 import com.genimen.djeneric.tools.specifier.components.DjChooserField; 54 import com.genimen.djeneric.tools.specifier.components.DjDateChooser; 55 import com.genimen.djeneric.util.DjLogger; 56 57 63 public class DjCellEditor extends AbstractCellEditor implements TableCellEditor , TreeCellEditor 64 { 65 66 private static final long serialVersionUID = 1L; 67 protected JComponent editorComponent; 68 protected DjDomainValue[] domainValues = null; 69 protected EditorDelegate delegate; 70 protected int clickCountToStart = 2; 71 72 public DjCellEditor(final JTextField textField) 73 { 74 editorComponent = textField; 75 this.clickCountToStart = 2; 76 delegate = new EditorDelegate() 77 { 78 private static final long serialVersionUID = 1L; 79 80 public void setValue(Object value) 81 { 82 textField.setText((value != null) ? value.toString() : ""); 83 } 84 85 public Object getCellEditorValue() 86 { 87 return textField.getText(); 88 } 89 }; 90 textField.removeActionListener(delegate); 91 textField.addActionListener(delegate); 92 } 93 94 public DjCellEditor(final DjChooserField chooser) 95 { 96 editorComponent = chooser; 97 this.clickCountToStart = 2; 98 delegate = new EditorDelegate() 99 { 100 private static final long serialVersionUID = 1L; 101 102 public void setValue(Object value) 103 { 104 chooser.setDisplayedValue(value); 105 } 106 107 public Object getCellEditorValue() 108 { 109 return chooser.getDisplayedValue(); 110 } 111 }; 112 chooser.removeActionListener(delegate); 113 chooser.addActionListener(delegate); 114 } 115 116 public DjCellEditor(final DjDateChooser datechooser) 117 { 118 editorComponent = datechooser; 119 this.clickCountToStart = 2; 120 delegate = new EditorDelegate() 121 { 122 private static final long serialVersionUID = 1L; 123 124 public void setValue(Object value) 125 { 126 try 127 { 128 datechooser.setDisplayedValue(value); 129 } 130 catch (Exception e) 131 { 132 DjLogger.log(e); 133 } 134 } 135 136 public Object getCellEditorValue() 137 { 138 try 139 { 140 return datechooser.getDisplayedValue(); 141 } 142 catch (Exception e) 143 { 144 return null; 145 } 146 } 147 }; 148 datechooser.removeActionListener(delegate); 149 datechooser.addActionListener(delegate); 150 } 151 152 public DjCellEditor(final JCheckBox checkBox) 153 { 154 setClickCountToStart(1); 155 editorComponent = checkBox; 156 delegate = new EditorDelegate() 157 { 158 private static final long serialVersionUID = 1L; 159 160 public void setValue(Object value) 161 { 162 boolean selected = false; 163 if (value instanceof Boolean ) 164 { 165 selected = ((Boolean ) value).booleanValue(); 166 } 167 else if (value instanceof String ) 168 { 169 selected = value.equals("true"); 170 } 171 checkBox.setSelected(selected); 172 } 173 174 public Object getCellEditorValue() 175 { 176 return new Boolean (checkBox.isSelected()); 177 } 178 }; 179 checkBox.removeActionListener(delegate); 180 checkBox.addActionListener(delegate); 181 } 182 183 public DjCellEditor(final JCheckBox checkBox, DjDomain dom) 184 { 185 editorComponent = checkBox; 186 setClickCountToStart(1); 187 domainValues = dom.getValidValues(); 188 delegate = new EditorDelegate() 189 { 190 private static final long serialVersionUID = 1L; 191 192 public void setValue(Object value) 193 { 194 boolean selected = false; 195 if (value instanceof Boolean ) 196 { 197 selected = ((Boolean ) value).booleanValue(); 198 } 199 else if (value instanceof String ) 200 { 201 selected = value.equals(domainValues[0].getDescription()); 202 } 203 checkBox.setSelected(selected); 204 } 205 206 public Object getCellEditorValue() 207 { 208 if (checkBox.isSelected()) 209 { 210 return domainValues[0].getDescription(); 211 } 212 return domainValues[1].getDescription(); 213 } 214 }; 215 checkBox.removeActionListener(delegate); 216 checkBox.addActionListener(delegate); 217 } 218 219 public DjCellEditor(final JComboBox comboBox) 220 { 221 editorComponent = comboBox; 222 223 delegate = new EditorDelegate() 226 { 227 private static final long serialVersionUID = 1L; 228 229 public void setValue(Object value) 230 { 231 if (comboBox.isEditable()) comboBox.getEditor().setItem(value); 232 else comboBox.setSelectedItem(value); 233 } 234 235 public Object getCellEditorValue() 236 { 237 if (comboBox.isEditable()) return comboBox.getEditor().getItem(); 238 else return comboBox.getSelectedItem(); 239 } 240 241 public boolean shouldSelectCell(EventObject anEvent) 242 { 243 if (anEvent instanceof MouseEvent ) 244 { 245 MouseEvent e = (MouseEvent ) anEvent; 246 return e.getID() != MouseEvent.MOUSE_DRAGGED; 247 } 248 return true; 249 } 250 }; 251 comboBox.removeActionListener(delegate); 252 comboBox.addActionListener(delegate); 253 } 254 255 public Component getComponent() 256 { 257 return editorComponent; 258 } 259 260 267 public void setClickCountToStart(int count) 268 { 269 clickCountToStart = count; 270 } 271 272 277 public int getClickCountToStart() 278 { 279 return clickCountToStart; 280 } 281 282 287 public Object getCellEditorValue() 288 { 289 return delegate.getCellEditorValue(); 290 } 291 292 public boolean isCellEditable(EventObject anEvent) 293 { 294 return delegate.isCellEditable(anEvent); 295 } 296 297 public boolean shouldSelectCell(EventObject anEvent) 298 { 299 return delegate.shouldSelectCell(anEvent); 300 } 301 302 public boolean stopCellEditing() 303 { 304 return delegate.stopCellEditing(); 305 } 306 307 public void cancelCellEditing() 308 { 309 delegate.cancelCellEditing(); 310 } 311 312 316 public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, 317 boolean leaf, int row) 318 { 319 String stringValue = tree.convertValueToText(value, isSelected, expanded, leaf, row, false); 320 321 delegate.setValue(stringValue); 322 return editorComponent; 323 } 324 325 329 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) 330 { 331 delegate.setValue(value); 332 return editorComponent; 333 } 334 335 339 protected class EditorDelegate implements ActionListener , ItemListener , Serializable 340 { 341 342 private static final long serialVersionUID = 1L; 343 protected Object value; 344 345 public Object getCellEditorValue() 346 { 347 return value; 348 } 349 350 public void setValue(Object value) 351 { 352 this.value = value; 353 } 354 355 public boolean isCellEditable(EventObject anEvent) 356 { 357 if (anEvent instanceof MouseEvent ) 358 { 359 return ((MouseEvent ) anEvent).getClickCount() >= clickCountToStart; 360 } 361 return true; 362 } 363 364 public boolean shouldSelectCell(EventObject anEvent) 365 { 366 return true; 367 } 368 369 public boolean startCellEditing(EventObject anEvent) 370 { 371 return true; 372 } 373 374 public boolean stopCellEditing() 375 { 376 fireEditingStopped(); 377 return true; 378 } 379 380 public void cancelCellEditing() 381 { 382 fireEditingCanceled(); 383 } 384 385 public void actionPerformed(ActionEvent e) 386 { 387 Component c = DjCellEditor.this.getComponent(); 388 389 if (c instanceof JCheckBox || c instanceof DjChooserField) DjCellEditor.this.stopCellEditing(); 390 } 391 392 public void itemStateChanged(ItemEvent e) 393 { 394 DjCellEditor.this.stopCellEditing(); 395 } 396 } 397 398 } | Popular Tags |