1 package com.explosion.datastream.exql.gui.table.editandrender;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.Component;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.awt.event.ItemEvent;
27 import java.awt.event.ItemListener;
28 import java.awt.event.MouseEvent;
29 import java.io.Serializable;
30 import java.util.EventObject;
31
32 import javax.swing.AbstractCellEditor;
33 import javax.swing.JComponent;
34 import javax.swing.JTable;
35 import javax.swing.JTextField;
36 import javax.swing.JTree;
37 import javax.swing.table.TableCellEditor;
38 import javax.swing.tree.TreeCellEditor;
39
40 /***
41 * This class gets around some fundamental hurdles
42 * not surmountable by extending the DefaultTableCellEditor
43 *
44 * The main factor is that the Delegate is protected in the original
45 */
46
47 public class DefaultDsProCellEditor extends AbstractCellEditor
48 implements TableCellEditor, TreeCellEditor {
49
50 /*** The Swing component being edited. */
51 protected JComponent editorComponent;
52 /***
53 * The delegate class which handles all methods sent from the
54 * <code>CellEditor</code>.
55 */
56 protected EditorDelegate delegate;
57 /***
58 * An integer specifying the number of clicks needed to start editing.
59 * Even if <code>clickCountToStart</code> is defined as zero, it
60 * will not initiate until a click occurs.
61 */
62 protected int clickCountToStart = 1;
63
64
65
66
67
68 /***
69 * Constructs a <code>DefaultDsProCellEditor</code> that uses a text field.
70 *
71 * @param textField a <code>JTextField</code> object
72 */
73 public DefaultDsProCellEditor(final JTextField textField) {
74 editorComponent = textField;
75 this.clickCountToStart = 2;
76 delegate = new EditorDelegate() {
77 public void setValue(Object value) {
78 textField.setText((value != null) ? value.toString() : "");
79 }
80
81 public Object getCellEditorValue() {
82 return textField.getText();
83 }
84 };
85 textField.addActionListener(delegate);
86 }
87
88 /***
89 * Returns a reference to the editor component.
90 *
91 * @return the editor <code>Component</code>
92 */
93 public Component getComponent() {
94 return editorComponent;
95 }
96
97
98
99
100
101 /***
102 * Specifies the number of clicks needed to start editing.
103 *
104 * @param count an int specifying the number of clicks needed to start editing
105 * @see #getClickCountToStart
106 */
107 public void setClickCountToStart(int count) {
108 clickCountToStart = count;
109 }
110
111 /***
112 * Returns the number of clicks needed to start editing.
113 * @return the number of clicks needed to start editing
114 */
115 public int getClickCountToStart() {
116 return clickCountToStart;
117 }
118
119
120
121
122
123
124 /***
125 * Forwards the message from the <code>CellEditor</code> to
126 * the <code>delegate</code>.
127 * @see EditorDelegate#getCellEditorValue
128 */
129 public Object getCellEditorValue() {
130 return delegate.getCellEditorValue();
131 }
132
133 /***
134 * Forwards the message from the <code>CellEditor</code> to
135 * the <code>delegate</code>.
136 * @see EditorDelegate#isCellEditable(EventObject)
137 */
138 public boolean isCellEditable(EventObject anEvent) {
139 return delegate.isCellEditable(anEvent);
140 }
141
142 /***
143 * Forwards the message from the <code>CellEditor</code> to
144 * the <code>delegate</code>.
145 * @see EditorDelegate#shouldSelectCell(EventObject)
146 */
147 public boolean shouldSelectCell(EventObject anEvent) {
148 return delegate.shouldSelectCell(anEvent);
149 }
150
151 /***
152 * Forwards the message from the <code>CellEditor</code> to
153 * the <code>delegate</code>.
154 * @see EditorDelegate#stopCellEditing
155 */
156 public boolean stopCellEditing() {
157 return delegate.stopCellEditing();
158 }
159
160 /***
161 * Forwards the message from the <code>CellEditor</code> to
162 * the <code>delegate</code>.
163 * @see EditorDelegate#cancelCellEditing
164 */
165 public void cancelCellEditing() {
166 delegate.cancelCellEditing();
167 }
168
169
170
171
172
173 /*** Implements the <code>TreeCellEditor</code> interface. */
174 public Component getTreeCellEditorComponent(JTree tree, Object value,
175 boolean isSelected,
176 boolean expanded,
177 boolean leaf, int row) {
178 String stringValue = tree.convertValueToText(value, isSelected,
179 expanded, leaf, row, false);
180
181 delegate.setValue(stringValue);
182 return editorComponent;
183 }
184
185
186
187
188 /*** Implements the <code>TableCellEditor</code> interface. */
189 public Component getTableCellEditorComponent(JTable table, Object value,
190 boolean isSelected,
191 int row, int column) {
192 delegate.setValue(value);
193 return editorComponent;
194 }
195
196
197
198
199
200
201 /***
202 * The protected <code>EditorDelegate</code> class.
203 */
204 protected class EditorDelegate implements ActionListener, ItemListener, Serializable {
205
206 /*** The value of this cell. */
207 protected Object value;
208
209 /***
210 * Returns the value of this cell.
211 * @return the value of this cell
212 */
213 public Object getCellEditorValue() {
214 return value;
215 }
216
217 /***
218 * Sets the value of this cell.
219 * @param value the new value of this cell
220 */
221 public void setValue(Object value) {
222 this.value = value;
223 }
224
225 /***
226 * Returns true if <code>anEvent</code> is <b>not</b> a
227 * <code>MouseEvent</code>. Otherwise, it returns true
228 * if the necessary number of clicks have occurred, and
229 * returns false otherwise.
230 *
231 * @param anEvent the event
232 * @return true if cell is ready for editing, false otherwise
233 * @see #setClickCountToStart
234 * @see #shouldSelectCell
235 */
236 public boolean isCellEditable(EventObject anEvent) {
237 if (anEvent instanceof MouseEvent) {
238 return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
239 }
240 return true;
241 }
242
243 /***
244 * Returns true to indicate that the editing cell may
245 * be selected.
246 *
247 * @param anEvent the event
248 * @return true
249 * @see #isCellEditable
250 */
251 public boolean shouldSelectCell(EventObject anEvent) {
252 return true;
253 }
254
255 /***
256 * Returns true to indicate that editing has begun.
257 *
258 * @param anEvent the event
259 */
260 public boolean startCellEditing(EventObject anEvent) {
261 return true;
262 }
263
264 /***
265 * Stops editing and
266 * returns true to indicate that editing has stopped.
267 * This method calls <code>fireEditingStopped</code>.
268 *
269 * @return true
270 */
271 public boolean stopCellEditing() {
272 fireEditingStopped();
273 return true;
274 }
275
276 /***
277 * Cancels editing. This method calls <code>fireEditingCanceled</code>.
278 */
279 public void cancelCellEditing() {
280 fireEditingCanceled();
281 }
282
283 /***
284 * When an action is performed, editing is ended.
285 * @param e the action event
286 * @see #stopCellEditing
287 */
288 public void actionPerformed(ActionEvent e) {
289 DefaultDsProCellEditor.this.stopCellEditing();
290 }
291
292 /***
293 * When an item's state changes, editing is ended.
294 * @param e the action event
295 * @see #stopCellEditing
296 */
297 public void itemStateChanged(ItemEvent e) {
298 DefaultDsProCellEditor.this.stopCellEditing();
299 }
300 }
301
302 }