Java Swing KeyStroke getActionForKeystroke(JComponent component, int inputMapId, KeyStroke keyStroke)

Here you can find the source of getActionForKeystroke(JComponent component, int inputMapId, KeyStroke keyStroke)

Description

Obtine actiunea inregistrata pentru apasarea de tasta

License

Open Source License

Declaration

public static Action getActionForKeystroke(JComponent component, int inputMapId, KeyStroke keyStroke) 

Method Source Code

//package com.java2s;
/*/* w  ww  .ja v a 2s .c o m*/
 *
 * This file is part of Genome Artist.
 *
 * Genome Artist is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Genome Artist is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Genome Artist.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

public class Main {
    /**
     * Obtine actiunea inregistrata pentru apasarea de tasta
     * @return actiune inregistrata pentru tasta sau null
     */
    public static Action getActionForKeystroke(JComponent component, KeyStroke keyStroke) {
        Action whenFocused = getActionForKeystroke(component, JComponent.WHEN_FOCUSED, keyStroke);
        Action whenAncestorOfFocused = getActionForKeystroke(component,
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, keyStroke);
        Action whenInWindow = getActionForKeystroke(component, JComponent.WHEN_IN_FOCUSED_WINDOW, keyStroke);

        //Ordinea preferata pentru a le returna
        if (whenFocused != null)
            return whenFocused;
        if (whenAncestorOfFocused != null)
            return whenAncestorOfFocused;
        if (whenInWindow != null)
            return whenInWindow;

        //Valoare default
        return null;
    }

    /**
     * Obtine actiunea inregistrata pentru apasarea de tasta
     * @return 
     */
    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;
        }
    }
}

Related

  1. convertShortcutMask(KeyStroke ks, int shortcutMask)
  2. createItem(String name, Integer mnem, KeyStroke accel)
  3. createKeyMask(final int aKeyStroke, final int... aMasks)
  4. fixTabKeys(final JComponent component)
  5. getAccelerator(final int key)
  6. getKeyStroke(String s)
  7. getKeyStrokeWithoutCtrlModifier(KeyStroke stroke)
  8. getMnemonic(final String key)
  9. installAction(JComponent comp, Action action, KeyStroke keyStroke, String actionKey)