KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > ConfigurableKeyBinding


1 package net.suberic.util.gui;
2 import java.awt.event.*;
3 import javax.swing.JComponent JavaDoc;
4 import javax.swing.KeyStroke JavaDoc;
5 import javax.swing.Action JavaDoc;
6 import javax.swing.InputMap JavaDoc;
7 import javax.swing.ActionMap JavaDoc;
8 import java.util.Hashtable JavaDoc;
9 import java.util.Vector JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import net.suberic.util.VariableBundle;
12
13 /**
14  * This class is a KeyBinding controller for a JComponent.
15  */

16
17 public class ConfigurableKeyBinding implements ConfigurableUI {
18   private JComponent JavaDoc currentComponent;
19   private Hashtable JavaDoc commands = new Hashtable JavaDoc();
20   private Hashtable JavaDoc keyTable = new Hashtable JavaDoc();
21   private int condition = JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
22   
23   /**
24    * This creates a new ConfigurableKeyBinding which attaches to component
25    * newComponent, and defines itself using componentID and vars.
26    */

27   public ConfigurableKeyBinding(JComponent JavaDoc newComponent, String JavaDoc componentID, VariableBundle vars) {
28     currentComponent = newComponent;
29     
30     configureComponent(componentID, vars);
31   }
32   
33   /**
34    * This configures the KeyBindings using the given componentID
35    * and VariableBundle.
36    *
37    * As defined in interface net.suberic.util.gui.ConfigurableUI.
38    */

39   
40   public void configureComponent(String JavaDoc componentID, VariableBundle vars) {
41     if (componentID != null && vars.getProperty(componentID, "") != "") {
42       Vector JavaDoc keys = vars.getPropertyAsVector(componentID, "");
43       for (int i = 0; i < keys.size(); i++) {
44     String JavaDoc keyID = componentID + "." + (String JavaDoc)keys.elementAt(i);
45     String JavaDoc keyAction = vars.getProperty(keyID + ".Action" , "");
46     KeyStroke JavaDoc keyStroke = getKeyStroke(keyID, vars);
47     if (keyAction != "" && keyStroke != null) {
48       putInKeyTable(keyAction, keyStroke);
49     }
50       }
51     }
52   }
53   
54   /**
55    * This method returns the key defined by the property keyID in the
56    * VariableBundle vars.
57    */

58   public KeyStroke JavaDoc getKeyStroke(String JavaDoc keyID, VariableBundle vars) {
59     return KeyStroke.getKeyStroke(vars.getProperty(keyID + ".Key", ""));
60   }
61   
62   /**
63    * This method actually binds the configured KeyStrokes to the current
64    * Actions.
65    *
66    * As defined in interface net.suberic.util.gui.ConfigurableUI.
67    */

68   public void setActive(Hashtable JavaDoc newCommands) {
69     commands = newCommands;
70     Enumeration JavaDoc hashKeys = getKeyTableKeys();
71     InputMap JavaDoc inputMap = currentComponent.getInputMap(getCondition());
72     ActionMap JavaDoc actionMap = currentComponent.getActionMap();
73     while (hashKeys.hasMoreElements()) {
74       String JavaDoc actionCmd = (String JavaDoc)hashKeys.nextElement();
75       Vector JavaDoc keyStrokeVector = getFromKeyTable(actionCmd);
76       Action JavaDoc a = getAction(actionCmd);
77       for (int i = 0; keyStrokeVector != null && i < keyStrokeVector.size(); i++) {
78     KeyStroke JavaDoc keyStroke = (KeyStroke JavaDoc)keyStrokeVector.elementAt(i);
79     inputMap.remove(keyStroke);
80     actionMap.remove(actionCmd);
81     if (a != null) {
82       inputMap.put(keyStroke, actionCmd);
83       actionMap.put(actionCmd, a);
84     }
85     /*
86     currentComponent.unregisterKeyboardAction(keyStroke);
87     if (a != null) {
88       currentComponent.registerKeyboardAction(a, actionCmd, keyStroke, getCondition() );
89     }
90     */

91       }
92     }
93   }
94   
95   /**
96    * This method actually binds the configured KeyStrokes to the current
97    * Actions.
98    *
99    * As defined in interface net.suberic.util.gui.ConfigurableUI.
100    */

101   public void setActive(Action JavaDoc[] newActions) {
102     Hashtable JavaDoc tmpHash = new Hashtable JavaDoc();
103     if (newActions != null && newActions.length > 0) {
104       for (int i = 0; i < newActions.length; i++) {
105     String JavaDoc cmdName = (String JavaDoc)newActions[i].getValue(Action.NAME);
106     tmpHash.put(cmdName, newActions[i]);
107       }
108     }
109     setActive(tmpHash);
110   }
111   
112   public void putInKeyTable(Object JavaDoc key, Object JavaDoc value) {
113     Vector JavaDoc valueList = (Vector JavaDoc) keyTable.get(key);
114     if (valueList != null) {
115       if (!valueList.contains(value))
116     valueList.add(value);
117     } else {
118       valueList = new Vector JavaDoc();
119       valueList.add(value);
120       keyTable.put(key, valueList);
121     }
122     
123   }
124   
125   public Vector JavaDoc getFromKeyTable(Object JavaDoc key) {
126     return (Vector JavaDoc) keyTable.get(key);
127   }
128   
129   public Enumeration JavaDoc getKeyTableKeys() {
130     return keyTable.keys();
131   }
132   
133   private Action JavaDoc getAction(String JavaDoc key) {
134     try {
135       return (Action JavaDoc)commands.get(key);
136     } catch (ClassCastException JavaDoc cce) {
137       return null;
138     }
139   }
140   
141   public void setCondition(int newCondition) {
142     condition = newCondition;
143   }
144   
145   public int getCondition() {
146     return condition;
147   }
148
149 }
150
Popular Tags