author | weijun |
Thu, 11 Dec 2014 15:19:12 +0800 | |
changeset 28228 | be83f404724d |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
2 |
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
package javax.swing.plaf.basic; |
|
26 |
||
4394 | 27 |
import javax.swing.ComboBoxEditor; |
28 |
import javax.swing.JTextField; |
|
2 | 29 |
import javax.swing.border.Border; |
4394 | 30 |
import java.awt.Component; |
2 | 31 |
import java.awt.event.*; |
32 |
||
33 |
import java.lang.reflect.Method; |
|
34 |
||
23693
0b8a5e89bac8
8035188: KSS: javax.swing.plaf.basic.BasicComboBoxEditor
malenkov
parents:
22574
diff
changeset
|
35 |
import sun.reflect.misc.MethodUtil; |
0b8a5e89bac8
8035188: KSS: javax.swing.plaf.basic.BasicComboBoxEditor
malenkov
parents:
22574
diff
changeset
|
36 |
|
2 | 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 { |
|
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
44 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
45 |
* An instance of {@code JTextField}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
46 |
*/ |
2 | 47 |
protected JTextField editor; |
48 |
private Object oldValue; |
|
49 |
||
25761
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
50 |
/** |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
51 |
* Constructs a new instance of {@code BasicComboBoxEditor}. |
c408b10ef757
8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents:
25565
diff
changeset
|
52 |
*/ |
2 | 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) { |
|
4394 | 80 |
String text; |
81 |
||
2 | 82 |
if ( anObject != null ) { |
4394 | 83 |
text = anObject.toString(); |
18126
ad2f991576db
8015336: BasicComboBoxEditor throws NullPointerException
malenkov
parents:
5506
diff
changeset
|
84 |
if (text == null) { |
ad2f991576db
8015336: BasicComboBoxEditor throws NullPointerException
malenkov
parents:
5506
diff
changeset
|
85 |
text = ""; |
ad2f991576db
8015336: BasicComboBoxEditor throws NullPointerException
malenkov
parents:
5506
diff
changeset
|
86 |
} |
2 | 87 |
oldValue = anObject; |
88 |
} else { |
|
4394 | 89 |
text = ""; |
90 |
} |
|
91 |
// workaround for 4530952 |
|
92 |
if (! text.equals(editor.getText())) { |
|
93 |
editor.setText(text); |
|
2 | 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. |
|
1290
da8902cd496c
6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents:
2
diff
changeset
|
107 |
Class<?> cls = oldValue.getClass(); |
2 | 108 |
try { |
25565
ce603b34c98d
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents:
23697
diff
changeset
|
109 |
Method method = MethodUtil.getMethod(cls, "valueOf", new Class<?>[]{String.class}); |
23693
0b8a5e89bac8
8035188: KSS: javax.swing.plaf.basic.BasicComboBoxEditor
malenkov
parents:
22574
diff
changeset
|
110 |
newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()}); |
2 | 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 |
||
23697 | 140 |
@SuppressWarnings("serial") // Superclass is not serializable across versions |
2 | 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 |
|
20458 | 172 |
* of all JavaBeans™ |
2 | 173 |
* has been added to the <code>java.beans</code> package. |
174 |
* Please see {@link java.beans.XMLEncoder}. |
|
175 |
*/ |
|
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
176 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 177 |
public static class UIResource extends BasicComboBoxEditor |
178 |
implements javax.swing.plaf.UIResource { |
|
179 |
} |
|
180 |
} |