javax.swing.MenuElement Java Examples

The following examples show how to use javax.swing.MenuElement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainFrameClassicMenu.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void addSeparator(String parentPath) {
    parentPath = mapping(parentPath);
    if (parentPath.equals("_") || parentPath.startsWith("_/")) {
        return;
    }
    if (!menuElements.containsKey(parentPath)) {
        throw new IllegalArgumentException("Menu does not exist: " + parentPath);
    }
    MenuElement parent = menuElements.get(parentPath);
    if (parent instanceof JMenu) {
        ((JMenu) parent).addSeparator();
    } else {
        throw new IllegalArgumentException("Not a menu: " + parentPath);
    }
}
 
Example #2
Source File: AmidstMenu.java    From amidst with GNU General Public License v3.0 6 votes vote down vote up
@CalledOnlyBy(AmidstThread.EDT)
private void runOnMenuItems(MenuElement menuElement, String[] textRepresentations, Consumer<JMenuItem> consumer) {
	MenuElement[] elements = menuElement.getSubElements();
	if(elements != null) {
		for(MenuElement element : elements) {
			if(element instanceof JMenuItem) {
				for(String s : textRepresentations) {
					if(((JMenuItem) element).getText().equals(s)) {
						consumer.accept((JMenuItem) element);
					}
				}
			}
			runOnMenuItems(element, textRepresentations, consumer);
		}
	}
}
 
Example #3
Source File: SwingUtils.java    From ib-controller with GNU General Public License v3.0 6 votes vote down vote up
private static void appendMenuItem(Component menuItem, StringBuilder builder, String indent) {
    if (menuItem instanceof JMenuBar) {
        appendMenuSubElements((MenuElement)menuItem, builder, indent);
    } else if (menuItem instanceof JPopupMenu) {
        appendMenuSubElements((MenuElement)menuItem, builder, indent);
    } else if (menuItem instanceof JMenuItem) {
        builder.append(NEWLINE);
        builder.append(indent);
        builder.append(((JMenuItem)menuItem).getText());
        builder.append(((JMenuItem)menuItem).isEnabled() ? "" : "[Disabled]");
        appendMenuSubElements((JMenuItem)menuItem, builder, "|   " + indent);
    } else if (menuItem instanceof JSeparator) {
        builder.append(NEWLINE);
        builder.append(indent);
        builder.append("--------");
    }
}
 
Example #4
Source File: StayOpenPopupMenu.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager) {
    if (isReturnAction(e)) { // Handle SPACE and ENTER
        MenuElement[] p = manager.getSelectedPath();
        MenuElement m = p != null && p.length > 0 ? p[p.length - 1] : null;
        if (m instanceof StayOpen) {
            e.consume();
            if (e.getID() == KeyEvent.KEY_PRESSED)
                performAction((StayOpen)m, e.getModifiers());
            return;
        }
    } else for (Component component : getComponents()) { // Handle mnemonics and accelerators
        if (component instanceof StayOpen) {
            StayOpen item = (StayOpen)component;
            JMenuItem i = item.getItem();
            KeyStroke k = KeyStroke.getKeyStrokeForEvent(e);
            if (k.equals(mnemonic(i)) || k.equals(i.getAccelerator())) {
                e.consume();
                manager.setSelectedPath(new MenuElement[] { this, i });
                performAction(item, e.getModifiers());
                return;
            }
        }
    }
    
    super.processKeyEvent(e, path, manager);
}
 
Example #5
Source File: GltfBrowserApplication.java    From JglTF with MIT License 6 votes vote down vote up
/**
 * Recursively attach the given action listener to all items in the
 * given menu that have a non-<code>null</code> action command
 *  
 * @param menuElement The menu element
 * @param actionListener The action listener
 */
private static void attachActionListener(
    MenuElement menuElement, ActionListener actionListener)
{
    if (menuElement instanceof JMenuItem)
    {
        JMenuItem menuItem = (JMenuItem)menuElement;
        if (menuItem.getActionCommand() != null)
        {
            menuItem.addActionListener(actionListener);
        }
    }
    MenuElement[] subElements = menuElement.getSubElements();
    for (MenuElement subElement : subElements)
    {
        attachActionListener(subElement, actionListener);
    }
}
 
