Example usage for javax.swing JMenuItem getAccelerator

List of usage examples for javax.swing JMenuItem getAccelerator

Introduction

In this page you can find the example usage for javax.swing JMenuItem getAccelerator.

Prototype

public KeyStroke getAccelerator() 

Source Link

Document

Returns the KeyStroke which serves as an accelerator for the menu item.

Usage

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 ww  .  jav a 2s . c  om
    }

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

/** Brings the destination menu item into sync with the source item. */
protected static void syncMenuItem(final JMenuItem source, final JMenuItem dest) {
    final boolean enabled = source.isEnabled();
    if (dest.isEnabled() != enabled)
        dest.setEnabled(enabled);// w w  w  . ja  va 2s.c o m
    final int mnemonic = source.getMnemonic();
    if (dest.getMnemonic() != mnemonic)
        dest.setMnemonic(mnemonic);
    final String text = source.getText();
    if (dest.getText() != text)
        dest.setText(text);
    final KeyStroke accel = source.getAccelerator();
    if (dest.getAccelerator() != accel)
        dest.setAccelerator(accel);
}

From source file:com.AandR.beans.plotting.imagePlotPanel.CanvasPanel.java

/**
 *
 *//*w w  w .  j  a v a2  s .  co m*/
public void actionPerformed(ActionEvent e) {
    JMenuItem item = (JMenuItem) e.getSource();
    int modifiers = item.getAccelerator().getModifiers();
    int keyCode = item.getAccelerator().getKeyCode();
    dispatchEvent(new KeyEvent(this, KeyEvent.KEY_PRESSED, 0, modifiers, keyCode, KeyEvent.CHAR_UNDEFINED));
}

From source file:org.kepler.gui.MenuMapper.java

/**
 * Recurse through all the submenu heirarchy beneath the passed JMenu
 * parameter, and for each "leaf node" (ie a menu item that is not a
 * container for other menu items), add the Action and its menu path to the
 * passed Map/*w  ww.  jav a2 s  .c  om*/
 * 
 * @param nextMenuItem
 *            the JMenu to recurse into
 * @param menuPathBuff
 *            a delimited String representation of the hierarchical "path"
 *            to this menu item. This will be used as the key in the
 *            actionsMap. For example, the "Graph Editor" menu item beneath
 *            the "New" item on the "File" menu would have a menuPath of
 *            File->New->Graph Editor. Delimeter is "->" (no quotes), and
 *            spaces are allowed within menu text strings, but not around
 *            the delimiters; i.e: New->Graph Editor is OK, but File ->New
 *            is not.
 * @param MENU_PATH_DELIMITER
 *            String
 * @param actionsMap
 *            the Map containing key => value pairs of the form: menuPath
 *            (as described above) => Action (the javax.swing.Action
 *            assigned to this menu item)
 */
public static void storePTIIMenuItems(JMenuItem nextMenuItem, StringBuffer menuPathBuff,
        final String MENU_PATH_DELIMITER, Map<String, Action> actionsMap) {

    menuPathBuff.append(MENU_PATH_DELIMITER);
    if (nextMenuItem != null && nextMenuItem.getText() != null) {
        String str = nextMenuItem.getText();
        // do not make the recent files menu item upper case since
        // it contains paths in the file system.
        if (menuPathBuff.toString().startsWith("FILE->RECENT FILES->")) {
            menuPathBuff = new StringBuffer("File->Recent Files->");
        } else {
            str = str.toUpperCase();
        }
        menuPathBuff.append(str);
    }

    if (isDebugging) {
        log.debug(menuPathBuff.toString());
    }
    // System.out.println(menuPathBuff.toString());

    if (nextMenuItem instanceof JMenu) {
        storePTIITopLevelMenus((JMenu) nextMenuItem, menuPathBuff.toString(), MENU_PATH_DELIMITER, actionsMap);
    } else {
        Action nextAction = nextMenuItem.getAction();
        // if there is no Action, look for an ActionListener
        // System.out.println("Processing menu " + nextMenuItem.getText());
        if (nextAction == null) {
            final ActionListener[] actionListeners = nextMenuItem.getActionListeners();
            // System.out.println("No Action for " + nextMenuItem.getText()
            // + "; found " + actionListeners.length
            // + " ActionListeners");
            if (actionListeners.length > 0) {
                if (isDebugging) {
                    log.debug(actionListeners[0].getClass().getName());
                }
                // ASSUMPTION: there is only one ActionListener
                nextAction = new AbstractAction() {
                    public void actionPerformed(ActionEvent a) {
                        actionListeners[0].actionPerformed(a);
                    }
                };
                // add all these values - @see diva.gui.GUIUtilities
                nextAction.putValue(Action.NAME, nextMenuItem.getText());
                // System.out.println("storing ptII action for menu " +
                // nextMenuItem.getText());
                nextAction.putValue(GUIUtilities.LARGE_ICON, nextMenuItem.getIcon());
                nextAction.putValue(GUIUtilities.MNEMONIC_KEY, new Integer(nextMenuItem.getMnemonic()));
                nextAction.putValue("tooltip", nextMenuItem.getToolTipText());
                nextAction.putValue(GUIUtilities.ACCELERATOR_KEY, nextMenuItem.getAccelerator());
                nextAction.putValue("menuItem", nextMenuItem);
            } else {
                if (isDebugging) {
                    log.warn("No Action or ActionListener found for " + nextMenuItem.getText());
                }
            }
        }
        if (!actionsMap.containsValue(nextAction)) {
            actionsMap.put(menuPathBuff.toString(), nextAction);
            if (isDebugging) {
                log.debug(menuPathBuff.toString() + " :: ACTION: " + nextAction);
            }
        }
    }
}

