下面列出了javax.swing.InputMap#get ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void dumpInputMap( PrintWriter out, InputMap inputMap, String indent ) {
if( indent == null )
indent = " ";
out.printf( "%d %s", inputMap.size(), dumpClass( inputMap ) );
KeyStroke[] keys = inputMap.keys();
if( keys != null ) {
Arrays.sort( keys, (keyStroke1, keyStroke2) -> {
return String.valueOf( keyStroke1 ).compareTo( String.valueOf( keyStroke2 ) );
} );
for( KeyStroke keyStroke : keys ) {
Object value = inputMap.get( keyStroke );
String strKeyStroke = keyStroke.toString().replace( "pressed ", "" );
out.printf( "%n%s%-20s %s", indent, strKeyStroke, value );
}
}
InputMap parent = inputMap.getParent();
if( parent != null )
dumpInputMap( out, parent, indent + " " );
}
private JavaAgentKeys getOSKey() {
KeyStroke selectall = null;
InputMap inputMap = new JTextField().getInputMap();
KeyStroke[] allKeys = inputMap.allKeys();
for (KeyStroke keyStroke : allKeys) {
Object object = inputMap.get(keyStroke);
if (object.equals("select-all")) {
selectall = keyStroke;
break;
}
}
if ((selectall.getModifiers() & InputEvent.CTRL_DOWN_MASK) == InputEvent.CTRL_DOWN_MASK) {
return JavaAgentKeys.CONTROL;
}
if ((selectall.getModifiers() & InputEvent.META_DOWN_MASK) == InputEvent.META_DOWN_MASK) {
return JavaAgentKeys.META;
}
throw new RuntimeException("Which key?");
}
public void checkKeystrokesForActions() throws Throwable {
StringBuilder sb = new StringBuilder();
driver = new JavaDriver();
WebElement textField = driver.findElement(By.cssSelector("text-field"));
JTextField f = new JTextField();
InputMap inputMap = f.getInputMap();
KeyStroke[] allKeys = inputMap.allKeys();
for (KeyStroke keyStroke : allKeys) {
Object object = inputMap.get(keyStroke);
try {
OSUtils.getKeysFor(textField, object.toString());
} catch (Throwable t) {
sb.append("failed for(" + object + "): " + keyStroke);
}
}
AssertJUnit.assertEquals("", sb.toString());
}
private void consumeIfKeyPressInActionMap(KeyEvent e) {
// get popup's registered keyboard actions
ActionMap am = popup.getActionMap();
InputMap im = popup.getInputMap();
// check whether popup registers keystroke
// If we consumed key pressed, we need to consume other key events as well:
KeyStroke ks = KeyStroke.getKeyStrokeForEvent(
new KeyEvent((Component) e.getSource(),
KeyEvent.KEY_PRESSED,
e.getWhen(),
e.getModifiers(),
KeyEvent.getExtendedKeyCodeForChar(e.getKeyChar()),
e.getKeyChar(),
e.getKeyLocation())
);
Object obj = im.get(ks);
if (shouldPopupReceiveForwardedKeyboardAction(obj)) {
// if yes, if there is a popup's action, consume key event
Action action = am.get(obj);
if (action != null && action.isEnabled()) {
// actionPerformed on key press only.
e.consume();
}
}
}
protected boolean emulatorAction(KeyStroke ks, KeyEvent e){
if (sessionGui == null)
return false;
InputMap map = getInputMap();
ActionMap am = getActionMap();
if(map != null && am != null && sessionGui.isEnabled()) {
Object binding = map.get(ks);
Action action = (binding == null) ? null : am.get(binding);
if (action != null) {
return true;
}
}
return false;
}
private void uninstallMaps() {
InputMap currMap = SwingUtilities.getUIInputMap(this.jcomp,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
InputMap newMap = new InputMap();
if (currMap != null) {
KeyStroke[] kss = currMap.allKeys();
for (int i = 0; i < kss.length; i++) {
KeyStroke stroke = kss[i];
Object val = currMap.get(stroke);
if (stroke
.equals(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, InputEvent.CTRL_DOWN_MASK))) {
if ("tabSwitcherForward".equals(val))
continue;
}
if (stroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, InputEvent.CTRL_DOWN_MASK))) {
if ("tabSwitcherBackward".equals(val))
continue;
}
if (stroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, 0, true))) {
if ("tabSwitcherClose".equals(val))
continue;
}
if (stroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0))) {
if ("tabSwitcherHide".equals(val))
continue;
}
newMap.put(stroke, val);
}
}
this.jcomp.getActionMap().remove("tabSwitcherForward");
this.jcomp.getActionMap().remove("tabSwitcherBackward");
this.jcomp.getActionMap().remove("tabSwitcherClose");
this.jcomp.getActionMap().remove("tabSwitcherHide");
SwingUtilities.replaceUIInputMap(this.jcomp, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
newMap);
}
/**
* Installs a "trigger key" action onto the current text component.
*
* @param ks The keystroke that should trigger the action.
* @see #uninstallTriggerKey()
*/
private void installTriggerKey(KeyStroke ks) {
InputMap im = textComponent.getInputMap();
oldTriggerKey = im.get(ks);
im.put(ks, PARAM_TRIGGER_KEY);
ActionMap am = textComponent.getActionMap();
oldTriggerAction = am.get(PARAM_TRIGGER_KEY);
am.put(PARAM_TRIGGER_KEY, createAutoCompleteAction());
}
private Action getCopyActionDelegate(InputMap map, KeyStroke ks) {
ActionMap am = getActionMap();
if(map != null && am != null && isEnabled()) {
Object binding = map.get(ks);
final Action action = (binding == null) ? null : am.get(binding);
if (action != null) {
return new CopyToClipboardAction(action);
}
}
return null;
}
@Override
public void actionPerformed(ActionEvent e) {
if (isEditing() || editorComp != null) {
removeEditor();
return;
} else {
Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
InputMap imp = getRootPane().getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap am = getRootPane().getActionMap();
KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
Object key = imp.get(escape);
if (key == null) {
//Default for NbDialog
key = "Cancel";
}
if (key != null) {
Action a = am.get(key);
if (a != null) {
String commandKey = (String)a.getValue(Action.ACTION_COMMAND_KEY);
if (commandKey == null) {
commandKey = key.toString();
}
a.actionPerformed(new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, commandKey)); //NOI18N
}
}
}
}
public @Override void keyPressed(KeyEvent e){
if (e != null && popup != null && popup.isShowing()) {
// get popup's registered keyboard actions
ActionMap am = popup.getActionMap();
InputMap im = popup.getInputMap();
// check whether popup registers keystroke
KeyStroke ks = KeyStroke.getKeyStrokeForEvent(e);
Object obj = im.get(ks);
LOG.log(Level.FINE, "Keystroke for event {0}: {1}; action-map-key={2}", new Object [] { e, ks, obj }); //NOI18N
if (shouldPopupReceiveForwardedKeyboardAction(obj)) {
// if yes, gets the popup's action for this keystroke, perform it
// and consume key event
Action action = am.get(obj);
LOG.log(Level.FINE, "Popup component''s action: {0}, {1}", new Object [] { action, action != null ? action.getValue(Action.NAME) : null }); //NOI18N
/* Make sure to use the popup as the source of the action, since the popup is
also providing the event. Not doing this, and instead invoking actionPerformed
with a null ActionEvent, was one part of the problem seen in NETBEANS-403. */
if (SwingUtilities.notifyAction(action, ks, e, popup, e.getModifiers())) {
e.consume();
return;
}
}
if (e.getKeyCode() != KeyEvent.VK_CONTROL &&
e.getKeyCode() != KeyEvent.VK_SHIFT &&
e.getKeyCode() != KeyEvent.VK_ALT &&
e.getKeyCode() != KeyEvent.VK_ALT_GRAPH &&
e.getKeyCode() != KeyEvent.VK_META
) {
// hide tooltip if any was shown
Utilities.getEditorUI(textComponent).getToolTipSupport().setToolTipVisible(false);
}
}
}
@CheckForNull
private Pair<String,JComponent> listActionFor(KeyEvent ev) {
InputMap map = matchesList.getInputMap();
Object o = map.get(KeyStroke.getKeyStrokeForEvent(ev));
if (o instanceof String) {
return Pair.<String,JComponent>of((String)o, matchesList);
}
map = matchesScrollPane1.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
o = map.get(KeyStroke.getKeyStrokeForEvent(ev));
if (o instanceof String) {
return Pair.<String,JComponent>of((String)o, matchesScrollPane1);
}
return null;
}
@CheckForNull
private Pair<String,JComponent> listActionFor(KeyEvent ev) {
InputMap map = resultList.getInputMap();
Object o = map.get(KeyStroke.getKeyStrokeForEvent(ev));
if (o instanceof String) {
return Pair.<String,JComponent>of((String)o, resultList);
}
map = resultScrollPane.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
o = map.get(KeyStroke.getKeyStrokeForEvent(ev));
if (o instanceof String) {
return Pair.<String,JComponent>of((String)o, resultScrollPane);
}
return null;
}
@CheckForNull
private Pair<String,JComponent> listActionFor(KeyEvent ev) {
InputMap map = matchesList.getInputMap();
Object o = map.get(KeyStroke.getKeyStrokeForEvent(ev));
if (o instanceof String) {
return Pair.<String,JComponent>of((String)o, matchesList);
}
map = matchesScrollPane1.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
o = map.get(KeyStroke.getKeyStrokeForEvent(ev));
if (o instanceof String) {
return Pair.<String,JComponent>of((String)o, matchesScrollPane1);
}
return null;
}
/**
* Installs this auto-completion on a text component. If this
* {@link AnimationAutoCompletion} is already installed on another text component,
* it is uninstalled first.
*
* @param c The text component.
* @see #uninstall()
*/
@Override
public void install(JTextComponent c) {
if (textComponent != null) {
uninstall();
}
this.textComponent = c;
installTriggerKey(getTriggerKey());
// Install the function completion key, if there is one.
// NOTE: We cannot do this if the start char is ' ' (e.g. just a space
// between the function name and parameters) because it overrides
// RSTA's special space action. It seems KeyStorke.getKeyStroke(' ')
// hoses ctrl+space, shift+space, etc., even though I think it
// shouldn't...
char start = provider.getParameterListStart();
if (start != 0 && start != ' ') {
InputMap im = c.getInputMap();
ActionMap am = c.getActionMap();
KeyStroke ks = KeyStroke.getKeyStroke(start);
oldParenKey = im.get(ks);
im.put(ks, PARAM_COMPLETE_KEY);
oldParenAction = am.get(PARAM_COMPLETE_KEY);
am.put(PARAM_COMPLETE_KEY, new ParameterizedCompletionStartAction(
start));
}
textComponentListener.addTo(this.textComponent);
// In case textComponent is already in a window...
textComponentListener.hierarchyChanged(null);
if (isAutoActivationEnabled()) {
autoActivationListener.addTo(this.textComponent);
}
UIManager.addPropertyChangeListener(lafListener);
updateUI(); // In case there have been changes since we uninstalled
}
/** Transmits escape sequence to dialog */
private void trySendEscToDialog() {
if (isTableUI()) {
//let the table decide, don't be preemptive
return;
}
// System.err.println("SendEscToDialog");
EventObject ev = EventQueue.getCurrentEvent();
if (ev instanceof KeyEvent && (((KeyEvent) ev).getKeyCode() == KeyEvent.VK_ESCAPE)) {
if (ev.getSource() instanceof JComboBox && ((JComboBox) ev.getSource()).isPopupVisible()) {
return;
}
if (
ev.getSource() instanceof JTextComponent &&
((JTextComponent) ev.getSource()).getParent() instanceof JComboBox &&
((JComboBox) ((JTextComponent) ev.getSource()).getParent()).isPopupVisible()
) {
return;
}
InputMap imp = getRootPane().getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap am = getRootPane().getActionMap();
KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
Object key = imp.get(escape);
if (key != null) {
Action a = am.get(key);
if (a != null) {
if (Boolean.getBoolean("netbeans.proppanel.logDialogActions")) { //NOI18N
System.err.println("Action bound to escape key is " + a); //NOI18N
}
String commandKey = (String) a.getValue(Action.ACTION_COMMAND_KEY);
if (commandKey == null) {
commandKey = "cancel"; //NOI18N
}
a.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, commandKey)); //NOI18N
}
}
}
}
private void trySendEscToDialog(JTable jt) {
// System.err.println("SendEscToDialog");
EventObject ev = EventQueue.getCurrentEvent();
if (ev instanceof KeyEvent && (((KeyEvent) ev).getKeyCode() == KeyEvent.VK_ESCAPE)) {
if (ev.getSource() instanceof JComboBox && ((JComboBox) ev.getSource()).isPopupVisible()) {
return;
}
if (
ev.getSource() instanceof JTextComponent &&
((JTextComponent) ev.getSource()).getParent() instanceof JComboBox &&
((JComboBox) ((JTextComponent) ev.getSource()).getParent()).isPopupVisible()
) {
return;
}
InputMap imp = jt.getRootPane().getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap am = jt.getRootPane().getActionMap();
KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
Object key = imp.get(escape);
if (key != null) {
Action a = am.get(key);
if (a != null) {
if (Boolean.getBoolean("netbeans.proppanel.logDialogActions")) { //NOI18N
System.err.println("Action bound to escape key is " + a); //NOI18N
}
//Actions registered with deprecated registerKeyboardAction will
//need this lookup of the action command
String commandKey = (String) a.getValue(Action.ACTION_COMMAND_KEY);
if (commandKey == null) {
commandKey = "cancel"; //NOI18N
}
a.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, commandKey)); //NOI18N
}
}
}
}