KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > AbstractAction


1 /*
2  * @(#)AbstractAction.java 1.51 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.beans.*;
12 import java.util.Hashtable JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.io.Serializable JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.ObjectInputStream JavaDoc;
17 import java.io.ObjectOutputStream JavaDoc;
18 import javax.swing.event.SwingPropertyChangeSupport JavaDoc;
19
20 /**
21  * This class provides default implementations for the JFC <code>Action</code>
22  * interface. Standard behaviors like the get and set methods for
23  * <code>Action</code> object properties (icon, text, and enabled) are defined
24  * here. The developer need only subclass this abstract class and
25  * define the <code>actionPerformed</code> method.
26  * <p>
27  * <strong>Warning:</strong>
28  * Serialized objects of this class will not be compatible with
29  * future Swing releases. The current serialization support is
30  * appropriate for short term storage or RMI between applications running
31  * the same version of Swing. As of 1.4, support for long term storage
32  * of all JavaBeans<sup><font size="-2">TM</font></sup>
33  * has been added to the <code>java.beans</code> package.
34  * Please see {@link java.beans.XMLEncoder}.
35  *
36  * @version 1.51 12/19/03
37  * @author Georges Saab
38  * @see Action
39  */

40 public abstract class AbstractAction implements Action JavaDoc, Cloneable JavaDoc, Serializable JavaDoc
41 {
42     /**
43      * Specifies whether action is enabled; the default is true.
44      */

45     protected boolean enabled = true;
46
47
48     /**
49      * Contains the array of key bindings.
50      */

51     private transient ArrayTable JavaDoc arrayTable;
52     
53     /**
54      * Defines an <code>Action</code> object with a default
55      * description string and default icon.
56      */

57     public AbstractAction() {
58     }
59     
60     /**
61      * Defines an <code>Action</code> object with the specified
62      * description string and a default icon.
63      */

64     public AbstractAction(String JavaDoc name) {
65     putValue(Action.NAME, name);
66     }
67
68     /**
69      * Defines an <code>Action</code> object with the specified
70      * description string and a the specified icon.
71      */

72     public AbstractAction(String JavaDoc name, Icon JavaDoc icon) {
73     this(name);
74     putValue(Action.SMALL_ICON, icon);
75     }
76     
77     /**
78      * Gets the <code>Object</code> associated with the specified key.
79      *
80      * @param key a string containing the specified <code>key</code>
81      * @return the binding <code>Object</code> stored with this key; if there
82      * are no keys, it will return <code>null</code>
83      * @see Action#getValue
84      */

85     public Object JavaDoc getValue(String JavaDoc key) {
86     if (arrayTable == null) {
87         return null;
88     }
89     return arrayTable.get(key);
90     }
91     
92     /**
93      * Sets the <code>Value</code> associated with the specified key.
94      *
95      * @param key the <code>String</code> that identifies the stored object
96      * @param newValue the <code>Object</code> to store using this key
97      * @see Action#putValue
98      */

99     public void putValue(String JavaDoc key, Object JavaDoc newValue) {
100     Object JavaDoc oldValue = null;
101     if (arrayTable == null) {
102         arrayTable = new ArrayTable JavaDoc();
103     }
104     if (arrayTable.containsKey(key))
105         oldValue = arrayTable.get(key);
106     // Remove the entry for key if newValue is null
107
// else put in the newValue for key.
108
if (newValue == null) {
109         arrayTable.remove(key);
110     } else {
111         arrayTable.put(key,newValue);
112     }
113     firePropertyChange(key, oldValue, newValue);
114     }
115
116     /**
117      * Returns true if the action is enabled.
118      *
119      * @return true if the action is enabled, false otherwise
120      * @see Action#isEnabled
121      */

122     public boolean isEnabled() {
123     return enabled;
124     }
125
126     /**
127      * Enables or disables the action.
128      *
129      * @param newValue true to enable the action, false to
130      * disable it
131      * @see Action#setEnabled
132      */

133     public void setEnabled(boolean newValue) {
134     boolean oldValue = this.enabled;
135
136     if (oldValue != newValue) {
137         this.enabled = newValue;
138         firePropertyChange("enabled",
139                    Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
140     }
141     }
142
143
144     /**
145      * Returns an array of <code>Object</code>s which are keys for
146      * which values have been set for this <code>AbstractAction</code>,
147      * or <code>null</code> if no keys have values set.
148      * @return an array of key objects, or <code>null</code> if no
149      * keys have values set
150      * @since 1.3
151      */

152     public Object JavaDoc[] getKeys() {
153     if (arrayTable == null) {
154         return null;
155     }
156     Object JavaDoc[] keys = new Object JavaDoc[arrayTable.size()];
157     arrayTable.getKeys(keys);
158     return keys;
159     }
160
161     /**
162      * If any <code>PropertyChangeListeners</code> have been registered, the
163      * <code>changeSupport</code> field describes them.
164      */

165     protected SwingPropertyChangeSupport JavaDoc changeSupport;
166
167     /**
168      * Supports reporting bound property changes. This method can be called
169      * when a bound property has changed and it will send the appropriate
170      * <code>PropertyChangeEvent</code> to any registered
171      * <code>PropertyChangeListeners</code>.
172      */

173     protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
174         if (changeSupport == null ||
175         (oldValue != null && newValue != null && oldValue.equals(newValue))) {
176             return;
177         }
178         changeSupport.firePropertyChange(propertyName, oldValue, newValue);
179     }
180
181
182     /**
183      * Adds a <code>PropertyChangeListener</code> to the listener list.
184      * The listener is registered for all properties.
185      * <p>
186      * A <code>PropertyChangeEvent</code> will get fired in response to setting
187      * a bound property, e.g. <code>setFont</code>, <code>setBackground</code>,
188      * or <code>setForeground</code>.
189      * Note that if the current component is inheriting its foreground,
190      * background, or font from its container, then no event will be
191      * fired in response to a change in the inherited property.
192      *
193      * @param listener The <code>PropertyChangeListener</code> to be added
194      *
195      * @see Action#addPropertyChangeListener
196      */

197     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
198         if (changeSupport == null) {
199         changeSupport = new SwingPropertyChangeSupport JavaDoc(this);
200         }
201         changeSupport.addPropertyChangeListener(listener);
202     }
203
204
205     /**
206      * Removes a <code>PropertyChangeListener</code> from the listener list.
207      * This removes a <code>PropertyChangeListener</code> that was registered
208      * for all properties.
209      *
210      * @param listener the <code>PropertyChangeListener</code> to be removed
211      *
212      * @see Action#removePropertyChangeListener
213      */

