KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > client > ComboBoxTableCellEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 //***************** package ***********************************************
22

23 package org.netbeans.modules.web.monitor.client;
24
25
26 //***************** import ************************************************
27

28 import java.util.EventObject JavaDoc;
29
30 import java.awt.Point JavaDoc;
31 import java.awt.Rectangle JavaDoc;
32 import java.awt.Component JavaDoc;
33 import java.awt.BorderLayout JavaDoc;
34 import java.awt.IllegalComponentStateException JavaDoc;
35
36 import java.awt.event.MouseAdapter JavaDoc;
37 import java.awt.event.MouseListener JavaDoc;
38 import java.awt.event.MouseEvent JavaDoc;
39 import java.awt.event.ActionEvent JavaDoc;
40 import java.awt.event.ActionListener JavaDoc;
41
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.JTable JavaDoc;
44 import javax.swing.JComboBox JavaDoc;
45 import javax.swing.DefaultCellEditor JavaDoc;
46 import javax.swing.ListCellRenderer JavaDoc;
47 import javax.swing.SwingUtilities JavaDoc;
48
49 import javax.swing.table.TableCellEditor JavaDoc;
50
51 import javax.swing.event.ListSelectionEvent JavaDoc;
52 import javax.swing.event.ListSelectionListener JavaDoc;
53
54
55 //#########################################################################
56
/**
57  * This is a table cell editor with a combobox as the editing component.
58  * <br>
59  * This is the accessibility patch made due to an accessibility issue with
60  * ComboBoxes inside the JTable.
61  *
62  */

63 //#########################################################################
64