Example #6
Source File: MainFrameClassicMenu.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setMenuEnabled(String path, boolean enabled) {
    path = mapping(path);
    if (path.equals("_") || path.startsWith("_/")) {
        return;
    }
    MenuElement menu = menuElements.get(path);
    if (menu == null) {
        throw new IllegalArgumentException("Menu " + path + " does not exist");
    }
    if (menu instanceof JMenuBar) {
        ((JMenuBar) menu).setEnabled(enabled);
    } else if (menu instanceof JMenu) {
        ((JMenu) menu).setEnabled(enabled);
    } else if (menu instanceof JMenuItem) {
        ((JMenuItem) menu).setEnabled(enabled);
    } else {
        throw new IllegalArgumentException(path + " is not a menu");
    }
}
 
Example #7
Source File: MenuChecker.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Open all menus in menubar
 * @param menu  to be visited */
public static void visitMenuBar(JMenuBar menu) {
    MenuElement[] elements = menu.getSubElements();

    JMenuBarOperator op = new JMenuBarOperator(menu);

    for (int k = 0; k < elements.length; k++) {
        if (elements[k] instanceof JMenuItem) {
            op.pushMenu(((JMenuItem) elements[k]).getText(), "/", true, true);
            try {
                op.wait(200);
            } catch (Exception e) {
            }
        }
    }
}
 
Example #8
Source File: WindowsRootPaneUI.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
void altPressed(KeyEvent ev) {
    MenuSelectionManager msm =
        MenuSelectionManager.defaultManager();
    MenuElement[] path = msm.getSelectedPath();
    if (path.length > 0 && ! (path[0] instanceof ComboPopup)) {
        msm.clearSelectedPath();
        menuCanceledOnPress = true;
        ev.consume();
    } else if(path.length > 0) { // We are in ComboBox
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        ev.consume();
    } else {
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        JMenuBar mbar = root != null ? root.getJMenuBar() : null;
        if(mbar == null && winAncestor instanceof JFrame) {
            mbar = ((JFrame)winAncestor).getJMenuBar();
        }
        JMenu menu = mbar != null ? mbar.getMenu(0) : null;
        if(menu != null) {
            ev.consume();
        }
    }
}
 
Example #9
Source File: MainFrameClassicMenu.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setMenuChecked(String path, boolean checked) {
    path = mapping(path);
    MenuElement menu = menuElements.get(path);
    if (menu == null) {
        throw new IllegalArgumentException("Menu " + path + " does not exist");
    }
    if (menu instanceof JCheckBoxMenuItem) {
        ((JCheckBoxMenuItem) menu).setSelected(checked);
    } else if (menu instanceof JRadioButtonMenuItem) {
        ((JRadioButtonMenuItem) menu).setSelected(checked);
    } else {
        throw new IllegalArgumentException(path + " is not selectable menu item");
    }
}
 
