KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > DefaultCellEditor


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4  
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7  
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9  
10    $Log: DefaultCellEditor.java,v $
11    Revision 1.2 2004/05/04 09:41:29 bobintetley
12    Removed Boolean.valueOf() - not supported prior to 1.4
13
14    Revision 1.1 2004/04/16 14:38:47 bobintetley
15    Table and Tree cell editor support
16
17  
18  */

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 /**
29  * Default editing class for JTree and JTable
30  *
31  * @author Robin Rawson-Tetley
32  */

33 public class DefaultCellEditor extends AbstractCellEditor
34                                implements TableCellEditor, TreeCellEditor {
35
36     /** The component being edited. */
37     protected JComponent editorComponent;
38     protected int clickCountToStart = 1;
39     /** The handling class */
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 JavaDoc value) {
47         textField.setText((value != null) ? value.toString() : "");
48             }
49         public Object JavaDoc 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 JavaDoc value) {
60                 boolean selected = false;
61         if (value instanceof Boolean JavaDoc) {
62             selected = ((Boolean JavaDoc)value).booleanValue();
63         }
64         else if (value instanceof String JavaDoc) {
65             selected = value.equals("true");
66         }
67         checkBox.setSelected(selected);
68             }
69
70         public Object JavaDoc getCellEditorValue() {
71         return new Boolean JavaDoc(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 JavaDoc value) {
81         comboBox.setSelectedItem(value);
82             }
83
84         public Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc value,
144                          boolean isSelected,
145                          int row, int column) {
146         handler.setValue(value);
147     return editorComponent;
148     }
149     
150     
151     
152     /**
153      * Handling class that maps details of the widget being
154      * used for editing. Different component types override
155      * the setValue/getCellEditorValue methods to return
156      * the correct stuff.
157      */

158     protected abstract class SWTEditor implements FocusListener, ChangeListener {
159
160         public abstract Object JavaDoc getCellEditorValue();
161
162         public abstract void setValue(Object JavaDoc 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(); // Tell listeners
181
editorComponent.getPeer().dispose(); // Destroy the editor component
182
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