Example usage for javax.swing JComponent getInputMap

List of usage examples for javax.swing JComponent getInputMap

Introduction

In this page you can find the example usage for javax.swing JComponent getInputMap.

Prototype

public final InputMap getInputMap(int condition) 

Source Link

Document

Returns the InputMap that is used during condition.

Usage

From source file:Main.java

/**
 * install focus forward and backward/*from w  w w  . j  a  v a 2s. c  o  m*/
 */
public static void installDefaultFocusHandling(Container c) {
    // focus TAB
    HashSet<KeyStroke> set = new HashSet<KeyStroke>(1);
    set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
    c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

    // focus shift-TAB
    set = new HashSet<KeyStroke>(1);
    set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
    c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set);

    // make input map WHEN_FOCUSED non-empty for Focus Traversal policy to work
    // correctly
    if (c instanceof JComponent) {
        JComponent jc = (JComponent) c;
        InputMap inputMapWhenFocused = jc.getInputMap(JComponent.WHEN_FOCUSED);
        if (inputMapWhenFocused.size() == 0) {
            inputMapWhenFocused.put(KeyStroke.getKeyStroke(KeyEvent.VK_STOP, KeyEvent.KEY_TYPED),
                    "swingDummyFocusKey");
        }
    }
}

From source file:Main.java

public static void mapAction(JComponent c, int condition, Object key, KeyStroke keyStroke, Action action) {
    c.getActionMap().put(key, action);//  w w  w . j  a v a  2 s .  c  o  m
    c.getInputMap(condition).put(keyStroke, key);
}

From source file:Main.java

public static void associaTeclaAtalho(JComponent component, Action action, String nomeAcao, String... atalhos) {
    final InputMap inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    final ActionMap actionMap = component.getActionMap();
    for (String atalho : atalhos) {
        KeyStroke keyStroke = KeyStroke.getKeyStroke(atalho);
        inputMap.put(keyStroke, nomeAcao);
        actionMap.put(nomeAcao, action);
    }/*ww w  . j a  v a2s  .c o  m*/
}

From source file:Main.java

public static void installActions(JComponent comp, Action actions[], int condition) {
    ActionMap actionMap = comp.getActionMap();
    InputMap inputMap = comp.getInputMap(condition);
    for (int i = 0; i < actions.length; i++) {
        String name = (String) actions[i].getValue(Action.NAME);
        actionMap.put(name, actions[i]);
        inputMap.put((KeyStroke) actions[i].getValue(Action.ACCELERATOR_KEY), name);
    }/*  w w w .j a v  a  2s.c o m*/
}

From source file:Main.java

public static void updateMnemonic(JComponent c, int oldKey, int newKey) {
    if (oldKey == newKey) {
        return;/*from  w ww . java2 s  .co  m*/
    }

    InputMap map = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    if (map != null && oldKey != 0) {
        map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.ALT_MASK));
    }
    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), "press");
    }
}

From source file:Main.java

public static InputMap installInputMap(JComponent c, InputMap map, int condition) {
    if (map == null) {
        return null;
    }/*from   w w w. j  a v a2 s  .c o m*/

    InputMap currentMap = c.getInputMap(condition);
    if (currentMap != null) {
        InputMap parent = currentMap;
        while (parent.getParent() != null) {
            parent = parent.getParent();
        }
        parent.setParent(map);
    } else {
        c.setInputMap(condition, map);
    }
    return map;
}

From source file:Main.java

public static void updateToggleMnemonic(JComponent c, int oldKey, int newKey) {
    if (oldKey == newKey) {
        return;/*  w w w  .j  av  a 2 s .c  o  m*/
    }

    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:Main.java

public static void uninstallInputMap(JComponent c, InputMap map, int condition) {
    if (map == null) {
        return;//from   w  ww.  j ava2  s  . c  o  m
    }

    InputMap firstMap = c.getInputMap(condition);
    InputMap parent = firstMap;
    InputMap child = null;
    InputMap newMap = null;
    while (parent != null) {
        if (parent == map) {
            if (child != null) {
                child.setParent(parent.getParent());
                child = parent;
            } else {
                newMap = parent.getParent();
            }
        } else {
            child = parent;
        }
        parent = parent.getParent();
    }

    if (newMap != null) {
        c.setInputMap(condition, newMap);
    }
}

From source file:Main.java

/**
 * Obtine actiunea inregistrata pentru apasarea de tasta
 * @return // ww  w.  j  a v  a2  s.  c o m
 */
public static Action getActionForKeystroke(JComponent component, int inputMapId, KeyStroke keyStroke) {
    //Identify the action key
    InputMap inputMap = component.getInputMap(inputMapId);
    String key = (String) inputMap.get(keyStroke);

    //Get the action
    if (key != null) {
        ActionMap localMap = component.getActionMap();
        return localMap.get(key);
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Registers an action for a key-stroke in a component.
 * @param actionName The name for the action related to the keystroke. If null, the keystroke description is used.
 * @param focusType e.g. {@link JComponent#WHEN_FOCUSED}
 * @param ks The keystroke that activates the action.
 *//*from   w ww .j  a va 2 s  .  c  om*/
public static void setKeyAction(JComponent component, Action action, String actionName, int focusType,
        KeyStroke ks) {

    if (actionName == null) {
        actionName = ks.toString();
    }
    component.getInputMap(focusType).put(ks, actionName);
    component.getActionMap().put(actionName, action);
}