001 /* ActionMap.java --
002 Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038 package javax.swing;
039
040 import java.io.Serializable;
041 import java.util.Arrays;
042 import java.util.HashMap;
043 import java.util.HashSet;
044 import java.util.Map;
045 import java.util.Set;
046
047
048 /**
049 * Maps arbitrary keys (usually Strings) to {@link Action} instances. This
050 * is used in combination with {@link InputMap}s.
051 *
052 * If a component receives an input event, this is looked up in
053 * the component's <code>InputMap</code>. The result is an object which
054 * serves as a key to the components <code>ActionMap</code>. Finally
055 * the <code>Action</code> that is stored is executed.
056 *
057 * @author Andrew Selkirk
058 * @author Michael Koch
059 */
060 public class ActionMap
061 implements Serializable
062 {
063 private static final long serialVersionUID = -6277518704513986346L;
064
065 /**
066 * actionMap
067 */
068 private Map actionMap = new HashMap();
069
070 /**
071 * parent
072 */
073 private ActionMap parent;
074
075 /**
076 * Creates a new <code>ActionMap</code> instance.
077 */
078 public ActionMap()
079 {
080 // Nothing to do here.
081 }
082
083 /**
084 * Returns an action associated with an object.
085 *
086 * @param key the key of the enty
087 *
088 * @return the action associated with key, may be null
089 */
090 public Action get(Object key)
091 {
092 Object result = actionMap.get(key);
093
094 if (result == null && parent != null)
095 result = parent.get(key);
096
097 return (Action) result;
098 }
099
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 }