001 /*
002 * $Id: ComboBoxAdaptor.java,v 1.5 2006/03/19 18:28:45 Bierhance Exp $
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.event.ActionEvent;
024 import java.awt.event.ActionListener;
025
026 import javax.swing.JComboBox;
027 import javax.swing.text.JTextComponent;
028
029 /**
030 * An implementation of the AbstractAutoCompleteAdaptor that is suitable for JComboBox.
031 *
032 * @author Thomas Bierhance
033 */
034 public class ComboBoxAdaptor extends AbstractAutoCompleteAdaptor implements ActionListener {
035
036 /** the combobox being adapted */
037 private JComboBox comboBox;
038
039 /**
040 * Creates a new ComobBoxAdaptor for the given combobox.
041 * @param comboBox the combobox that should be adapted
042 */
043 public ComboBoxAdaptor(JComboBox comboBox) {
044 this.comboBox = comboBox;
045 // mark the entire text when a new item is selected
046 comboBox.addActionListener(this);
047 }
048
049 /**
050 * Implementation side effect - do not invoke.
051 * @param actionEvent -
052 */
053 // ActionListener (listening to comboBox)
054 public void actionPerformed(ActionEvent actionEvent) {
055 markEntireText();
056 }
057
058 public int getItemCount() {
059 return comboBox.getItemCount();
060 }
061
062 public Object getItem(int index) {
063 return comboBox.getItemAt(index);
064 }
065
066 public void setSelectedItem(Object item) {
067 comboBox.setSelectedItem(item);
068 }
069
070 public Object getSelectedItem() {
071 return comboBox.getModel().getSelectedItem();
072 }
073
074 public JTextComponent getTextComponent() {
075 // returning the component of the combobox' editor
076 return (JTextComponent) comboBox.getEditor().getEditorComponent();
077 }
078 }