1 /* AbstractAction.java --
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.beans
.PropertyChangeListener
;
42 import java
.io
.IOException
;
43 import java
.io
.ObjectInputStream
;
44 import java
.io
.ObjectOutputStream
;
45 import java
.io
.Serializable
;
46 import java
.util
.HashMap
;
48 import javax
.swing
.event
.SwingPropertyChangeSupport
;
51 * A base class for implementing the {@link Action} interface.
53 * @author Andrew Selkirk
55 public abstract class AbstractAction
56 implements Action
, Cloneable
, Serializable
58 private static final long serialVersionUID
= -6803159439231523484L;
61 * A flag that indicates whether or not the action is enabled.
63 protected boolean enabled
= true;
66 * Provides support for property change event notification.
68 protected SwingPropertyChangeSupport changeSupport
=
69 new SwingPropertyChangeSupport(this);
74 private transient HashMap store
= new HashMap();
77 * Creates a new action with an empty string for the name. All other
78 * properties are initialised to <code>null</code>
80 public AbstractAction()
86 * Creates a new action with the specified name. All other properties are
87 * initialised to <code>null</code>.
89 * @param name the name (<code>null</code> permitted).
91 public AbstractAction(String name
)
97 * Creates a new action with the specified name and icon. All other
98 * properties are initialised to <code>null</code>.
100 * @param name the name (<code>null</code> permitted).
101 * @param icon the icon (<code>null</code> permitted).
103 public AbstractAction(String name
, Icon icon
)
105 putValue(NAME
, name
);
106 putValue(SMALL_ICON
, icon
);
112 * @param stream the stream to read from
114 * @exception ClassNotFoundException TODO
115 * @exception IOException if an error occurs
117 private void readObject(ObjectInputStream stream
)
118 throws ClassNotFoundException
, IOException
126 * @param stream the stream to write to
128 * @exception IOException if an error occurs
130 private void writeObject(ObjectOutputStream stream
) throws IOException
140 * @exception CloneNotSupportedException TODO
142 protected Object
clone() throws CloneNotSupportedException
144 AbstractAction copy
= (AbstractAction
) super.clone();
145 copy
.store
= (HashMap
) store
.clone();
150 * Returns the value associated with the specified key.
152 * @param key the key (not <code>null</code>).
154 * @return The value associated with the specified key, or
155 * <code>null</code> if the key is not found.
157 public Object
getValue(String key
)
159 return store
.get(key
);
163 * Sets the value associated with the specified key and sends a
164 * {@link java.beans.PropertyChangeEvent} to all registered listeners.
165 * The standard keys are: {@link #NAME}, {@link #SHORT_DESCRIPTION},
166 * {@link #LONG_DESCRIPTION}, {@link #SMALL_ICON},
167 * {@link #ACTION_COMMAND_KEY}, {@link #ACCELERATOR_KEY} and
168 * {@link #MNEMONIC_KEY}. Any existing value associated with the key will be
171 * @param key the key (not <code>null</code>).
172 * @param value the value (<code>null</code> permitted).
174 public void putValue(String key
, Object value
)
176 Object old
= getValue(key
);
177 if (old
== null || !old
.equals(value
))
179 store
.put(key
, value
);
180 firePropertyChange(key
, old
, value
);
185 * Returns the flag that indicates whether or not the action is enabled.
189 public boolean isEnabled()
195 * Sets the flag that indicates whether or not the action is enabled and, if
196 * the value of the flag changed from the previous setting, sends a
197 * {@link java.beans.PropertyChangeEvent} to all registered listeners.
199 * @param enabled the new flag value.
201 public void setEnabled(boolean enabled
)
203 if (enabled
!= this.enabled
)
205 this.enabled
= enabled
;
206 firePropertyChange("enabled", !this.enabled
, this.enabled
);
214 public Object
[] getKeys()
216 return store
.keySet().toArray();
220 * This method fires a PropertyChangeEvent given the propertyName
221 * and the old and new values.
223 * @param propertyName The property that changed.
224 * @param oldValue The old value of the property.
225 * @param newValue The new value of the property.
227 protected void firePropertyChange(String propertyName
, Object oldValue
,
230 changeSupport
.firePropertyChange(propertyName
, oldValue
, newValue
);
234 * This convenience method fires a PropertyChangeEvent given
235 * the propertyName and the old and new values.
237 * @param propertyName The property that changed.
238 * @param oldValue The old value of the property.
239 * @param newValue The new value of the property.
241 private void firePropertyChange(String propertyName
, boolean oldValue
, boolean newValue
)
243 changeSupport
.firePropertyChange(propertyName
, oldValue
, newValue
);
247 * addPropertyChangeListener
249 * @param listener the listener to add
251 public void addPropertyChangeListener(PropertyChangeListener listener
)
253 changeSupport
.addPropertyChangeListener(listener
);
257 * removePropertyChangeListener
259 * @param listener the listener to remove
261 public void removePropertyChangeListener(PropertyChangeListener listener
)
263 changeSupport
.removePropertyChangeListener(listener
);
267 * Returns all registered listeners.
269 * @return array of listeners.
273 public PropertyChangeListener
[] getPropertyChangeListeners()
275 return changeSupport
.getPropertyChangeListeners();