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