001    /*
002     * $Id: AutoCompleteComboBoxEditor.java 3013 2008-07-31 18:12:19Z 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     */
021    package org.jdesktop.swingx.autocomplete;
022    
023    import java.awt.Component;
024    import java.awt.event.ActionListener;
025    
026    import 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     */
045    public class AutoCompleteComboBoxEditor implements ComboBoxEditor {
046    
047        /** the original combo box editor*/
048        private final ComboBoxEditor wrapped;
049        /** the converter used to convert items into their string representation */
050        private 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        public Component getEditorComponent() {
070            return wrapped.getEditorComponent();
071        }
072    
073        /* (non-javadoc)
074         * @see javax.swing.ComboBoxEditor#setItem(java.lang.Object)
075         */
076        public void setItem(Object anObject) {
077            this.oldItem = anObject;
078            wrapped.setItem(stringConverter.getPreferredStringForItem(anObject));
079        }
080    
081        /* (non-javadoc)
082         * @see javax.swing.ComboBoxEditor#getItem()
083         */
084        public Object getItem() {
085            final Object wrappedItem = wrapped.getItem();
086            
087            String[] oldAsStrings = stringConverter.getPossibleStringsForItem(oldItem);
088            for (int i=0, n=oldAsStrings.length; i<n; i++) {
089                String oldAsString = oldAsStrings[i];
090                if (oldAsString != null &&  oldAsString.equals(wrappedItem)) {
091                    return oldItem;
092                }
093            }
094            return null;
095        }
096    
097        /* (non-javadoc)
098         * @see javax.swing.ComboBoxEditor#selectAll()
099         */
100        public void selectAll() {
101            wrapped.selectAll();
102        }
103    
104        /* (non-javadoc)
105         * @see javax.swing.ComboBoxEditor#addActionListener(java.awt.event.ActionListener)
106         */
107        public void addActionListener(ActionListener l) {
108            wrapped.addActionListener(l);
109        }
110    
111        /* (non-javadoc)
112         * @see javax.swing.ComboBoxEditor#removeActionListener(java.awt.event.ActionListener)
113         */
114        public void removeActionListener(ActionListener l) {
115            wrapped.removeActionListener(l);
116        }
117    }