001/* BasicComboBoxEditor.java --
002   Copyright (C) 2004, 2005, 2006  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package javax.swing.plaf.basic;
040
041import java.awt.Component;
042import java.awt.event.ActionListener;
043import java.awt.event.FocusEvent;
044import java.awt.event.FocusListener;
045
046import javax.swing.ComboBoxEditor;
047import javax.swing.JTextField;
048
049/**
050 * An editor used by the {@link BasicComboBoxUI} class.  This editor uses a
051 * {@link JTextField} as the editor component.
052 *
053 * @author Olga Rodimina
054 */
055public class BasicComboBoxEditor extends Object implements ComboBoxEditor,
056                                                           FocusListener
057{
058  /** The editor component. */
059  protected JTextField editor;
060
061  /**
062   * Creates a new <code>BasicComboBoxEditor</code> instance.
063   */
064  public BasicComboBoxEditor()
065  {
066    editor = new JTextField();
067    editor.setBorder(null);
068    editor.setColumns(9);
069  }
070
071  /**
072   * Returns the component that will be used by the combo box to display and
073   * edit the currently selected item in the combo box.
074   *
075   * @return The editor component, which is a {@link JTextField} in this case.
076   */
077  public Component getEditorComponent()
078  {
079    return editor;
080  }
081
082  /**
083   * Sets item that should be edited when any editing operation is performed
084   * by the user. The value is always equal to the currently selected value
085   * in the combo box. Thus whenever a different value is selected from the
086   * combo box list then this method should be  called to change editing
087   * item to the new selected item.
088   *
089   * @param item item that is currently selected in the combo box
090   */
091  public void setItem(Object item)
092  {
093     if (item == null)
094        editor.setText("");
095     else
096        editor.setText(item.toString());
097  }
098
099  /**
100   * Returns the text from the editor component.
101   *
102   * @return The text from the editor component.
103   */
104  public Object getItem()
105  {
106    return editor.getText();
107  }
108
109  /**
110   * Selects all the text in the editor component.
111   */
112  public void selectAll()
113  {
114    editor.selectAll();
115  }
116
117  /**
118   * This method is called when textfield gains focus. This will enable
119   * editing of the selected item.
120   *
121   * @param e the FocusEvent describing change in focus.
122   */
123  public void focusGained(FocusEvent e)
124  {
125    // FIXME: Need to implement
126  }
127
128  /**
129   * This method is called when textfield loses focus. If during this time any
130   * editting operation was performed by the user, then it will be cancelled
131   * and selected item will not be changed.
132   *
133   * @param e the FocusEvent describing change in focus
134   */
135  public void focusLost(FocusEvent e)
136  {
137    // FIXME: Need to implement
138  }
139
140  /**
141   * Adds an {@link ActionListener} to the editor component.  If the user will
142   * edit currently selected item in the textfield and pressEnter, then action
143   * will be performed. The actionPerformed of this ActionListener should
144   * change the selected item of the comboBox to the newly editted  selected
145   * item.
146   *
147   * @param l the ActionListener responsible for changing selected item of the
148   *        combo box when it is editted by the user.
149   */
150  public void addActionListener(ActionListener l)
151  {
152    editor.addActionListener(l);
153  }
154
155  /**
156   * Removes the {@link ActionListener} from the editor component.
157   *
158   * @param l the listener to remove.
159   */
160  public void removeActionListener(ActionListener l)
161  {
162    editor.removeActionListener(l);
163  }
164
165  /**
166   * A subclass of {@link BasicComboBoxEditor} that implements the
167   * {@link UIResource} interface.
168   */
169  public static class UIResource extends BasicComboBoxEditor
170    implements javax.swing.plaf.UIResource
171  {
172    /**
173     * Creates a new <code>BasicComboBoxEditor.UIResource</code> instance.
174     */
175    public UIResource()
176    {
177      // Nothing to do here.
178    }
179  }
180
181}