001: package net.suberic.util.gui;
002:
003: import java.awt.event.*;
004: import javax.swing.JComponent;
005: import javax.swing.KeyStroke;
006: import javax.swing.Action;
007: import javax.swing.InputMap;
008: import javax.swing.ActionMap;
009: import java.util.Hashtable;
010: import java.util.Vector;
011: import java.util.Enumeration;
012: import net.suberic.util.VariableBundle;
013:
014: /**
015: * This class is a KeyBinding controller for a JComponent.
016: */
017:
018: public class ConfigurableKeyBinding implements ConfigurableUI {
019: private JComponent currentComponent;
020: private Hashtable commands = new Hashtable();
021: private Hashtable keyTable = new Hashtable();
022: private int condition = JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
023:
024: /**
025: * This creates a new ConfigurableKeyBinding which attaches to component
026: * newComponent, and defines itself using componentID and vars.
027: */
028: public ConfigurableKeyBinding(JComponent newComponent,
029: String componentID, VariableBundle vars) {
030: currentComponent = newComponent;
031:
032: configureComponent(componentID, vars);
033: }
034:
035: /**
036: * This configures the KeyBindings using the given componentID
037: * and VariableBundle.
038: *
039: * As defined in interface net.suberic.util.gui.ConfigurableUI.
040: */
041:
042: public void configureComponent(String componentID,
043: VariableBundle vars) {
044: if (componentID != null
045: && vars.getProperty(componentID, "") != "") {
046: Vector keys = vars.getPropertyAsVector(componentID, "");
047: for (int i = 0; i < keys.size(); i++) {
048: String keyID = componentID + "."
049: + (String) keys.elementAt(i);
050: String keyAction = vars.getProperty(keyID + ".Action",
051: "");
052: KeyStroke keyStroke = getKeyStroke(keyID, vars);
053: if (keyAction != "" && keyStroke != null) {
054: putInKeyTable(keyAction, keyStroke);
055: }
056: }
057: }
058: }
059:
060: /**
061: * This method returns the key defined by the property keyID in the
062: * VariableBundle vars.
063: */
064: public KeyStroke getKeyStroke(String keyID, VariableBundle vars) {
065: return KeyStroke.getKeyStroke(vars.getProperty(keyID + ".Key",
066: ""));
067: }
068:
069: /**
070: * This method actually binds the configured KeyStrokes to the current
071: * Actions.
072: *
073: * As defined in interface net.suberic.util.gui.ConfigurableUI.
074: */
075: public void setActive(Hashtable newCommands) {
076: commands = newCommands;
077: Enumeration hashKeys = getKeyTableKeys();
078: InputMap inputMap = currentComponent
079: .getInputMap(getCondition());
080: ActionMap actionMap = currentComponent.getActionMap();
081: while (hashKeys.hasMoreElements()) {
082: String actionCmd = (String) hashKeys.nextElement();
083: Vector keyStrokeVector = getFromKeyTable(actionCmd);
084: Action a = getAction(actionCmd);
085: for (int i = 0; keyStrokeVector != null
086: && i < keyStrokeVector.size(); i++) {
087: KeyStroke keyStroke = (KeyStroke) keyStrokeVector
088: .elementAt(i);
089: inputMap.remove(keyStroke);
090: actionMap.remove(actionCmd);
091: if (a != null) {
092: inputMap.put(keyStroke, actionCmd);
093: actionMap.put(actionCmd, a);
094: }
095: /*
096: currentComponent.unregisterKeyboardAction(keyStroke);
097: if (a != null) {
098: currentComponent.registerKeyboardAction(a, actionCmd, keyStroke, getCondition() );
099: }
100: */
101: }
102: }
103: }
104:
105: /**
106: * This method actually binds the configured KeyStrokes to the current
107: * Actions.
108: *
109: * As defined in interface net.suberic.util.gui.ConfigurableUI.
110: */
111: public void setActive(Action[] newActions) {
112: Hashtable tmpHash = new Hashtable();
113: if (newActions != null && newActions.length > 0) {
114: for (int i = 0; i < newActions.length; i++) {
115: String cmdName = (String) newActions[i]
116: .getValue(Action.NAME);
117: tmpHash.put(cmdName, newActions[i]);
118: }
119: }
120: setActive(tmpHash);
121: }
122:
123: public void putInKeyTable(Object key, Object value) {
124: Vector valueList = (Vector) keyTable.get(key);
125: if (valueList != null) {
126: if (!valueList.contains(value))
127: valueList.add(value);
128: } else {
129: valueList = new Vector();
130: valueList.add(value);
131: keyTable.put(key, valueList);
132: }
133:
134: }
135:
136: public Vector getFromKeyTable(Object key) {
137: return (Vector) keyTable.get(key);
138: }
139:
140: public Enumeration getKeyTableKeys() {
141: return keyTable.keys();
142: }
143:
144: private Action getAction(String key) {
145: try {
146: return (Action) commands.get(key);
147: } catch (ClassCastException cce) {
148: return null;
149: }
150: }
151:
152: public void setCondition(int newCondition) {
153: condition = newCondition;
154: }
155:
156: public int getCondition() {
157: return condition;
158: }
159:
160: }
|