Example #10
Source File: ContextMenuUtility.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static JPopupMenu getMenu( final ReportDesignerContext context, final Object selectedElement ) {
  ReportDesignerView view = context.getView();
  if ( selectedElement == null || selectedElement instanceof MasterReport ) { // This check assumes that we've click on a report band see JIRA
    // PRD-1076
    return view.getPopupMenu( "popup-ReportDefinition" ); // NON-NLS
  }
  final ReportDesignerDocumentContext activeContext = context.getActiveContext();
  ReportRenderContext doc = null;
  if ( activeContext instanceof ReportRenderContext ) {
    doc = (ReportRenderContext) activeContext;
    if ( selectedElement == doc.getReportDefinition() ) {
      if ( selectedElement instanceof CrosstabElement ) {
        return view.getPopupMenu( "popup-CrosstabElement" ); // NON-NLS
      }
      return view.getPopupMenu( "popup-ReportDefinition" ); // NON-NLS
    }
  }
  if ( selectedElement instanceof SubReport ) {
    return view.getPopupMenu( "popup-SubReport" ); // NON-NLS
  }
  if ( selectedElement instanceof CompoundDataFactory ) {
    return createDataSourcePopup( context );
  }
  if ( selectedElement instanceof DataFactory ) {
    return view.getPopupMenu( "popup-DataSource" ); // NON-NLS
  }
  if ( selectedElement instanceof ReportFunctionNode ) {
    return view.getPopupMenu( "popup-Expressions" ); // NON-NLS
  }
  if ( selectedElement instanceof ReportQueryNode ) {
    final ReportQueryNode rqn = (ReportQueryNode) selectedElement;
    JPopupMenu popupMenu;
    if ( rqn.isAllowEdit() ) {
      popupMenu = view.getPopupMenu( "popup-Query" ); // NON-NLS
    } else {
      popupMenu = view.getPopupMenu( "popup-Inherited-Query" ); // NON-NLS
    }
    final MenuElement activationItem = popupMenu.getSubElements()[0];
    toggleActivationItem( doc, rqn, activationItem );
    return popupMenu;
  }
  if ( selectedElement instanceof Expression ) {
    return view.getPopupMenu( "popup-Expression" ); // NON-NLS
  }
  if ( selectedElement instanceof RootLevelBand ) {
    return view.getPopupMenu( "popup-RootLevelBand" ); // NON-NLS
  }
  if ( selectedElement instanceof RelationalGroup ) {
    return view.getPopupMenu( "popup-RelationalGroup" ); // NON-NLS
  }
  if ( selectedElement instanceof CrosstabGroup ) {
    return view.getPopupMenu( "popup-CrosstabGroup" ); // NON-NLS
  }
  if ( selectedElement instanceof CrosstabOtherGroup ) {
    return view.getPopupMenu( "popup-CrosstabOtherGroup" ); // NON-NLS
  }
  if ( selectedElement instanceof CrosstabRowGroup ) {
    return view.getPopupMenu( "popup-CrosstabRowGroup" ); // NON-NLS
  }
  if ( selectedElement instanceof CrosstabColumnGroup ) {
    return view.getPopupMenu( "popup-CrosstabColumnGroup" ); // NON-NLS
  }
  if ( selectedElement instanceof CrosstabCellBody ) {
    return view.getPopupMenu( "popup-CrosstabCellBody" ); // NON-NLS
  }
  if ( selectedElement instanceof Group ) {
    return view.getPopupMenu( "popup-Group" ); // NON-NLS
  }
  if ( selectedElement instanceof Band ) {
    return view.getPopupMenu( "popup-Band" ); // NON-NLS
  }
  if ( selectedElement instanceof Element ) {
    final Element element = (Element) selectedElement;
    final JPopupMenu popup = view.getPopupMenu( "popup-" + element.getElementTypeName() ); // NON-NLS
    if ( popup != null ) {
      return popup;
    }
    return view.getPopupMenu( "popup-Element" ); // NON-NLS
  }
  if ( selectedElement instanceof ReportParameterDefinition ) {
    return view.getPopupMenu( "popup-Parameters" ); // NON-NLS
  }
  if ( selectedElement instanceof ParameterDefinitionEntry ) {
    return view.getPopupMenu( "popup-Parameter" ); // NON-NLS
  }
  if ( selectedElement instanceof ReportParametersNode ) {
    return view.getPopupMenu( "popup-Parameters" ); // NON-NLS
  }
  if ( selectedElement instanceof SubReportParametersNode ) {
    return view.getPopupMenu( "popup-SubReportParameters" ); // NON-NLS
  }
  return null;
}
 
Example #11
Source File: WindowsRootPaneUI.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
void altPressed(KeyEvent ev) {
    MenuSelectionManager msm =
        MenuSelectionManager.defaultManager();
    MenuElement[] path = msm.getSelectedPath();
    if (path.length > 0 && ! (path[0] instanceof ComboPopup)) {
        msm.clearSelectedPath();
        menuCanceledOnPress = true;
        ev.consume();
    } else if(path.length > 0) { // We are in ComboBox
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        ev.consume();
    } else {
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        JMenuBar mbar = root != null ? root.getJMenuBar() : null;
        if(mbar == null && winAncestor instanceof JFrame) {
            mbar = ((JFrame)winAncestor).getJMenuBar();
        }
        JMenu menu = mbar != null ? mbar.getMenu(0) : null;
        if(menu != null) {
            ev.consume();
        }
    }
}
 
