1 package net.suberic.util.gui; 2 import java.awt.event.*; 3 import javax.swing.JComponent ; 4 import javax.swing.KeyStroke ; 5 import javax.swing.Action ; 6 import javax.swing.InputMap ; 7 import javax.swing.ActionMap ; 8 import java.util.Hashtable ; 9 import java.util.Vector ; 10 import java.util.Enumeration ; 11 import net.suberic.util.VariableBundle; 12 13 16 17 public class ConfigurableKeyBinding implements ConfigurableUI { 18 private JComponent currentComponent; 19 private Hashtable commands = new Hashtable (); 20 private Hashtable keyTable = new Hashtable (); 21 private int condition = JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT; 22 23 27 public ConfigurableKeyBinding(JComponent newComponent, String componentID, VariableBundle vars) { 28 currentComponent = newComponent; 29 30 configureComponent(componentID, vars); 31 } 32 33 39 40 public void configureComponent(String componentID, VariableBundle vars) { 41 if (componentID != null && vars.getProperty(componentID, "") != "") { 42 Vector keys = vars.getPropertyAsVector(componentID, ""); 43 for (int i = 0; i < keys.size(); i++) { 44 String keyID = componentID + "." + (String )keys.elementAt(i); 45 String keyAction = vars.getProperty(keyID + ".Action" , ""); 46 KeyStroke keyStroke = getKeyStroke(keyID, vars); 47 if (keyAction != "" && keyStroke != null) { 48 putInKeyTable(keyAction, keyStroke); 49 } 50 } 51 } 52 } 53 54 58 public KeyStroke getKeyStroke(String keyID, VariableBundle vars) { 59 return KeyStroke.getKeyStroke(vars.getProperty(keyID + ".Key", "")); 60 } 61 62 68 public void setActive(Hashtable newCommands) { 69 commands = newCommands; 70 Enumeration hashKeys = getKeyTableKeys(); 71 InputMap inputMap = currentComponent.getInputMap(getCondition()); 72 ActionMap actionMap = currentComponent.getActionMap(); 73 while (hashKeys.hasMoreElements()) { 74 String actionCmd = (String )hashKeys.nextElement(); 75 Vector keyStrokeVector = getFromKeyTable(actionCmd); 76 Action a = getAction(actionCmd); 77 for (int i = 0; keyStrokeVector != null && i < keyStrokeVector.size(); i++) { 78 KeyStroke keyStroke = (KeyStroke )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 91 } 92 } 93 } 94 95 101 public void setActive(Action [] newActions) { 102 Hashtable tmpHash = new Hashtable (); 103 if (newActions != null && newActions.length > 0) { 104 for (int i = 0; i < newActions.length; i++) { 105 String cmdName = (String )newActions[i].getValue(Action.NAME); 106 tmpHash.put(cmdName, newActions[i]); 107 } 108 } 109 setActive(tmpHash); 110 } 111 112 public void putInKeyTable(Object key, Object value) { 113 Vector valueList = (Vector ) keyTable.get(key); 114 if (valueList != null) { 115 if (!valueList.contains(value)) 116 valueList.add(value); 117 } else { 118 valueList = new Vector (); 119 valueList.add(value); 120 keyTable.put(key, valueList); 121 } 122 123 } 124 125 public Vector getFromKeyTable(Object key) { 126 return (Vector ) keyTable.get(key); 127 } 128 129 public Enumeration getKeyTableKeys() { 130 return keyTable.keys(); 131 } 132 133 private Action getAction(String key) { 134 try { 135 return (Action )commands.get(key); 136 } catch (ClassCastException 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 |