1 /*
2 * Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25 package javax.swing;
26
27 import java.io.IOException;
28 import java.io.ObjectInputStream;
29 import java.io.ObjectOutputStream;
30 import java.io.Serializable;
31 import java.util.HashMap;
32 import java.util.Set;
33
34 /**
35 * <code>ActionMap</code> provides mappings from
36 * <code>Object</code>s
37 * (called <em>keys</em> or <em><code>Action</code> names</em>)
38 * to <code>Action</code>s.
39 * An <code>ActionMap</code> is usually used with an <code>InputMap</code>
40 * to locate a particular action
41 * when a key is pressed. As with <code>InputMap</code>,
42 * an <code>ActionMap</code> can have a parent
43 * that is searched for keys not defined in the <code>ActionMap</code>.
44 * <p>As with <code>InputMap</code> if you create a cycle, eg:
45 * <pre>
46 * ActionMap am = new ActionMap();
47 * ActionMap bm = new ActionMap():
48 * am.setParent(bm);
49 * bm.setParent(am);
50 * </pre>
51 * some of the methods will cause a StackOverflowError to be thrown.
52 *
53 * @see InputMap
54 *
55 * @author Scott Violet
56 * @since 1.3
57 */
58 public class ActionMap implements Serializable {
59 /** Handles the mapping between Action name and Action. */
60 private transient ArrayTable arrayTable;
61 /** Parent that handles any bindings we don't contain. */
62 private ActionMap parent;
63
64
65 /**
66 * Creates an <code>ActionMap</code> with no parent and no mappings.
67 */
68 public ActionMap() {
69 }
70
71 /**
72 * Sets this <code>ActionMap</code>'s parent.
73 *
74 * @param map the <code>ActionMap</code> that is the parent of this one
75 */
76 public void setParent(ActionMap map) {
77 this.parent = map;
78 }
79
80 /**
81 * Returns this <code>ActionMap</code>'s parent.
82 *
83 * @return the <code>ActionMap</code> that is the parent of this one,
84 * or null if this <code>ActionMap</code> has no parent
85 */
86 public ActionMap getParent() {
87 return parent;
88 }
89
90 /**
91 * Adds a binding for <code>key</code> to <code>action</code>.
92 * If <code>action</code> is null, this removes the current binding
93 * for <code>key</code>.
94 * <p>In most instances, <code>key</code> will be
95 * <code>action.getValue(NAME)</code>.
96 */
97 public void put(Object key, Action action) {
98 if (key == null) {
99 return;
100 }
101 if (action == null) {
102 remove(key);
103 }
104 else {
105 if (arrayTable == null) {
106 arrayTable = new ArrayTable();
107 }
108 arrayTable.put(key, action);
109 }
110 }
111
112 /**
113 * Returns the binding for <code>key</code>, messaging the
114 * parent <code>ActionMap</code> if the binding is not locally defined.
115 */
116 public Action get(Object key) {
117 Action value = (arrayTable == null) ? null :
118 (Action)arrayTable.get(key);
119
120 if (value == null) {
121 ActionMap parent = getParent();
122
123 if (parent != null) {
124 return parent.get(key);
125 }
126 }
127 return value;
128 }
129
130 /**
131 * Removes the binding for <code>key</code> from this <code>ActionMap</code>.
132 */
133 public void remove(Object key) {
134 if (arrayTable != null) {
135 arrayTable.remove(key);
136 }
137 }
138
139 /**
140 * Removes all the mappings from this <code>ActionMap</code>.
141 */
142 public void clear() {
143 if (arrayTable != null) {
144 arrayTable.clear();
145 }
146 }
147
148 /**
149 * Returns the <code>Action</code> names that are bound in this <code>ActionMap</code>.
150 */
151 public Object[] keys() {
152 if (arrayTable == null) {
153 return null;
154 }
155 return arrayTable.getKeys(null);
156 }
157
158 /**
159 * Returns the number of bindings in this {@code ActionMap}.
160 *
161 * @return the number of bindings in this {@code ActionMap}
162 */
163 public int size() {
164 if (arrayTable == null) {
165 return 0;
166 }
167 return arrayTable.size();
168 }
169
170 /**
171 * Returns an array of the keys defined in this <code>ActionMap</code> and
172 * its parent. This method differs from <code>keys()</code> in that
173 * this method includes the keys defined in the parent.
174 */
175 public Object[] allKeys() {
176 int count = size();
177 ActionMap parent = getParent();
178
179 if (count == 0) {
180 if (parent != null) {
181 return parent.allKeys();
182 }
183 return keys();
184 }
185 if (parent == null) {
186 return keys();
187 }
188 Object[] keys = keys();
189 Object[] pKeys = parent.allKeys();
190
191 if (pKeys == null) {
192 return keys;
193 }
194 if (keys == null) {
195 // Should only happen if size() != keys.length, which should only
196 // happen if mutated from multiple threads (or a bogus subclass).
197 return pKeys;
198 }
199
200 HashMap keyMap = new HashMap();
201 int counter;
202
203 for (counter = keys.length - 1; counter >= 0; counter--) {
204 keyMap.put(keys[counter], keys[counter]);
205 }
206 for (counter = pKeys.length - 1; counter >= 0; counter--) {
207 keyMap.put(pKeys[counter], pKeys[counter]);
208 }
209 return keyMap.keySet().toArray();
210 }
211
212 private void writeObject(ObjectOutputStream s) throws IOException {
213 s.defaultWriteObject();
214
215 ArrayTable.writeArrayTable(s, arrayTable);
216 }
217
218 private void readObject(ObjectInputStream s) throws ClassNotFoundException,
219 IOException {
220 s.defaultReadObject();
221 for (int counter = s.readInt() - 1; counter >= 0; counter--) {
222 put(s.readObject(), (Action)s.readObject());
223 }
224 }
225 }