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

/**
 * Registers the keystroke of the given action as "command" of the given
 * component./*from  w  w w.ja va2s  . c  o  m*/
 * <p>
 * This code is based on the Sulky-tools, found at
 * &lt;http://github.com/huxi/sulky&gt;.
 * </p>
 * 
 * @param aComponent
 *          the component that should react on the keystroke, cannot be
 *          <code>null</code>;
 * @param aAction
 *          the action of the keystroke, cannot be <code>null</code>;
 * @param aCommandName
 *          the name of the command to register the keystore under.
 */
public static void registerKeystroke(final JComponent aComponent, final Action aAction,
        final String aCommandName) {
    final KeyStroke keyStroke = (KeyStroke) aAction.getValue(Action.ACCELERATOR_KEY);
    if (keyStroke == null) {
        return;
    }

    InputMap inputMap = aComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = aComponent.getActionMap();
    inputMap.put(keyStroke, aCommandName);
    actionMap.put(aCommandName, aAction);

    inputMap = aComponent.getInputMap(JComponent.WHEN_FOCUSED);
    Object value = inputMap.get(keyStroke);
    if (value != null) {
        inputMap.put(keyStroke, aCommandName);
    }

    inputMap = aComponent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    value = inputMap.get(keyStroke);
    if (value != null) {
        inputMap.put(keyStroke, aCommandName);
    }
}

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);
    }//from  ww w  . ja v a2  s  .  c om
}

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);
    }// www .  j a va 2s .c  o  m
}

From source file:Main.java

/**
 * Adds a component action./*from  ww w  .j  av a 2s  .  c  o  m*/
 *
 * @param component
 *            The compoennt 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:Main.java

/**
 * Sets the hot key for focus.// w ww  .j  a  va  2 s  .c o  m
 *
 * @param comp
 *            the comp
 * @param keyStroke
 *            the key stroke
 * @param actionName
 *            the action name
 */
public static void setHotKeyForFocus(final JComponent comp, final String keyStroke, final String actionName) {
    // get the button's Action map
    final ActionMap amap = comp.getActionMap();
    // add an action to the button's action map
    // and give it a name(it can be any object not just String)
    amap.put(actionName, new AbstractAction() {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(final ActionEvent e) {
            // call your a method that contains your action code
            comp.requestFocus();
        }
    });
    // get the input map for the button
    final InputMap imap = comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    // add a key stroke associated with an action in the action map(action
    // name).
    // imap.put(KeyStroke.getKeyStroke("F1"),"ActionName");
    // you can do the same for more than one key.
    imap.put(KeyStroke.getKeyStroke(keyStroke), actionName);
}

From source file:com.projity.pm.graphic.spreadsheet.common.transfer.NodeListTransferHandler.java

public static void registerWith(SpreadSheet spreadSheet) {
    NodeListTransferHandler handler = new NodeListTransferHandler(spreadSheet);
    //          if (c instanceof SpreadSheet){
    //             SpreadSheet spreadSheet=(SpreadSheet)c;
    //             handler.setSpreadSheet(spreadSheet);
    //          }
    spreadSheet.setTransferHandler(handler);

    InputMap imap = spreadSheet.getInputMap();
    imap.put(KeyStroke.getKeyStroke("ctrl X"), NodeListTransferHandler.getCutAction().getValue(Action.NAME));
    imap.put(KeyStroke.getKeyStroke("ctrl C"), NodeListTransferHandler.getCopyAction().getValue(Action.NAME));
    imap.put(KeyStroke.getKeyStroke("ctrl V"), NodeListTransferHandler.getPasteAction().getValue(Action.NAME));
    //c.setInputMap(JComponent.WHEN_FOCUSED,imap);

    ActionMap amap = spreadSheet.getActionMap();
    amap.put(NodeListTransferHandler.getCutAction().getValue(Action.NAME),
            NodeListTransferHandler.getCutAction());
    amap.put(NodeListTransferHandler.getCopyAction().getValue(Action.NAME),
            NodeListTransferHandler.getCopyAction());
    amap.put(NodeListTransferHandler.getPasteAction().getValue(Action.NAME),
            NodeListTransferHandler.getPasteAction());

}

From source file:Main.java

public static void updateMnemonic(JComponent c, int oldKey, int newKey) {
    if (oldKey == newKey) {
        return;//from  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));
    }
    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 void updateAccelerator(JMenuItem menuItem, KeyStroke oldAccelerator) {
    KeyStroke accelerator = menuItem.getAccelerator();
    if (oldAccelerator != null && oldAccelerator.equals(accelerator)) {
        return;/*from   w w w.j  ava2s .  c  o m*/
    }

    InputMap map = menuItem.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    if (map != null && oldAccelerator != null) {
        map.remove(oldAccelerator);
    }
    if (accelerator != null) {
        if (map == null) {
            map = new ComponentInputMap(menuItem);
            menuItem.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map);
        }
        map.put(accelerator, "click");
    }
}

