KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > DjCellEditor


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.ui;
31
32 import java.awt.Component JavaDoc;
33 import java.awt.event.ActionEvent JavaDoc;
34 import java.awt.event.ActionListener JavaDoc;
35 import java.awt.event.ItemEvent JavaDoc;
36 import java.awt.event.ItemListener JavaDoc;
37 import java.awt.event.MouseEvent JavaDoc;
38 import java.io.Serializable JavaDoc;
39 import java.util.EventObject JavaDoc;
40
41 import javax.swing.AbstractCellEditor JavaDoc;
42 import javax.swing.JCheckBox JavaDoc;
43 import javax.swing.JComboBox JavaDoc;
44 import javax.swing.JComponent JavaDoc;
45 import javax.swing.JTable JavaDoc;
46 import javax.swing.JTextField JavaDoc;
47 import javax.swing.JTree JavaDoc;
48 import javax.swing.table.TableCellEditor JavaDoc;
49 import javax.swing.tree.TreeCellEditor JavaDoc;
50
51 import com.genimen.djeneric.repository.DjDomain;
52 import com.genimen.djeneric.repository.DjDomainValue;
53 import com.genimen.djeneric.tools.specifier.components.DjChooserField;
54 import com.genimen.djeneric.tools.specifier.components.DjDateChooser;
55 import com.genimen.djeneric.util.DjLogger;
56
57 /**
58  * Description of the Class
59  *
60  *@author Wido Riezebos
61  *@created November 5, 2001
62  */

63 public class DjCellEditor extends AbstractCellEditor JavaDoc implements TableCellEditor JavaDoc, TreeCellEditor JavaDoc
64 {
65
66   private static final long serialVersionUID = 1L;
67   protected JComponent JavaDoc editorComponent;
68   protected DjDomainValue[] domainValues = null;
69   protected EditorDelegate delegate;
70   protected int clickCountToStart = 2;
71
72   public DjCellEditor(final JTextField JavaDoc textField)
73   {
74     editorComponent = textField;
75     this.clickCountToStart = 2;
76     delegate = new EditorDelegate()
77     {
78       private static final long serialVersionUID = 1L;
79
80       public void setValue(Object JavaDoc value)
81       {
82         textField.setText((value != null) ? value.toString() : "");
83       }
84
85       public Object JavaDoc getCellEditorValue()
86       {
87         return textField.getText();
88       }
89     };
90     textField.removeActionListener(delegate);
91     textField.addActionListener(delegate);
92   }
93
94   public DjCellEditor(final DjChooserField chooser)
95   {
96     editorComponent = chooser;
97     this.clickCountToStart = 2;
98     delegate = new EditorDelegate()
99     {
100       private static final long serialVersionUID = 1L;
101
102       public void setValue(Object JavaDoc value)
103       {
104         chooser.setDisplayedValue(value);
105       }
106
107       public Object JavaDoc getCellEditorValue()
108       {
109         return chooser.getDisplayedValue();
110       }
111     };
112     chooser.removeActionListener(delegate);
113     chooser.addActionListener(delegate);
114   }
115
116   public DjCellEditor(final DjDateChooser datechooser)
117   {
118     editorComponent = datechooser;
119     this.clickCountToStart = 2;
120     delegate = new EditorDelegate()
121     {
122       private static final long serialVersionUID = 1L;
123
124       public void setValue(Object JavaDoc value)
125       {
126         try
127         {
128           datechooser.setDisplayedValue(value);
129         }
130         catch (Exception JavaDoc e)
131         {
132           DjLogger.log(e);
133         }
134       }
135
136       public Object JavaDoc getCellEditorValue()
137       {
138         try
139         {
140           return datechooser.getDisplayedValue();
141         }
142         catch (Exception JavaDoc e)
143         {
144           return null;
145         }
146       }
147     };
148     datechooser.removeActionListener(delegate);
149     datechooser.addActionListener(delegate);
150   }
151
152   public DjCellEditor(final JCheckBox JavaDoc checkBox)
153   {
154     setClickCountToStart(1);
155     editorComponent = checkBox;
156     delegate = new EditorDelegate()
157     {
158       private static final long serialVersionUID = 1L;
159
160       public void setValue(Object JavaDoc value)
161       {
162         boolean selected = false;
163         if (value instanceof Boolean JavaDoc)
164         {
165           selected = ((Boolean JavaDoc) value).booleanValue();
166         }
167         else if (value instanceof String JavaDoc)
168         {
169           selected = value.equals("true");
170         }
171         checkBox.setSelected(selected);
172       }
173
174       public Object JavaDoc getCellEditorValue()
175       {
176         return new Boolean JavaDoc(checkBox.isSelected());
177       }
178     };
179     checkBox.removeActionListener(delegate);
180     checkBox.addActionListener(delegate);
181   }
182
183   public DjCellEditor(final JCheckBox JavaDoc checkBox, DjDomain dom)
184   {
185     editorComponent = checkBox;
186     setClickCountToStart(1);
187     domainValues = dom.getValidValues();
188     delegate = new EditorDelegate()
189     {
190       private static final long serialVersionUID = 1L;
191
192       public void setValue(Object JavaDoc value)
193       {
194         boolean selected = false;
195         if (value instanceof Boolean JavaDoc)
196         {
197           selected = ((Boolean JavaDoc) value).booleanValue();
198         }
199         else if (value instanceof String JavaDoc)
200         {
201           selected = value.equals(domainValues[0].getDescription());
202         }
203         checkBox.setSelected(selected);
204       }
205
206       public Object JavaDoc getCellEditorValue()
207       {
208         if (checkBox.isSelected())
209         {
210           return domainValues[0].getDescription();
211         }
212         return domainValues[1].getDescription();
213       }
214     };
215     checkBox.removeActionListener(delegate);
216     checkBox.addActionListener(delegate);
217   }
218
219   public DjCellEditor(final JComboBox JavaDoc comboBox)
220   {
221     editorComponent = comboBox;
222
223     // Force up/down not to fire select events:
224
//comboBox.putClientProperty("JComboBox.lightweightKeyboardNavigation", "Lightweight");
225
delegate = new EditorDelegate()
226     {
227       private static final long serialVersionUID = 1L;
228
229       public void setValue(Object JavaDoc value)
230       {
231         if (comboBox.isEditable()) comboBox.getEditor().setItem(value);
232         else comboBox.setSelectedItem(value);
233       }
234
235       public Object JavaDoc getCellEditorValue()
236       {
237         if (comboBox.isEditable()) return comboBox.getEditor().getItem();
238         else return comboBox.getSelectedItem();
239       }
240
241       public boolean shouldSelectCell(EventObject JavaDoc anEvent)
242       {
243         if (anEvent instanceof MouseEvent JavaDoc)
244         {
245           MouseEvent JavaDoc e = (MouseEvent JavaDoc) anEvent;
246           return e.getID() != MouseEvent.MOUSE_DRAGGED;
247         }
248         return true;
249       }
250     };
251     comboBox.removeActionListener(delegate);
252     comboBox.addActionListener(delegate);
253   }
254
255   public Component JavaDoc getComponent()
256   {
257     return editorComponent;
258   }
259
260   /**
261    * Specifies the number of clicks needed to start editing.
262    *
263    *@param count an int specifying the number of clicks needed to start
264    * editing
265    *@see #getClickCountToStart
266    */

267   public void setClickCountToStart(int count)
268   {
269     clickCountToStart = count;
270   }
271
272   /**
273    * ClickCountToStart controls the number of clicks required to start editing.
274    *
275    *@return The clickCountToStart value
276    */

277   public int getClickCountToStart()
278   {
279     return clickCountToStart;
280   }
281
282   //
283
// Override the implementations of the superclass, forwarding all methods
284
// from the CellEditor interface to our delegate.
285
//
286

287   public Object JavaDoc getCellEditorValue()
288   {
289     return delegate.getCellEditorValue();
290   }
291
292   public boolean isCellEditable(EventObject JavaDoc anEvent)
293   {
294     return delegate.isCellEditable(anEvent);
295   }
296
297   public boolean shouldSelectCell(EventObject JavaDoc anEvent)
298   {
299     return delegate.shouldSelectCell(anEvent);
300   }
301
302   public boolean stopCellEditing()
303   {
304     return delegate.stopCellEditing();
305   }
306
307   public void cancelCellEditing()
308   {
309     delegate.cancelCellEditing();
310   }
311
312   //
313
// Implementing the TreeCellEditor Interface
314
//
315

316   public Component JavaDoc getTreeCellEditorComponent(JTree JavaDoc tree, Object JavaDoc value, boolean isSelected, boolean expanded,
317                                               boolean leaf, int row)
318   {
319     String JavaDoc stringValue = tree.convertValueToText(value, isSelected, expanded, leaf, row, false);
320
321     delegate.setValue(stringValue);
322     return editorComponent;
323   }
324
325   //
326
// Implementing the CellEditor Interface
327
//
328

329   public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row, int column)
330   {
331     delegate.setValue(value);
332     return editorComponent;
333   }
334
335   //
336
// Protected EditorDelegate class
337
//
338

