KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > ActionMap


1 /*
2  * @(#)ActionMap.java 1.14 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.io.IOException JavaDoc;
10 import java.io.ObjectInputStream JavaDoc;
11 import java.io.ObjectOutputStream JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Set JavaDoc;
15
16 /**
17  * <code>ActionMap</code> provides mappings from
18  * <code>Object</code>s
19  * (called <em>keys</em> or <em><code>Action</code> names</em>)
20  * to <code>Action</code>s.
21  * An <code>ActionMap</code> is usually used with an <code>InputMap</code>
22  * to locate a particular action
23  * when a key is pressed. As with <code>InputMap</code>,
24  * an <code>ActionMap</code> can have a parent
25  * that is searched for keys not defined in the <code>ActionMap</code>.
26  * <p>As with <code>InputMap</code> if you create a cycle, eg:
27  * <pre>
28  * ActionMap am = new ActionMap();
29  * ActionMap bm = new ActionMap():
30  * am.setParent(bm);
31  * bm.setParent(am);
32  * </pre>
33  * some of the methods will cause a StackOverflowError to be thrown.
34  *
35  * @see InputMap
36  *
37  * @version 1.14 12/19/03
38  * @author Scott Violet
39  */

40 public class ActionMap implements Serializable JavaDoc {
41     /** Handles the mapping between Action name and Action. */
42     private transient ArrayTable JavaDoc arrayTable;
43     /** Parent that handles any bindings we don't contain. */
44     private ActionMap JavaDoc parent;
45
46
47     /**
48      * Creates an <code>ActionMap</code> with no parent and no mappings.
49      */

50     public ActionMap() {
51     }
52
53     /**
54      * Sets this <code>ActionMap</code>'s parent.
55      *
56      * @param map the <code>ActionMap</code> that is the parent of this one
57      */

58     public void setParent(ActionMap JavaDoc map) {
59     this.parent = map;
60     }
61
62     /**
63      * Returns this <code>ActionMap</code>'s parent.
64      *
65      * @return the <code>ActionMap</code> that is the parent of this one,
66      * or null if this <code>ActionMap</code> has no parent
67      */

68     public ActionMap JavaDoc getParent() {
69     return parent;
70     }
71
72     /**
73      * Adds a binding for <code>key</code> to <code>action</code>.
74      * If <code>action</code> is null, this removes the current binding
75      * for <code>key</code>.
76      * <p>In most instances, <code>key</code> will be
77      * <code>action.getValue(NAME)</code>.
78      */

79     public void put(Object JavaDoc key, Action JavaDoc action) {
80     if (key == null) {
81         return;
82     }
83     if (action == null) {
84         remove(key);
85     }
86     else {
87         if (arrayTable == null) {
88         arrayTable = new ArrayTable JavaDoc();
89         }
90         arrayTable.put(key, action);
91     }
92     }
93
94     /**
95      * Returns the binding for <code>key</code>, messaging the
96      * parent <code>ActionMap</code> if the binding is not locally defined.
97      */

98     public Action JavaDoc get(Object JavaDoc key) {
99     Action JavaDoc value = (arrayTable == null) ? null :
100                    (Action JavaDoc)arrayTable.get(key);
101
102     if (value == null) {
103         ActionMap JavaDoc parent = getParent();
104
105         if (parent != null) {
106         return parent.get(key);
107         }
108     }
109     return value;
110     }
111
112     /**
113      * Removes the binding for <code>key</code> from this <code>ActionMap</code>.
114      */

115     public void remove(Object JavaDoc key) {
116     if (arrayTable != null) {
117         arrayTable.remove(key);
118     }
119     }
120
121     /**
122      * Removes all the mappings from this <code>ActionMap</code>.
123      */

124     public void clear() {
125     if (arrayTable != null) {
126         arrayTable.clear();
127     }
128     }
129
130     /**
131      * Returns the <code>Action</code> names that are bound in this <code>ActionMap</code>.
132      */

133     public Object JavaDoc[] keys() {
134     if (arrayTable == null) {
135         return null;
136     }
137     return arrayTable.getKeys(null);
138     }
139
140     /**
141      * Returns the number of <code>KeyStroke</code> bindings.
142      */

143     public int size() {
144     if (arrayTable == null) {
145         return 0;
146     }
147     return arrayTable.size();
148     }
149
150     /**
151      * Returns an array of the keys defined in this <code>ActionMap</code> and
152      * its parent. This method differs from <code>keys()</code> in that
153      * this method includes the keys defined in the parent.
154      */

155     public Object JavaDoc[] allKeys() {
156     int count = size();
157     ActionMap JavaDoc parent = getParent();
158
159     if (count == 0) {
160         if (parent != null) {
161         return parent.allKeys();
162         }
163         return keys();
164     }
165     if (parent == null) {
166         return keys();
167     }
168     Object JavaDoc[] keys = keys();
169     Object JavaDoc[] pKeys = parent.allKeys();
170
171     if (pKeys == null) {
172         return keys;
173     }
174     if (keys == null) {
175         // Should only happen if size() != keys.length, which should only
176
// happen if mutated from multiple threads (or a bogus subclass).
177
return pKeys;
178     }
179
180     HashMap JavaDoc keyMap = new HashMap JavaDoc();
181     int counter;
182
183     for (counter = keys.length - 1; counter >= 0; counter--) {
184         keyMap.put(keys[counter], keys[counter]);
185     }
186     for (counter = pKeys.length - 1; counter >= 0; counter--) {
187         keyMap.put(pKeys[counter], pKeys[counter]);
188     }
189     return keyMap.keySet().toArray();
190     }
191
192     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
193         s.defaultWriteObject();
194
195         ArrayTable.writeArrayTable(s, arrayTable);
196     }
197
198     private void readObject(ObjectInputStream JavaDoc s) throws ClassNotFoundException JavaDoc,
199                                              IOException JavaDoc {
200         s.defaultReadObject();
201     for (int counter = s.readInt() - 1; counter >= 0; counter--) {
202         put(s.readObject(), (Action JavaDoc)s.readObject());
203     }
204     }
205 }
206
Popular Tags