Example #12
Source File: MenuChecker.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static int getElementPosition(String element, MenuElement[] array) {
    int p = -1;
    for (int i = 0; i <
            array.length; i++) {
        if (element.equals(((JMenuItem) array[i]).getText())) {
            return i;
        }

    }
    return p;
}
 
Example #13
Source File: WindowsRootPaneUI.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
public boolean postProcessKeyEvent(KeyEvent ev) {
    if(ev.isConsumed() && ev.getKeyCode() != KeyEvent.VK_ALT) {
        // mnemonic combination, it's consumed, but we need
        // set altKeyPressed to false, otherwise after selection
        // component by mnemonic combination a menu will be open
        altKeyPressed = false;
        return false;
    }
    if (ev.getKeyCode() == KeyEvent.VK_ALT) {
        root = SwingUtilities.getRootPane(ev.getComponent());
        winAncestor = (root == null ? null :
                SwingUtilities.getWindowAncestor(root));

        if (ev.getID() == KeyEvent.KEY_PRESSED) {
            if (!altKeyPressed) {
                altPressed(ev);
            }
            altKeyPressed = true;
            return true;
        } else if (ev.getID() == KeyEvent.KEY_RELEASED) {
            if (altKeyPressed) {
                altReleased(ev);
            } else {
                MenuSelectionManager msm =
                    MenuSelectionManager.defaultManager();
                MenuElement[] path = msm.getSelectedPath();
                if (path.length <= 0) {
                    WindowsLookAndFeel.setMnemonicHidden(true);
                    WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
                }
            }
            altKeyPressed = false;
        }
        root = null;
        winAncestor = null;
    } else {
        altKeyPressed = false;
    }
    return false;
}
 
Example #14
Source File: MainMenuTestCase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Construct path to menu item.
 *
 * @param mainMenuItem item in menu.
 * @return path.
 */
protected NbMenuItem getMainMenuItem(String mainMenuItem) {

    JMenuBarOperator mainmenuOp = pushMainMenuItem(mainMenuItem);
    //parse all the menu elements
    int position = MenuChecker.getElementPosition(mainMenuItem, mainmenuOp.getSubElements());
    MenuElement theMenuElement = mainmenuOp.getSubElements()[position];
    NbMenuItem theMenu = new NbMenuItem((JMenuItem) theMenuElement);
    theMenu.setSubmenu(MenuChecker.getMenuArrayList(mainmenuOp.getMenu(position)));

    return theMenu;
}
 
Example #15
Source File: WindowsRootPaneUI.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
void altPressed(KeyEvent ev) {
    MenuSelectionManager msm =
        MenuSelectionManager.defaultManager();
    MenuElement[] path = msm.getSelectedPath();
    if (path.length > 0 && ! (path[0] instanceof ComboPopup)) {
        msm.clearSelectedPath();
        menuCanceledOnPress = true;
        ev.consume();
    } else if(path.length > 0) { // We are in ComboBox
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        ev.consume();
    } else {
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        JMenuBar mbar = root != null ? root.getJMenuBar() : null;
        if(mbar == null && winAncestor instanceof JFrame) {
            mbar = ((JFrame)winAncestor).getJMenuBar();
        }
        JMenu menu = mbar != null ? mbar.getMenu(0) : null;
        if(menu != null) {
            ev.consume();
        }
    }
}
 
Example #16
Source File: WindowsRootPaneUI.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean postProcessKeyEvent(KeyEvent ev) {
    if(ev.isConsumed() && ev.getKeyCode() != KeyEvent.VK_ALT) {
        // mnemonic combination, it's consumed, but we need
        // set altKeyPressed to false, otherwise after selection
        // component by mnemonic combination a menu will be open
        altKeyPressed = false;
        return false;
    }
    if (ev.getKeyCode() == KeyEvent.VK_ALT) {
        root = SwingUtilities.getRootPane(ev.getComponent());
        winAncestor = (root == null ? null :
                SwingUtilities.getWindowAncestor(root));

        if (ev.getID() == KeyEvent.KEY_PRESSED) {
            if (!altKeyPressed) {
                altPressed(ev);
            }
            altKeyPressed = true;
            return true;
        } else if (ev.getID() == KeyEvent.KEY_RELEASED) {
            if (altKeyPressed) {
                altReleased(ev);
            } else {
                MenuSelectionManager msm =
                    MenuSelectionManager.defaultManager();
                MenuElement[] path = msm.getSelectedPath();
                if (path.length <= 0) {
                    WindowsLookAndFeel.setMnemonicHidden(true);
                    WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
                }
            }
            altKeyPressed = false;
        }
        root = null;
        winAncestor = null;
    } else {
        altKeyPressed = false;
    }
    return false;
}
 
