KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > DefaultCellEditor


1 /*
2  * @(#)DefaultCellEditor.java 1.52 05/08/09
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing;
9
10 import java.awt.Component JavaDoc;
11 import java.awt.event.*;
12 import java.awt.AWTEvent JavaDoc;
13 import java.lang.Boolean JavaDoc;
14 import javax.swing.table.*;
15 import javax.swing.event.*;
16 import java.util.EventObject JavaDoc;
17 import javax.swing.tree.*;
18 import java.io.Serializable JavaDoc;
19 import static com.sun.java.swing.SwingUtilities2.DRAG_FIX;
20
21 /**
22  * The default editor for table and tree cells.
23  * <p>
24  * <strong>Warning:</strong>
25  * Serialized objects of this class will not be compatible with
26  * future Swing releases. The current serialization support is
27  * appropriate for short term storage or RMI between applications running
28  * the same version of Swing. As of 1.4, support for long term storage
29  * of all JavaBeans<sup><font size="-2">TM</font></sup>
30  * has been added to the <code>java.beans</code> package.
31  * Please see {@link java.beans.XMLEncoder}.
32  *
33  * @version 1.52 08/09/05
34  * @author Alan Chung
35  * @author Philip Milne
36  */

37
38 public class DefaultCellEditor extends AbstractCellEditor JavaDoc
39     implements TableCellEditor, TreeCellEditor {
40
41 //
42
// Instance Variables
43
//
44

45     /** The Swing component being edited. */
46     protected JComponent JavaDoc editorComponent;
47     /**
48      * The delegate class which handles all methods sent from the
49      * <code>CellEditor</code>.
50      */

51     protected EditorDelegate delegate;
52     /**
53      * An integer specifying the number of clicks needed to start editing.
54      * Even if <code>clickCountToStart</code> is defined as zero, it
55      * will not initiate until a click occurs.
56      */

57     protected int clickCountToStart = 1;
58
59 //
60
// Constructors
61
//
62

63     /**
64      * Constructs a <code>DefaultCellEditor</code> that uses a text field.
65      *
66      * @param textField a <code>JTextField</code> object
67      */

68     public DefaultCellEditor(final JTextField JavaDoc textField) {
69         editorComponent = textField;
70     this.clickCountToStart = 2;
71         delegate = new EditorDelegate() {
72             public void setValue(Object JavaDoc value) {
73         textField.setText((value != null) ? value.toString() : "");
74             }
75
76         public Object JavaDoc getCellEditorValue() {
77         return textField.getText();
78         }
79         };
80     textField.addActionListener(delegate);
81     }
82
83     /**
84      * Constructs a <code>DefaultCellEditor</code> object that uses a check box.
85      *
86      * @param checkBox a <code>JCheckBox</code> object
87      */

88     public DefaultCellEditor(final JCheckBox JavaDoc checkBox) {
89         editorComponent = checkBox;
90         delegate = new EditorDelegate() {
91             public void setValue(Object JavaDoc value) {
92                 boolean selected = false;
93         if (value instanceof Boolean JavaDoc) {
94             selected = ((Boolean JavaDoc)value).booleanValue();
95         }
96         else if (value instanceof String JavaDoc) {
97             selected = value.equals("true");
98         }
99         checkBox.setSelected(selected);
100             }
101
102         public Object JavaDoc 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     /**
114      * Constructs a <code>DefaultCellEditor</code> object that uses a
115      * combo box.
116      *
117      * @param comboBox a <code>JComboBox</code> object
118      */

119     public DefaultCellEditor(final JComboBox JavaDoc comboBox) {
120         editorComponent = comboBox;
121     comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
122         delegate = new EditorDelegate() {
123         public void setValue(Object JavaDoc value) {
124         comboBox.setSelectedItem(value);
125             }
126
127         public Object JavaDoc getCellEditorValue() {
128         return comboBox.getSelectedItem();
129         }
130                 
131             public boolean shouldSelectCell(EventObject JavaDoc 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             // Commit edited value.
141
comboBox.actionPerformed(new ActionEvent(
142                      DefaultCellEditor.this, 0, ""));
143         }
144         return super.stopCellEditing();
145         }
146         };
147     comboBox.addActionListener(delegate);
148     }
149
150     /**
151      * Returns a reference to the editor component.
152      *
153      * @return the editor <code>Component</code>
154      */

155     public Component JavaDoc getComponent() {
156     return editorComponent;
157     }
158
159 //
160
// Modifying
161
//
162

163     /**
164      * Specifies the number of clicks needed to start editing.
165      *
166      * @param count an int specifying the number of clicks needed to start editing
167      * @see #getClickCountToStart
168      */

169     public void setClickCountToStart(int count) {
170     clickCountToStart = count;
171     }
172
173     /**
174      * Returns the number of clicks needed to start editing.
175      * @return the number of clicks needed to start editing
176      */

177     public int getClickCountToStart() {
178     return clickCountToStart;
179     }
180
181 //
182
// Override the implementations of the superclass, forwarding all methods
183
// from the CellEditor interface to our delegate.
184
//
185

186     /**
187      * Forwards the message from the <code>CellEditor</code> to
188      * the <code>delegate</code>.
189      * @see EditorDelegate#getCellEditorValue
190      */

191     public Object JavaDoc getCellEditorValue() {
192         return delegate.getCellEditorValue();
193     }
194
195     /**
196      * Forwards the message from the <code>CellEditor</code> to
197      * the <code>delegate</code>.
198      * @see EditorDelegate#isCellEditable(EventObject)
199      */

200     public boolean isCellEditable(EventObject JavaDoc anEvent) {
201     return delegate.isCellEditable(anEvent);
202     }
203     
204     /**
205      * Forwards the message from the <code>CellEditor</code> to
206      * the <code>delegate</code>.
207      * @see EditorDelegate#shouldSelectCell(EventObject)
208      */

209     public boolean shouldSelectCell(EventObject JavaDoc anEvent) {
210     return delegate.shouldSelectCell(anEvent);
211     }
212
213     /**
214      * Forwards the message from the <code>CellEditor</code> to
215      * the <code>delegate</code>.
216      * @see EditorDelegate#stopCellEditing
217      */

218     public boolean stopCellEditing() {
219     return delegate.stopCellEditing();
220     }
221
222     /**
223      * Forwards the message from the <code>CellEditor</code> to
224      * the <code>delegate</code>.
225      * @see EditorDelegate#cancelCellEditing
226      */

227     public void cancelCellEditing() {
228     delegate.cancelCellEditing();
229     }
230
231 //
232
// Implementing the TreeCellEditor Interface
233
//
234

235     /** Implements the <code>TreeCellEditor</code> interface. */
236     public Component JavaDoc getTreeCellEditorComponent(JTree JavaDoc tree, Object JavaDoc value,
237                         boolean isSelected,
238                         boolean expanded,
239                         boolean leaf, int row) {
240     String JavaDoc stringValue = tree.convertValueToText(value, isSelected,
241                         expanded, leaf, row, false);
242
243     delegate.setValue(stringValue);
244     return editorComponent;
245     }
246
247 //
248
// Implementing the CellEditor Interface
249
//
250
/** Implements the <code>TableCellEditor</code> interface. */
251     public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value,
252                          boolean isSelected,
253                          int row, int column) {
254         delegate.setValue(value);
255     return editorComponent;
256     }
257
258
259 //
260
// Protected EditorDelegate class
261
//
262

263     /**
264      * The protected <code>EditorDelegate</code> class.
265      */

266     protected class EditorDelegate implements ActionListener, ItemListener, Serializable JavaDoc {
267
268         /** The value of this cell. */
269         protected Object JavaDoc value;
270
271        /**
272         * Returns the value of this cell.
273         * @return the value of this cell
274         */

275         public Object JavaDoc getCellEditorValue() {
276             return value;
277         }
278
279        /**
280         * Sets the value of this cell.
281         * @param value the new value of this cell
282         */

283         public void setValue(Object JavaDoc value) {
284         this.value = value;
285     }
286
287        /**
288         * Returns true if <code>anEvent</code> is <b>not</b> a
289         * <code>MouseEvent</code>. Otherwise, it returns true
290         * if the necessary number of clicks have occurred, and
291         * returns false otherwise.
292         *
293         * @param anEvent the event
294         * @return true if cell is ready for editing, false otherwise
295         * @see #setClickCountToStart
296         * @see #shouldSelectCell
297         */

298         public boolean isCellEditable(EventObject JavaDoc anEvent) {
299         if (anEvent instanceof MouseEvent) {
300         return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
301         }
302         return true;
303     }
304         
305        /**
306         * Returns true to indicate that the editing cell may
307         * be selected.
308         *
309         * @param anEvent the event
310         * @return true
311         * @see #isCellEditable
312         */

313         public boolean shouldSelectCell(EventObject JavaDoc anEvent) {
314             return true;
315         }
316
317        /**
318         * Returns true to indicate that editing has begun.
319         *
320         * @param anEvent the event
321         */

322         public boolean startCellEditing(EventObject JavaDoc anEvent) {
323         return true;
324     }
325
326        /**
327         * Stops editing and
328         * returns true to indicate that editing has stopped.
329         * This method calls <code>fireEditingStopped</code>.
330         *
331         * @return true
332         */

333         public boolean stopCellEditing() {
334         fireEditingStopped();
335         return true;
336     }
337
338        /**
339         * Cancels editing. This method calls <code>fireEditingCanceled</code>.
340         */

341        public void cancelCellEditing() {
342        fireEditingCanceled();
343        }
344
345        /**
346         * When an action is performed, editing is ended.
347         * @param e the action event
348         * @see #stopCellEditing
349         */

350         public void actionPerformed(ActionEvent e) {
351             DefaultCellEditor.this.stopCellEditing();
352     }
353
354        /**
355         * When an item's state changes, editing is ended.
356         * @param e the action event
357         * @see #stopCellEditing
358         */

359         public void itemStateChanged(ItemEvent e) {
360         DefaultCellEditor.this.stopCellEditing();
361     }
362     }
363
364 } // End of class JCellEditor
365
Popular Tags