下面列出了javax.swing.text.Keymap#addActionForKeyStroke ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void setUp () throws Exception {
tc = new TopComponent ();
tc.requestActive();
MockServices.setServices( MyKeymap.class );
Keymap km = Lookup.getDefault().lookup(Keymap.class);
km.addActionForKeyStroke( KEY_STROKE, myGlobalAction );
MyContextAwareAction.globalActionWasPerformed = false;
MyContextAwareAction.contextActionWasPerformed = false;
}
/** Loads key to action mappings into this keymap
* @param bindings array of bindings
* @param actions map of [action_name, action] pairs
*/
public void load(JTextComponent.KeyBinding[] bindings, Map actions) {
// now create bindings in keymap(s)
for (int i = 0; i < bindings.length; i++) {
Action a = (Action)actions.get(bindings[i].actionName);
if (a != null) {
boolean added = false;
if (bindings[i] instanceof MultiKeyBinding) {
MultiKeyBinding mb = (MultiKeyBinding)bindings[i];
if (mb.keys != null) {
Keymap cur = delegate;
for (int j = 0; j < mb.keys.length; j++) {
if (j == mb.keys.length - 1) { // last keystroke in sequence
cur.addActionForKeyStroke(mb.keys[j], a);
} else { // not the last keystroke
Action sca = cur.getAction(mb.keys[j]);
if (!(sca instanceof KeymapSetContextAction)) {
sca = new KeymapSetContextAction(JTextComponent.addKeymap(null, null));
cur.addActionForKeyStroke(mb.keys[j], sca);
}
cur = ((KeymapSetContextAction)sca).contextKeymap;
}
}
added = true;
}
}
if (!added) {
if (bindings[i].key != null) {
delegate.addActionForKeyStroke(bindings[i].key, a);
} else { // key is null -> set default action
setDefaultAction(a);
}
}
}
}
}
/**
* Add keyboard actions to this control using the Configuration we have
* @param map
* @param prefix
*/
public void addSyntaxActions(Keymap map, String prefix) {
// look at all keys that either start with prefix.Action, or
// that start with Action.
Pattern splitter = CONFIG.getValueSeparator(prefix);
Configuration actionsConf = CONFIG.subConfig(prefix, "Action.");
for (String actionName : actionsConf.stringPropertyNames()) {
String[] values = splitter.split(
actionsConf.getProperty(actionName));
String actionClass = values[0];
SyntaxAction action = editorActions.get(actionClass);
if (action == null) {
action = createAction(actionClass);
action.config(CONFIG, prefix, actionName);
}
String keyStrokeString = values[1];
KeyStroke ks = KeyStroke.getKeyStroke(keyStrokeString);
// KeyEvent.VK_QUOTEDBL
if (ks == null) {
throw new IllegalArgumentException("Invalid KeyStroke: " +
keyStrokeString);
}
TextAction ta = action.getAction(actionName);
if(ta == null) {
throw new IllegalArgumentException("Invalid ActionName: " +
actionName);
}
map.addActionForKeyStroke(ks, ta);
}
}
public void initKeyMap(JTextArea text) {
Keymap keyMap = JTextArea.addKeymap("EnterSubmit", text.getKeymap());
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
keyMap.addActionForKeyStroke(key, new SubmitAction());
text.setKeymap(keyMap);
}
public void initKeyMap(JTextArea text) {
Keymap keyMap = JTextArea.addKeymap("EnterSubmit", text.getKeymap());
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
keyMap.addActionForKeyStroke(key, new SubmitAction());
text.setKeymap(keyMap);
}
public void initKeyMap(JTextArea text) {
Keymap keyMap = JTextArea.addKeymap("EnterSubmit", text.getKeymap());
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
keyMap.addActionForKeyStroke(key, new RefreshAction());
text.setKeymap(keyMap);
}