author | serb |
Mon, 07 Apr 2014 15:34:27 +0400 | |
changeset 24163 | d56df89854c4 |
parent 23697 | e556a715949f |
child 25565 | ce603b34c98d |
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 { |
|
44 |
protected JTextField editor; |
|
45 |
private Object oldValue; |
|
46 |
||
47 |
public BasicComboBoxEditor() { |
|
48 |
editor = createEditorComponent(); |
|
49 |
} |
|
50 |
||
51 |
public Component getEditorComponent() { |
|
52 |
return editor; |
|
53 |
} |
|
54 |
||
55 |
/** |
|
56 |
* Creates the internal editor component. Override this to provide |
|
57 |
* a custom implementation. |
|
58 |
* |
|
59 |
* @return a new editor component |
|
60 |
* @since 1.6 |
|
61 |
*/ |
|
62 |
protected JTextField createEditorComponent() { |
|
63 |
JTextField editor = new BorderlessTextField("",9); |
|
64 |
editor.setBorder(null); |
|
65 |
return editor; |
|
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
* Sets the item that should be edited. |
|
70 |
* |
|
71 |
* @param anObject the displayed value of the editor |
|
72 |
*/ |
|
73 |
public void setItem(Object anObject) { |
|
4394 | 74 |
String text; |
75 |
||
2 | 76 |
if ( anObject != null ) { |
4394 | 77 |
text = anObject.toString(); |
18126
ad2f991576db
8015336: BasicComboBoxEditor throws NullPointerException
malenkov
parents:
5506
diff
changeset
|
78 |
if (text == null) { |
ad2f991576db
8015336: BasicComboBoxEditor throws NullPointerException
malenkov
parents:
5506
diff
changeset
|
79 |
text = ""; |
ad2f991576db
8015336: BasicComboBoxEditor throws NullPointerException
malenkov
parents:
5506
diff
changeset
|
80 |
} |
2 | 81 |
oldValue = anObject; |
82 |
} else { |
|
4394 | 83 |
text = ""; |
84 |
} |
|
85 |
// workaround for 4530952 |
|
86 |
if (! text.equals(editor.getText())) { |
|
87 |
editor.setText(text); |
|
2 | 88 |
} |
89 |
} |
|
90 |
||
91 |
public Object getItem() { |
|
92 |
Object newValue = editor.getText(); |
|
93 |
||
94 |
if (oldValue != null && !(oldValue instanceof String)) { |
|
95 |
// The original value is not a string. Should return the value in it's |
|
96 |
// original type. |
|
97 |
if (newValue.equals(oldValue.toString())) { |
|
98 |
return oldValue; |
|
99 |
} else { |
|
100 |
// 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
|
101 |
Class<?> cls = oldValue.getClass(); |
2 | 102 |
try { |
23693
0b8a5e89bac8
8035188: KSS: javax.swing.plaf.basic.BasicComboBoxEditor
malenkov
parents:
22574
diff
changeset
|
103 |
Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class}); |
0b8a5e89bac8
8035188: KSS: javax.swing.plaf.basic.BasicComboBoxEditor
malenkov
parents:
22574
diff
changeset
|
104 |
newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()}); |
2 | 105 |
} catch (Exception ex) { |
106 |
// Fail silently and return the newValue (a String object) |
|
107 |
} |
|
108 |
} |
|
109 |
} |
|
110 |
return newValue; |
|
111 |
} |
|
112 |
||
113 |
public void selectAll() { |
|
114 |
editor.selectAll(); |
|
115 |
editor.requestFocus(); |
|
116 |
} |
|
117 |
||
118 |
// This used to do something but now it doesn't. It couldn't be |
|
119 |
// removed because it would be an API change to do so. |
|
120 |
public void focusGained(FocusEvent e) {} |
|
121 |
||
122 |
// This used to do something but now it doesn't. It couldn't be |
|
123 |
// removed because it would be an API change to do so. |
|
124 |
public void focusLost(FocusEvent e) {} |
|
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 |
||
23697 | 134 |
@SuppressWarnings("serial") // Superclass is not serializable across versions |
2 | 135 |
static class BorderlessTextField extends JTextField { |
136 |
public BorderlessTextField(String value,int n) { |
|
137 |
super(value,n); |
|
138 |
} |
|
139 |
||
140 |
// workaround for 4530952 |
|
141 |
public void setText(String s) { |
|
142 |
if (getText().equals(s)) { |
|
143 |
return; |
|
144 |
} |
|
145 |
super.setText(s); |
|
146 |
} |
|
147 |
||
148 |
public void setBorder(Border b) { |
|
149 |
if (!(b instanceof UIResource)) { |
|
150 |
super.setBorder(b); |
|
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* A subclass of BasicComboBoxEditor that implements UIResource. |
|
157 |
* BasicComboBoxEditor doesn't implement UIResource |
|
158 |
* directly so that applications can safely override the |
|
159 |
* cellRenderer property with BasicListCellRenderer subclasses. |
|
160 |
* <p> |
|
161 |
* <strong>Warning:</strong> |
|
162 |
* Serialized objects of this class will not be compatible with |
|
163 |
* future Swing releases. The current serialization support is |
|
164 |
* appropriate for short term storage or RMI between applications running |
|
165 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 166 |
* of all JavaBeans™ |
2 | 167 |
* has been added to the <code>java.beans</code> package. |
168 |
* Please see {@link java.beans.XMLEncoder}. |
|
169 |
*/ |
|
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
170 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 171 |
public static class UIResource extends BasicComboBoxEditor |
172 |
implements javax.swing.plaf.UIResource { |
|
173 |
} |
|
174 |
} |