1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package javax.swing.plaf.basic;
26
27 import javax.swing.ComboBoxEditor;
28 import javax.swing.JTextField;
29 import javax.swing.border.Border;
30 import java.awt.Component;
31 import java.awt.event.*;
32
33 import java.lang.reflect.Method;
34
35 import sun.reflect.misc.MethodUtil;
36
37 /**
38 * The default editor for editable combo boxes. The editor is implemented as a JTextField.
39 *
40 * @author Arnaud Weber
41 * @author Mark Davidson
42 */
43 public class BasicComboBoxEditor implements ComboBoxEditor,FocusListener {
44 /**
45 * An instance of {@code JTextField}.
46 */
47 protected JTextField editor;
48 private Object oldValue;
49
50 /**
51 * Constructs a new instance of {@code BasicComboBoxEditor}.
52 */
53 public BasicComboBoxEditor() {
54 editor = createEditorComponent();
55 }
56
57 public Component getEditorComponent() {
58 return editor;
59 }
60
61 /**
62 * Creates the internal editor component. Override this to provide
63 * a custom implementation.
64 *
65 * @return a new editor component
66 * @since 1.6
67 */
68 protected JTextField createEditorComponent() {
69 JTextField editor = new BorderlessTextField("",9);
70 editor.setBorder(null);
71 return editor;
72 }
73
74 /**
75 * Sets the item that should be edited.
76 *
77 * @param anObject the displayed value of the editor
78 */
79 public void setItem(Object anObject) {
80 String text;
81
82 if ( anObject != null ) {
83 text = anObject.toString();
84 if (text == null) {
85 text = "";
86 }
87 oldValue = anObject;
88 } else {
89 text = "";
90 }
91 // workaround for 4530952
92 if (! text.equals(editor.getText())) {
93 editor.setText(text);
94 }
95 }
96
97 public Object getItem() {
98 Object newValue = editor.getText();
99
100 if (oldValue != null && !(oldValue instanceof String)) {
101 // The original value is not a string. Should return the value in it's
102 // original type.
103 if (newValue.equals(oldValue.toString())) {
104 return oldValue;
105 } else {
106 // Must take the value from the editor and get the value and cast it to the new type.
107 Class<?> cls = oldValue.getClass();
108 try {
109 Method method = MethodUtil.getMethod(cls, "valueOf", new Class<?>[]{String.class});
110 newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
111 } catch (Exception ex) {
112 // Fail silently and return the newValue (a String object)
113 }
114 }
115 }
116 return newValue;
117 }
118
119 public void selectAll() {
120 editor.selectAll();
121 editor.requestFocus();
122 }
123
124 // This used to do something but now it doesn't. It couldn't be
125 // removed because it would be an API change to do so.
126 public void focusGained(FocusEvent e) {}
127
128 // This used to do something but now it doesn't. It couldn't be
129 // removed because it would be an API change to do so.
130 public void focusLost(FocusEvent e) {}
131
132 public void addActionListener(ActionListener l) {
133 editor.addActionListener(l);
134 }
135
136 public void removeActionListener(ActionListener l) {
137 editor.removeActionListener(l);
138 }
139
140 @SuppressWarnings("serial") // Superclass is not serializable across versions
141 static class BorderlessTextField extends JTextField {
142 public BorderlessTextField(String value,int n) {
143 super(value,n);
144 }
145
146 // workaround for 4530952
147 public void setText(String s) {
148 if (getText().equals(s)) {
149 return;
150 }
151 super.setText(s);
152 }
153
154 public void setBorder(Border b) {
155 if (!(b instanceof UIResource)) {
156 super.setBorder(b);
157 }
158 }
159 }
160
161 /**
162 * A subclass of BasicComboBoxEditor that implements UIResource.
163 * BasicComboBoxEditor doesn't implement UIResource
164 * directly so that applications can safely override the
165 * cellRenderer property with BasicListCellRenderer subclasses.
166 * <p>
167 * <strong>Warning:</strong>
168 * Serialized objects of this class will not be compatible with
169 * future Swing releases. The current serialization support is
170 * appropriate for short term storage or RMI between applications running
171 * the same version of Swing. As of 1.4, support for long term storage
172 * of all JavaBeans™
173 * has been added to the {@code java.beans} package.
174 * Please see {@link java.beans.XMLEncoder}.
175 */
176 @SuppressWarnings("serial") // Same-version serialization only
177 public static class UIResource extends BasicComboBoxEditor
178 implements javax.swing.plaf.UIResource {
179 }
180 }
--- EOF ---