001: /*
002: * $Id: ComboBoxCellEditor.java,v 1.5 2007/01/29 08:52:44 Bierhance Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021: package org.jdesktop.swingx.autocomplete;
022:
023: import java.awt.event.ActionEvent;
024: import java.awt.event.KeyAdapter;
025: import java.awt.event.KeyEvent;
026: import java.beans.PropertyChangeEvent;
027: import java.beans.PropertyChangeListener;
028: import java.io.Serializable;
029:
030: import javax.swing.AbstractCellEditor;
031: import javax.swing.ComboBoxEditor;
032: import javax.swing.JComboBox;
033: import javax.swing.JComponent;
034: import javax.swing.table.TableCellEditor;
035:
036: /**
037: * <p>This is a cell editor that can be used when a combo box (that has been set
038: * up for automatic completion) is to be used in a JTable. The
039: * {@link javax.swing.DefaultCellEditor DefaultCellEditor} won't work in this
040: * case, because each time an item gets selected it stops cell editing and hides
041: * the combo box.
042: * </p>
043: * <p>
044: * Usage example:
045: * </p>
046: * <p>
047: * <pre><code>
048: * JTable table = ...;
049: * JComboBox comboBox = ...;
050: * ...
051: * TableColumn column = table.getColumnModel().getColumn(0);
052: * column.setCellEditor(new ComboBoxCellEditor(comboBox));
053: * </code></pre>
054: * </p>
055: */
056: public class ComboBoxCellEditor extends AbstractCellEditor implements
057: TableCellEditor, Serializable {
058:
059: /** the combo box */
060: private JComboBox comboBox;
061: /** a Listener listening for key events (handling enter-key) and changes of
062: * the combo box' editor component.*/
063: private Handler handler;
064:
065: /**
066: * Creates a new ComboBoxCellEditor.
067: * @param comboBox the comboBox that should be used as the cell editor.
068: */
069: public ComboBoxCellEditor(final JComboBox comboBox) {
070: this .comboBox = comboBox;
071:
072: handler = new Handler();
073:
074: // Don't do this:
075: // this.comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
076: // it probably breaks various things
077:
078: // hitting enter in the combo box should stop cellediting
079: JComponent editorComponent = (JComponent) comboBox.getEditor()
080: .getEditorComponent();
081: editorComponent.addKeyListener(handler);
082: // remove the editor's border - the cell itself already has one
083: editorComponent.setBorder(null);
084:
085: // editor component might change (e.g. a look&feel change)
086: // the new editor component needs to be modified then (keyListener, border)
087: comboBox.addPropertyChangeListener(handler);
088: }
089:
090: // ------ Implementing CellEditor ------
091: /**
092: * Returns the value contained in the combo box
093: * @return the value contained in the combo box
094: */
095: public Object getCellEditorValue() {
096: return comboBox.getSelectedItem();
097: }
098:
099: /**
100: * Tells the combo box to stop editing and accept any partially edited value as the value of the combo box.
101: * Always returns true.
102: * @return true
103: */
104: public boolean stopCellEditing() {
105: if (comboBox.isEditable()) {
106: // Notify the combo box that editing has stopped (e.g. User pressed F2)
107: comboBox.actionPerformed(new ActionEvent(this , 0, ""));
108: }
109: fireEditingStopped();
110: return true;
111: }
112:
113: // ------ Implementing TableCellEditor ------
114: /**
115: * Sets an initial value for the combo box.
116: * Returns the combo box that should be added to the client's Component hierarchy.
117: * Once installed in the client's hierarchy this combo box will then be able to draw and receive user input.
118: * @param table the JTable that is asking the editor to edit; can be null
119: * @param value the value of the cell to be edited; null is a valid value
120: * @param isSelected will be ignored
121: * @param row the row of the cell being edited
122: * @param column the column of the cell being edited
123: * @return the combo box for editing
124: */
125: public java.awt.Component getTableCellEditorComponent(
126: javax.swing.JTable table, Object value, boolean isSelected,
127: int row, int column) {
128: comboBox.setSelectedItem(value);
129: return comboBox;
130: }
131:
132: // ------ Implementing TreeCellEditor ------
133: // public java.awt.Component getTreeCellEditorComponent(javax.swing.JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
134: // String stringValue = tree.convertValueToText(value, isSelected, expanded, leaf, row, false);
135: // comboBox.setSelectedItem(stringValue);
136: // return comboBox;
137: // }
138:
139: class Handler extends KeyAdapter implements PropertyChangeListener {
140: public void keyPressed(KeyEvent keyEvent) {
141: int keyCode = keyEvent.getKeyCode();
142: if (keyCode == keyEvent.VK_ENTER)
143: stopCellEditing();
144: }
145:
146: public void propertyChange(PropertyChangeEvent e) {
147: if (e.getPropertyName().equals("editor")) {
148: ComboBoxEditor editor = comboBox.getEditor();
149: if (editor != null
150: && editor.getEditorComponent() != null) {
151: JComponent editorComponent = (JComponent) comboBox
152: .getEditor().getEditorComponent();
153: editorComponent.addKeyListener(this);
154: editorComponent.setBorder(null);
155: }
156: }
157: }
158: }
159: }
|