/** Opens the SubMenu */
    public void menuKeyTyped(MenuKeyEvent e) {
      if (!crossMenuMnemonic) {
        JPopupMenu pm = getActivePopupMenu();
        if (pm != null && pm != menuItem.getParent()) {
          return;
        }
      }

      int key = menuItem.getMnemonic();
      if (key == 0) return;
      MenuElement path[] = e.getPath();
      if (lower((char) key) == lower(e.getKeyChar())) {
        JPopupMenu popupMenu = ((JMenu) menuItem).getPopupMenu();
        MenuElement sub[] = popupMenu.getSubElements();
        if (sub.length > 0) {
          MenuSelectionManager manager = e.getMenuSelectionManager();
          MenuElement newPath[] = new MenuElement[path.length + 2];
          System.arraycopy(path, 0, newPath, 0, path.length);
          newPath[path.length] = popupMenu;
          newPath[path.length + 1] = sub[0];
          manager.setSelectedPath(newPath);
        }
        e.consume();
      }
    }
예제 #2
0
    public void menuKeyPressed(MenuKeyEvent e) {

      if (e.getKeyCode() == mnemonic) {
        ActionEvent ae =
            new ActionEvent(
                e.getSource(), e.getID(), (String) getValue(Action.NAME), e.getModifiers());
        actionPerformed(ae);

        e.consume();
      }
    }
예제 #3
0
  /**
   * Processes a key event forwarded from the <code>MenuSelectionManager</code> and changes the menu
   * selection, if necessary, by using <code>MenuSelectionManager</code>'s API.
   *
   * <p>Note: you do not have to forward the event to sub-components. This is done automatically by
   * the <code>MenuSelectionManager</code>.
   *
   * @param e a <code>KeyEvent</code>
   * @param path the <code>MenuElement</code> path array
   * @param manager the <code>MenuSelectionManager</code>
   */
  public void processKeyEvent(KeyEvent e, MenuElement path[], MenuSelectionManager manager) {
    MenuKeyEvent mke =
        new MenuKeyEvent(
            e.getComponent(),
            e.getID(),
            e.getWhen(),
            e.getModifiers(),
            e.getKeyCode(),
            e.getKeyChar(),
            path,
            manager);
    processMenuKeyEvent(mke);

    if (mke.isConsumed()) {
      e.consume();
    }
  }
    /**
     * Handles the mnemonics for the menu items. Will also handle duplicate mnemonics. Perhaps this
     * should be moved into BasicPopupMenuUI. See 4670831
     */
    public void menuKeyPressed(MenuKeyEvent e) {
      // Handle the case for Escape or Enter...
      char keyChar = e.getKeyChar();
      if (!Character.isLetterOrDigit(keyChar)) return;

      MenuSelectionManager manager = e.getMenuSelectionManager();
      MenuElement selectedPath[] = manager.getSelectedPath();

      for (int i = selectedPath.length - 1; i >= 0; i--) {
        if (selectedPath[i] == menuItem) {
          JPopupMenu popupMenu = ((JMenu) menuItem).getPopupMenu();
          MenuElement items[] = popupMenu.getSubElements();

          int index = -1;

          for (int j = 0; j < items.length; j++) {
            int key = ((JMenuItem) items[j]).getMnemonic();
            if (Character.toLowerCase((char) key) == Character.toLowerCase(keyChar)) {
              index = j;
              break;
            }
          }

          if (index != -1) {
            // Invoke the menu action
            JMenuItem item = (JMenuItem) items[index];
            if (!(item instanceof JMenu)) {
              // Let Submenus be handled by menuKeyTyped
              manager.clearSelectedPath();
              item.doClick();
            }
          }

          e.consume();
          return;
        }
      }
    }
예제 #5
0
 /**
  * Handles a keystroke in a menu.
  *
  * @param e a <code>MenuKeyEvent</code> object
  * @since 1.5
  */
 private void processMenuKeyEvent(MenuKeyEvent e) {
   switch (e.getID()) {
     case KeyEvent.KEY_PRESSED:
       fireMenuKeyPressed(e);
       break;
     case KeyEvent.KEY_RELEASED:
       fireMenuKeyReleased(e);
       break;
     case KeyEvent.KEY_TYPED:
       fireMenuKeyTyped(e);
       break;
     default:
       break;
   }
 }