Example #17
Source File: AutoHidingMenuBar.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean isMenuItemSelected() {
    MenuElement[] selectedPath = MenuSelectionManager.defaultManager().getSelectedPath();
    return selectedPath != null && selectedPath.length > 0 &&
            /* Make sure the selection is in the main menu bar, not just a context menu
            somewhere. */
            selectedPath[0] == menuBar;
}
 
Example #18
Source File: JMenuItemOperator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps {@code JMenuItem.getSubElements()} through queue
 */
public MenuElement[] getSubElements() {
    return ((MenuElement[]) runMapping(new MapAction<Object>("getSubElements") {
        @Override
        public Object map() {
            return ((JMenuItem) getSource()).getSubElements();
        }
    }));
}
 
Example #19
Source File: MenuChecker.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Open all menus in menubar
 * @param menu  to be visited */
private static void visitMenuBar(JMenuBar menu) {
    JMenuBarOperator op = new JMenuBarOperator(menu);
    for (MenuElement element : menu.getSubElements()) {
        if (element instanceof JMenuItem) {
            op.pushMenu(op.parseString(((JMenuItem) element).getText(), "/"), new DefaultStringComparator(true, true));
            try {
                op.wait(200);
            }catch(Exception e) {}
        }
    }
}
 
Example #20
Source File: WindowsRootPaneUI.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
void altPressed(KeyEvent ev) {
    MenuSelectionManager msm =
        MenuSelectionManager.defaultManager();
    MenuElement[] path = msm.getSelectedPath();
    if (path.length > 0 && ! (path[0] instanceof ComboPopup)) {
        msm.clearSelectedPath();
        menuCanceledOnPress = true;
        ev.consume();
    } else if(path.length > 0) { // We are in ComboBox
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        ev.consume();
    } else {
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        JMenuBar mbar = root != null ? root.getJMenuBar() : null;
        if(mbar == null && winAncestor instanceof JFrame) {
            mbar = ((JFrame)winAncestor).getJMenuBar();
        }
        JMenu menu = mbar != null ? mbar.getMenu(0) : null;
        if(menu != null) {
            ev.consume();
        }
    }
}
 
Example #21
Source File: JPopupMenuOperator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps
 * {@code JPopupMenu.processMouseEvent(MouseEvent, MenuElement[], MenuSelectionManager)}
 * through queue
 */
public void processMouseEvent(final MouseEvent mouseEvent, final MenuElement[] menuElement, final MenuSelectionManager menuSelectionManager) {
    runMapping(new MapVoidAction("processMouseEvent") {
        @Override
        public void map() {
            ((JPopupMenu) getSource()).processMouseEvent(mouseEvent, menuElement, menuSelectionManager);
        }
    });
}
 
Example #22
Source File: MainFrameClassicMenu.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isMenuChecked(String path) {
    path = mapping(path);
    MenuElement menu = menuElements.get(path);
    if (menu == null) {
        throw new IllegalArgumentException("Menu " + path + " does not exist");
    }
    if (menu instanceof JCheckBoxMenuItem) {
        return ((JCheckBoxMenuItem) menu).isSelected();
    } else if (menu instanceof JRadioButtonMenuItem) {
        return ((JRadioButtonMenuItem) menu).isSelected();
    } else {
        throw new IllegalArgumentException(path + " is not selectable menu item");
    }
}
 
Example #23
Source File: WindowsRootPaneUI.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
void altPressed(KeyEvent ev) {
    MenuSelectionManager msm =
        MenuSelectionManager.defaultManager();
    MenuElement[] path = msm.getSelectedPath();
    if (path.length > 0 && ! (path[0] instanceof ComboPopup)) {
        msm.clearSelectedPath();
        menuCanceledOnPress = true;
        ev.consume();
    } else if(path.length > 0) { // We are in ComboBox
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        ev.consume();
    } else {
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        JMenuBar mbar = root != null ? root.getJMenuBar() : null;
        if(mbar == null && winAncestor instanceof JFrame) {
            mbar = ((JFrame)winAncestor).getJMenuBar();
        }
        JMenu menu = mbar != null ? mbar.getMenu(0) : null;
        if(menu != null) {
            ev.consume();
        }
    }
}
 
