Source for javax.swing.ActionMap

   1: /* ActionMap.java --
   2:    Copyright (C) 2002, 2004, 2006, 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: package javax.swing;
  39: 
  40: import java.io.Serializable;
  41: import java.util.Arrays;
  42: import java.util.HashMap;
  43: import java.util.HashSet;
  44: import java.util.Map;
  45: import java.util.Set;
  46: 
  47: 
  48: /**
  49:  * Maps arbitrary keys (usually Strings) to {@link Action} instances. This
  50:  * is used in combination with {@link InputMap}s.
  51:  *
  52:  * If a component receives an input event, this is looked up in
  53:  * the component's <code>InputMap</code>. The result is an object which
  54:  * serves as a key to the components <code>ActionMap</code>. Finally
  55:  * the <code>Action</code> that is stored is executed.
  56:  *
  57:  * @author Andrew Selkirk
  58:  * @author Michael Koch
  59:  */
  60: public class ActionMap
  61:   implements Serializable
  62: {
  63:   private static final long serialVersionUID = -6277518704513986346L;
  64: 
  65:   /**
  66:    * actionMap
  67:    */
  68:   private Map actionMap = new HashMap();
  69: 
  70:   /**
  71:    * parent
  72:    */
  73:   private ActionMap parent;
  74: 
  75:   /**
  76:    * Creates a new <code>ActionMap</code> instance.
  77:    */
  78:   public ActionMap()
  79:   {
  80:     // Nothing to do here.
  81:   }
  82: 
  83:   /**
  84:    * Returns an action associated with an object.
  85:    *
  86:    * @param key the key of the enty
  87:    *
  88:    * @return the action associated with key, may be null
  89:    */
  90:   public Action get(Object key)
  91:   {
  92:     Object result = actionMap.get(key);
  93: 
  94:     if (result == null && parent != null)
  95:       result = parent.get(key);
  96: 
  97:     return (Action) result;
  98:   }
  99: 
 100:   /**
 101:    * Puts a new <code>Action</code> into the <code>ActionMap</code>.
 102:    * If action is null an existing entry will be removed.
 103:    *
 104:    * @param key the key for the entry
 105:    * @param action the action.
 106:    */
 107:   public void put(Object key, Action action)
 108:   {
 109:     if (action == null)
 110:       actionMap.remove(key);
 111:     else
 112:       actionMap.put(key, action);
 113:   }
 114: 
 115:   /**
 116:    * Remove an entry from the <code>ActionMap</code>.
 117:    *
 118:    * @param key the key of the entry to remove
 119:    */
 120:   public void remove(Object key)
 121:   {
 122:     actionMap.remove(key);
 123:   }
 124: 
 125:   /**
 126:    * Returns the parent of this <code>ActionMap</code>.
 127:    *
 128:    * @return the parent, may be null.
 129:    */
 130:   public ActionMap getParent()
 131:   {
 132:     return parent;
 133:   }
 134: 
 135:   /**
 136:    * Sets a parent for this <code>ActionMap</code>.
 137:    *
 138:    * @param parentMap the new parent
 139:    */
 140:   public void setParent(ActionMap parentMap)
 141:   {
 142:     if (parentMap != this)
 143:       parent = parentMap;
 144:   }
 145: 
 146:   /**
 147:    * Returns the number of entries in this <code>ActionMap</code>.
 148:    *
 149:    * @return the number of entries
 150:    */
 151:   public int size()
 152:   {
 153:     return actionMap.size();
 154:   }
 155: 
 156:   /**
 157:    * Clears the <code>ActionMap</code>.
 158:    */
 159:   public void clear()
 160:   {
 161:     actionMap.clear();
 162:   }
 163: 
 164:   /**
 165:    * Returns all keys of entries in this <code>ActionMap</code>.
 166:    *
 167:    * @return an array of keys
 168:    */
 169:   public Object[] keys()
 170:   {
 171:     if (size() != 0)
 172:       return actionMap.keySet().toArray();
 173:     return null;
 174:   }
 175: 
 176:   /**
 177:    * Returns all keys of entries in this <code>ActionMap</code>
 178:    * and all its parents.
 179:    *
 180:    * @return an array of keys
 181:    */
 182:   public Object[] allKeys()
 183:   {
 184:     Set set = new HashSet();
 185: 
 186:     if (parent != null)
 187:       set.addAll(Arrays.asList(parent.allKeys()));
 188: 
 189:     set.addAll(actionMap.keySet());
 190:     if (set.size() != 0)
 191:       return set.toArray();
 192:     return null;
 193:   }
 194: 
 195: }