1 19 package swingwtx.swing; 20 21 import java.util.*; 22 import swingwt.awt.*; 23 import swingwt.awt.event.*; 24 import swingwtx.swing.table.*; 25 import swingwtx.swing.tree.*; 26 import swingwtx.swing.event.*; 27 28 33 public class DefaultCellEditor extends AbstractCellEditor 34 implements TableCellEditor, TreeCellEditor { 35 36 37 protected JComponent editorComponent; 38 protected int clickCountToStart = 1; 39 40 protected SWTEditor handler = null; 41 42 public DefaultCellEditor(final JTextField textField) { 43 editorComponent = textField; 44 this.clickCountToStart = 2; 45 handler = new SWTEditor() { 46 public void setValue(Object value) { 47 textField.setText((value != null) ? value.toString() : ""); 48 } 49 public Object getCellEditorValue() { 50 return textField.getText(); 51 } 52 }; 53 textField.addFocusListener(handler); 54 } 55 56 public DefaultCellEditor(final JCheckBox checkBox) { 57 editorComponent = checkBox; 58 handler = new SWTEditor() { 59 public void setValue(Object value) { 60 boolean selected = false; 61 if (value instanceof Boolean ) { 62 selected = ((Boolean )value).booleanValue(); 63 } 64 else if (value instanceof String ) { 65 selected = value.equals("true"); 66 } 67 checkBox.setSelected(selected); 68 } 69 70 public Object getCellEditorValue() { 71 return new Boolean (checkBox.isSelected()); 72 } 73 }; 74 checkBox.addFocusListener(handler); 75 } 76 77 public DefaultCellEditor(final JComboBox comboBox) { 78 editorComponent = comboBox; 79 handler = new SWTEditor() { 80 public void setValue(Object value) { 81 comboBox.setSelectedItem(value); 82 } 83 84 public Object getCellEditorValue() { 85 return comboBox.getSelectedItem(); 86 } 87 88 public boolean shouldSelectCell(EventObject anEvent) { 89 return true; 90 } 91 public boolean stopCellEditing() { 92 return super.stopCellEditing(); 93 } 94 }; 95 comboBox.addChangeListener(handler); 96 } 97 98 public SWTEditor getHandler() { 99 return handler; 100 } 101 102 public Component getComponent() { 103 return editorComponent; 104 } 105 106 public void setClickCountToStart(int count) { 107 clickCountToStart = count; 108 } 109 110 public int getClickCountToStart() { 111 return clickCountToStart; 112 } 113 114 public Object getCellEditorValue() { 115 return handler.getCellEditorValue(); 116 } 117 118 public boolean isCellEditable(EventObject anEvent) { 119 return handler.isCellEditable(anEvent); 120 } 121 122 public boolean shouldSelectCell(EventObject anEvent) { 123 return handler.shouldSelectCell(anEvent); 124 } 125 126 public boolean stopCellEditing() { 127 return handler.stopCellEditing(); 128 } 129 130 public void cancelCellEditing() { 131 handler.cancelCellEditing(); 132 } 133 134 public Component getTreeCellEditorComponent(JTree tree, Object value, 135 boolean isSelected, 136 boolean expanded, 137 boolean leaf, int row) { 138 139 handler.setValue(value); 140 return editorComponent; 141 } 142 143 public Component getTableCellEditorComponent(JTable table, Object value, 144 boolean isSelected, 145 int row, int column) { 146 handler.setValue(value); 147 return editorComponent; 148 } 149 150 151 152 158 protected abstract class SWTEditor implements FocusListener, ChangeListener { 159 160 public abstract Object getCellEditorValue(); 161 162 public abstract void setValue(Object value); 163 164 public boolean isCellEditable(EventObject anEvent) { 165 if (anEvent instanceof MouseEvent) { 166 return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart; 167 } 168 return true; 169 } 170 171 public boolean shouldSelectCell(EventObject anEvent) { 172 return true; 173 } 174 175 public boolean startCellEditing(EventObject anEvent) { 176 return true; 177 } 178 179 public boolean stopCellEditing() { 180 fireEditingStopped(); editorComponent.getPeer().dispose(); return true; 183 } 184 185 public void cancelCellEditing() { 186 fireEditingCanceled(); 187 } 188 189 public void focusGained(FocusEvent e) { 190 } 191 192 public void focusLost(FocusEvent e) { 193 stopCellEditing(); 194 } 195 196 public void stateChanged(ChangeEvent e) { 197 stopCellEditing(); 198 } 199 200 } 201 202 } 203 | Popular Tags |