Example #24
Source File: JPopupMenuOperator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Maps {@code JPopupMenu.getSubElements()} through queue
 */
public MenuElement[] getSubElements() {
    return ((MenuElement[]) runMapping(new MapAction<Object>("getSubElements") {
        @Override
        public Object map() {
            return ((JPopupMenu) getSource()).getSubElements();
        }
    }));
}
 
Example #25
Source File: MenuKeysDispatcher.java    From PyramidShader with GNU General Public License v3.0 5 votes vote down vote up
private static void setActionsMenu(JDialog dialog, MenuElement menu) {
        MenuElement[] subItems = menu.getSubElements();
        InputMap inputMap = dialog.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        for (MenuElement menuElement : subItems) {
            if (menuElement instanceof JMenuItem) {
                final JMenuItem menuItem = (JMenuItem) menuElement;
                if (menuItem.getAccelerator() != null) {
                    String key = "hackAction" + counter++;
                    inputMap.put(menuItem.getAccelerator(), key);

                    if (menuItem.getAction() == null) {
                        dialog.getRootPane().getActionMap().put(key,
                                new AbstractAction() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                menuItem.doClick();
                            }
                        });
                    } else {
                        dialog.getRootPane().getActionMap().put(key,
                                menuItem.getAction());
                    }

//                     System.out.println(key + " : "
//                     + menuItem.getText()+ " : "
//                     + menuItem.getActionCommand() + " : "
//                     + menuItem.getAccelerator() + " : "
//                     + menuItem.getAction());
                }
            }
            // the original code had an else condition here, which is wrong.
            // JMenu is a subclass of JMenuItem, which prevents sub-menus from being traversed.
            if (menuElement.getSubElements().length > 0) {
                setActionsMenu(dialog, menuElement);
            }
        }
    }
 
Example #26
Source File: WindowsRootPaneUI.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public boolean postProcessKeyEvent(KeyEvent ev) {
    if(ev.isConsumed()) {
        // do not manage consumed event
        return false;
    }
    if (ev.getKeyCode() == KeyEvent.VK_ALT) {
        root = SwingUtilities.getRootPane(ev.getComponent());
        winAncestor = (root == null ? null :
                SwingUtilities.getWindowAncestor(root));

        if (ev.getID() == KeyEvent.KEY_PRESSED) {
            if (!altKeyPressed) {
                altPressed(ev);
            }
            altKeyPressed = true;
            return true;
        } else if (ev.getID() == KeyEvent.KEY_RELEASED) {
            if (altKeyPressed) {
                altReleased(ev);
            } else {
                MenuSelectionManager msm =
                    MenuSelectionManager.defaultManager();
                MenuElement[] path = msm.getSelectedPath();
                if (path.length <= 0) {
                    WindowsLookAndFeel.setMnemonicHidden(true);
                    WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
                }
            }
            altKeyPressed = false;
        }
        root = null;
        winAncestor = null;
    } else {
        altKeyPressed = false;
    }
    return false;
}
 
Example #27
Source File: WindowsRootPaneUI.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public boolean postProcessKeyEvent(KeyEvent ev) {
    if(ev.isConsumed() && ev.getKeyCode() != KeyEvent.VK_ALT) {
        // mnemonic combination, it's consumed, but we need
        // set altKeyPressed to false, otherwise after selection
        // component by mnemonic combination a menu will be open
        altKeyPressed = false;
        return false;
    }
    if (ev.getKeyCode() == KeyEvent.VK_ALT) {
        root = SwingUtilities.getRootPane(ev.getComponent());
        winAncestor = (root == null ? null :
                SwingUtilities.getWindowAncestor(root));

        if (ev.getID() == KeyEvent.KEY_PRESSED) {
            if (!altKeyPressed) {
                altPressed(ev);
            }
            altKeyPressed = true;
            return true;
        } else if (ev.getID() == KeyEvent.KEY_RELEASED) {
            if (altKeyPressed) {
                altReleased(ev);
            } else {
                MenuSelectionManager msm =
                    MenuSelectionManager.defaultManager();
                MenuElement[] path = msm.getSelectedPath();
                if (path.length <= 0) {
                    WindowsLookAndFeel.setMnemonicHidden(true);
                    WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
                }
            }
            altKeyPressed = false;
        }
        root = null;
        winAncestor = null;
    } else {
        altKeyPressed = false;
    }
    return false;
}
 
