KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > InputMap


1 /*
2    SwingWT
3    Copyright(c)2003-2004 Daniel Naab
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: dannaab@users.sourceforge.net
9
10    $Log: InputMap.java,v $
11    Revision 1.2 2004/04/16 22:45:50 dannaab
12    Add copyright msg
13
14 */

15
16 package swingwtx.swing;
17
18 import java.util.HashMap JavaDoc;
19
20 /**
21  * InputMap implementation... used by JComponent to define behavior on state changes.
22  *
23  * @author Naab
24  * @version %I%, %G%
25  */

26 public class InputMap
27 {
28     private HashMap JavaDoc hashMap = new HashMap JavaDoc();
29     private InputMap parent = null;
30
31     public void setParent(InputMap parent) { this.parent = parent; }
32     public InputMap getParent() { return parent; }
33
34     public Object JavaDoc get(KeyStroke keyStroke)
35     {
36         Object JavaDoc value = null;
37
38         if (hashMap.containsKey(value))
39             value = hashMap.get(keyStroke);
40         else if (parent != null)
41             value = parent.get(keyStroke);
42
43         return value;
44     }
45
46     public void put(KeyStroke keyStroke, Object JavaDoc actionMapKey)
47     {
48         if (keyStroke != null)
49         {
50             if (actionMapKey == null) remove(keyStroke);
51             else hashMap.put(keyStroke, actionMapKey);
52         }
53     }
54
55     public void remove(KeyStroke key) { hashMap.remove(key); }
56     public void clear() { hashMap.clear(); }
57     public KeyStroke[] keys() { return (KeyStroke[]) hashMap.keySet().toArray(new KeyStroke[0]); }
58     public int size() { return hashMap.size(); }
59
60     // Package protected helpers for allKeys() method
61
HashMap JavaDoc getHashMap() { return hashMap; }
62     HashMap JavaDoc allKeyValues()
63     {
64         HashMap JavaDoc keyValues = (HashMap JavaDoc) hashMap.clone();
65         if (parent != null) keyValues.putAll(parent.allKeyValues());
66         return keyValues;
67     }
68
69     public KeyStroke[] allKeys()
70     {
71         return (KeyStroke[]) allKeyValues().keySet().toArray(new KeyStroke[0]);
72     }
73 }
74
Popular Tags