public void testAddGetRemoveMenuListener() { MenuListener listener1 = new ConcreteMenuListener(); MenuListener listener2 = new ConcreteMenuListener(); MenuListener listener3 = new ConcreteMenuListener(); EventListener[] listenersArray = null; listenersArray = menu.getMenuListeners(); int initialValue = listenersArray.length; menu.addMenuListener(listener1); menu.addMenuListener(listener2); menu.addMenuListener(listener2); listenersArray = menu.getMenuListeners(); assertEquals(initialValue + 3, listenersArray.length); menu.removeMenuListener(listener1); menu.addMenuListener(listener3); menu.addMenuListener(listener3); listenersArray = menu.getMenuListeners(); assertEquals(initialValue + 4, listenersArray.length); menu.removeMenuListener(listener3); menu.removeMenuListener(listener3); listenersArray = menu.getMenuListeners(); assertEquals(initialValue + 2, listenersArray.length); menu.removeMenuListener(listener2); menu.removeMenuListener(listener2); listenersArray = menu.getMenuListeners(); assertEquals(initialValue, listenersArray.length); }
@Override public void dispose() { MenuListener[] listeners = PotGUIControl.viewMenu.getMenuListeners(); for (MenuListener listener : listeners) { PotGUIControl.viewMenu.removeMenuListener(listener); } super.dispose(); }
private static void createAndShowGUI() { JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); menu.setMnemonic('F'); menuBar.add(menu); JFrame frame = new JFrame(); frame.setJMenuBar(menuBar); frame.pack(); frame.setVisible(true); MenuListener listener = new TestMenuListener(); menu.addMenuListener(listener); }
/** * Maps {@code JMenu.addMenuListener(MenuListener)} through queue */ public void addMenuListener(final MenuListener menuListener) { runMapping(new MapVoidAction("addMenuListener") { @Override public void map() { ((JMenu) getSource()).addMenuListener(menuListener); } }); }
/** * Maps {@code JMenu.removeMenuListener(MenuListener)} through queue */ public void removeMenuListener(final MenuListener menuListener) { runMapping(new MapVoidAction("removeMenuListener") { @Override public void map() { ((JMenu) getSource()).removeMenuListener(menuListener); } }); }
/** * Feed a menu with the dynamic content of this NameSet. * * @param menu the menu to be fed, if null it is allocated by this method * @param itemListener the listener to be called on item selection * @return the menu properly dynamized */ public JMenu feedMenu (JMenu menu, final ActionListener itemListener) { final JMenu finalMenu = (menu != null) ? menu : new JMenu(setName); MenuListener menuListener = new AbstractMenuListener() { @Override public void menuSelected (MenuEvent e) { // Clean up the whole menu finalMenu.removeAll(); // Rebuild the whole list of menu items on the fly synchronized (NameSet.this) { for (String f : names) { JMenuItem menuItem = new JMenuItem(f); menuItem.addActionListener(itemListener); finalMenu.add(menuItem); } } } }; // Listener to menu selection, to modify content on-the-fly finalMenu.addMenuListener(menuListener); return finalMenu; }
/** * This method fires MenuEvents to all menu's MenuListeners. In this case * menuSelected() method of MenuListeners is called to indicated that the menu * was selected. */ protected void fireMenuSelected() { MenuListener[] listeners = getMenuListeners(); for (int index = 0; index < listeners.length; ++index) listeners[index].menuSelected(menuEvent); }
/** * This method fires MenuEvents to all menu's MenuListeners. In this case * menuDeselected() method of MenuListeners is called to indicated that the menu * was deselected. */ protected void fireMenuDeselected() { EventListener[] ll = listenerList.getListeners(MenuListener.class); for (int i = 0; i < ll.length; i++) ((MenuListener) ll[i]).menuDeselected(menuEvent); }
/** * This method fires MenuEvents to all menu's MenuListeners. In this case * menuSelected() method of MenuListeners is called to indicated that the menu * was cancelled. The menu is cancelled when it's popup menu is close without selection. */ protected void fireMenuCanceled() { EventListener[] ll = listenerList.getListeners(MenuListener.class); for (int i = 0; i < ll.length; i++) ((MenuListener) ll[i]).menuCanceled(menuEvent); }
public Object run() { Editor editor = FlowerPlatformPlugin.getInstance().getEditor(); JMenuBar menuBar = editor.getJMenuBar(); JMenu toolsMenu = menuBar.getMenu(3); toolsMenu.getListeners(MenuListener.class)[0].menuSelected(null); toolsMenu.getListeners(MenuListener.class)[1].menuSelected(null); // set menu options for (Component c : toolsMenu.getMenuComponents()) { if ((c instanceof JMenu) && c.isVisible()) { JMenu menu = (JMenu) c; String option = menu.getText(); if (option == null) { continue; } int index = option.indexOf(':'); if (index > 0) { option = option.substring(0, index); } String newValue = options.get(option); for (int i = 0; i < menu.getItemCount(); i++) { JMenuItem item = menu.getItem(i); if (item != null && item.isVisible() && item.getText().equals(newValue)) { item.setSelected(true); } } } } return null; }
public Object run() { Editor editor = FlowerPlatformPlugin.getInstance().getEditor(); JMenuBar menuBar = editor.getJMenuBar(); JMenu toolsMenu = menuBar.getMenu(3); toolsMenu.getListeners(MenuListener.class)[0].menuSelected(null); JMenu boardsMenu = null; for (Component c : toolsMenu.getMenuComponents()) { if (!(c instanceof JMenu) || !c.isVisible()) { continue; } JMenu menu = (JMenu) c; if (menu.getText().startsWith("Board")) { boardsMenu = menu; break; } } List<String> boards = new ArrayList<>(); // start from 1 (skip first entry - "Boards manager") for (int i = 1; i < boardsMenu.getItemCount(); i++) { JMenuItem item = boardsMenu.getItem(i); if (item != null && item.isEnabled()) { // System.out.println(item.getText()); boards.add(item.getText()); } } return boards; }
protected void firePopupMenuWillBecomeVisible() { super.firePopupMenuWillBecomeVisible(); logger.fine("Popup firePopupMenuWillBecomeVisible called."); for (Iterator i = listeners.iterator(); i.hasNext();) { MenuListener listener = (MenuListener) i.next(); listener.menuSelected(null); } }
protected void firePopupMenuCanceled() { super.firePopupMenuCanceled(); // logger.info("Popup firePopupMenuCanceled called."); for (Iterator i = listeners.iterator(); i.hasNext();) { MenuListener listener = (MenuListener) i.next(); listener.menuCanceled(null); } }
protected void firePopupMenuWillBecomeInvisible() { super.firePopupMenuWillBecomeInvisible(); // logger.info("Popup firePopupMenuWillBecomeInvisible called."); for (Iterator i = listeners.iterator(); i.hasNext();) { MenuListener listener = (MenuListener) i.next(); listener.menuDeselected(null); } }
protected void fireMenuCanceled() { final MenuListener[] listeners = getMenuListeners(); if (listeners.length == 0) { return; } if (menuEvent == null) { menuEvent = new MenuEvent(this); } for (int i = 0; i < listeners.length; i++) { listeners[i].menuCanceled(menuEvent); } }
protected void fireMenuDeselected() { final MenuListener[] listeners = getMenuListeners(); if (listeners.length == 0) { return; } if (menuEvent == null) { menuEvent = new MenuEvent(this); } for (int i = 0; i < listeners.length; i++) { listeners[i].menuDeselected(menuEvent); } }
protected void fireMenuSelected() { final MenuListener[] listeners = getMenuListeners(); if (listeners.length == 0) { return; } if (menuEvent == null) { menuEvent = new MenuEvent(this); } for (int i = 0; i < listeners.length; i++) { listeners[i].menuSelected(menuEvent); } }