214     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
215         if (changeSupport == null) {
216             return;
217         }
218         changeSupport.removePropertyChangeListener(listener);
219     }
220
221
222     /**
223      * Returns an array of all the <code>PropertyChangeListener</code>s added
224      * to this AbstractAction with addPropertyChangeListener().
225      *
226      * @return all of the <code>PropertyChangeListener</code>s added or an empty
227      * array if no listeners have been added
228      * @since 1.4
229      */

230     public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
231         if (changeSupport == null) {
232             return new PropertyChangeListener[0];
233         }
234         return changeSupport.getPropertyChangeListeners();
235     }
236
237
238     /**
239      * Clones the abstract action. This gives the clone
240      * its own copy of the key/value list,
241      * which is not handled for you by <code>Object.clone()</code>.
242      **/

243
244     protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
245     AbstractAction JavaDoc newAction = (AbstractAction JavaDoc)super.clone();
246     synchronized(this) {
247         if (arrayTable != null) {
248         newAction.arrayTable = (ArrayTable JavaDoc)arrayTable.clone();
249         }
250     }
251     return newAction;
252     }
253
254     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
255     // Store the default fields
256
s.defaultWriteObject();
257
258         // And the keys
259
ArrayTable.writeArrayTable(s, arrayTable);
260     }
261
262     private void readObject(ObjectInputStream JavaDoc s) throws ClassNotFoundException JavaDoc,
263     IOException JavaDoc {
264         s.defaultReadObject();
265     for (int counter = s.readInt() - 1; counter >= 0; counter--) {
266         putValue((String JavaDoc)s.readObject(), s.readObject());
267     }
268     }
269 }
270
Popular Tags