ComboBoxCellEditor.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:6k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: ComboBoxCellEditor.java,v 1.3 2005/10/10 18:01:33 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.autocomplete;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.KeyAdapter;
  24. import java.awt.event.KeyEvent;
  25. import java.beans.PropertyChangeEvent;
  26. import java.beans.PropertyChangeListener;
  27. import java.io.Serializable;
  28. import javax.swing.AbstractCellEditor;
  29. import javax.swing.ComboBoxEditor;
  30. import javax.swing.JComboBox;
  31. import javax.swing.JComponent;
  32. import javax.swing.table.TableCellEditor;
  33. /**
  34.  * <p>This is a cell editor that can be used when a combo box (that has been set
  35.  * up for automatic completion) is to be used in a JTable. The
  36.  * {@link javax.swing.DefaultCellEditor DefaultCellEditor} won't work in this
  37.  * case, because each time an item gets selected it stops cell editing and hides
  38.  * the combo box.
  39.  * </p>
  40.  * <p>
  41.  * Usage example:
  42.  * </p>
  43.  * <code>
  44.  * JTable table = ...;</br>
  45.  * JComboBox comboBox = ...;</br>
  46.  * ...</br>
  47.  * TableColumn column = table.getColumnModel().getColumn(0);</br>
  48.  * column.setCellEditor(new ComboBoxCellEditor(comboBox));
  49.  *  </code>
  50.  */
  51. public class ComboBoxCellEditor extends AbstractCellEditor implements TableCellEditor, Serializable {
  52.     
  53.     private JComboBox comboBox;
  54.     // a Listener listening for key events (handling enter-key)
  55.     // and changes of the combo box' editor component.
  56.     private Handler handler;
  57.     
  58.     /**
  59.      * Creates a new ComboBoxCellEditor.
  60.      * @param comboBox the comboBox that should be used as the cell editor.
  61.      */
  62.     public ComboBoxCellEditor(final JComboBox comboBox) {
  63.         this.comboBox = comboBox;
  64.         
  65.         handler = new Handler();
  66.         
  67.         // Don't do this:
  68.         // this.comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
  69.         // it probably breaks various things
  70.         
  71.         // hitting enter in the combo box should stop cellediting
  72.         JComponent editorComponent = (JComponent) comboBox.getEditor().getEditorComponent();
  73.         editorComponent.addKeyListener(handler);
  74.         // remove the editor's border - the cell itself already has one
  75.         editorComponent.setBorder(null);
  76.         // editor component might change (e.g. a look&feel change)
  77.         // the new editor component needs to be modified then (keyListener, border)
  78.         comboBox.addPropertyChangeListener(handler);
  79.     }
  80.     
  81.     // ------ Implementing CellEditor ------
  82.     /**
  83.      * Returns the value contained in the combo box
  84.      * @return the value contained in the combo box
  85.      */
  86.     public Object getCellEditorValue() {
  87.         return comboBox.getSelectedItem();
  88.     }
  89.     
  90.     /**
  91.      * Tells the combo box to stop editing and accept any partially edited value as the value of the combo box.
  92.      * Always returns true.
  93.      * @return true
  94.      */
  95.     public boolean stopCellEditing() {
  96.         if (comboBox.isEditable()) {
  97.             // Notify the combo box that editing has stopped (e.g. User pressed F2)
  98.             comboBox.actionPerformed(new ActionEvent(this, 0, ""));
  99.         }
  100.         fireEditingStopped();
  101.         return true;
  102.     }
  103.     
  104.     // ------ Implementing TableCellEditor ------
  105.     /**
  106.      * Sets an initial value for the combo box.
  107.      * Returns the combo box that should be added to the client's Component hierarchy.
  108.      * Once installed in the client's hierarchy this combo box will then be able to draw and receive user input.
  109.      * @param table the JTable that is asking the editor to edit; can be null
  110.      * @param value the value of the cell to be edited; null is a valid value
  111.      * @param isSelected will be ignored
  112.      * @param row the row of the cell being edited
  113.      * @param column the column of the cell being edited
  114.      * @return the combo box for editing
  115.      */
  116.     public java.awt.Component getTableCellEditorComponent(javax.swing.JTable table, Object value, boolean isSelected, int row, int column) {
  117.         comboBox.setSelectedItem(value);
  118.         return comboBox;
  119.     }
  120.     
  121.     // ------ Implementing TreeCellEditor ------
  122. //    public java.awt.Component getTreeCellEditorComponent(javax.swing.JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
  123. //        String stringValue = tree.convertValueToText(value, isSelected, expanded, leaf, row, false);
  124. //        comboBox.setSelectedItem(stringValue);
  125. //        return comboBox;
  126. //    }
  127.     
  128.     class Handler extends KeyAdapter implements PropertyChangeListener {
  129.         public void keyPressed(KeyEvent keyEvent) {
  130.             int keyCode = keyEvent.getKeyCode();
  131.             if (keyCode==keyEvent.VK_ENTER) stopCellEditing();
  132.         }
  133.         public void propertyChange(PropertyChangeEvent e) {
  134.             if (e.getPropertyName().equals("editor")) {
  135.                 ComboBoxEditor editor = comboBox.getEditor();
  136.                 if (editor!=null && editor.getEditorComponent()!=null) {
  137.                     JComponent editorComponent = (JComponent) comboBox.getEditor().getEditorComponent();
  138.                     editorComponent.addKeyListener(this);
  139.                     editorComponent.setBorder(null);
  140.                 }
  141.             }
  142.         }
  143.     }
  144. }