001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.plaf.basic;
026
027 import javax.swing.*;
028 import javax.swing.border.Border;
029
030 import javax.swing.text.AttributeSet;
031 import javax.swing.text.BadLocationException;
032 import javax.swing.text.PlainDocument;
033
034 import java.awt.*;
035 import java.awt.event.*;
036
037 import java.lang.reflect.Method;
038
039 /**
040 * The default editor for editable combo boxes. The editor is implemented as a JTextField.
041 *
042 * @version 1.34 05/05/07
043 * @author Arnaud Weber
044 * @author Mark Davidson
045 */
046 public class BasicComboBoxEditor implements ComboBoxEditor,
047 FocusListener {
048 protected JTextField editor;
049 private Object oldValue;
050
051 public BasicComboBoxEditor() {
052 editor = createEditorComponent();
053 }
054
055 public Component getEditorComponent() {
056 return editor;
057 }
058
059 /**
060 * Creates the internal editor component. Override this to provide
061 * a custom implementation.
062 *
063 * @return a new editor component
064 * @since 1.6
065 */
066 protected JTextField createEditorComponent() {
067 JTextField editor = new BorderlessTextField("", 9);
068 editor.setBorder(null);
069 return editor;
070 }
071
072 /**
073 * Sets the item that should be edited.
074 *
075 * @param anObject the displayed value of the editor
076 */
077 public void setItem(Object anObject) {
078 if (anObject != null) {
079 editor.setText(anObject.toString());
080
081 oldValue = anObject;
082 } else {
083 editor.setText("");
084 }
085 }
086
087 public Object getItem() {
088 Object newValue = editor.getText();
089
090 if (oldValue != null && !(oldValue instanceof String)) {
091 // The original value is not a string. Should return the value in it's
092 // original type.
093 if (newValue.equals(oldValue.toString())) {
094 return oldValue;
095 } else {
096 // Must take the value from the editor and get the value and cast it to the new type.
097 Class cls = oldValue.getClass();
098 try {
099 Method method = cls.getMethod("valueOf",
100 new Class[] { String.class });
101 newValue = method.invoke(oldValue,
102 new Object[] { editor.getText() });
103 } catch (Exception ex) {
104 // Fail silently and return the newValue (a String object)
105 }
106 }
107 }
108 return newValue;
109 }
110
111 public void selectAll() {
112 editor.selectAll();
113 editor.requestFocus();
114 }
115
116 // This used to do something but now it doesn't. It couldn't be
117 // removed because it would be an API change to do so.
118 public void focusGained(FocusEvent e) {
119 }
120
121 // This used to do something but now it doesn't. It couldn't be
122 // removed because it would be an API change to do so.
123 public void focusLost(FocusEvent e) {
124 }
125
126 public void addActionListener(ActionListener l) {
127 editor.addActionListener(l);
128 }
129
130 public void removeActionListener(ActionListener l) {
131 editor.removeActionListener(l);
132 }
133
134 static class BorderlessTextField extends JTextField {
135 public BorderlessTextField(String value, int n) {
136 super (value, n);
137 }
138
139 // workaround for 4530952
140 public void setText(String s) {
141 if (getText().equals(s)) {
142 return;
143 }
144 super .setText(s);
145 }
146
147 public void setBorder(Border b) {
148 if (!(b instanceof UIResource)) {
149 super .setBorder(b);
150 }
151 }
152 }
153
154 /**
155 * A subclass of BasicComboBoxEditor that implements UIResource.
156 * BasicComboBoxEditor doesn't implement UIResource
157 * directly so that applications can safely override the
158 * cellRenderer property with BasicListCellRenderer subclasses.
159 * <p>
160 * <strong>Warning:</strong>
161 * Serialized objects of this class will not be compatible with
162 * future Swing releases. The current serialization support is
163 * appropriate for short term storage or RMI between applications running
164 * the same version of Swing. As of 1.4, support for long term storage
165 * of all JavaBeans<sup><font size="-2">TM</font></sup>
166 * has been added to the <code>java.beans</code> package.
167 * Please see {@link java.beans.XMLEncoder}.
168 */
169 public static class UIResource extends BasicComboBoxEditor
170 implements javax.swing.plaf.UIResource {
171 }
172 }
|