blob: bc41bfb0821c6926238ee92477763f11b0fb48de [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package javax.swing.plaf.basic;
26
27import javax.swing.*;
28import javax.swing.border.Border;
29
30import javax.swing.text.AttributeSet;
31import javax.swing.text.BadLocationException;
32import javax.swing.text.PlainDocument;
33
34import java.awt.*;
35import java.awt.event.*;
36
37import java.lang.reflect.Method;
38
39/**
40 * The default editor for editable combo boxes. The editor is implemented as a JTextField.
41 *
42 * @author Arnaud Weber
43 * @author Mark Davidson
44 */
45public class BasicComboBoxEditor implements ComboBoxEditor,FocusListener {
46 protected JTextField editor;
47 private Object oldValue;
48
49 public BasicComboBoxEditor() {
50 editor = createEditorComponent();
51 }
52
53 public Component getEditorComponent() {
54 return editor;
55 }
56
57 /**
58 * Creates the internal editor component. Override this to provide
59 * a custom implementation.
60 *
61 * @return a new editor component
62 * @since 1.6
63 */
64 protected JTextField createEditorComponent() {
65 JTextField editor = new BorderlessTextField("",9);
66 editor.setBorder(null);
67 return editor;
68 }
69
70 /**
71 * Sets the item that should be edited.
72 *
73 * @param anObject the displayed value of the editor
74 */
75 public void setItem(Object anObject) {
76 if ( anObject != null ) {
77 editor.setText(anObject.toString());
78
79 oldValue = anObject;
80 } else {
81 editor.setText("");
82 }
83 }
84
85 public Object getItem() {
86 Object newValue = editor.getText();
87
88 if (oldValue != null && !(oldValue instanceof String)) {
89 // The original value is not a string. Should return the value in it's
90 // original type.
91 if (newValue.equals(oldValue.toString())) {
92 return oldValue;
93 } else {
94 // Must take the value from the editor and get the value and cast it to the new type.
95 Class cls = oldValue.getClass();
96 try {
97 Method method = cls.getMethod("valueOf", new Class[]{String.class});
98 newValue = method.invoke(oldValue, new Object[] { editor.getText()});
99 } catch (Exception ex) {
100 // Fail silently and return the newValue (a String object)
101 }
102 }
103 }
104 return newValue;
105 }
106
107 public void selectAll() {
108 editor.selectAll();
109 editor.requestFocus();
110 }
111
112 // This used to do something but now it doesn't. It couldn't be
113 // removed because it would be an API change to do so.
114 public void focusGained(FocusEvent e) {}
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 focusLost(FocusEvent e) {}
119
120 public void addActionListener(ActionListener l) {
121 editor.addActionListener(l);
122 }
123
124 public void removeActionListener(ActionListener l) {
125 editor.removeActionListener(l);
126 }
127
128 static class BorderlessTextField extends JTextField {
129 public BorderlessTextField(String value,int n) {
130 super(value,n);
131 }
132
133 // workaround for 4530952
134 public void setText(String s) {
135 if (getText().equals(s)) {
136 return;
137 }
138 super.setText(s);
139 }
140
141 public void setBorder(Border b) {
142 if (!(b instanceof UIResource)) {
143 super.setBorder(b);
144 }
145 }
146 }
147
148 /**
149 * A subclass of BasicComboBoxEditor that implements UIResource.
150 * BasicComboBoxEditor doesn't implement UIResource
151 * directly so that applications can safely override the
152 * cellRenderer property with BasicListCellRenderer subclasses.
153 * <p>
154 * <strong>Warning:</strong>
155 * Serialized objects of this class will not be compatible with
156 * future Swing releases. The current serialization support is
157 * appropriate for short term storage or RMI between applications running
158 * the same version of Swing. As of 1.4, support for long term storage
159 * of all JavaBeans<sup><font size="-2">TM</font></sup>
160 * has been added to the <code>java.beans</code> package.
161 * Please see {@link java.beans.XMLEncoder}.
162 */
163 public static class UIResource extends BasicComboBoxEditor
164 implements javax.swing.plaf.UIResource {
165 }
166}