001/*
002 * $Id: AutoCompleteComboBoxEditor.java 4045 2011-07-19 18:39:17Z 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.Component;
024import java.awt.event.ActionListener;
025
026import javax.swing.ComboBoxEditor;
027
028/**
029 * <p>
030 * Wrapper around the combobox editor that translates combobox items into
031 * strings. The methods <tt>setItem</tt> and <tt>getItem</tt> are modified
032 * to account for the string conversion.
033 * </p><p>
034 * This is necessary for those cases where the combobox items have no useful
035 * <tt>toString()</tt> method and a custom <tt>ObjectToStringConverter</tt> is
036 * used.
037 * </p><p>
038 * If we do not do this, the interaction between ComboBoxEditor and JComboBox
039 * will result in firing ActionListener events with the string value of
040 * ComboBoxEditor as the currently selected value.
041 * </p>
042 * @author Noel Grandin noelgrandin@gmail.com
043 * @author Thomas Bierhance
044 */
045public class AutoCompleteComboBoxEditor implements ComboBoxEditor {
046
047    /** the original combo box editor*/
048    final ComboBoxEditor wrapped;
049    /** the converter used to convert items into their string representation */
050    final ObjectToStringConverter stringConverter;
051    /** last selected item */
052    private Object oldItem;
053
054    /**
055     * Creates a new <tt>AutoCompleteComboBoxEditor</tt>.
056     *
057     * @param wrapped the original <tt>ComboBoxEditor</tt> to be wrapped
058     * @param stringConverter the converter to use to convert items into their
059     * string representation.
060     */
061    public AutoCompleteComboBoxEditor(ComboBoxEditor wrapped, ObjectToStringConverter stringConverter) {
062        this.wrapped = wrapped;
063        this.stringConverter = stringConverter;
064    }
065
066    /* (non-javadoc)
067     * @see javax.swing.ComboBoxEditor#getEditorComponent()
068     */
069    @Override
070    public Component getEditorComponent() {
071        return wrapped.getEditorComponent();
072    }
073
074    /* (non-javadoc)
075     * @see javax.swing.ComboBoxEditor#setItem(java.lang.Object)
076     */
077    @Override
078    public void setItem(Object anObject) {
079        this.oldItem = anObject;
080        wrapped.setItem(stringConverter.getPreferredStringForItem(anObject));
081    }
082
083    /* (non-javadoc)
084     * @see javax.swing.ComboBoxEditor#getItem()
085     */
086    @Override
087    public Object getItem() {
088        final Object wrappedItem = wrapped.getItem();
089        
090        String[] oldAsStrings = stringConverter.getPossibleStringsForItem(oldItem);
091        for (int i=0, n=oldAsStrings.length; i<n; i++) {
092            String oldAsString = oldAsStrings[i];
093            if (oldAsString != null &&  oldAsString.equals(wrappedItem)) {
094                return oldItem;
095            }
096        }
097        return null;
098    }
099
100    /* (non-javadoc)
101     * @see javax.swing.ComboBoxEditor#selectAll()
102     */
103    @Override
104    public void selectAll() {
105        wrapped.selectAll();
106    }
107
108    /* (non-javadoc)
109     * @see javax.swing.ComboBoxEditor#addActionListener(java.awt.event.ActionListener)
110     */
111    @Override
112    public void addActionListener(ActionListener l) {
113        wrapped.addActionListener(l);
114    }
115
116    /* (non-javadoc)
117     * @see javax.swing.ComboBoxEditor#removeActionListener(java.awt.event.ActionListener)
118     */
119    @Override
120    public void removeActionListener(ActionListener l) {
121        wrapped.removeActionListener(l);
122    }
123}