Source for javax.swing.AbstractAction

   1: /* AbstractAction.java --
   2:    Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package javax.swing;
  40: 
  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;
  47: 
  48: import javax.swing.event.SwingPropertyChangeSupport;
  49: 
  50: /**
  51:  * A base class for implementing the {@link Action} interface.
  52:  * 
  53:  * @author Andrew Selkirk
  54:  */
  55: public abstract class AbstractAction
  56:   implements Action, Cloneable, Serializable
  57: {
  58:   private static final long serialVersionUID = -6803159439231523484L;
  59: 
  60:   /**
  61:    * A flag that indicates whether or not the action is enabled.
  62:    */
  63:   protected boolean enabled = true;
  64:   
  65:   /**
  66:    * Provides support for property change event notification. 
  67:    */
  68:   protected SwingPropertyChangeSupport changeSupport =
  69:     new SwingPropertyChangeSupport(this);
  70: 
  71:   /**
  72:    * store
  73:    */
  74:   private transient HashMap store = new HashMap();
  75: 
  76:   /**
  77:    * Creates a new action with an empty string for the name.  All other 
  78:    * properties are initialised to <code>null</code>
  79:    */
  80:   public AbstractAction()
  81:   {
  82:     this(null);
  83:   }
  84: 
  85:   /**
  86:    * Creates a new action with the specified name.  All other properties are
  87:    * initialised to <code>null</code>.
  88:    *
  89:    * @param name  the name (<code>null</code> permitted).
  90:    */
  91:   public AbstractAction(String name)
  92:   {
  93:     this(name, null);
  94:   }
  95: 
  96:   /**
  97:    * Creates a new action with the specified name and icon.  All other 
  98:    * properties are initialised to <code>null</code>.
  99:    *
 100:    * @param name  the name (<code>null</code> permitted).
 101:    * @param icon  the icon (<code>null</code> permitted).
 102:    */
 103:   public AbstractAction(String name, Icon icon)
 104:   {
 105:     putValue(NAME, name);
 106:     putValue(SMALL_ICON, icon);
 107:   }
 108: 
 109:   /**
 110:    * readObject
 111:    *
 112:    * @param stream the stream to read from
 113:    *
 114:    * @exception ClassNotFoundException TODO
 115:    * @exception IOException if an error occurs
 116:    */
 117:   private void readObject(ObjectInputStream stream)
 118:     throws ClassNotFoundException, IOException
 119:   {
 120:     // TODO
 121:   }
 122: 
 123:   /**
 124:    * writeObject
 125:    *
 126:    * @param stream the stream to write to
 127:    *
 128:    * @exception IOException if an error occurs
 129:    */
 130:   private void writeObject(ObjectOutputStream stream) throws IOException
 131:   {
 132:     // TODO
 133:   }
 134: 
 135:   /**
 136:    * clone
 137:    *
 138:    * @return Object
 139:    *
 140:    * @exception CloneNotSupportedException TODO
 141:    */
 142:   protected Object clone() throws CloneNotSupportedException
 143:   {
 144:     AbstractAction copy = (AbstractAction) super.clone();
 145:     copy.store = (HashMap) store.clone();
 146:     return copy;
 147:   }
 148: 
 149:   /**
 150:    * Returns the value associated with the specified key.
 151:    * 
 152:    * @param key  the key (not <code>null</code>).
 153:    * 
 154:    * @return The value associated with the specified key, or 
 155:    *         <code>null</code> if the key is not found.
 156:    */
 157:   public Object getValue(String key)
 158:   {
 159:     return store.get(key);
 160:   }
 161: 
 162:   /**
 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 
 169:    * overwritten.
 170:    * 
 171:    * @param key  the key (not <code>null</code>).
 172:    * @param value  the value (<code>null</code> permitted).
 173:    */
 174:   public void putValue(String key, Object value)
 175:   {
 176:     Object old = getValue(key);
 177:     if (old == null || !old.equals(value))
 178:     {
 179:       store.put(key, value);
 180:       firePropertyChange(key, old, value);
 181:     }
 182:   }
 183: 
 184:   /**
 185:    * Returns the flag that indicates whether or not the action is enabled.
 186:    *
 187:    * @return The flag.
 188:    */
 189:   public boolean isEnabled()
 190:   {
 191:     return enabled;
 192:   }
 193: 
 194:   /**
 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.
 198:    *
 199:    * @param enabled  the new flag value.
 200:    */
 201:   public void setEnabled(boolean enabled)
 202:   {
 203:     if (enabled != this.enabled)
 204:     {
 205:       this.enabled = enabled;
 206:       firePropertyChange("enabled", !this.enabled, this.enabled);
 207:     }
 208:   }
 209: 
 210:   /**
 211:    * getKeys
 212:    * @returns Object[]
 213:    */
 214:   public Object[] getKeys()
 215:   {
 216:     return store.keySet().toArray();
 217:   }
 218: 
 219:   /**
 220:    * This method fires a PropertyChangeEvent given the propertyName 
 221:    * and the old and new values.
 222:    *
 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.
 226:    */
 227:   protected void firePropertyChange(String propertyName, Object oldValue,
 228:                                     Object newValue)
 229:   {
 230:     changeSupport.firePropertyChange(propertyName, oldValue, newValue);
 231:   }
 232:   
 233:   /**
 234:    * This convenience method fires a PropertyChangeEvent given 
 235:    * the propertyName and the old and new values.
 236:    *
 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.
 240:    */
 241:   private void firePropertyChange(String propertyName, boolean oldValue, boolean newValue)
 242:   {
 243:     changeSupport.firePropertyChange(propertyName, oldValue, newValue);
 244:   }
 245: 
 246:   /**
 247:    * addPropertyChangeListener
 248:    *
 249:    * @param listener the listener to add
 250:    */
 251:   public void addPropertyChangeListener(PropertyChangeListener listener)
 252:   {
 253:     changeSupport.addPropertyChangeListener(listener);
 254:   }
 255: 
 256:   /**
 257:    * removePropertyChangeListener
 258:    *
 259:    * @param listener the listener to remove
 260:    */
 261:   public void removePropertyChangeListener(PropertyChangeListener listener)
 262:   {
 263:     changeSupport.removePropertyChangeListener(listener);
 264:   }
 265: 
 266:   /**
 267:    * Returns all registered listeners.
 268:    *
 269:    * @return array of listeners.
 270:    * 
 271:    * @since 1.4
 272:    */
 273:   public PropertyChangeListener[] getPropertyChangeListeners()
 274:   {
 275:     return changeSupport.getPropertyChangeListeners();
 276:   }
 277: }