001 package org.openstreetmap.josm.tools;
002
003 import java.awt.event.InputEvent;
004 import java.awt.event.KeyEvent;
005 import javax.swing.Action;
006 import javax.swing.InputMap;
007 import javax.swing.JButton;
008 import javax.swing.JComponent;
009 import javax.swing.KeyStroke;
010 import javax.swing.SwingUtilities;
011
012 /**
013 * Tools to work with Swing InputMap
014 *
015 */
016 public class InputMapUtils {
017 public static void unassignCtrlShiftUpDown(JComponent cmp, int condition) {
018 InputMap inputMap=SwingUtilities.getUIInputMap(cmp, condition);
019 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP,InputEvent.CTRL_MASK|InputEvent.SHIFT_MASK));
020 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,InputEvent.CTRL_MASK|InputEvent.SHIFT_MASK));
021 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_UP,InputEvent.ALT_MASK|InputEvent.SHIFT_MASK));
022 inputMap.remove(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,InputEvent.ALT_MASK|InputEvent.SHIFT_MASK));
023 SwingUtilities.replaceUIInputMap(cmp,JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,inputMap);
024 }
025
026
027 /**
028 * Enable activating button on Enter (which is replaced with spacebar for certain Look-And-Feels)
029 */
030 public static void enableEnter(JButton b) {
031 b.setFocusable(true);
032 b.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
033 b.getActionMap().put("enter",b.getAction());
034 }
035
036 public static void addEnterAction(JComponent c, Action a) {
037 c.getActionMap().put("enter", a);
038 c.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
039 }
040
041 public static void addSpacebarAction(JComponent c, Action a) {
042 c.getActionMap().put("spacebar", a);
043 c.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "spacebar");
044 }
045
046 }