org.jdesktop.swingx.autocomplete.AutoCompleteComboBoxEditor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swingx-all Show documentation
Show all versions of swingx-all Show documentation
A Maven project to aggregate all modules into a single artifact.
The newest version!
/*
* $Id$
*
* Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
* Santa Clara, California 95054, U.S.A. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.jdesktop.swingx.autocomplete;
import java.awt.Component;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxEditor;
/**
*
* Wrapper around the combobox editor that translates combobox items into
* strings. The methods setItem and getItem are modified
* to account for the string conversion.
*
* This is necessary for those cases where the combobox items have no useful
* toString() method and a custom ObjectToStringConverter is
* used.
*
* If we do not do this, the interaction between ComboBoxEditor and JComboBox
* will result in firing ActionListener events with the string value of
* ComboBoxEditor as the currently selected value.
*
* @author Noel Grandin [email protected]
* @author Thomas Bierhance
*/
public class AutoCompleteComboBoxEditor implements ComboBoxEditor {
/** the original combo box editor*/
final ComboBoxEditor wrapped;
/** the converter used to convert items into their string representation */
final ObjectToStringConverter stringConverter;
/** last selected item */
private Object oldItem;
/**
* Creates a new AutoCompleteComboBoxEditor.
*
* @param wrapped the original ComboBoxEditor to be wrapped
* @param stringConverter the converter to use to convert items into their
* string representation.
*/
public AutoCompleteComboBoxEditor(ComboBoxEditor wrapped, ObjectToStringConverter stringConverter) {
this.wrapped = wrapped;
this.stringConverter = stringConverter;
}
/* (non-javadoc)
* @see javax.swing.ComboBoxEditor#getEditorComponent()
*/
@Override
public Component getEditorComponent() {
return wrapped.getEditorComponent();
}
/* (non-javadoc)
* @see javax.swing.ComboBoxEditor#setItem(java.lang.Object)
*/
@Override
public void setItem(Object anObject) {
this.oldItem = anObject;
wrapped.setItem(stringConverter.getPreferredStringForItem(anObject));
}
/* (non-javadoc)
* @see javax.swing.ComboBoxEditor#getItem()
*/
@Override
public Object getItem() {
final Object wrappedItem = wrapped.getItem();
String[] oldAsStrings = stringConverter.getPossibleStringsForItem(oldItem);
for (int i=0, n=oldAsStrings.length; i