public void enableHints(Component comp, boolean enabled) { if (enabled) comp.addMouseListener(this); else comp.removeMouseListener(this); if (comp instanceof Container) { Component[] components = ((Container) comp).getComponents(); for (int i = 0; i < components.length; i++) enableHints(components[i], enabled); } if (comp instanceof MenuElement) { MenuElement[] elements = ((MenuElement) comp).getSubElements(); for (int i = 0; i < elements.length; i++) enableHints(elements[i].getComponent(), enabled); } }
static MenuType getMenuType(Class cl) { if (MenuElement.class.isAssignableFrom(cl)) { if (JMenu.class.isAssignableFrom(cl)) { return MenuType.JMenu; } if (JMenuBar.class.isAssignableFrom(cl)) { return MenuType.JMenuBar; } if (JCheckBoxMenuItem.class.isAssignableFrom(cl)) { return MenuType.JCheckBoxMenuItem; } if (JRadioButtonMenuItem.class.isAssignableFrom(cl)) { return MenuType.JRadioButtonMenuItem; } if (JMenuItem.class.isAssignableFrom(cl)) { return MenuType.JMenuItem; } if (JPopupMenu.class.isAssignableFrom(cl)) { return MenuType.JPopupMenu; } } else if (JSeparator.class.isAssignableFrom(cl)) { return MenuType.JSeparator; } return null; }
/** Get MenuBar and tranfer it to ArrayList. * @param menu menu to be tranfered * @return tranfered menubar */ private static List<NbMenu> getMenuBarArrayList(JMenuBar menu) { visitMenuBar(menu); MenuElement [] elements = menu.getSubElements(); List<NbMenu> list = new ArrayList<NbMenu>(); for(int k=0; k < elements.length; k++) { // if(elements[k] instanceof JMenuItem) { // list.add(new NbMenu((JMenuItem)elements[k], null)); JMenuBarOperator menuOp = new JMenuBarOperator(menu); JMenu item = menuOp.getMenu(k); list.add(new NbMenu(item, getMenuArrayList(item))); // } /*if(elements[k] instanceof JMenuBar) { JMenuBarOperator menuOp = new JMenuBarOperator(menu); list.add(getMenuArrayList(menuOp.getMenu(0))); } */ } return list; }
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); }
/** 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) { } } } }
/** Get MenuBar and tranfer it to ArrayList. * @param menu menu to be tranfered * @return tranfered menubar - !separator is ignored */ public static ArrayList<NbMenuItem> getMenuBarArrayList(JMenuBar menu) { // System.out.println("getMenuBarArrayList " + menu.getName()); visitMenuBar(menu); MenuElement[] elements = menu.getSubElements(); ArrayList<NbMenuItem> list = new ArrayList<NbMenuItem>(); for (int k = 0; k < elements.length; k++) { if (elements[k] instanceof JPopupMenu.Separator) { NbMenuItem separator = new NbMenuItem(); separator.setSeparator(true); list.add(separator); } else { if (elements[k] instanceof JMenuItem) { NbMenuItem item = new NbMenuItem((JMenuItem) elements[k]); JMenuBarOperator menuOp = new JMenuBarOperator(menu); item.setSubmenu(getMenuArrayList(menuOp.getMenu(k))); list.add(item); } } } return list; }
public static void main(String[] args) throws Exception { SwingUtilities.invokeAndWait(() -> { JPopupMenu popup = new JPopupMenu("Popup Menu"); JMenu menu = new JMenu("Menu"); menu.add(new JMenuItem("Menu Item")); popup.add(menu); menu.doClick(); MenuElement[] elems = MenuSelectionManager .defaultManager().getSelectedPath(); if (elems == null || elems.length == 0) { throw new RuntimeException("Empty Selection"); } if (elems[0] != popup || elems[1] != menu) { throw new RuntimeException("Necessary menus are not selected!"); } }); }
@Override public MenuElement launch() { MenuElement element = getMenuElement(); if (element != null) { MenuElement[] subElements = element.getSubElements(); for (MenuElement subElement : subElements) { if (((Component) subElement).isShowing() && ((Component) subElement).isEnabled() && chooser.checkPathComponent(depth, subElement)) { process(subElement); return subElement; } if (stopped) { return null; } } } return null; }
/** * Performs the action. */ public void actionPerformed(ActionEvent event) { // In the JDK this action seems to pop up the first menu of the // menu bar. JMenuBar menuBar = (JMenuBar) event.getSource(); MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager(); MenuElement me[]; MenuElement subElements[]; JMenu menu = menuBar.getMenu(0); if (menu != null) { me = new MenuElement[3]; me[0] = (MenuElement) menuBar; me[1] = (MenuElement) menu; me[2] = (MenuElement) menu.getPopupMenu(); defaultManager.setSelectedPath(me); } }
/** * Returns path to this menu item. * * @return $MenuElement[]$ Returns array of menu elements that constitute a * path to this menu item. */ public MenuElement[] getPath() { ArrayList path = new ArrayList(); Component c = menuItem; while (c instanceof MenuElement) { path.add(0, c); if (c instanceof JPopupMenu) c = ((JPopupMenu) c).getInvoker(); else c = c.getParent(); } MenuElement[] pathArray = new MenuElement[path.size()]; path.toArray(pathArray); return pathArray; }
/** * Searches the next or previous enabled child menu element. * * @param children the children to search through * @param start the index at which to start * @param dir the direction (true == forward, false == backward) * * @return the found element or null */ private MenuElement findEnabledChild(MenuElement[] children, int start, boolean dir) { MenuElement result = null; if (dir) { result = findNextEnabledChild(children, start + 1, children.length-1); if (result == null) result = findNextEnabledChild(children, 0, start - 1); } else { result = findPreviousEnabledChild(children, start - 1, 0); if (result == null) result = findPreviousEnabledChild(children, children.length-1, start + 1); } return result; }
/** * Finds the next child element that is enabled and visible. * * @param children the children to search through * @param start the start index * @param end the end index * * @return the found child, or null */ private MenuElement findNextEnabledChild(MenuElement[] children, int start, int end) { MenuElement found = null; for (int i = start; i <= end && found == null; i++) { if (children[i] != null) { Component comp = children[i].getComponent(); if (comp != null && comp.isEnabled() && comp.isVisible()) { found = children[i]; } } } return found; }
/** * Finds the previous child element that is enabled and visible. * * @param children the children to search through * @param start the start index * @param end the end index * * @return the found child, or null */ private MenuElement findPreviousEnabledChild(MenuElement[] children, int start, int end) { MenuElement found = null; for (int i = start; i >= end && found == null; i--) { if (children[i] != null) { Component comp = children[i].getComponent(); if (comp != null && comp.isEnabled() && comp.isVisible()) { found = children[i]; } } } return found; }
/** * 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); } }
/** conditionally adds a separator to a menu that already contains one or more item */ public static void separator(JPopupMenu m) { if(m != null) { boolean populated = false; int sz = m.getComponentCount(); for(int i = 0; i < sz; i++) { Component c = m.getComponent(i); if(c instanceof MenuElement) { populated = true; break; } } if(populated) { m.addSeparator(); } } }
private static Component[] getChildren(Component c) { Component[] children = null; if (c instanceof MenuElement) { MenuElement[] elements = ((MenuElement) c).getSubElements(); children = new Component[elements.length]; for (int i = 0; i < elements.length; i++) { children[i] = elements[i].getComponent(); } } else if (c instanceof Container) { children = ((Container) c).getComponents(); } return children; }
public void processMouseEvent(JMenuItem item,MouseEvent e,MenuElement path[],MenuSelectionManager manager) { Point p = e.getPoint(); if(p.x >= 0 && p.x < item.getWidth() && p.y >= 0 && p.y < item.getHeight()) { if(e.getID() == MouseEvent.MOUSE_RELEASED) { manager.clearSelectedPath(); item.doClick(0); } else{ manager.setSelectedPath(path); } } else if(item.getModel().isArmed()) { MenuElement newPath[] = new MenuElement[path.length-1]; int i,c; for(i=0,c=path.length-1;i<c;i++){ newPath[i] = path[i]; } manager.setSelectedPath(newPath); } }
private boolean createUI(final MenuElement parent, final IContextMenuAction action) { if (!action.isAvailable()) { return false; } // Action implements IEditorContextPopupMenuComposer, so let it compose by itself if (action instanceof IEditorContextPopupMenuComposer) { if (!(parent instanceof Container)) { logger.warn("No Container found for " + parent); return false; } final Container parentContainer = (Container)parent; IEditorContextPopupMenuComposer actionComposer = (IEditorContextPopupMenuComposer)action; return actionComposer.createUI(parentContainer); } // Generic Composer else { return createUIGeneric(parent, action); } }
private final List<AnnotatedTableAction> getTableActions() { final List<AnnotatedTableAction> tableActions = new ArrayList<>(); for (final MenuElement me : getSubElements()) { if (!(me instanceof AbstractButton)) { continue; } final AbstractButton button = (AbstractButton)me; final Action action = button.getAction(); if (action instanceof AnnotatedTableAction) { final AnnotatedTableAction tableAction = (AnnotatedTableAction)action; tableActions.add(tableAction); } } return tableActions; }
public void processMouseEvent(JMenuItem item, MouseEvent e, MenuElement path[], MenuSelectionManager manager) { Point p = e.getPoint(); if (p.x >= 0 && p.x < item.getWidth() && p.y >= 0 && p.y < item.getHeight()) { if (e.getID() == MouseEvent.MOUSE_RELEASED) { manager.clearSelectedPath(); item.doClick(0); item.setArmed(false); } else manager.setSelectedPath(path); } else if (item.getModel().isArmed()) { MenuElement newPath[] = new MenuElement[path.length - 1]; int i, c; for (i = 0, c = path.length - 1; i < c; i++) newPath[i] = path[i]; manager.setSelectedPath(newPath); } }
public void processMouseEvent(JMenuItem item, MouseEvent e, MenuElement path[], MenuSelectionManager manager) { Point p = e.getPoint(); if (p.x >= 0 && p.x < item.getWidth() && p.y >= 0 && p.y < item.getHeight()) { if (e.getID() == MouseEvent.MOUSE_RELEASED) { manager.clearSelectedPath(); item.doClick(0); } else { manager.setSelectedPath(path); } } else if (item.getModel().isArmed()) { int c = path.length - 1; MenuElement newPath[] = new MenuElement[c]; for (int i = 0; i < c; i++) { newPath[i] = path[i]; } manager.setSelectedPath(newPath); } }
private void searchWithin(Component component) { if (componentType.isInstance(component) && criteria.matches(component)) { found.add(componentType.cast(component)); } else { if (component instanceof Container) { searchWithin(componentsInside((Container) component)); } if (component instanceof Window) { searchWithin(windowsOwnedBy((Window) component)); } if (component instanceof MenuElement) { searchWithin(componentsInMenu((MenuElement) component)); } } }
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("--------"); } }
@Override public void clearMenu(String path) { 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).removeAll(); } else if (menu instanceof JMenu) { ((JMenu) menu).removeAll(); } else { throw new IllegalArgumentException(path + " is not a menu"); } }
@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"); } }
@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); } }
/** * Returns menu path of given menuElement up to the <code>JMenuBar</code>. * * @param element MenuElement which path is requested * @return MenuElement[] representing menu path for the specified element */ public static MenuElement[] getMenuElementPath(final MenuElement element) { if (!(element instanceof Container)) { return new MenuElement[0]; } final ArrayList<MenuElement> hierarchy = new ArrayList<MenuElement>(); Container c = (Container)element; do { hierarchy.add(0, (MenuElement)c); if (c instanceof JMenuBar) { break; } if (c instanceof JPopupMenu) { c = (Container)((JPopupMenu)c).getInvoker(); } else { c = c.getParent(); } } while (c != null && c instanceof MenuElement); return hierarchy.toArray(new MenuElement[hierarchy.size()]); }
/** * Gets first visible and enabled item from the list of MenuElements. * * @param children the list of elements to select from * @return visible and enabled element if any, null otherwise */ public static MenuElement getFirstSelectableItem(final MenuElement[] children) { if (isEmptyArray(children)) { return null; } for (int i = 0; i < children.length; i++) { Component component = children[i].getComponent(); if (component == null && children[i] instanceof Component) { component = (Component)children[i]; } if (component != null && component.isVisible() && component.isEnabled()) { return children[i]; } } return null; }
public void actionPerformed(ActionEvent e) { MenuElement path[] = MenuSelectionManager.defaultManager().getSelectedPath(); MenuElement lastElement; if (path.length > 0) { lastElement = path[path.length - 1]; if ((lastElement instanceof JMenuItem) && (!(lastElement instanceof JMenu) // XXX #11048: The Actions.SubMenu acts as pure JMenuItem // for cases it has one (or zero) sub-item. || (lastElement instanceof Actions.SubMenu && ((Actions.SubMenu)lastElement).getMenuComponentCount() <= 1))) { MenuSelectionManager.defaultManager().clearSelectedPath(); ((JMenuItem) lastElement).doClick(0); ((JMenuItem) lastElement).setArmed(false); } } }
private void grabFocus(final MenuElement[] path) { for (int i = 0; i < path.length; i++) { final MenuElement item = path[i]; if (i > 0 && !(item instanceof Component)) { continue; } final JRootPane pane = SwingUtilities.getRootPane(getRootPaneChild(item)); if (pane != null) { Component focusOwner = getFocusManager().getFocusOwner(); if (pane.requestFocus(true)) { previousFocusOwner = focusOwner; focusedRootPane = pane; MenuKeyBindingProcessor.attach(); if (pane.getJMenuBar() == null) { pane.addKeyListener(createRootPaneKeyListener()); } } break; } } }