author | malenkov |
Wed, 30 Apr 2014 19:28:05 +0400 | |
changeset 24544 | c0133e7c7162 |
parent 22574 | 7f8ce0c8c20a |
child 25201 | 4adc75e0c4e5 |
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; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import java.awt.event.*; |
|
29 |
import java.beans.*; |
|
30 |
import java.util.Hashtable; |
|
31 |
import java.util.Enumeration; |
|
32 |
import java.io.Serializable; |
|
33 |
import java.io.IOException; |
|
34 |
import java.io.ObjectInputStream; |
|
35 |
import java.io.ObjectOutputStream; |
|
36 |
import java.security.AccessController; |
|
37 |
import javax.swing.event.SwingPropertyChangeSupport; |
|
38 |
import sun.security.action.GetPropertyAction; |
|
39 |
||
40 |
/** |
|
41 |
* This class provides default implementations for the JFC <code>Action</code> |
|
42 |
* interface. Standard behaviors like the get and set methods for |
|
43 |
* <code>Action</code> object properties (icon, text, and enabled) are defined |
|
44 |
* here. The developer need only subclass this abstract class and |
|
45 |
* define the <code>actionPerformed</code> method. |
|
46 |
* <p> |
|
47 |
* <strong>Warning:</strong> |
|
48 |
* Serialized objects of this class will not be compatible with |
|
49 |
* future Swing releases. The current serialization support is |
|
50 |
* appropriate for short term storage or RMI between applications running |
|
51 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 52 |
* of all JavaBeans™ |
2 | 53 |
* has been added to the <code>java.beans</code> package. |
54 |
* Please see {@link java.beans.XMLEncoder}. |
|
55 |
* |
|
56 |
* @author Georges Saab |
|
57 |
* @see Action |
|
58 |
*/ |
|
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
59 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 60 |
public abstract class AbstractAction implements Action, Cloneable, Serializable |
61 |
{ |
|
62 |
/** |
|
63 |
* Whether or not actions should reconfigure all properties on null. |
|
64 |
*/ |
|
65 |
private static Boolean RECONFIGURE_ON_NULL; |
|
66 |
||
67 |
/** |
|
68 |
* Specifies whether action is enabled; the default is true. |
|
69 |
*/ |
|
70 |
protected boolean enabled = true; |
|
71 |
||
72 |
||
73 |
/** |
|
74 |
* Contains the array of key bindings. |
|
75 |
*/ |
|
76 |
private transient ArrayTable arrayTable; |
|
77 |
||
78 |
/** |
|
79 |
* Whether or not to reconfigure all action properties from the |
|
80 |
* specified event. |
|
81 |
*/ |
|
82 |
static boolean shouldReconfigure(PropertyChangeEvent e) { |
|
83 |
if (e.getPropertyName() == null) { |
|
84 |
synchronized(AbstractAction.class) { |
|
85 |
if (RECONFIGURE_ON_NULL == null) { |
|
86 |
RECONFIGURE_ON_NULL = Boolean.valueOf( |
|
87 |
AccessController.doPrivileged(new GetPropertyAction( |
|
88 |
"swing.actions.reconfigureOnNull", "false"))); |
|
89 |
} |
|
90 |
return RECONFIGURE_ON_NULL; |
|
91 |
} |
|
92 |
} |
|
93 |
return false; |
|
94 |
} |
|
95 |
||
96 |
/** |
|
97 |
* Sets the enabled state of a component from an Action. |
|
98 |
* |
|
99 |
* @param c the Component to set the enabled state on |
|
100 |
* @param a the Action to set the enabled state from, may be null |
|
101 |
*/ |
|
102 |
static void setEnabledFromAction(JComponent c, Action a) { |
|
103 |
c.setEnabled((a != null) ? a.isEnabled() : true); |
|
104 |
} |
|
105 |
||
106 |
/** |
|
107 |
* Sets the tooltip text of a component from an Action. |
|
108 |
* |
|
109 |
* @param c the Component to set the tooltip text on |
|
110 |
* @param a the Action to set the tooltip text from, may be null |
|
111 |
*/ |
|
112 |
static void setToolTipTextFromAction(JComponent c, Action a) { |
|
113 |
c.setToolTipText(a != null ? |
|
114 |
(String)a.getValue(Action.SHORT_DESCRIPTION) : null); |
|
115 |
} |
|
116 |
||
117 |
static boolean hasSelectedKey(Action a) { |
|
118 |
return (a != null && a.getValue(Action.SELECTED_KEY) != null); |
|
119 |
} |
|
120 |
||
121 |
static boolean isSelected(Action a) { |
|
122 |
return Boolean.TRUE.equals(a.getValue(Action.SELECTED_KEY)); |
|
123 |
} |
|
124 |
||
125 |
||
126 |
||
127 |
/** |
|
128 |
* Creates an {@code Action}. |
|
129 |
*/ |
|
130 |
public AbstractAction() { |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Creates an {@code Action} with the specified name. |
|
135 |
* |
|
136 |
* @param name the name ({@code Action.NAME}) for the action; a |
|
137 |
* value of {@code null} is ignored |
|
138 |
*/ |
|
139 |
public AbstractAction(String name) { |
|
140 |
putValue(Action.NAME, name); |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* Creates an {@code Action} with the specified name and small icon. |
|
145 |
* |
|
146 |
* @param name the name ({@code Action.NAME}) for the action; a |
|
147 |
* value of {@code null} is ignored |
|
148 |
* @param icon the small icon ({@code Action.SMALL_ICON}) for the action; a |
|
149 |
* value of {@code null} is ignored |
|
150 |
*/ |
|
151 |
public AbstractAction(String name, Icon icon) { |
|
152 |
this(name); |
|
153 |
putValue(Action.SMALL_ICON, icon); |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Gets the <code>Object</code> associated with the specified key. |
|
158 |
* |
|
159 |
* @param key a string containing the specified <code>key</code> |
|
160 |
* @return the binding <code>Object</code> stored with this key; if there |
|
161 |
* are no keys, it will return <code>null</code> |
|
162 |
* @see Action#getValue |
|
163 |
*/ |
|
164 |
public Object getValue(String key) { |
|
165 |
if (key == "enabled") { |
|
166 |
return enabled; |
|
167 |
} |
|
168 |
if (arrayTable == null) { |
|
169 |
return null; |
|
170 |
} |
|
171 |
return arrayTable.get(key); |
|
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* Sets the <code>Value</code> associated with the specified key. |
|
176 |
* |
|
177 |
* @param key the <code>String</code> that identifies the stored object |
|
178 |
* @param newValue the <code>Object</code> to store using this key |
|
179 |
* @see Action#putValue |
|
180 |
*/ |
|
181 |
public void putValue(String key, Object newValue) { |
|
182 |
Object oldValue = null; |
|
183 |
if (key == "enabled") { |
|
184 |
// Treat putValue("enabled") the same way as a call to setEnabled. |
|
185 |
// If we don't do this it means the two may get out of sync, and a |
|
186 |
// bogus property change notification would be sent. |
|
187 |
// |
|
188 |
// To avoid dependencies between putValue & setEnabled this |
|
189 |
// directly changes enabled. If we instead called setEnabled |
|
190 |
// to change enabled, it would be possible for stack |
|
191 |
// overflow in the case where a developer implemented setEnabled |
|
192 |
// in terms of putValue. |
|
193 |
if (newValue == null || !(newValue instanceof Boolean)) { |
|
194 |
newValue = false; |
|
195 |
} |
|
196 |
oldValue = enabled; |
|
197 |
enabled = (Boolean)newValue; |
|
198 |
} else { |
|
199 |
if (arrayTable == null) { |
|
200 |
arrayTable = new ArrayTable(); |
|
201 |
} |
|
202 |
if (arrayTable.containsKey(key)) |
|
203 |
oldValue = arrayTable.get(key); |
|
204 |
// Remove the entry for key if newValue is null |
|
205 |
// else put in the newValue for key. |
|
206 |
if (newValue == null) { |
|
207 |
arrayTable.remove(key); |
|
208 |
} else { |
|
209 |
arrayTable.put(key,newValue); |
|
210 |
} |
|
211 |
} |
|
212 |
firePropertyChange(key, oldValue, newValue); |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Returns true if the action is enabled. |
|
217 |
* |
|
218 |
* @return true if the action is enabled, false otherwise |
|
219 |
* @see Action#isEnabled |
|
220 |
*/ |
|
221 |
public boolean isEnabled() { |
|
222 |
return enabled; |
|
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* Sets whether the {@code Action} is enabled. The default is {@code true}. |
|
227 |
* |
|
228 |
* @param newValue {@code true} to enable the action, {@code false} to |
|
229 |
* disable it |
|
230 |
* @see Action#setEnabled |
|
231 |
*/ |
|
232 |
public void setEnabled(boolean newValue) { |
|
233 |
boolean oldValue = this.enabled; |
|
234 |
||
235 |
if (oldValue != newValue) { |
|
236 |
this.enabled = newValue; |
|
237 |
firePropertyChange("enabled", |
|
238 |
Boolean.valueOf(oldValue), Boolean.valueOf(newValue)); |
|
239 |
} |
|
240 |
} |
|
241 |
||
242 |
||
243 |
/** |
|
244 |
* Returns an array of <code>Object</code>s which are keys for |
|
245 |
* which values have been set for this <code>AbstractAction</code>, |
|
246 |
* or <code>null</code> if no keys have values set. |
|
247 |
* @return an array of key objects, or <code>null</code> if no |
|
248 |
* keys have values set |
|
249 |
* @since 1.3 |
|
250 |
*/ |
|
251 |
public Object[] getKeys() { |
|
252 |
if (arrayTable == null) { |
|
253 |
return null; |
|
254 |
} |
|
255 |
Object[] keys = new Object[arrayTable.size()]; |
|
256 |
arrayTable.getKeys(keys); |
|
257 |
return keys; |
|
258 |
} |
|
259 |
||
260 |
/** |
|
261 |
* If any <code>PropertyChangeListeners</code> have been registered, the |
|
262 |
* <code>changeSupport</code> field describes them. |
|
263 |
*/ |
|
264 |
protected SwingPropertyChangeSupport changeSupport; |
|
265 |
||
266 |
/** |
|
267 |
* Supports reporting bound property changes. This method can be called |
|
268 |
* when a bound property has changed and it will send the appropriate |
|
269 |
* <code>PropertyChangeEvent</code> to any registered |
|
270 |
* <code>PropertyChangeListeners</code>. |
|
271 |
*/ |
|
272 |
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { |
|
273 |
if (changeSupport == null || |
|
274 |
(oldValue != null && newValue != null && oldValue.equals(newValue))) { |
|
275 |
return; |
|
276 |
} |
|
277 |
changeSupport.firePropertyChange(propertyName, oldValue, newValue); |
|
278 |
} |
|
279 |
||
280 |
||
281 |
/** |
|
282 |
* Adds a <code>PropertyChangeListener</code> to the listener list. |
|
283 |
* The listener is registered for all properties. |
|
284 |
* <p> |
|
285 |
* A <code>PropertyChangeEvent</code> will get fired in response to setting |
|
286 |
* a bound property, e.g. <code>setFont</code>, <code>setBackground</code>, |
|
287 |
* or <code>setForeground</code>. |
|
288 |
* Note that if the current component is inheriting its foreground, |
|
289 |
* background, or font from its container, then no event will be |
|
290 |
* fired in response to a change in the inherited property. |
|
291 |
* |
|
292 |
* @param listener The <code>PropertyChangeListener</code> to be added |
|
293 |
* |
|
294 |
* @see Action#addPropertyChangeListener |
|
295 |
*/ |
|
296 |
public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { |
|
297 |
if (changeSupport == null) { |
|
298 |
changeSupport = new SwingPropertyChangeSupport(this); |
|
299 |
} |
|
300 |
changeSupport.addPropertyChangeListener(listener); |
|
301 |
} |
|
302 |
||
303 |
||
304 |
/** |
|
305 |
* Removes a <code>PropertyChangeListener</code> from the listener list. |
|
306 |
* This removes a <code>PropertyChangeListener</code> that was registered |
|
307 |
* for all properties. |
|
308 |
* |
|
309 |
* @param listener the <code>PropertyChangeListener</code> to be removed |
|
310 |
* |
|
311 |
* @see Action#removePropertyChangeListener |
|
312 |
*/ |
|
313 |
public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { |
|
314 |
if (changeSupport == null) { |
|
315 |
return; |
|
316 |
} |
|
317 |
changeSupport.removePropertyChangeListener(listener); |
|
318 |
} |
|
319 |
||
320 |
||
321 |
/** |
|
322 |
* Returns an array of all the <code>PropertyChangeListener</code>s added |
|
323 |
* to this AbstractAction with addPropertyChangeListener(). |
|
324 |
* |
|
325 |
* @return all of the <code>PropertyChangeListener</code>s added or an empty |
|
326 |
* array if no listeners have been added |
|
327 |
* @since 1.4 |
|
328 |
*/ |
|
329 |
public synchronized PropertyChangeListener[] getPropertyChangeListeners() { |
|
330 |
if (changeSupport == null) { |
|
331 |
return new PropertyChangeListener[0]; |
|
332 |
} |
|
333 |
return changeSupport.getPropertyChangeListeners(); |
|
334 |
} |
|
335 |
||
336 |
||
337 |
/** |
|
338 |
* Clones the abstract action. This gives the clone |
|
339 |
* its own copy of the key/value list, |
|
340 |
* which is not handled for you by <code>Object.clone()</code>. |
|
341 |
**/ |
|
342 |
||
343 |
protected Object clone() throws CloneNotSupportedException { |
|
344 |
AbstractAction newAction = (AbstractAction)super.clone(); |
|
345 |
synchronized(this) { |
|
346 |
if (arrayTable != null) { |
|
347 |
newAction.arrayTable = (ArrayTable)arrayTable.clone(); |
|
348 |
} |
|
349 |
} |
|
350 |
return newAction; |
|
351 |
} |
|
352 |
||
353 |
private void writeObject(ObjectOutputStream s) throws IOException { |
|
354 |
// Store the default fields |
|
355 |
s.defaultWriteObject(); |
|
356 |
||
357 |
// And the keys |
|
358 |
ArrayTable.writeArrayTable(s, arrayTable); |
|
359 |
} |
|
360 |
||
361 |
private void readObject(ObjectInputStream s) throws ClassNotFoundException, |
|
362 |
IOException { |
|
363 |
s.defaultReadObject(); |
|
364 |
for (int counter = s.readInt() - 1; counter >= 0; counter--) { |
|
365 |
putValue((String)s.readObject(), s.readObject()); |
|
366 |
} |
|
367 |
} |
|
368 |
} |