65 public class ComboBoxTableCellEditor
66        extends DefaultCellEditor JavaDoc
67        implements TableCellEditor JavaDoc
68 {
69
70     /**
71      * The surrounding panel for the label and the combobox.
72      */

73     private JPanel JavaDoc editor;
74
75     /**
76      * Listeners for the table added?
77      */

78     private boolean tableListenerAdded = false;
79
80     /**
81      * The table.
82      */

83     private JTable JavaDoc table;
84
85     /**
86      * To request the focus for the combobox (with SwingUtilities.invokeLater())
87      */

88     private Runnable JavaDoc comboBoxFocusRequester;
89
90     /**
91      * To popup the combobox (with SwingUtilities.invokeLater())
92      */

93     private Runnable JavaDoc comboBoxOpener;
94
95     /**
96      * The current row.
97      */

98     private int currentRow = -1;
99
100     /**
101      * The previously selected value in the editor.
102      */

103     private Object JavaDoc prevItem;
104
105     /**
106      * The initial value of the editor.
107      */

108     private Object JavaDoc initialValue;
109
110     /**
111      * React on action events on the combobox?
112      */

113     private boolean consumeComboBoxActionEvent = true;
114
115     /**
116      * The event that causes the editing to start. We need it to know
117      * if we should open the popup automatically.
118      */

119     private EventObject JavaDoc startEditingEvent = null;
120
121
122     /**********************************************************************
123      * Creates a new CellEditor.
124      *********************************************************************/

125
126     public ComboBoxTableCellEditor (Object JavaDoc [] values,
127         ListCellRenderer JavaDoc customRenderer)
128     {
129
130         super (new JComboBox JavaDoc ());
131
132         setItems (values);
133         this.editor = new JPanel JavaDoc (new BorderLayout JavaDoc ());
134         setClickCountToStart (1);
135
136         //show the combobox if the mouse clicks at the panel
137
this.editor.addMouseListener (new MouseAdapter JavaDoc ()
138             {
139                 public final void mousePressed (MouseEvent JavaDoc evt)
140                 {
141                     eventEditorMousePressed ();
142                 }
143             });
144
145         JComboBox JavaDoc cb = getComboBox ();
146
147         if (customRenderer != null)
148             cb.setRenderer(customRenderer);
149
150         cb.addActionListener (new ActionListener JavaDoc ()
151             {
152                 public final void actionPerformed (ActionEvent JavaDoc evt)
153                 {
154                     eventComboBoxActionPerformed ();
155                 }
156             });
157
158         this.comboBoxFocusRequester = new Runnable JavaDoc ()
159             {
160                 public final void run ()
161                 {
162                     getComboBox ().requestFocus ();
163                 }
164             };
165         this.comboBoxOpener = new Runnable JavaDoc ()
166             {
167                 public final void run ()
168                 {
169                     if (startEditingEvent instanceof MouseEvent JavaDoc)
170                     {
171                         try
172                         {
173                             getComboBox ().setPopupVisible (true);
174                         }
175                         catch (IllegalComponentStateException JavaDoc ex)
176                         {
177                             //silently ignore - seems to be a bug in JTable
178
}
179                     }
180                 }
181             };
182
183     }
184
185     public ComboBoxTableCellEditor (Object JavaDoc [] values)
186     {
187
188         this (values, null);
189
190     }
191
192     public ComboBoxTableCellEditor ()
193     {
194
195         this (new Object JavaDoc [0], null);
196
197     }
198
199
200     public ComboBoxTableCellEditor (ListCellRenderer JavaDoc customRenderer)
201     {
202
203         this (new Object JavaDoc [0], customRenderer);
204
205     }
206
207     /**********************************************************************
208      * Returns the renderer component of the cell.
209      *
210      * @interfaceMethod javax.swing.table.TableCellEditor
211      *********************************************************************/

212
213     public final Component JavaDoc getTableCellEditorComponent (JTable JavaDoc table,
214                                                         Object JavaDoc value,
215                                                         boolean selected,
216                                                         int row,
217                                                         int col)
218     {
219
220         //add a listener to the table
221
if ( ! this.tableListenerAdded)
222         {
223             this.tableListenerAdded = true;
224             this.table = table;
225             this.table.getSelectionModel ().addListSelectionListener (new ListSelectionListener JavaDoc ()
226             {
227                 public final void valueChanged (ListSelectionEvent JavaDoc evt)
228                 {
229                     eventTableSelectionChanged ();
230                 }
231             });
232         }
233         this.currentRow = row;
234         this.initialValue = value;
235
236         return getEditorComponent (table, value, selected, row, col);
237
238     }
239
240     protected Component JavaDoc getEditorComponent (JTable JavaDoc table,
241                                             Object JavaDoc value,
242                                             boolean selected,
243                                             int row,
244                                             int col)
245     {
246
247         setSelectedItem (value);
248
249         //new or old row?
250
selected = table.isRowSelected (row);
251         if (selected) //old row
252
{
253             SwingUtilities.invokeLater (this.comboBoxFocusRequester);
254             SwingUtilities.invokeLater (this.comboBoxOpener);
255             return getComboBox ();
256         }
257
258         //the user selected a new row
259
this.editor.removeAll (); //remove the combobox from the panel
260

261         return this.editor;
262
263     }
264
265
266     /**********************************************************************
267      * Is the cell editable? If the mouse was pressed at a margin
268      * we don't want the cell to be editable.
269      *
270      * @param evt The event-object.
271      *
272      * @interfaceMethod javax.swing.table.TableCellEditor
273      *********************************************************************/

274
275     public boolean isCellEditable (EventObject JavaDoc evt)
276     {
277
278         this.startEditingEvent = evt;
279         if (evt instanceof MouseEvent JavaDoc && evt.getSource () instanceof JTable JavaDoc)
280         {
281             MouseEvent JavaDoc me = (MouseEvent JavaDoc) evt;
282             JTable JavaDoc table = (JTable JavaDoc) me.getSource ();
283             Point JavaDoc pt = new Point JavaDoc (me.getX (), me.getY ());
284             int row = table.rowAtPoint (pt);
285             int col = table.columnAtPoint (pt);
286             Rectangle JavaDoc rec = table.getCellRect (row, col, false);
287             if (me.getY () >= rec.y + rec.height || me.getX () >= rec.x + rec.width)
288             {
289                 return false;
290             }
291         }
292
293         return super.isCellEditable (evt);
294
295     }
296
297     public Object JavaDoc getCellEditorValue ()
298     {
299
300         return prevItem ;
301
302     }
303
304     protected void setSelectedItem (Object JavaDoc item)
305     {
306
307         if (getComboBox ().getSelectedItem () != item)
308         {
309             this.consumeComboBoxActionEvent = false;
310             getComboBox ().setSelectedItem (item);
311             this.consumeComboBoxActionEvent = true;
312         }
313
314     }
315
316     public final void setItems (Object JavaDoc [] items)
317     {
318
319         JComboBox JavaDoc cb = getComboBox ();
320         cb.removeAllItems ();
321         final int n = (items != null ? items.length : 0);
322         for (int i = 0; i < n; i++)
323         {
324             cb.addItem (items [i]);
325         }
326
327     }
328
329     final void eventEditorMousePressed ()
330     {
331
332         this.editor.add (getComboBox ());
333         this.editor.revalidate ();
334         SwingUtilities.invokeLater (this.comboBoxFocusRequester);
335
336     }
337
338     protected void eventTableSelectionChanged ()
339     {
340
341         //stop editing if a new row is selected
342
if ( ! this.table.isRowSelected (this.currentRow))
343         {
344             stopCellEditing ();
345         }
346
347     }
348
349     protected void eventComboBoxActionPerformed ()
350     {
351
352         Object JavaDoc item = getComboBox ().getSelectedItem ();
353         if (item != null) prevItem = item;
354         if (consumeComboBoxActionEvent) stopCellEditing ();
355
356     }
357
358     public final JComboBox JavaDoc getComboBox ()
359     {
360
361         return (JComboBox JavaDoc) getComponent ();
362
363     }
364
365
366     public final Object JavaDoc getInitialValue ()
367     {
368
369         return this.initialValue;
370
371     }
372
373
374     public final int getCurrentRow ()
375     {
376
377         return this.currentRow;
378
379     }
380
381
382 }
383
Popular Tags