339   protected class EditorDelegate implements ActionListener JavaDoc, ItemListener JavaDoc, Serializable JavaDoc
340   {
341
342     private static final long serialVersionUID = 1L;
343     protected Object JavaDoc value;
344
345     public Object JavaDoc getCellEditorValue()
346     {
347       return value;
348     }
349
350     public void setValue(Object JavaDoc value)
351     {
352       this.value = value;
353     }
354
355     public boolean isCellEditable(EventObject JavaDoc anEvent)
356     {
357       if (anEvent instanceof MouseEvent JavaDoc)
358       {
359         return ((MouseEvent JavaDoc) anEvent).getClickCount() >= clickCountToStart;
360       }
361       return true;
362     }
363
364     public boolean shouldSelectCell(EventObject JavaDoc anEvent)
365     {
366       return true;
367     }
368
369     public boolean startCellEditing(EventObject JavaDoc anEvent)
370     {
371       return true;
372     }
373
374     public boolean stopCellEditing()
375     {
376       fireEditingStopped();
377       return true;
378     }
379
380     public void cancelCellEditing()
381     {
382       fireEditingCanceled();
383     }
384
385     public void actionPerformed(ActionEvent JavaDoc e)
386     {
387       Component JavaDoc c = DjCellEditor.this.getComponent();
388
389       if (c instanceof JCheckBox JavaDoc || c instanceof DjChooserField) DjCellEditor.this.stopCellEditing();
390     }
391
392     public void itemStateChanged(ItemEvent JavaDoc e)
393     {
394       DjCellEditor.this.stopCellEditing();
395     }
396   }
397
398 }
Popular Tags