001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: //***************** package ***********************************************
043: package org.netbeans.modules.web.monitor.client;
044:
045: //***************** import ************************************************
046:
047: import java.util.EventObject;
048:
049: import java.awt.Point;
050: import java.awt.Rectangle;
051: import java.awt.Component;
052: import java.awt.BorderLayout;
053: import java.awt.IllegalComponentStateException;
054:
055: import java.awt.event.MouseAdapter;
056: import java.awt.event.MouseListener;
057: import java.awt.event.MouseEvent;
058: import java.awt.event.ActionEvent;
059: import java.awt.event.ActionListener;
060:
061: import javax.swing.JPanel;
062: import javax.swing.JTable;
063: import javax.swing.JComboBox;
064: import javax.swing.DefaultCellEditor;
065: import javax.swing.ListCellRenderer;
066: import javax.swing.SwingUtilities;
067:
068: import javax.swing.table.TableCellEditor;
069:
070: import javax.swing.event.ListSelectionEvent;
071: import javax.swing.event.ListSelectionListener;
072:
073: //#########################################################################
074: /**
075: * This is a table cell editor with a combobox as the editing component.
076: * <br>
077: * This is the accessibility patch made due to an accessibility issue with
078: * ComboBoxes inside the JTable.
079: *
080: */
081: //#########################################################################
082: public class ComboBoxTableCellEditor extends DefaultCellEditor
083: implements TableCellEditor {
084:
085: /**
086: * The surrounding panel for the label and the combobox.
087: */
088: private JPanel editor;
089:
090: /**
091: * Listeners for the table added?
092: */
093: private boolean tableListenerAdded = false;
094:
095: /**
096: * The table.
097: */
098: private JTable table;
099:
100: /**
101: * To request the focus for the combobox (with SwingUtilities.invokeLater())
102: */
103: private Runnable comboBoxFocusRequester;
104:
105: /**
106: * To popup the combobox (with SwingUtilities.invokeLater())
107: */
108: private Runnable comboBoxOpener;
109:
110: /**
111: * The current row.
112: */
113: private int currentRow = -1;
114:
115: /**
116: * The previously selected value in the editor.
117: */
118: private Object prevItem;
119:
120: /**
121: * The initial value of the editor.
122: */
123: private Object initialValue;
124:
125: /**
126: * React on action events on the combobox?
127: */
128: private boolean consumeComboBoxActionEvent = true;
129:
130: /**
131: * The event that causes the editing to start. We need it to know
132: * if we should open the popup automatically.
133: */
134: private EventObject startEditingEvent = null;
135:
136: /**********************************************************************
137: * Creates a new CellEditor.
138: *********************************************************************/
139:
140: public ComboBoxTableCellEditor(Object[] values,
141: ListCellRenderer customRenderer) {
142:
143: super (new JComboBox());
144:
145: setItems(values);
146: this .editor = new JPanel(new BorderLayout());
147: setClickCountToStart(1);
148:
149: //show the combobox if the mouse clicks at the panel
150: this .editor.addMouseListener(new MouseAdapter() {
151: public final void mousePressed(MouseEvent evt) {
152: eventEditorMousePressed();
153: }
154: });
155:
156: JComboBox cb = getComboBox();
157:
158: if (customRenderer != null)
159: cb.setRenderer(customRenderer);
160:
161: cb.addActionListener(new ActionListener() {
162: public final void actionPerformed(ActionEvent evt) {
163: eventComboBoxActionPerformed();
164: }
165: });
166:
167: this .comboBoxFocusRequester = new Runnable() {
168: public final void run() {
169: getComboBox().requestFocus();
170: }
171: };
172: this .comboBoxOpener = new Runnable() {
173: public final void run() {
174: if (startEditingEvent instanceof MouseEvent) {
175: try {
176: getComboBox().setPopupVisible(true);
177: } catch (IllegalComponentStateException ex) {
178: //silently ignore - seems to be a bug in JTable
179: }
180: }
181: }
182: };
183:
184: }
185:
186: public ComboBoxTableCellEditor(Object[] values) {
187:
188: this (values, null);
189:
190: }
191:
192: public ComboBoxTableCellEditor() {
193:
194: this (new Object[0], null);
195:
196: }
197:
198: public ComboBoxTableCellEditor(ListCellRenderer customRenderer) {
199:
200: this (new Object[0], customRenderer);
201:
202: }
203:
204: /**********************************************************************
205: * Returns the renderer component of the cell.
206: *
207: * @interfaceMethod javax.swing.table.TableCellEditor
208: *********************************************************************/
209:
210: public final Component getTableCellEditorComponent(JTable table,
211: Object value, boolean selected, int row, int col) {
212:
213: //add a listener to the table
214: if (!this .tableListenerAdded) {
215: this .tableListenerAdded = true;
216: this .table = table;
217: this .table.getSelectionModel().addListSelectionListener(
218: new ListSelectionListener() {
219: public final void valueChanged(
220: ListSelectionEvent evt) {
221: eventTableSelectionChanged();
222: }
223: });
224: }
225: this .currentRow = row;
226: this .initialValue = value;
227:
228: return getEditorComponent(table, value, selected, row, col);
229:
230: }
231:
232: protected Component getEditorComponent(JTable table, Object value,
233: boolean selected, int row, int col) {
234:
235: setSelectedItem(value);
236:
237: //new or old row?
238: selected = table.isRowSelected(row);
239: if (selected) //old row
240: {
241: SwingUtilities.invokeLater(this .comboBoxFocusRequester);
242: SwingUtilities.invokeLater(this .comboBoxOpener);
243: return getComboBox();
244: }
245:
246: //the user selected a new row
247: this .editor.removeAll(); //remove the combobox from the panel
248:
249: return this .editor;
250:
251: }
252:
253: /**********************************************************************
254: * Is the cell editable? If the mouse was pressed at a margin
255: * we don't want the cell to be editable.
256: *
257: * @param evt The event-object.
258: *
259: * @interfaceMethod javax.swing.table.TableCellEditor
260: *********************************************************************/
261:
262: public boolean isCellEditable(EventObject evt) {
263:
264: this .startEditingEvent = evt;
265: if (evt instanceof MouseEvent
266: && evt.getSource() instanceof JTable) {
267: MouseEvent me = (MouseEvent) evt;
268: JTable table = (JTable) me.getSource();
269: Point pt = new Point(me.getX(), me.getY());
270: int row = table.rowAtPoint(pt);
271: int col = table.columnAtPoint(pt);
272: Rectangle rec = table.getCellRect(row, col, false);
273: if (me.getY() >= rec.y + rec.height
274: || me.getX() >= rec.x + rec.width) {
275: return false;
276: }
277: }
278:
279: return super .isCellEditable(evt);
280:
281: }
282:
283: public Object getCellEditorValue() {
284:
285: return prevItem;
286:
287: }
288:
289: protected void setSelectedItem(Object item) {
290:
291: if (getComboBox().getSelectedItem() != item) {
292: this .consumeComboBoxActionEvent = false;
293: getComboBox().setSelectedItem(item);
294: this .consumeComboBoxActionEvent = true;
295: }
296:
297: }
298:
299: public final void setItems(Object[] items) {
300:
301: JComboBox cb = getComboBox();
302: cb.removeAllItems();
303: final int n = (items != null ? items.length : 0);
304: for (int i = 0; i < n; i++) {
305: cb.addItem(items[i]);
306: }
307:
308: }
309:
310: final void eventEditorMousePressed() {
311:
312: this .editor.add(getComboBox());
313: this .editor.revalidate();
314: SwingUtilities.invokeLater(this .comboBoxFocusRequester);
315:
316: }
317:
318: protected void eventTableSelectionChanged() {
319:
320: //stop editing if a new row is selected
321: if (!this .table.isRowSelected(this .currentRow)) {
322: stopCellEditing();
323: }
324:
325: }
326:
327: protected void eventComboBoxActionPerformed() {
328:
329: Object item = getComboBox().getSelectedItem();
330: if (item != null)
331: prevItem = item;
332: if (consumeComboBoxActionEvent)
333: stopCellEditing();
334:
335: }
336:
337: public final JComboBox getComboBox() {
338:
339: return (JComboBox) getComponent();
340:
341: }
342:
343: public final Object getInitialValue() {
344:
345: return this .initialValue;
346:
347: }
348:
349: public final int getCurrentRow() {
350:
351: return this.currentRow;
352:
353: }
354:
355: }
|