KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > InputMap


1 /*
2  * @(#)InputMap.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>InputMap</code> provides a binding between an input event
18  * (currently only <code>KeyStroke</code>s are used)
19  * and an <code>Object</code>. <code>InputMap</code>s
20  * are usually used with an <code>ActionMap</code>,
21  * to determine an <code>Action</code> to perform
22  * when a key is pressed.
23  * An <code>InputMap</code> can have a parent
24  * that is searched for bindings not defined in the <code>InputMap</code>.
25  * <p>As with <code>ActionMap</code> if you create a cycle, eg:
26  * <pre>
27  * InputMap am = new InputMap();
28  * InputMap bm = new InputMap():
29  * am.setParent(bm);
30  * bm.setParent(am);
31  * </pre>
32  * some of the methods will cause a StackOverflowError to be thrown.
33  *
34  * @version 1.14 12/19/03
35  * @author Scott Violet
36  * @since 1.3
37  */

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

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

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

66     public InputMap JavaDoc getParent() {
67     return parent;
68     }
69
70     /**
71      * Adds a binding for <code>keyStroke</code> to <code>actionMapKey</code>.
72      * If <code>actionMapKey</code> is null, this removes the current binding
73      * for <code>keyStroke</code>.
74      */

75     public void put(KeyStroke JavaDoc keyStroke, Object JavaDoc actionMapKey) {
76     if (keyStroke == null) {
77         return;
78     }
79     if (actionMapKey == null) {
80         remove(keyStroke);
81     }
82     else {
83         if (arrayTable == null) {
84         arrayTable = new ArrayTable JavaDoc();
85         }
86         arrayTable.put(keyStroke, actionMapKey);
87     }
88     }
89
90     /**
91      * Returns the binding for <code>keyStroke</code>, messaging the
92      * parent <code>InputMap</code> if the binding is not locally defined.
93      */

94     public Object JavaDoc get(KeyStroke JavaDoc keyStroke) {
95     if (arrayTable == null) {
96         InputMap JavaDoc parent = getParent();
97
98         if (parent != null) {
99         return parent.get(keyStroke);
100         }
101         return null;
102     }
103     Object JavaDoc value = arrayTable.get(keyStroke);
104
105     if (value == null) {
106         InputMap JavaDoc parent = getParent();
107
108         if (parent != null) {
109         return parent.get(keyStroke);
110         }
111     }
112     return value;
113     }
114
115     /**
116      * Removes the binding for <code>key</code> from this
117      * <code>InputMap</code>.
118      */

119     public void remove(KeyStroke JavaDoc key) {
120     if (arrayTable != null) {
121         arrayTable.remove(key);
122     }
123     }
124
125     /**
126      * Removes all the mappings from this <code>InputMap</code>.
127      */

128     public void clear() {
129     if (arrayTable != null) {
130         arrayTable.clear();
131     }
132     }
133
134     /**
135      * Returns the <code>KeyStroke</code>s that are bound in this <code>InputMap</code>.
136      */

137     public KeyStroke JavaDoc[] keys() {
138     if (arrayTable == null) {
139         return null;
140     }
141     KeyStroke JavaDoc[] keys = new KeyStroke JavaDoc[arrayTable.size()];
142     arrayTable.getKeys(keys);
143     return keys;
144     }
145
146     /**
147      * Returns the number of <code>KeyStroke</code> bindings.
148      */

149     public int size() {
150     if (arrayTable == null) {
151         return 0;
152     }
153     return arrayTable.size();
154     }
155
156     /**
157      * Returns an array of the <code>KeyStroke</code>s defined in this
158      * <code>InputMap</code> and its parent. This differs from <code>keys()</code> in that
159      * this method includes the keys defined in the parent.
160      */

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