From source file:com.mirth.connect.client.ui.Mirth.java

public static void initUIManager() {
    try {/*from   www . j  a  v a2  s  . c  om*/
        PlasticLookAndFeel.setPlasticTheme(new MirthTheme());
        PlasticXPLookAndFeel look = new PlasticXPLookAndFeel();
        UIManager.setLookAndFeel(look);
        UIManager.put("win.xpstyle.name", "metallic");
        LookAndFeelAddons.setAddon(WindowsLookAndFeelAddons.class);

        /*
         * MIRTH-1225 and MIRTH-2019: Create alternate key bindings if CTRL is not the same as
         * the menu shortcut key (i.e. COMMAND on OSX)
         */
        if (InputEvent.CTRL_MASK != Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) {
            createAlternateKeyBindings();
        }

        if (SystemUtils.IS_OS_MAC) {
            OSXAdapter.setAboutHandler(Mirth.class, Mirth.class.getDeclaredMethod("aboutMac", (Class[]) null));
            OSXAdapter.setQuitHandler(Mirth.class, Mirth.class.getDeclaredMethod("quitMac", (Class[]) null));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    // keep the tooltips from disappearing
    ToolTipManager.sharedInstance().setDismissDelay(3600000);

    // TabbedPane defaults
    // UIManager.put("TabbedPane.selected", new Color(0xffffff));
    // UIManager.put("TabbedPane.background",new Color(225,225,225));
    // UIManager.put("TabbedPane.tabAreaBackground",new Color(225,225,225));
    UIManager.put("TabbedPane.highlight", new Color(225, 225, 225));
    UIManager.put("TabbedPane.selectHighlight", new Color(0xc3c3c3));
    UIManager.put("TabbedPane.contentBorderInsets", new InsetsUIResource(0, 0, 0, 0));

    // TaskPane defaults
    UIManager.put("TaskPane.titleBackgroundGradientStart", new Color(0xffffff));
    UIManager.put("TaskPane.titleBackgroundGradientEnd", new Color(0xffffff));

    // Set fonts
    UIManager.put("TextPane.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("ToggleButton.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("Panel.font", UIConstants.DIALOG_FONT);
    UIManager.put("PopupMenu.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("OptionPane.font", UIConstants.DIALOG_FONT);
    UIManager.put("Label.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("Tree.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("ScrollPane.font", UIConstants.DIALOG_FONT);
    UIManager.put("TextField.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("Viewport.font", UIConstants.DIALOG_FONT);
    UIManager.put("MenuBar.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("FormattedTextField.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("DesktopIcon.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("TableHeader.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("ToolTip.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("PasswordField.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("TaskPane.font", UIConstants.TEXTFIELD_BOLD_FONT);
    UIManager.put("Table.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("TabbedPane.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("ProgressBar.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("CheckBoxMenuItem.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("ColorChooser.font", UIConstants.DIALOG_FONT);
    UIManager.put("Button.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("TextArea.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("Spinner.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("RadioButton.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("TitledBorder.font", UIConstants.TEXTFIELD_BOLD_FONT);
    UIManager.put("EditorPane.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("RadioButtonMenuItem.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("ToolBar.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("MenuItem.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("CheckBox.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("JXTitledPanel.title.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("Menu.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("ComboBox.font", UIConstants.TEXTFIELD_PLAIN_FONT);
    UIManager.put("JXLoginPanel.banner.font", UIConstants.BANNER_FONT);
    UIManager.put("List.font", UIConstants.TEXTFIELD_PLAIN_FONT);

    InputMap im = (InputMap) UIManager.get("Button.focusInputMap");
    im.put(KeyStroke.getKeyStroke("pressed ENTER"), "pressed");
    im.put(KeyStroke.getKeyStroke("released ENTER"), "released");

    try {
        UIManager.put("wizard.sidebar.image",
                ImageIO.read(com.mirth.connect.client.ui.Frame.class.getResource("images/wizardsidebar.png")));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

@SuppressWarnings("serial")
public static void installUndoManager(JTextComponent textComponent, final UndoManager undoManager) {

    Document doc = textComponent.getDocument();
    doc.addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent e) {
            undoManager.addEdit(e.getEdit());
        }//from  w  w  w  .  j  a va2s .  com
    });

    ActionMap am = textComponent.getActionMap();
    InputMap im = textComponent.getInputMap();
    am.put("undo", new AbstractAction("undo") {
        @Override
        public void actionPerformed(ActionEvent e) {
            undoManager.undo();
        }

        @Override
        public boolean isEnabled() {
            return undoManager.canUndo();
        }
    });
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, getMenuShortcutKeyMask()), "undo");

    am.put("redo", new AbstractAction("redo") {
        @Override
        public void actionPerformed(ActionEvent e) {
            undoManager.redo();
        }

        @Override
        public boolean isEnabled() {
            return undoManager.canRedo();
        }
    });
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, getMenuShortcutKeyMask()), "redo");
}