KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SDefaultCellEditor


1 /*
2  * $Id: SDefaultCellEditor.java,v 1.4 2005/05/12 15:09:01 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import org.wings.session.SessionManager;
17 import org.wings.table.STableCellEditor;
18
19 import javax.swing.event.CellEditorListener JavaDoc;
20 import javax.swing.event.ChangeEvent JavaDoc;
21 import javax.swing.event.EventListenerList JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.util.EventObject JavaDoc;
25
26 /**
27  * A default Table cell Editor. In order to see the graphics, you need the
28  * Java look and feel graphics (jlfgr*.jar)
29  *
30  * @author <a HREF="mailto:engels@mercatis.de">Holger Engels</a>
31  * @version $Revision: 1.4 $
32  */

33 public class SDefaultCellEditor
34         implements STableCellEditor {
35     /**
36      * The default ok button icon.
37      */

38     private static final SIcon OK_BUTTON_ICON = (SIcon) SessionManager.getSession()
39     .getCGManager().getObject("SDefaultCellEditor.okIcon", SIcon.class);
40
41     /**
42      * The default cancel button icon.
43      */

44     private static final SIcon CANCEL_BUTTON_ICON = (SIcon) SessionManager.getSession()
45     .getCGManager().getObject("SDefaultCellEditor.cancelIcon", SIcon.class);
46
47     /**
48      * Label for displaying (error)-messages. It is unvisible, until a message
49      * is set.
50      */

51     protected final SLabel messageLabel;
52
53     /**
54      * Panel for edit fields.
55      */

56     protected final SPanel editorPanel;
57
58     /**
59      * If this button is pressed, editing is tried to stop. If input validation
60      * found no error, editing is stopped, else an error message is displayed
61      */

62     protected final SButton ok;
63
64     /**
65      * If this button is pressed, editing is canceled.
66      */

67     protected final SButton cancel;
68
69     /**
70      * Store here the CellEditorListener
71      */

72     protected final EventListenerList JavaDoc listenerList;
73
74     /**
75      * Event listener, which set the fire... indicators. This event listener is
76      * added to the three buttons {@link #ok}, {@link #cancel} and {@link #undo}
77      * and the {@link #editorFrom form}
78      */

79     private final ActionListener JavaDoc fireEventListener = new ActionListener JavaDoc() {
80         public void actionPerformed(ActionEvent JavaDoc e) {
81             if (e.getSource() == ok) {
82                 stopCellEditing();
83             } else if (e.getSource() == cancel) {
84                 cancelCellEditing();
85             }
86         }
87     };
88
89     /**
90      * Fast edit support is editing with reduced interaction. E.g. a boolean
91      * value can only have to states, true or false. So if editing is started,
92      * the editor just flips the state and fires editing stopped.
93      */

94     private boolean fastEditSupport = true;
95
96     protected EditorDelegate delegate;
97
98     protected SComponent editorComponent;
99
100     /**
101      * Initialize the DefaultCellEditor with an editor component (like an text
102      * field for instance). After calling this constructor, the
103      * {@link EditorDelegate}, that links the CellEditor and the
104      * editorComponent has to be passed to the delegate instance variable.
105      *
106      * @param editorComponent the component used
107      * @param initializeButtons flag to indicate if the button texts and icons
108      * should be initialized.
109      */

110     protected SDefaultCellEditor(SComponent editorComponent,
111                                  boolean initializeButtons) {
112         this.messageLabel = new SLabel();
113         this.editorPanel = new SPanel(new SFlowLayout());
114         this.ok = new SButton();
115         this.cancel = new SButton();
116         this.listenerList = new EventListenerList JavaDoc();
117         this.editorComponent = editorComponent;
118
119         editorPanel.add(messageLabel);
120         editorPanel.add(editorComponent);
121         if (initializeButtons) {
122             initButtons();
123         }
124     }
125
126     /**
127      * Constructs a DefaultCellEditor that uses a text field.
128      *
129      * @param x a STextField object ...
130      */

131     public SDefaultCellEditor(STextField x) {
132         this(x, true);
133         this.delegate = new EditorDelegate() {
134             public void setValue(Object JavaDoc v) {
135                 super.setValue(v);
136                 ((STextField) editorComponent).setText(v != null ? v.toString() : null);
137             }
138
139             public Object JavaDoc getCellEditorValue() {
140                 String JavaDoc text = ((STextField) editorComponent).getText();
141                 return "".equals(text) ? null : text;
142             }
143
144             public boolean stopCellEditing() {
145                 return true;
146             }
147
148             public boolean shouldSelectCell(EventObject JavaDoc anEvent) {
149                 return true;
150             }
151         };
152     }
153
154     /**
155      * Constructs a DefaultCellEditor object that uses a check box.
156      *
157      * @param x a SCheckBox object ...
158      */

159     public SDefaultCellEditor(SCheckBox x) {
160         this(x, true);
161         this.delegate = new EditorDelegate() {
162             public void setValue(Object JavaDoc v) {
163                 // Try my best to do the right thing with v
164
boolean bool;
165                 if (v instanceof Boolean JavaDoc) {
166                     bool = ((Boolean JavaDoc) v).booleanValue();
167                 } else if (v instanceof String JavaDoc) {
168                     Boolean JavaDoc b = Boolean.valueOf((String JavaDoc) v);
169                     bool = b.booleanValue();
170                 } else {
171                     bool = false;
172                 }
173
174                 if (fastEditSupport) {
175                     ((SCheckBox) editorComponent).setSelected(!bool);
176                     SDefaultCellEditor.this.stopCellEditing();
177                 } else {
178                     ((SCheckBox) editorComponent).setSelected(bool);
179                 }
180             }
181
182             public Object JavaDoc getCellEditorValue() {
183                 return Boolean.valueOf(((SCheckBox) editorComponent).isSelected());
184             }
185
186             public boolean stopCellEditing() {
187                 return true;
188             }
189
190             public boolean shouldSelectCell(EventObject JavaDoc anEvent) {
191                 return false;
192             }
193         };
194     }
195
196     /**
197      * Intializes the buttons with default icons, tooltip text and listener.
198      */

199     protected void initButtons() {
200         ok.addActionListener(fireEventListener);
201         ok.setIcon(OK_BUTTON_ICON);
202         ok.setToolTipText("ok");
203
204         cancel.addActionListener(fireEventListener);
205         cancel.setIcon(CANCEL_BUTTON_ICON);
206         cancel.setToolTipText("cancel");
207
208         editorPanel.add(ok);
209         editorPanel.add(cancel);
210     }
211
212     /**
213      * Returns a reference to the editor component.
214      *
215      * @return the editor Component
216      */

217     public final SComponent getComponent() {
218         return editorComponent;
219     }
220
221     public final SButton getOKButton() {
222         return ok;
223     }
224
225     public final SButton getCancelButton() {
226         return cancel;
227     }
228
229     /**
230      * Fast edit support is editing with reduced interaction. E.g. a boolean
231      * value can only have to states, true or false. So if editing is started,
232      * the editor just flips the state and fires editing stopped.
233      *
234      * @param b a <code>boolean</code> value
235      */

236     public final void setFastEdit(boolean b) {
237         fastEditSupport = b;
238     }
239
240     /**
241      * Return if fast edit is activated.
242      *
243      * @return a <code>boolean</code> value
244      * @see #setFastEdit
245      */

246     public final boolean getFastEdit() {
247         return fastEditSupport;
248     }
249
250     public Object JavaDoc getCellEditorValue() {
251         return delegate.getCellEditorValue();
252     }
253
254     public boolean isCellEditable(EventObject JavaDoc anEvent) {
255         return delegate.isCellEditable(anEvent);
256     }
257
258     public boolean shouldSelectCell(EventObject JavaDoc anEvent) {
259         return delegate.shouldSelectCell(anEvent);
260     }
261
262     public boolean stopCellEditing() {
263         if (delegate.stopCellEditing()) {
264             fireEditingStopped();
265             return true;
266         }
267
268         return false;
269     }
270
271     public void cancelCellEditing() {
272         delegate.cancelCellEditing();
273         fireEditingCanceled();
274     }
275
276     public void addCellEditorListener(CellEditorListener JavaDoc l) {
277         listenerList.add(CellEditorListener JavaDoc.class, l);
278     }
279
280     public void removeCellEditorListener(CellEditorListener JavaDoc l) {
281         listenerList.remove(CellEditorListener JavaDoc.class, l);
282     }
283
284     private ChangeEvent JavaDoc changeEvent = null;
285
286     /*
287      * Notify all listeners that have registered interest for
288      * notification on this event type. The event instance
289      * is lazily created using the parameters passed into
290      * the fire method.
291      * @see EventListenerList
292      */

293     protected void fireEditingStopped() {
294         Object JavaDoc[] listeners = listenerList.getListenerList();
295         for (int i = listeners.length - 2; i >= 0; i -= 2) {
296             if (listeners[i] == CellEditorListener JavaDoc.class) {
297                 if (changeEvent == null)
298                     changeEvent = new ChangeEvent JavaDoc(this);
299                 ((CellEditorListener JavaDoc) listeners[i + 1]).editingStopped(changeEvent);
300             }
301         }
302     }
303
304
305     /*
306      * Notify all listeners that have registered interest for
307      * notification on this event type. The event instance
308      * is lazily created using the parameters passed into
309      * the fire method.
310      * @see EventListenerList
311      */

312     protected void fireEditingCanceled() {
313         Object JavaDoc[] listeners = listenerList.getListenerList();
314         for (int i = listeners.length - 2; i >= 0; i -= 2) {
315             if (listeners[i] == CellEditorListener JavaDoc.class) {
316                 if (changeEvent == null)
317                     changeEvent = new ChangeEvent JavaDoc(this);
318                 ((CellEditorListener JavaDoc) listeners[i + 1]).editingCanceled(changeEvent);
319             }
320         }
321     }
322
323     public SComponent getTreeCellEditorComponent(STree tree, Object JavaDoc value,
324                                                  boolean isSelected,
325                                                  boolean expanded,
326                                                  boolean leaf, int row) {
327
328         delegate.setValue(value);
329         return editorPanel;
330     }
331
332     public SComponent getTableCellEditorComponent(STable table, Object JavaDoc value,
333                                                   boolean isSelected,
334                                                   int row, int column) {
335         delegate.setValue(value);
336
337         return editorPanel;
338     }
339
340
341     //
342
// Protected EditorDelegate class
343
//
344

345     /**
346      * The interface all editing boils down to: setting the value for
347      * the editor and retrieve its value.
348      */

349     protected class EditorDelegate {
350         protected Object JavaDoc value;
351
352         /**
353          * Retrieve the value from the component used as Editor.
354          * This method is called by the CellEditor to retrieve the
355          * value after editing.
356          *
357          * @return the value managed by the Editor.
358          */

359         public Object JavaDoc getCellEditorValue() {
360             return value;
361         }
362
363         /**
364          * Set the Editors value. The task of this method is to
365          * pass the value to the editor component so that editing
366          * can be started.
367          *
368          * @param x the value to be edited.
369          */

370         public void setValue(Object JavaDoc x) {
371             this.value = x;
372         }
373
374         public boolean isCellEditable(EventObject JavaDoc anEvent) {
375             return true;
376         }
377
378         public boolean stopCellEditing() {
379             return true;
380         }
381
382         public void cancelCellEditing() {
383         }
384
385         public boolean shouldSelectCell(EventObject JavaDoc anEvent) {
386             return true;
387         }
388     }
389 }
390
Popular Tags