Example #28
Source File: MainFrameClassicMenu.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void addMenuItem(String path, String title, String icon, ActionListener action, int priority, final ActionListener subLoader, boolean isLeaf, HotKey key, boolean isOptional) {
    path = mapping(path);

    menuHotkeys.put(path, key);
    menuActions.put(path, action);

    if (!isLeaf) {
        //action is ignored
        addMenu(path, title, icon, subLoader);
        return;
    }
    if (path.startsWith("_/")) {
        return;
    }
    String parentPath = "";
    if (path.contains("/")) {
        parentPath = path.substring(0, path.lastIndexOf('/'));
    }
    MenuElement parentMenu = menuElements.get(parentPath);
    if (parentMenu == null) {
        throw new IllegalArgumentException("Parent menu " + path + " does not exist");
    }
    JMenuItem menuItem = new JMenuItem(title);
    if (icon != null) {
        menuItem.setIcon(View.getIcon(icon, 16));
    }
    if (action != null) {
        menuItem.addActionListener(action);
    }
    if (key != null) {
        menuItem.setAccelerator(KeyStroke.getKeyStroke(key.key, key.getModifier()));
    }
    if (parentMenu instanceof JMenu) {
        ((JMenu) parentMenu).add(menuItem);
    } else {
        throw new IllegalArgumentException("Parent path " + path + " is not a menu");
    }
    menuElements.put(path, menuItem);
}
 
Example #29
Source File: WindowsRootPaneUI.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void altPressed(KeyEvent ev) {
    MenuSelectionManager msm =
        MenuSelectionManager.defaultManager();
    MenuElement[] path = msm.getSelectedPath();
    if (path.length > 0 && ! (path[0] instanceof ComboPopup)) {
        msm.clearSelectedPath();
        menuCanceledOnPress = true;
        ev.consume();
    } else if(path.length > 0) { // We are in ComboBox
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        ev.consume();
    } else {
        menuCanceledOnPress = false;
        WindowsLookAndFeel.setMnemonicHidden(false);
        WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
        JMenuBar mbar = root != null ? root.getJMenuBar() : null;
        if(mbar == null && winAncestor instanceof JFrame) {
            mbar = ((JFrame)winAncestor).getJMenuBar();
        }
        JMenu menu = mbar != null ? mbar.getMenu(0) : null;
        if(menu != null) {
            ev.consume();
        }
    }
}
 
Example #30
Source File: WindowsRootPaneUI.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean postProcessKeyEvent(KeyEvent ev) {
    if(ev.isConsumed()) {
        // do not manage consumed event
        return false;
    }
    if (ev.getKeyCode() == KeyEvent.VK_ALT) {
        root = SwingUtilities.getRootPane(ev.getComponent());
        winAncestor = (root == null ? null :
                SwingUtilities.getWindowAncestor(root));

        if (ev.getID() == KeyEvent.KEY_PRESSED) {
            if (!altKeyPressed) {
                altPressed(ev);
            }
            altKeyPressed = true;
            return true;
        } else if (ev.getID() == KeyEvent.KEY_RELEASED) {
            if (altKeyPressed) {
                altReleased(ev);
            } else {
                MenuSelectionManager msm =
                    MenuSelectionManager.defaultManager();
                MenuElement[] path = msm.getSelectedPath();
                if (path.length <= 0) {
                    WindowsLookAndFeel.setMnemonicHidden(true);
                    WindowsGraphicsUtils.repaintMnemonicsInWindow(winAncestor);
                }
            }
            altKeyPressed = false;
        }
        root = null;
        winAncestor = null;
    } else {
        altKeyPressed = false;
    }
    return false;
}