From source file:org.omegat.gui.shortcuts.PropertiesShortcutsTest.java

/**
 * Test of bindKeyStrokes method, of class PropertiesShortcuts.
 *///from ww w. j av a  2s  .c om
@Test
public void testBindKeyStrokes_JMenuBar() {
    JMenuBar menu = new JMenuBar();
    JMenu parent = new JMenu();
    JMenuItem child1 = new JMenu();
    JMenuItem child2 = new JMenuItem();
    child2.setActionCommand(TEST_DELETE);
    child2.setAccelerator(CTRL_D);
    JMenuItem grandchild1 = new JMenuItem();
    grandchild1.setActionCommand(TEST_USER_1);
    JMenuItem grandchild2 = new JMenuItem();
    grandchild2.setActionCommand(OUT_OF_LIST);
    grandchild2.setAccelerator(CTRL_X);
    menu.add(parent);
    parent.add(child1);
    parent.add(child2);
    child1.add(grandchild1);
    child1.add(grandchild2);

    // bind
    shotcuts.bindKeyStrokes(menu);

    KeyStroke result = parent.getAccelerator();
    assertNull(result);

    result = child1.getAccelerator();
    assertNull(result);

    result = child2.getAccelerator();
    assertNull(result);

    KeyStroke expected = CTRL_P;
    result = grandchild1.getAccelerator();
    assertEquals(expected, result);

    expected = CTRL_X;
    result = grandchild2.getAccelerator();
    assertEquals(expected, result);
}

From source file:org.omegat.gui.shortcuts.PropertiesShortcutsTest.java

/**
 * Test of bindKeyStrokes method, of class PropertiesShortcuts.
 *///  ww  w  .  ja va 2  s. com
@Test
public void testBindKeyStrokes_JMenuItem() {
    // case JMenuItem with no children
    JMenuItem item = new JMenuItem();
    item.setActionCommand(TEST_SAVE);
    KeyStroke expected = CTRL_S;
    KeyStroke result = item.getAccelerator();
    assertNull(result); // before binding
    shotcuts.bindKeyStrokes(item); // bind
    result = item.getAccelerator();
    assertEquals(expected, result); // after binding(1)

    item.setActionCommand(TEST_DELETE);
    shotcuts.bindKeyStrokes(item); // bind
    result = item.getAccelerator();
    assertNull(result); // after binding(2)

    item.setActionCommand(OUT_OF_LIST);
    item.setAccelerator(CTRL_D);
    shotcuts.bindKeyStrokes(item); // bind
    expected = CTRL_D;
    result = item.getAccelerator();
    assertEquals(expected, result); // after binding(3) - nothing has changed
}

From source file:org.omegat.gui.shortcuts.PropertiesShortcutsTest.java

/**
 * Test of bindKeyStrokes method, of class PropertiesShortcuts.
 *///from   www. j a v  a  2 s.c  o m
@Test
public void testBindKeyStrokes_JMenuItem_Recursive() {
    // case JMenu with children
    JMenu parent = new JMenu();
    JMenuItem child1 = new JMenu();
    JMenuItem child2 = new JMenuItem();
    child2.setActionCommand(TEST_DELETE);
    child2.setAccelerator(CTRL_D);
    JMenuItem grandchild1 = new JMenuItem();
    grandchild1.setActionCommand(TEST_USER_1);
    JMenuItem grandchild2 = new JMenuItem();
    grandchild2.setActionCommand(OUT_OF_LIST);
    grandchild2.setAccelerator(CTRL_X);
    parent.add(child1);
    parent.add(child2);
    child1.add(grandchild1);
    child1.add(grandchild2);

    // bind
    shotcuts.bindKeyStrokes(parent);

    KeyStroke result = parent.getAccelerator();
    assertNull(result);

    result = child1.getAccelerator();
    assertNull(result);

    result = child2.getAccelerator();
    assertNull(result);

    KeyStroke expected = CTRL_P;
    result = grandchild1.getAccelerator();
    assertEquals(expected, result);

    expected = CTRL_X;
    result = grandchild2.getAccelerator();
    assertEquals(expected, result);
}