1 7 8 package javax.swing; 9 10 import java.awt.Component ; 11 import java.awt.event.*; 12 import java.awt.AWTEvent ; 13 import java.lang.Boolean ; 14 import javax.swing.table.*; 15 import javax.swing.event.*; 16 import java.util.EventObject ; 17 import javax.swing.tree.*; 18 import java.io.Serializable ; 19 import static com.sun.java.swing.SwingUtilities2.DRAG_FIX; 20 21 37 38 public class DefaultCellEditor extends AbstractCellEditor 39 implements TableCellEditor, TreeCellEditor { 40 41 45 46 protected JComponent editorComponent; 47 51 protected EditorDelegate delegate; 52 57 protected int clickCountToStart = 1; 58 59 63 68 public DefaultCellEditor(final JTextField textField) { 69 editorComponent = textField; 70 this.clickCountToStart = 2; 71 delegate = new EditorDelegate() { 72 public void setValue(Object value) { 73 textField.setText((value != null) ? value.toString() : ""); 74 } 75 76 public Object getCellEditorValue() { 77 return textField.getText(); 78 } 79 }; 80 textField.addActionListener(delegate); 81 } 82 83 88 public DefaultCellEditor(final JCheckBox checkBox) { 89 editorComponent = checkBox; 90 delegate = new EditorDelegate() { 91 public void setValue(Object value) { 92 boolean selected = false; 93 if (value instanceof Boolean ) { 94 selected = ((Boolean )value).booleanValue(); 95 } 96 else if (value instanceof String ) { 97 selected = value.equals("true"); 98 } 99 checkBox.setSelected(selected); 100 } 101 102 public Object getCellEditorValue() { 103 return Boolean.valueOf(checkBox.isSelected()); 104 } 105 }; 106 checkBox.addActionListener(delegate); 107 108 if (DRAG_FIX) { 109 checkBox.setRequestFocusEnabled(false); 110 } 111 } 112 113 119 public DefaultCellEditor(final JComboBox comboBox) { 120 editorComponent = comboBox; 121 comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); 122 delegate = new EditorDelegate() { 123 public void setValue(Object value) { 124 comboBox.setSelectedItem(value); 125 } 126 127 public Object getCellEditorValue() { 128 return comboBox.getSelectedItem(); 129 } 130 131 public boolean shouldSelectCell(EventObject anEvent) { 132 if (anEvent instanceof MouseEvent) { 133 MouseEvent e = (MouseEvent)anEvent; 134 return e.getID() != MouseEvent.MOUSE_DRAGGED; 135 } 136 return true; 137 } 138 public boolean stopCellEditing() { 139 if (comboBox.isEditable()) { 140 comboBox.actionPerformed(new ActionEvent( 142 DefaultCellEditor.this, 0, "")); 143 } 144 return super.stopCellEditing(); 145 } 146 }; 147 comboBox.addActionListener(delegate); 148 } 149 150 155 public Component getComponent() { 156 return editorComponent; 157 } 158 159 163 169 public void setClickCountToStart(int count) { 170 clickCountToStart = count; 171 } 172 173 177 public int getClickCountToStart() { 178 return clickCountToStart; 179 } 180 181 186 191 public Object getCellEditorValue() { 192 return delegate.getCellEditorValue(); 193 } 194 195 200 public boolean isCellEditable(EventObject anEvent) { 201 return delegate.isCellEditable(anEvent); 202 } 203 204 209 public boolean shouldSelectCell(EventObject anEvent) { 210 return delegate.shouldSelectCell(anEvent); 211 } 212 213 218 public boolean stopCellEditing() { 219 return delegate.stopCellEditing(); 220 } 221 222 227 public void cancelCellEditing() { 228 delegate.cancelCellEditing(); 229 } 230 231 235 236 public Component getTreeCellEditorComponent(JTree tree, Object value, 237 boolean isSelected, 238 boolean expanded, 239 boolean leaf, int row) { 240 String stringValue = tree.convertValueToText(value, isSelected, 241 expanded, leaf, row, false); 242 243 delegate.setValue(stringValue); 244 return editorComponent; 245 } 246 247 251 public Component getTableCellEditorComponent(JTable table, Object value, 252 boolean isSelected, 253 int row, int column) { 254 delegate.setValue(value); 255 return editorComponent; 256 } 257 258 259 263 266 protected class EditorDelegate implements ActionListener, ItemListener, Serializable { 267 268 269 protected Object value; 270 271 275 public Object getCellEditorValue() { 276 return value; 277 } 278 279 283 public void setValue(Object value) { 284 this.value = value; 285 } 286 287 298 public boolean isCellEditable(EventObject anEvent) { 299 if (anEvent instanceof MouseEvent) { 300 return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart; 301 } 302 return true; 303 } 304 305 313 public boolean shouldSelectCell(EventObject anEvent) { 314 return true; 315 } 316 317 322 public boolean startCellEditing(EventObject anEvent) { 323 return true; 324 } 325 326 333 public boolean stopCellEditing() { 334 fireEditingStopped(); 335 return true; 336 } 337 338 341 public void cancelCellEditing() { 342 fireEditingCanceled(); 343 } 344 345 350 public void actionPerformed(ActionEvent e) { 351 DefaultCellEditor.this.stopCellEditing(); 352 } 353 354 359 public void itemStateChanged(ItemEvent e) { 360 DefaultCellEditor.this.stopCellEditing(); 361 } 362 } 363 364 } | Popular Tags |