Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / AbstractAction.java
blob4a2334570aa8ae8d33bd3efbbc3647a24610fdd1
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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. */
39 package javax.swing;
41 import java.beans.PropertyChangeEvent;
42 import java.beans.PropertyChangeListener;
43 import java.io.IOException;
44 import java.io.ObjectInputStream;
45 import java.io.ObjectOutputStream;
46 import java.io.Serializable;
47 import java.util.HashMap;
49 import javax.swing.event.SwingPropertyChangeSupport;
51 /**
52 * A base class for implementing the {@link Action} interface.
54 * @author Andrew Selkirk
56 public abstract class AbstractAction
57 implements Action, Cloneable, Serializable
59 private static final long serialVersionUID = -6803159439231523484L;
61 /**
62 * A flag that indicates whether or not the action is enabled.
64 protected boolean enabled = true;
66 /**
67 * Provides support for property change event notification.
69 protected SwingPropertyChangeSupport changeSupport =
70 new SwingPropertyChangeSupport(this);
72 /**
73 * store
75 private transient HashMap store = new HashMap();
77 /**
78 * Creates a new action with no properties set.
80 public AbstractAction()
82 // Nothing to do.
85 /**
86 * Creates a new action with the specified name. The name is stored as a
87 * property with the key {@link Action#NAME}, and no other properties are
88 * initialised.
90 * @param name the name (<code>null</code> permitted).
92 public AbstractAction(String name)
94 putValue(NAME, name);
97 /**
98 * Creates a new action with the specified name and icon. The name is stored
99 * as a property with the key {@link Action#NAME}, the icon is stored as a
100 * property with the key {@link Action#SMALL_ICON}, and no other properties
101 * are initialised.
103 * @param name the name (<code>null</code> permitted).
104 * @param icon the icon (<code>null</code> permitted).
106 public AbstractAction(String name, Icon icon)
108 putValue(NAME, name);
109 putValue(SMALL_ICON, icon);
113 * readObject
115 * @param stream the stream to read from
117 * @exception ClassNotFoundException TODO
118 * @exception IOException if an error occurs
120 private void readObject(ObjectInputStream stream)
121 throws ClassNotFoundException, IOException
123 // TODO
127 * writeObject
129 * @param stream the stream to write to
131 * @exception IOException if an error occurs
133 private void writeObject(ObjectOutputStream stream) throws IOException
135 // TODO
139 * Returns a clone of the action.
141 * @return A clone of the action.
143 * @exception CloneNotSupportedException if there is a problem cloning the
144 * action.
146 protected Object clone() throws CloneNotSupportedException
148 AbstractAction copy = (AbstractAction) super.clone();
149 copy.store = (HashMap) store.clone();
150 return copy;
154 * Returns the value associated with the specified key.
156 * @param key the key (not <code>null</code>).
158 * @return The value associated with the specified key, or
159 * <code>null</code> if the key is not found.
161 * @see #putValue(String, Object)
163 public Object getValue(String key)
165 return store.get(key);
169 * Sets the value associated with the specified key and sends a
170 * {@link java.beans.PropertyChangeEvent} to all registered listeners.
171 * The standard keys are:
172 * <ul>
173 * <li>{@link #NAME}</li>
174 * <li>{@link #SHORT_DESCRIPTION}</li>
175 * <li>{@link #LONG_DESCRIPTION}</li>
176 * <li>{@link #SMALL_ICON}</li>
177 * <li>{@link #ACTION_COMMAND_KEY}</li>
178 * <li>{@link #ACCELERATOR_KEY}</li>
179 * <li>{@link #MNEMONIC_KEY}</li>
180 * </ul>
181 * Any existing value associated with the key will be overwritten.
183 * @param key the key (not <code>null</code>).
184 * @param value the value (<code>null</code> permitted).
186 public void putValue(String key, Object value)
188 Object old = getValue(key);
189 if ((old == null && value != null) || (old != null && !old.equals(value)))
191 store.put(key, value);
192 firePropertyChange(key, old, value);
197 * Returns the flag that indicates whether or not the action is enabled.
199 * @return The flag.
201 * @see #setEnabled(boolean)
203 public boolean isEnabled()
205 return enabled;
209 * Sets the flag that indicates whether or not the action is enabled and, if
210 * the value of the flag changed from the previous setting, sends a
211 * {@link java.beans.PropertyChangeEvent} to all registered listeners (using
212 * the property name 'enabled').
214 * @param enabled the new flag value.
216 * @see #isEnabled()
218 public void setEnabled(boolean enabled)
220 if (enabled != this.enabled)
222 this.enabled = enabled;
223 firePropertyChange("enabled", !this.enabled, this.enabled);
228 * Returns an array of the keys for the property values that have been
229 * defined via the {@link #putValue(String, Object)} method (or the class
230 * constructor).
232 * @return An array of keys.
234 public Object[] getKeys()
236 return store.keySet().toArray();
240 * Sends a {@link PropertyChangeEvent} for the named property to all
241 * registered listeners.
243 * @param propertyName the property name.
244 * @param oldValue the old value of the property.
245 * @param newValue the new value of the property.
247 protected void firePropertyChange(String propertyName, Object oldValue,
248 Object newValue)
250 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
254 * Sends a {@link PropertyChangeEvent} for the named property to all
255 * registered listeners. This private method is called by the
256 * {@link #setEnabled(boolean)} method.
258 * @param propertyName the property name.
259 * @param oldValue the old value of the property.
260 * @param newValue the new value of the property.
262 private void firePropertyChange(String propertyName, boolean oldValue,
263 boolean newValue)
265 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
269 * Registers a listener to receive {@link PropertyChangeEvent} notifications
270 * from this action.
272 * @param listener the listener.
274 * @see #removePropertyChangeListener(PropertyChangeListener)
276 public void addPropertyChangeListener(PropertyChangeListener listener)
278 changeSupport.addPropertyChangeListener(listener);
282 * Deregisters a listener so that it no longer receives
283 * {@link PropertyChangeEvent} notifications from this action.
285 * @param listener the listener.
287 * @see #addPropertyChangeListener(PropertyChangeListener)
289 public void removePropertyChangeListener(PropertyChangeListener listener)
291 changeSupport.removePropertyChangeListener(listener);
295 * Returns all registered listeners.
297 * @return An array of listeners.
299 * @since 1.4
301 public PropertyChangeListener[] getPropertyChangeListeners()
303 return changeSupport.getPropertyChangeListeners();