下面列出了怎么用javax.swing.text.Keymap的API类实例代码及写法,或者点击链接到github查看源代码。
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
// This method is implemented due to the issue
// #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
KeyStroke[] ret = new KeyStroke[] { defaultKey };
if (component != null && editorActionName != null) {
Action a = component.getActionMap().get(editorActionName);
Keymap km = component.getKeymap();
if (a != null && km != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if (keys != null && keys.length > 0) {
ret = keys;
}
}
}
return ret;
}
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
// This method is implemented due to the issue
// #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
KeyStroke[] ret = new KeyStroke[] { defaultKey };
if (component != null) {
TextUI componentUI = component.getUI();
Keymap km = component.getKeymap();
if (componentUI != null && km != null) {
EditorKit kit = componentUI.getEditorKit(component);
if (kit instanceof BaseKit) {
Action a = ((BaseKit)kit).getActionByName(editorActionName);
if (a != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if (keys != null && keys.length > 0) {
ret = keys;
}
}
}
}
}
return ret;
}
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
// This method is implemented due to the issue
// #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
KeyStroke[] ret = new KeyStroke[] { defaultKey };
if (component != null) {
TextUI componentUI = component.getUI();
Keymap km = component.getKeymap();
if (componentUI != null && km != null) {
EditorKit kit = componentUI.getEditorKit(component);
if (kit instanceof BaseKit) {
Action a = ((BaseKit)kit).getActionByName(editorActionName);
if (a != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if (keys != null && keys.length > 0) {
ret = keys;
}
}
}
}
}
return ret;
}
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
// This method is implemented due to the issue
// #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
KeyStroke[] ret = new KeyStroke[] { defaultKey };
if (component != null) {
TextUI componentUI = component.getUI();
Keymap km = component.getKeymap();
if (componentUI != null && km != null) {
EditorKit kit = componentUI.getEditorKit(component);
if (kit instanceof BaseKit) {
Action a = ((BaseKit)kit).getActionByName(editorActionName);
if (a != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if (keys != null && keys.length > 0) {
ret = keys;
}
}
}
}
}
return ret;
}
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey) {
// This method is implemented due to the issue
// #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
KeyStroke[] ret = new KeyStroke[] { defaultKey };
if (component != null) {
Action a = component.getActionMap().get(editorActionName);
Keymap km = component.getKeymap();
if (a != null && km != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if (keys != null && keys.length > 0) {
ret = keys;
}
}
}
return ret;
}
private void addAcceleretors(Action a, JMenuItem item, JTextComponent target) {
// Try to get the accelerator
Keymap km = target.getKeymap();
if (km != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if ((keys != null) && (keys.length > 0)) {
item.setAccelerator(keys[0]);
} else if (a != null) {
KeyStroke ks = (KeyStroke)a.getValue(Action.ACCELERATOR_KEY);
if (ks != null) {
item.setAccelerator(ks);
}
}
}
}
private void applyShortcuts() {
if (!termOptions.getIgnoreKeymap()) {
Set<String> actions = new HashSet<String>();
for (FileObject def : shortcutsDir.getChildren()) {
try {
DataObject dobj = DataObject.find(def);
InstanceCookie ic = dobj.getLookup().lookup(InstanceCookie.class);
if (ic != null) {
// put class names in the map,
// otherwise we may end with several instances of the action
actions.add(ic.instanceCreate().getClass().getName());
}
} catch (Exception e) {
Exceptions.printStackTrace(e);
}
}
term.setKeymap(Lookup.getDefault().lookup(Keymap.class), actions);
// needed for Ctrl+Tab, Ctrl+Shift+Tab switching
term.getScreen().setFocusTraversalKeysEnabled(false);
} else {
term.setKeymap(null, null);
term.getScreen().setFocusTraversalKeysEnabled(true);
}
}
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
// This method is implemented due to the issue
// #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
KeyStroke[] ret = new KeyStroke[] { defaultKey };
if (component != null && editorActionName != null) {
Action a = component.getActionMap().get(editorActionName);
Keymap km = component.getKeymap();
if (a != null && km != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if (keys != null && keys.length > 0) {
ret = keys;
}
}
}
return ret;
}
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
// This method is implemented due to the issue
// #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
KeyStroke[] ret = new KeyStroke[] { defaultKey };
if (component != null) {
TextUI componentUI = component.getUI();
Keymap km = component.getKeymap();
if (componentUI != null && km != null) {
EditorKit kit = componentUI.getEditorKit(component);
if (kit instanceof BaseKit) {
Action a = ((BaseKit)kit).getActionByName(editorActionName);
if (a != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if (keys != null && keys.length > 0) {
ret = keys;
}
}
}
}
}
return ret;
}
private static void assignAccelerator(Keymap km, Action action, JMenuItem item) {
if (item.getAccelerator() == null){
KeyStroke ks = (KeyStroke)action.getValue(Action.ACCELERATOR_KEY);
if (ks!=null) {
item.setMnemonic(ks.getKeyCode());
} else {
// Try to get the accelerator from keymap
if (km != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(action);
if (keys != null && keys.length > 0) {
item.setMnemonic(keys[0].getKeyCode());
}
}
}
}
}
private void addAcceleretors(Action a, JMenuItem item, JTextComponent target){
// Try to get the accelerator
Keymap km = (target == null) ? BaseKit.getKit(BaseKit.class).getKeymap() :
target.getKeymap();
if (km != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if (keys != null && keys.length > 0) {
boolean added = false;
for (int i = 0; i<keys.length; i++){
if ((keys[i].getKeyCode() == KeyEvent.VK_MULTIPLY) ||
keys[i].getKeyCode() == KeyEvent.VK_ADD){
item.setMnemonic(keys[i].getKeyCode());
added = true;
break;
}
}
if (added == false) item.setMnemonic(keys[0].getKeyCode());
}
}
}
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(JTextComponent component, String editorActionName, KeyStroke defaultKey) {
// This method is implemented due to the issue
// #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
KeyStroke[] ret = new KeyStroke[] { defaultKey };
if (component != null) {
Action a = component.getActionMap().get(editorActionName);
Keymap km = component.getKeymap();
if (a != null && km != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if (keys != null && keys.length > 0) {
ret = keys;
}
}
}
return ret;
}
/** Adds accelerators to given JMenuItem taken from the action */
protected static void addAccelerators(Action a, JMenuItem item, JTextComponent target){
if (target == null || a==null || item==null) return;
// get accelerators from kitAction
Action kitAction = getActionByName((String)a.getValue(Action.NAME));
if (kitAction!=null) a = kitAction;
// Try to get the accelerator, TopComponent action could be obsoleted
Keymap km = target.getKeymap();
if (km != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
KeyStroke itemAccelerator = item.getAccelerator();
if (keys != null && keys.length > 0) {
if (itemAccelerator==null || !itemAccelerator.equals(keys[0])){
item.setAccelerator(keys[0]);
}
}else{
if (itemAccelerator!=null && kitAction!=null){
item.setAccelerator(null);
}
}
}
}
protected @Override KeyStroke keyStrokeForAction(Action action, FileObject definingFile) {
Keymap km = Lookup.getDefault().lookup(Keymap.class);
if (km instanceof NbKeymap) {
return ((NbKeymap) km).keyStrokeForAction(action, definingFile);
} else {
LOG.log(Level.WARNING, "unexpected keymap: {0}", km);
return null;
}
}
private boolean processShortcut(KeyEvent ev) {
//ignore shortcut keys when the IDE is shutting down
if (NbLifecycleManager.isExiting()) {
ev.consume();
return true;
}
KeyStroke ks = KeyStroke.getKeyStrokeForEvent(ev);
Window w = SwingUtilities.windowForComponent(ev.getComponent());
// don't process shortcuts if this is a help frame
if ((w instanceof JFrame) && ((JFrame)w).getRootPane().getClientProperty("netbeans.helpframe") != null) // NOI18N
return true;
// don't let action keystrokes to propagate from both
// modal and nonmodal dialogs, but propagate from separate floating windows,
// even if they are backed by JDialog
if ((w instanceof Dialog) &&
!WindowManagerImpl.isSeparateWindow(w) &&
!isTransmodalAction(ks)) {
return false;
}
// Provide a reasonably useful action event that identifies what was focused
// when the key was pressed, as well as what keystroke ran the action.
ActionEvent aev = new ActionEvent(
ev.getSource(), ActionEvent.ACTION_PERFORMED, Utilities.keyToString(ks));
Keymap root = Lookup.getDefault().lookup(Keymap.class);
Action a = root.getAction (ks);
if (a != null && a.isEnabled()) {
ActionManager am = Lookup.getDefault().lookup(ActionManager.class);
am.invokeAction(a, aev);
ev.consume();
return true;
}
return false;
}
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);
}
}
}
}
}
public void setResolveParent(Keymap parent) {
if (context != null) {
context.setResolveParent(parent);
} else {
delegate.setResolveParent(parent);
}
}
@Override
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
boolean ret = super.processKeyBinding(ks, e, condition, pressed);
// XXX #250546 Reason of overriding: to process global shortcut.
if ((JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT == condition) && (ret == false) && !e.isConsumed()) {
Keymap km = Lookup.getDefault().lookup(Keymap.class);
Action action = (km != null) ? km.getAction(ks) : null;
if (action == null) {
return false;
}
if (action instanceof CallbackSystemAction) {
CallbackSystemAction csAction = (CallbackSystemAction) action;
if (tabbedPane != null) {
Action a = tabbedPane.getActionMap().get(csAction.getActionMapKey());
if (a != null) {
a.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, Utilities.keyToString(ks)));
return true;
}
}
}
return false;
} else {
return ret;
}
}
@Override
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
boolean ret = super.processKeyBinding(ks, e, condition, pressed);
// XXX #250546 Reason of overriding: to process global shortcut.
if ((JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT == condition) && (ret == false) && !e.isConsumed()) {
Keymap km = Lookup.getDefault().lookup(Keymap.class);
Action action = (km != null) ? km.getAction(ks) : null;
if (action == null) {
return false;
}
if (action instanceof CallbackSystemAction) {
CallbackSystemAction csAction = (CallbackSystemAction) action;
if (tabbedPane != null) {
Action a = tabbedPane.getActionMap().get(csAction.getActionMapKey());
if (a != null) {
a.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, Utilities.keyToString(ks)));
return true;
}
}
}
return false;
} else {
return ret;
}
}
@Override
public void setKeymap(Keymap map) {
//#181722: keymaps are shared among components with the same UI
//a default action will be set to the Keymap of this component below,
//so it is necessary to use a Keymap that is not shared with other JTextAreas
super.setKeymap(addKeymap(null, map));
}
private static void addAcceleretors(Action a, JMenuItem item, JTextComponent target) {
// Try to get the accelerator
Keymap km = (target == null) ? BaseKit.getKit(BaseKit.class).getKeymap() :
target.getKeymap();
if (km != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if (keys != null && keys.length > 0) {
boolean added = false;
for (int i = 0; i<keys.length; i++){
if ((keys[i].getKeyCode() == KeyEvent.VK_MULTIPLY) ||
keys[i].getKeyCode() == KeyEvent.VK_ADD){
item.setMnemonic(keys[i].getKeyCode());
added = true;
break;
}
}
if (added == false) {
item.setMnemonic(keys[0].getKeyCode());
}
}else if (a!=null){
KeyStroke ks = (KeyStroke)a.getValue(Action.ACCELERATOR_KEY);
if (ks!=null) {
item.setMnemonic(ks.getKeyCode());
}
}
}
}
/** Attempt to find the editor keystroke for the given action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey) {
KeyStroke[] ret = new KeyStroke[] { defaultKey };
JTextComponent comp = getComponent();
if (editorActionName != null && comp != null) {
TextUI textUI = comp.getUI();
Keymap km = comp.getKeymap();
if (textUI != null && km != null) {
EditorKit kit = textUI.getEditorKit(comp);
if (kit instanceof BaseKit) {
Action a = ((BaseKit)kit).getActionByName(editorActionName);
if (a != null) {
KeyStroke[] keys = km.getKeyStrokesForAction(a);
if (keys != null && keys.length > 0) {
ret = keys;
} else {
// try kit's keymap
Keymap km2 = ((BaseKit)kit).getKeymap();
KeyStroke[] keys2 = km2.getKeyStrokesForAction(a);
if (keys2 != null && keys2.length > 0) {
ret = keys2;
}
}
}
}
}
}
return ret;
}
@Override
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
boolean ret = super.processKeyBinding(ks, e, condition, pressed);
// XXX #250546 Reason of overriding: to process global shortcut.
if ((JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT == condition) && (ret == false) && !e.isConsumed()) {
Keymap km = Lookup.getDefault().lookup(Keymap.class);
Action action = (km != null) ? km.getAction(ks) : null;
if (action == null) {
return false;
}
if (action instanceof CallbackSystemAction) {
CallbackSystemAction csAction = (CallbackSystemAction) action;
if (tabbedPane != null) {
Action a = tabbedPane.getActionMap().get(csAction.getActionMapKey());
if (a != null) {
a.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, Utilities.keyToString(ks)));
return true;
}
}
}
return false;
} else {
return ret;
}
}
/**
* Maps {@code JTextComponent.getKeymap()} through queue
*/
public Keymap getKeymap() {
return (runMapping(new MapAction<Keymap>("getKeymap") {
@Override
public Keymap map() {
return ((JTextComponent) getSource()).getKeymap();
}
}));
}
/**
* Maps {@code JTextComponent.setKeymap(Keymap)} through queue
*/
public void setKeymap(final Keymap keymap) {
runMapping(new MapVoidAction("setKeymap") {
@Override
public void map() {
((JTextComponent) getSource()).setKeymap(keymap);
}
});
}
/**
* 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);
}
}
@Override
public void install(JEditorPane target) {
super.install(target);
// add key bindings
Keymap keymap = target.getKeymap();
JTextComponent.loadKeymap(keymap, getKeyBindings(), target.getActions());
}
/**
* JTextPane上でCommand+Cとかでコピペをできるようにする
*/
private void addShortcutForMac() {
if (!PacketProxyUtility.getInstance().isMac()) {
return;
}
JPanel p = (JPanel) getContentPane();
InputMap im = p.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = p.getActionMap();
int hotkey = (KeyEvent.CTRL_MASK | KeyEvent.META_MASK);
registerTabShortcut(KeyEvent.VK_H, hotkey, im, am, Panes.HISTORY.ordinal());
registerTabShortcut(KeyEvent.VK_I, hotkey, im, am, Panes.INTERCEPT.ordinal());
registerTabShortcut(KeyEvent.VK_R, hotkey, im, am, Panes.REPEATER.ordinal());
registerTabShortcut(KeyEvent.VK_B, hotkey, im, am, Panes.BULKSENDER.ordinal());
registerTabShortcut(KeyEvent.VK_O, hotkey, im, am, Panes.OPTIONS.ordinal());
registerTabShortcut(KeyEvent.VK_L, hotkey, im, am, Panes.LOG.ordinal());
JTextComponent.KeyBinding[] bindings1 = {
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
DefaultEditorKit.copyAction),
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
DefaultEditorKit.pasteAction),
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
DefaultEditorKit.cutAction),
new JTextComponent.KeyBinding(
KeyStroke.getKeyStroke(KeyEvent.VK_A, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()),
DefaultEditorKit.selectAllAction),
};
JTextPane component_tp = new JTextPane();
Keymap keymap_tp = component_tp.getKeymap();
JTextComponent.loadKeymap(keymap_tp, bindings1, component_tp.getActions());
JTextField component_tf = new JTextField();
Keymap keymap_tf = component_tf.getKeymap();
JTextComponent.loadKeymap(keymap_tf, bindings1, component_tf.getActions());
JTextArea component_ta = new JTextArea();
Keymap keymap_ta = component_ta.getKeymap();
JTextComponent.loadKeymap(keymap_ta, bindings1, component_ta.getActions());
}
public Keymap createKeymap() {
if (keymap == null) {
keymap = new SimpleKeymap(this, interp);
}
return keymap;
}