Example usage for javax.swing InputMap put

List of usage examples for javax.swing InputMap put

Introduction

In this page you can find the example usage for javax.swing InputMap put.

Prototype

public void put(KeyStroke keyStroke, Object actionMapKey) 

Source Link

Document

Adds a binding for keyStroke to actionMapKey .

Usage

From source file:Main.java

/**
 * A helper for creating and updating key bindings for components with
 * mnemonics. The {@code pressed} action will be invoked when the mnemonic
 * is activated and the {@code released} action will be invoked when the
 * mnemonic is deactivated.//from ww w .  j  a v  a 2s .  co m
 * <p>
 * TODO establish an interface for the mnemonic properties, such as {@code
 * MnemonicEnabled} and change signature to {@code public static <T extends
 * JComponent & MnemonicEnabled> void updateMnemonicBinding(T c, String
 * pressed, String released)}
 * 
 * @param c
 *            the component bindings to update
 * @param pressed
 *            the name of the action in the action map to invoke when the
 *            mnemonic is pressed
 * @param released
 *            the name of the action in the action map to invoke when the
 *            mnemonic is released (if the action is a toggle style, then
 *            this parameter should be {@code null})
 * @throws NullPointerException
 *             if the component is {@code null}
 */
public static void updateMnemonicBinding(JComponent c, String pressed, String released) {
    Class<?> clazz = c.getClass();
    int m = -1;

    try {
        Method mtd = clazz.getMethod("getMnemonic");
        m = (Integer) mtd.invoke(c);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalArgumentException("unable to access mnemonic", e);
    }

    InputMap map = SwingUtilities.getUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW);

    if (m != 0) {
        if (map == null) {
            map = new ComponentInputMapUIResource(c);
            SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW, map);
        }

        map.clear();

        //TODO is ALT_MASK right for all platforms?
        map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, false), pressed);
        map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, true), released);
        map.put(KeyStroke.getKeyStroke(m, 0, true), released);
    } else {
        if (map != null) {
            map.clear();
        }
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopComponentsHelper.java

/**
 * Add shortcut action to any JComponent.
 *
 * @param name name of action that used as action key in {@link InputMap} and {@link ActionMap}.
 * @param component//  w ww .  jav  a 2  s . co  m
 * @param key
 * @param action
 */
public static void addShortcutAction(String name, JComponent component, KeyStroke key, Action action) {
    ActionMap actionMap = component.getActionMap();
    InputMap inputMap = component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(key, name);
    actionMap.put(name, action);
}

From source file:Main.java

public static void updateToggleMnemonic(JComponent c, int oldKey, int newKey) {
    if (oldKey == newKey) {
        return;/*from w w w .j a va2  s  .  com*/
    }

    InputMap map = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    if (map != null && oldKey != 0) {
        map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.ALT_MASK, false));
        map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.ALT_MASK, true));
        map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.VK_UNDEFINED, true));
    }
    if (newKey != 0) {
        if (map == null) {
            map = new ComponentInputMap(c);
            c.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map);
        }
        map.put(KeyStroke.getKeyStroke(newKey, KeyEvent.ALT_MASK, false), "press");
        map.put(KeyStroke.getKeyStroke(newKey, KeyEvent.ALT_MASK, true), "release");
        map.put(KeyStroke.getKeyStroke(newKey, KeyEvent.VK_UNDEFINED, true), "release");
    }
}

From source file:de.ailis.xadrian.utils.SwingUtils.java

/**
 * Adds a component action./*from  ww  w  .  j  a  v a 2  s . c  o m*/
 * 
 * @param component
 *            The compoenet to add the action to
 * @param action
 *            The action to add
 */
public static void addComponentAction(final JComponent component, final Action action) {
    final InputMap imap = component
            .getInputMap(component.isFocusable() ? JComponent.WHEN_FOCUSED : JComponent.WHEN_IN_FOCUSED_WINDOW);
    final ActionMap amap = component.getActionMap();
    final KeyStroke ks = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
    imap.put(ks, action.getValue(Action.NAME));
    amap.put(action.getValue(Action.NAME), action);
}

From source file:net.sf.jabref.util.Util.java

/**
 * Binds ESC-Key to cancel button//from   www.ja  v  a 2s.  com
 *
 * @param rootPane     the pane to bind the action to. Typically, this variable is retrieved by this.getRootPane();
 * @param cancelAction the action to bind
 */
// TODO: move to GUI
public static void bindCloseDialogKeyToCancelAction(JRootPane rootPane, Action cancelAction) {
    InputMap im = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = rootPane.getActionMap();
    im.put(Globals.getKeyPrefs().getKey(KeyBinding.CLOSE_DIALOG), "close");
    am.put("close", cancelAction);
}

From source file:com.limegroup.gnutella.gui.GUIUtils.java

/**
 * Adds an action to hide a window / dialog.
 *
 * On OSX, this is done by typing 'Command-W'.
 * On all other platforms, this is done by hitting 'ESC'.
 *//*from w w  w  .java  2s  .  co  m*/
public static void addHideAction(JComponent jc) {
    InputMap map = jc.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    map.put(getHideKeystroke(), "limewire.hideWindow");
    jc.getActionMap().put("limewire.hideWindow", getDisposeAction());
}

From source file:com.limegroup.gnutella.gui.GUIUtils.java

/**
 * Binds a key stroke to the given action for the component. The action is 
 * triggered when the key is pressed and the keyboard focus is withing the
 * specifiedd scope./*from  w  w  w .  j av  a2  s .c o  m*/
 *  
 * @param c component for which the keybinding is installed
 * @param key the key that triggers the action
 * @param a the action
 * @param focusScope one of {@link JComponent.WHEN_FOCUSED},
 * {@link JComponent.WHEN_IN_FOCUSED_WINDOW},
 * {@link JComponent.WHEN_ANCESTOR_OF_FOCUSED_WINDOW}
 */
public static void bindKeyToAction(JComponent c, KeyStroke key, Action a, int focusScope) {
    InputMap inputMap = c.getInputMap(focusScope);
    ActionMap actionMap = c.getActionMap();
    if (inputMap != null && actionMap != null) {
        inputMap.put(key, a);
        actionMap.put(a, a);
    }
}

From source file:EscapeDialog.java

License:asdf

protected JRootPane createRootPane() {
    JRootPane rootPane = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("about to disappear");
            setVisible(false);/*from  w w  w . j  a va 2  s.  com*/
        }
    };
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPane.getActionMap().put("ESCAPE", actionListener);

    return rootPane;
}

From source file:Main.java

private void addKeyBind(JComponent contentPane, String key) {
    InputMap iMap = contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap aMap = contentPane.getActionMap();
    iMap.put(KeyStroke.getKeyStroke(key), DISABLE_CLICKER);
    aMap.put(DISABLE_CLICKER, disableButtonAction);
}

From source file:FrameKey.java

protected JRootPane createRootPane() {
    JRootPane rootPane = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            setVisible(false);//from w  w  w .  j av a2 s .  c om
        }
    };
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPane.getActionMap().put("ESCAPE", actionListener);

    return rootPane;
}