001/* 002 * $Id: ComboBoxCellEditor.java 4051 2011-07-19 20:17:05Z kschaefe $ 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 */ 021package org.jdesktop.swingx.autocomplete; 022 023import java.awt.event.ActionEvent; 024import java.awt.event.MouseEvent; 025import java.util.EventObject; 026 027import javax.swing.DefaultCellEditor; 028import javax.swing.JComboBox; 029import javax.swing.text.JTextComponent; 030 031/** 032 * <p>This is a cell editor that can be used when a combo box (that has been set 033 * up for automatic completion) is to be used in a JTable. The 034 * {@link javax.swing.DefaultCellEditor DefaultCellEditor} won't work in this 035 * case, because each time an item gets selected it stops cell editing and hides 036 * the combo box. 037 * </p> 038 * <p> 039 * Usage example: 040 * </p> 041 * <p> 042 * <pre><code> 043 * JTable table = ...; 044 * JComboBox comboBox = ...; 045 * ... 046 * TableColumn column = table.getColumnModel().getColumn(0); 047 * column.setCellEditor(new ComboBoxCellEditor(comboBox)); 048 * </code></pre> 049 * </p> 050 */ 051public class ComboBoxCellEditor extends DefaultCellEditor { 052 053 /** 054 * Creates a new ComboBoxCellEditor. 055 * @param comboBox the comboBox that should be used as the cell editor. 056 */ 057 public ComboBoxCellEditor(final JComboBox comboBox) { 058 super(comboBox); 059 060 comboBox.removeActionListener(this.delegate); 061 062 this.delegate = new EditorDelegate() { 063 @Override 064 public void setValue(Object value) { 065 comboBox.setSelectedItem(value); 066 } 067 068 @Override 069 public Object getCellEditorValue() { 070 return comboBox.getSelectedItem(); 071 } 072 073 @Override 074 public boolean shouldSelectCell(EventObject anEvent) { 075 if (anEvent instanceof MouseEvent) { 076 MouseEvent e = (MouseEvent) anEvent; 077 return e.getID() != MouseEvent.MOUSE_DRAGGED; 078 } 079 return true; 080 } 081 082 @Override 083 public boolean stopCellEditing() { 084 if (comboBox.isEditable()) { 085 // Commit edited value. 086 comboBox.actionPerformed(new ActionEvent(ComboBoxCellEditor.this, 0, "")); 087 } 088 return super.stopCellEditing(); 089 } 090 091 @Override 092 public void actionPerformed(ActionEvent e) { 093 JTextComponent editorComponent = (JTextComponent) comboBox.getEditor() 094 .getEditorComponent(); 095 096 if (editorComponent.getDocument() instanceof AutoCompleteDocument) { 097 AutoCompleteDocument document = (AutoCompleteDocument) editorComponent 098 .getDocument(); 099 // if auto completion is happening right now, cell editing should not be stopped 100 if (!document.selecting) { 101 ComboBoxCellEditor.this.stopCellEditing(); 102 } 103 104 } else { 105 ComboBoxCellEditor.this.stopCellEditing(); 106 } 107 } 108 }; 109 110 comboBox.addActionListener(this.delegate); 111 } 112}