1 /* AbstractAction.java --
2 Copyright (C) 2002, 2004, 2005, 2006 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.PropertyChangeEvent;
42 import java.beans.PropertyChangeListener;
43 import java.io.Serializable;
44 import java.util.HashMap;
46 import javax.swing.event.SwingPropertyChangeSupport;
49 * A base class for implementing the {@link Action} interface.
51 * @author Andrew Selkirk
53 public abstract class AbstractAction
54 implements Action, Cloneable, Serializable
56 private static final long serialVersionUID = -6803159439231523484L;
59 * A flag that indicates whether or not the action is enabled.
61 protected boolean enabled = true;
64 * Provides support for property change event notification.
66 protected SwingPropertyChangeSupport changeSupport =
67 new SwingPropertyChangeSupport(this);
72 private transient HashMap store = new HashMap();
75 * Creates a new action with no properties set.
77 public AbstractAction()
83 * Creates a new action with the specified name. The name is stored as a
84 * property with the key {@link Action#NAME}, and no other properties are
87 * @param name the name (<code>null</code> permitted).
89 public AbstractAction(String name)
95 * Creates a new action with the specified name and icon. The name is stored
96 * as a property with the key {@link Action#NAME}, the icon is stored as a
97 * property with the key {@link Action#SMALL_ICON}, and no other properties
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);
110 * Returns a clone of the action.
112 * @return A clone of the action.
114 * @exception CloneNotSupportedException if there is a problem cloning the
117 protected Object clone() throws CloneNotSupportedException
119 AbstractAction copy = (AbstractAction) super.clone();
120 copy.store = (HashMap) store.clone();
125 * Returns the value associated with the specified key.
127 * @param key the key (not <code>null</code>).
129 * @return The value associated with the specified key, or
130 * <code>null</code> if the key is not found.
132 * @see #putValue(String, Object)
134 public Object getValue(String key)
136 return store.get(key);
140 * Sets the value associated with the specified key and sends a
141 * {@link java.beans.PropertyChangeEvent} to all registered listeners.
142 * The standard keys are:
144 * <li>{@link #NAME}</li>
145 * <li>{@link #SHORT_DESCRIPTION}</li>
146 * <li>{@link #LONG_DESCRIPTION}</li>
147 * <li>{@link #SMALL_ICON}</li>
148 * <li>{@link #ACTION_COMMAND_KEY}</li>
149 * <li>{@link #ACCELERATOR_KEY}</li>
150 * <li>{@link #MNEMONIC_KEY}</li>
152 * Any existing value associated with the key will be overwritten.
154 * @param key the key (not <code>null</code>).
155 * @param value the value (<code>null</code> permitted).
157 public void putValue(String key, Object value)
159 Object old = getValue(key);
160 if ((old == null && value != null) || (old != null && !old.equals(value)))
162 store.put(key, value);
163 firePropertyChange(key, old, value);
168 * Returns the flag that indicates whether or not the action is enabled.
172 * @see #setEnabled(boolean)
174 public boolean isEnabled()
180 * Sets the flag that indicates whether or not the action is enabled and, if
181 * the value of the flag changed from the previous setting, sends a
182 * {@link java.beans.PropertyChangeEvent} to all registered listeners (using
183 * the property name 'enabled').
185 * @param enabled the new flag value.
189 public void setEnabled(boolean enabled)
191 if (enabled != this.enabled)
193 this.enabled = enabled;
194 firePropertyChange("enabled", !this.enabled, this.enabled);
199 * Returns an array of the keys for the property values that have been
200 * defined via the {@link #putValue(String, Object)} method (or the class
203 * @return An array of keys.
205 public Object[] getKeys()
207 return store.keySet().toArray();
211 * Sends a {@link PropertyChangeEvent} for the named property to all
212 * registered listeners.
214 * @param propertyName the property name.
215 * @param oldValue the old value of the property.
216 * @param newValue the new value of the property.
218 protected void firePropertyChange(String propertyName, Object oldValue,
221 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
225 * Sends a {@link PropertyChangeEvent} for the named property to all
226 * registered listeners. This private method is called by the
227 * {@link #setEnabled(boolean)} method.
229 * @param propertyName the property name.
230 * @param oldValue the old value of the property.
231 * @param newValue the new value of the property.
233 private void firePropertyChange(String propertyName, boolean oldValue,
236 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
240 * Registers a listener to receive {@link PropertyChangeEvent} notifications
243 * @param listener the listener.
245 * @see #removePropertyChangeListener(PropertyChangeListener)
247 public void addPropertyChangeListener(PropertyChangeListener listener)
249 changeSupport.addPropertyChangeListener(listener);
253 * Deregisters a listener so that it no longer receives
254 * {@link PropertyChangeEvent} notifications from this action.
256 * @param listener the listener.
258 * @see #addPropertyChangeListener(PropertyChangeListener)
260 public void removePropertyChangeListener(PropertyChangeListener listener)
262 changeSupport.removePropertyChangeListener(listener);
266 * Returns all registered listeners.
268 * @return An array of listeners.
272 public PropertyChangeListener[] getPropertyChangeListeners()
274 return changeSupport.getPropertyChangeListeners();