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