List of usage examples for javax.swing JMenu getMenuComponentCount
@BeanProperty(bound = false) public int getMenuComponentCount()
From source file:core.PlanC.java
private static void configureWorkMenuBar() { menuBar = new JMenuBar(); // --------------------------------------------------------------- // app menu - Only master user // --------------------------------------------------------------- JMenu menu = new JMenu(TStringUtils.getBundleString("about.app.id")); // MenuActionFactory maf = null; // maf = new MenuActionFactory(SystemVars.class); // menu.add(maf); // maf = new MenuActionFactory(TConnectionDialog.class); // maf.setDimension(MenuActionFactory.PACK_DIMMENTION); // menu.add(maf); // maf = new MenuActionFactory(TDriverDialog.class); // maf.setDimension(MenuActionFactory.PACK_DIMMENTION); // menu.add(maf); // menu.add(new LoadView()); // menu.add(new SaveView()); // menu.add(new UpdateManifestView()); // menuBar.add(menu); // apend plugin actions. menu = new JMenu("Extensions"); Vector<String> ip = PluginManager.getInstalledPlungin(); for (String pic : ip) { String act = PluginManager.getPluginProperty(pic, "plugin.type"); if (act.equals(Plugin.TYPE_UI)) { Object obj = PluginManager.getPlugin(pic).executePlugin(null); if (obj instanceof TAbstractAction) { menu.add((TAbstractAction) obj); }/*from w w w .j a v a 2 s . c o m*/ // TODO: 180116: is necesary grant plugins more control over jmenubar. !!!! if (obj instanceof Vector) { Vector v = (Vector) obj; // for plancplugin, every action in vector is a main menu if (pic.equals("SLEPlanC")) { for (Object jc : v) { menuBar.add((JComponent) jc); } } else { JMenu jm = new JMenu(PluginManager.getPluginProperty(pic, "plugin.caption")); for (Object aa : v) { jm.add((AbstractAction) aa); } menu.add(jm); } } } } if (menu.getMenuComponentCount() > 0) { menuBar.add(menu); } // --------------------------------------------------------------- // Help // --------------------------------------------------------------- menu = new JMenu(TStringUtils.getBundleString("main.menuitem.help")); // g7.add(new Help()); // menu.add(new UpdateManifestView()); // menu.add(new JSeparator(JSeparator.HORIZONTAL)); // menu.add(new DockingAction(Wellcome.class)); // menu.add(new DockingAction(HelpBrowser.class)); // HelpRecorderAction act = new HelpRecorderAction(); // act.setEnabled(SLESession.getUserFieldValue("t_ususer_id").equals("*master")); // g7.add(act); // maf = new MenuActionFactory(AuditLog.class); // maf.setDimension(MenuActionFactory.LARGE_DIMMENTION); // menu.add(maf); // maf = new MenuActionFactory(References.class); // maf.setDimension(MenuActionFactory.LARGE_DIMMENTION); // menu.add(maf); menu.add(new About()); menuBar.add(menu); // --------------------------------------------------------------- // user // --------------------------------------------------------------- // 171201 1.24: Mierdaaa ya es diciembre y yo pelandooooooooo otro aooo !?!?! user options moved to // plancselector // menu = new JMenu((String) Session.getUserName()); // menu.setIcon(TResourceUtils.getSmallIcon("user_user")); // menu.add(new PChangePasswordAction()); // menu.add(new SignOut()); // menu.add(new JSeparator(JSeparator.HORIZONTAL)); // menu.add(new Exit()); // menuBar.add(menu); // --------------------------------------------------------------- // selector // --------------------------------------------------------------- final PlanCSelector pcs = new PlanCSelector(); menuBar.add(pcs.getBreadcrumb()); frame.setJMenuBar(menuBar); }
From source file:com.isencia.passerelle.hmi.HMIBase.java
/** * Constructs a default menu./*from www . j a v a2 s.c o m*/ * <ul> * <li>If the menuItems set is null, all default items are created. * <li>If the set is not null, only the menu items whose names are in there * are shown * </ul> * For an overview of the names, check HMIMessages.MENU_... * * @param menuItemsToShow * @param menuItemsToHide * @return */ public JMenuBar createDefaultMenu(final Set<String> menuItemsToShow, final Set<String> menuItemsToHide) { final JMenuBar menuBar = new JMenuBar(); final JMenu fileMenu = new JMenu(HMIMessages.getString(HMIMessages.MENU_FILE)); fileMenu.setMnemonic(HMIMessages.getString(HMIMessages.MENU_FILE + HMIMessages.KEY).charAt(0)); if (showThing(HMIMessages.MENU_TEMPLATES, menuItemsToShow, menuItemsToHide)) { final JMenu templatesSubMenu = new JMenu(HMIMessages.getString(HMIMessages.MENU_TEMPLATES)); final Iterator itr = hmiModelsDef.getModels().keySet().iterator(); final List<String> orderedList = new ArrayList<String>(); while (itr.hasNext()) { final String modelKey = (String) itr.next(); final String labelKey = modelKey; // HMIMessages.getString(modelKey); orderedList.add(labelKey); } Collections.sort(orderedList); for (int i = 0; i < orderedList.size(); i++) { try { // JMenuItem templateMenuItem = new // JMenuItem(HMIMessages.getString(HMIMessages.MENU_TEMPLATES) // + " " + HMIMessages.getString(modelKey)); final JMenuItem templateMenuItem = new JMenuItem(HMIMessages.getString(orderedList.get(i))); templateMenuItem.addActionListener(new TemplateModelOpener(orderedList.get(i))); templatesSubMenu.add(templateMenuItem); } catch (final Exception e1) { e1.printStackTrace(); logger.error("", e1); } } fileMenu.add(templatesSubMenu); StateMachine.getInstance().registerActionForState(StateMachine.READY, HMIMessages.MENU_TEMPLATES, templatesSubMenu); StateMachine.getInstance().registerActionForState(StateMachine.MODEL_OPEN, HMIMessages.MENU_TEMPLATES, templatesSubMenu); } if (showModelGraph) { if (showThing(HMIMessages.MENU_NEW, menuItemsToShow, menuItemsToHide)) { if (modelCreatorAction == null) { modelCreatorAction = new ModelCreator(this); } fileMenu.add(modelCreatorAction); // final JMenuItem fileNewMenuItem = new JMenuItem(HMIMessages // .getString(HMIMessages.MENU_NEW), HMIMessages.getString( // HMIMessages.MENU_NEW + HMIMessages.KEY).charAt(0)); // fileNewMenuItem.setAccelerator(KeyStroke.getKeyStroke( // KeyEvent.VK_N, InputEvent.CTRL_MASK)); // fileNewMenuItem.addActionListener(new ModelCreator()); // fileMenu.add(fileNewMenuItem); // StateMachine.getInstance().registerActionForState( // StateMachine.READY, HMIMessages.MENU_NEW, fileNewMenuItem); // StateMachine.getInstance().registerActionForState( // StateMachine.MODEL_OPEN, HMIMessages.MENU_NEW, // fileNewMenuItem); } } if (showThing(HMIMessages.MENU_OPEN, menuItemsToShow, menuItemsToHide)) { final JMenuItem fileOpenMenuItem = new JMenuItem(HMIMessages.getString(HMIMessages.MENU_OPEN), HMIMessages.getString(HMIMessages.MENU_OPEN + HMIMessages.KEY).charAt(0)); fileOpenMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK)); fileOpenMenuItem.addActionListener(new ModelOpener()); fileMenu.add(fileOpenMenuItem); StateMachine.getInstance().registerActionForState(StateMachine.READY, HMIMessages.MENU_OPEN, fileOpenMenuItem); StateMachine.getInstance().registerActionForState(StateMachine.MODEL_OPEN, HMIMessages.MENU_OPEN, fileOpenMenuItem); } if (showThing(HMIMessages.MENU_CLOSE, menuItemsToShow, menuItemsToHide)) { final JMenuItem fileCloseMenuItem = new JMenuItem(HMIMessages.getString(HMIMessages.MENU_CLOSE), HMIMessages.getString(HMIMessages.MENU_CLOSE + HMIMessages.KEY).charAt(0)); fileCloseMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK)); fileCloseMenuItem.addActionListener(new ModelCloser()); fileMenu.add(fileCloseMenuItem); StateMachine.getInstance().registerActionForState(StateMachine.MODEL_OPEN, HMIMessages.MENU_CLOSE, fileCloseMenuItem); } if (showThing(HMIMessages.MENU_SAVE, menuItemsToShow, menuItemsToHide)) { fileMenu.add(new JSeparator()); if (saveAction == null) { saveAction = new SaveAction(this); } fileMenu.add(saveAction); // StateMachine.getInstance().registerActionForState( // StateMachine.MODEL_OPEN, HMIMessages.MENU_SAVE, save); } if (showThing(HMIMessages.MENU_SAVEAS, menuItemsToShow, menuItemsToHide)) { if (saveAsAction == null) { saveAsAction = new SaveAsAction(this); } fileMenu.add(saveAsAction); // StateMachine.getInstance().registerActionForState( // StateMachine.MODEL_OPEN, HMIMessages.MENU_SAVEAS, save); } if (showThing(HMIMessages.MENU_EXIT, menuItemsToShow, menuItemsToHide)) { final JMenuItem exitMenuItem = new JMenuItem(HMIMessages.getString(HMIMessages.MENU_EXIT), HMIMessages.getString(HMIMessages.MENU_EXIT + HMIMessages.KEY).charAt(0)); exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK)); exitMenuItem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { if (logger.isTraceEnabled()) { logger.trace("Exit action - entry"); //$NON-NLS-1$ } logger.info(HMIMessages.getString(HMIMessages.INFO_EXIT)); // DBA to be validating... // Application which use HMIBase needs to call exit // treatment before exiting exitApplication(); if (logger.isTraceEnabled()) { logger.trace("Exit action - exit"); //$NON-NLS-1$ } System.exit(0); } }); fileMenu.add(new JSeparator()); fileMenu.add(exitMenuItem); StateMachine.getInstance().registerActionForState(StateMachine.READY, HMIMessages.MENU_EXIT, exitMenuItem); StateMachine.getInstance().registerActionForState(StateMachine.MODEL_OPEN, HMIMessages.MENU_EXIT, exitMenuItem); } final JMenu runMenu = new JMenu(HMIMessages.getString(HMIMessages.MENU_RUN)); runMenu.setMnemonic(HMIMessages.getString(HMIMessages.MENU_RUN + HMIMessages.KEY).charAt(0)); if (showThing(HMIMessages.MENU_EXECUTE, menuItemsToShow, menuItemsToHide)) { if (modelExecutor == null) { modelExecutor = new ModelExecutor(this); } final JMenuItem runExecuteMenuItem = new JMenuItem(modelExecutor); runMenu.add(runExecuteMenuItem); StateMachine.getInstance().registerActionForState(StateMachine.MODEL_OPEN, HMIMessages.MENU_EXECUTE, modelExecutor); } if (showThing(HMIMessages.MENU_STOP, menuItemsToShow, menuItemsToHide)) { if (modelStopper == null) { modelStopper = new ModelStopper(this); } final JMenuItem stopExecuteMenuItem = new JMenuItem(modelStopper); runMenu.add(stopExecuteMenuItem); StateMachine.getInstance().registerActionForState(StateMachine.MODEL_EXECUTING, HMIMessages.MENU_STOP, modelStopper); } if (showThing(HMIMessages.MENU_INTERACTIVE_ERRORHANDLING, menuItemsToShow, menuItemsToHide)) { final JMenuItem interactiveErrorCtrlMenuItem = new JCheckBoxMenuItem( HMIMessages.getString(HMIMessages.MENU_INTERACTIVE_ERRORHANDLING)); interactiveErrorCtrlMenuItem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { interactiveErrorControl = ((JCheckBoxMenuItem) e.getSource()).getState(); } }); runMenu.add(new JSeparator()); runMenu.add(interactiveErrorCtrlMenuItem); } final JMenu graphMenu = new JMenu(HMIMessages.getString(HMIMessages.MENU_GRAPH)); graphMenu.setMnemonic(HMIMessages.getString(HMIMessages.MENU_GRAPH + HMIMessages.KEY).charAt(0)); // if (showThing(HMIMessages.MENU_SHOW, menuItemsToShow, // menuItemsToHide)) { // JMenuItem graphViewMenuItem = new // JCheckBoxMenuItem(HMIMessages.getString(HMIMessages.MENU_SHOW)); // graphMenu.add(graphViewMenuItem); // // graphViewMenuItem.addActionListener(new ActionListener() { // public void actionPerformed(ActionEvent e) { // if (logger.isTraceEnabled()) { // logger.trace("Graph Show action - entry"); //$NON-NLS-1$ // } // boolean showGraph = ((JCheckBoxMenuItem) e.getSource()).getState(); // if(showGraph) { // applyFieldValuesToParameters(); // clearModelForms(); // showModelGraph(HMIBase.this.currentModel.getName()); // } else { // clearModelGraphs(); // showModelForm(HMIBase.this.currentModel.getName()); // } // if (logger.isTraceEnabled()) { // logger.trace("Graph Show action - exit"); //$NON-NLS-1$ // } // } // // // }); // } if (showThing(HMIMessages.MENU_ANIMATE, menuItemsToShow, menuItemsToHide)) { final JMenuItem animateGraphViewMenuItem = new JCheckBoxMenuItem( HMIMessages.getString(HMIMessages.MENU_ANIMATE)); animateGraphViewMenuItem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { animateModelExecution = ((JCheckBoxMenuItem) e.getSource()).getState(); } }); graphMenu.add(new JSeparator()); graphMenu.add(animateGraphViewMenuItem); } if (fileMenu.getMenuComponentCount() > 0) { menuBar.add(fileMenu); } if (runMenu.getMenuComponentCount() > 0) { menuBar.add(runMenu); StateMachine.getInstance().registerActionForState(StateMachine.MODEL_OPEN, HMIMessages.MENU_RUN, runMenu); StateMachine.getInstance().registerActionForState(StateMachine.MODEL_EXECUTING, HMIMessages.MENU_RUN, runMenu); } if (graphMenu.getMenuComponentCount() > 0) { menuBar.add(graphMenu); StateMachine.getInstance().registerActionForState(StateMachine.MODEL_OPEN, HMIMessages.MENU_GRAPH, graphMenu); StateMachine.getInstance().registerActionForState(StateMachine.MODEL_EXECUTING, HMIMessages.MENU_GRAPH, graphMenu); } final JMenu monitoringMenu = new JMenu(HMIMessages.getString(HMIMessages.MENU_MONITORING)); monitoringMenu.setMnemonic(HMIMessages.getString(HMIMessages.MENU_MONITORING + HMIMessages.KEY).charAt(0)); if (showThing(HMIMessages.MENU_TRACING, menuItemsToShow, menuItemsToHide)) { final JMenuItem traceMenuItem = new JMenuItem(HMIMessages.getString(HMIMessages.MENU_TRACING), HMIMessages.getString(HMIMessages.MENU_TRACING + HMIMessages.KEY).charAt(0)); traceMenuItem.addActionListener(new TraceDialogOpener()); monitoringMenu.add(traceMenuItem); } if (monitoringMenu.getMenuComponentCount() > 0) { menuBar.add(monitoringMenu); } StateMachine.getInstance().compile(); StateMachine.getInstance().transitionTo(StateMachine.READY); return menuBar; }
From source file:no.java.ems.client.swing.EmsClient.java
private void removeIcons(final JMenu menu) { for (int index = 0; index < menu.getMenuComponentCount(); index++) { Component item = menu.getMenuComponent(index); if (item instanceof JMenu) { removeIcons((JMenu) item); }//from ww w .ja va 2 s . co m if (item instanceof JMenuItem) { ((JMenuItem) item).setIcon(null); } } }
From source file:org.datacleaner.panels.DatabaseDriversPanel.java
private void updateComponents() { this.removeAll(); final PopupButton addDriverButton = WidgetFactory.createDefaultPopupButton("Add database driver", IconUtils.ACTION_ADD);/* ww w. ja v a 2 s . co m*/ final JPopupMenu addDriverMenu = addDriverButton.getMenu(); final JMenu automaticDownloadAndInstallMenu = new JMenu("Automatic download and install"); automaticDownloadAndInstallMenu .setIcon(imageManager.getImageIcon(IconUtils.ACTION_DOWNLOAD, IconUtils.ICON_SIZE_MENU_ITEM)); final List<DatabaseDriverDescriptor> drivers = _databaseDriverCatalog.getDatabaseDrivers(); for (DatabaseDriverDescriptor dd : drivers) { final String[] urls = dd.getDownloadUrls(); if (urls != null && _databaseDriverCatalog.getState(dd) == DatabaseDriverState.NOT_INSTALLED) { final JMenuItem downloadAndInstallMenuItem = WidgetFactory.createMenuItem(dd.getDisplayName(), dd.getIconImagePath()); downloadAndInstallMenuItem.addActionListener(createDownloadActionListener(dd)); automaticDownloadAndInstallMenu.add(downloadAndInstallMenuItem); } } if (automaticDownloadAndInstallMenu.getMenuComponentCount() == 0) { automaticDownloadAndInstallMenu.setEnabled(false); } final JMenuItem localJarFilesMenuItem = WidgetFactory.createMenuItem("Local JAR file(s)...", IconUtils.FILE_ARCHIVE); localJarFilesMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { AddDatabaseDriverDialog dialog = new AddDatabaseDriverDialog(_databaseDriverCatalog, DatabaseDriversPanel.this, _windowContext, _userPreferences); dialog.setVisible(true); } }); addDriverMenu.add(automaticDownloadAndInstallMenu); addDriverMenu.add(localJarFilesMenuItem); final DCTable table = getDatabaseDriverTable(); this.add(DCPanel.flow(Alignment.RIGHT, addDriverButton), BorderLayout.NORTH); this.add(table.toPanel(), BorderLayout.CENTER); }
From source file:org.eclipse.wb.internal.swing.model.component.exposed.SwingHierarchyProvider.java
@Override public Object[] getChildrenObjects(Object object) throws Exception { // javax.swing.JMenu if (object instanceof JMenu) { JMenu menu = (JMenu) object; int componentCount = menu.getMenuComponentCount(); Component[] menuComponents = new Component[componentCount]; for (int i = 0; i < componentCount; i++) { menuComponents[i] = menu.getMenuComponent(i); }/*from ww w .j ava 2 s .c om*/ return menuComponents; } // generic java.awt.Container if (object instanceof Container) { Container container = (Container) object; return container.getComponents(); } // unknown return ArrayUtils.EMPTY_OBJECT_ARRAY; }
From source file:org.eobjects.datacleaner.panels.DatabaseDriversPanel.java
private void updateComponents() { this.removeAll(); final JToolBar toolBar = WidgetFactory.createToolBar(); toolBar.add(WidgetFactory.createToolBarSeparator()); final JButton addDriverButton = new JButton("Add database driver", imageManager.getImageIcon(IconUtils.ACTION_ADD)); addDriverButton.addActionListener(new ActionListener() { @Override// w ww . j ava 2 s. c om public void actionPerformed(ActionEvent e) { final JMenu menu = new JMenu("Automatic download and install"); menu.setIcon(imageManager.getImageIcon("images/actions/download.png")); final List<DatabaseDriverDescriptor> drivers = _databaseDriverCatalog.getDatabaseDrivers(); for (DatabaseDriverDescriptor dd : drivers) { final String[] urls = dd.getDownloadUrls(); if (urls != null && _databaseDriverCatalog.getState(dd) == DatabaseDriverState.NOT_INSTALLED) { final JMenuItem downloadAndInstallMenuItem = WidgetFactory .createMenuItem(dd.getDisplayName(), dd.getIconImagePath()); downloadAndInstallMenuItem.addActionListener(createDownloadActionListener(dd)); menu.add(downloadAndInstallMenuItem); } } if (menu.getMenuComponentCount() == 0) { menu.setEnabled(false); } final JMenuItem localJarFilesMenuItem = WidgetFactory.createMenuItem("Local JAR file(s)...", "images/filetypes/archive.png"); localJarFilesMenuItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { AddDatabaseDriverDialog dialog = new AddDatabaseDriverDialog(_databaseDriverCatalog, DatabaseDriversPanel.this, _windowContext, _userPreferences); dialog.setVisible(true); } }); final JPopupMenu popup = new JPopupMenu(); popup.add(menu); popup.add(localJarFilesMenuItem); popup.show(addDriverButton, 0, addDriverButton.getHeight()); } }); toolBar.add(addDriverButton); final DCTable table = getDatabaseDriverTable(); this.add(toolBar, BorderLayout.NORTH); this.add(table.toPanel(), BorderLayout.CENTER); }
From source file:org.kepler.gui.MenuMapper.java
public void init() { if (_tableauFrameInstance == null) { log.error(//from www . ja v a 2 s . com "MenuMapper cannot proceed, due to one or more NULL values:" + "\nptiiMenubar = " + _ptiiMenubar + "\ntableauFrameInstance = " + _tableauFrameInstance + "\ndefaulting to PTII menus"); return; } // First, we need to make sure PTII has finished constructing its menus. // Those menus are created and assembled in a thread that is started in // the // pack() method of ptolemy.gui.Top. As that thread finishes, it adds // the // new ptii JMenuBar to the frame - so we test for that here, and don't // proceed until the frame has the new JMenuBar added // For safety, so we don't have an infinite loop, we also set a safety // counter: // maxWaitMS is the longest the app waits for the ptii // menus to be added, before it continues anyway. final int maxWaitMS = 500; // sleepTimeMS is the amount of time it sleeps per loop: final int sleepTimeMS = 5; // //// final int maxLoops = maxWaitMS / sleepTimeMS; int safetyCounter = 0; while (safetyCounter++ < maxLoops && !_tableauFrameInstance.isMenuPopulated()) { if (isDebugging) { log.debug("Waiting for PTII menus to be created... " + safetyCounter); } try { Thread.sleep(sleepTimeMS); } catch (Exception e) { // ignore } } JMenuBar _keplerMenubar = null; if (_ptiiMenubar == null) { _ptiiMenubar = _tableauFrameInstance.getJMenuBar(); } if (_ptiiMenubar != null) { // gets here if a PTII menubar has been added to frame... // 1) Now PTII has finished constructing its menus, get all // menu items and put them in a Map for easier access later... _ptiiMenuActionsMap = getPTIIMenuActionsMap(); Map<String, Action> ptiiMenuActionsMap = _ptiiMenuActionsMap; // 2) Now we have all the PTII menu items, get the // Kepler-specific menu mappings from the preferences file, // then go thru the Kepler menu mappings and // populate the new menubar with Kepler menus, // creating any new menu items that don't exist yet // this is a Map that will be used to keep track of // what we have added to the menus, and in what order _keplerMenubar = createKeplerMenuBar(ptiiMenuActionsMap); if (_keplerMenubar != null) { // First, look to see if any menus are empty. If // they are, remove the top-level menu from the menubar... // ** NOTE - do these by counting *down* to zero, otherwise the // menus' // indices change dynamically as we remove menus, causing // errors! for (int m = _keplerMenubar.getMenuCount() - 1; m >= 0; m--) { JMenu nextMenu = _keplerMenubar.getMenu(m); if (nextMenu.getMenuComponentCount() < 1) { if (isDebugging) { log.debug("deleting empty menu: " + nextMenu.getText()); } _keplerMenubar.remove(nextMenu); } } // hide the ptii menubar _tableauFrameInstance.hideMenuBar(); // add the new menu bar _tableauFrameInstance.setJMenuBar(_keplerMenubar); } else { log.error("Problem creating Kepler menus - defaulting to PTII menus"); } } else { // gets here if a PTII menubar has *NOT* been added to frame... // Therefore, this frame doesn't have a menubar by default, // so we probably shouldn't add one, for now, at least // hide the ptii menubar (may not be necessary) _tableauFrameInstance.hideMenuBar(); // add the new menu bar (may not be necessary) _tableauFrameInstance.setJMenuBar(null); } }
From source file:org.kepler.gui.MenuMapper.java
private static void storePTIITopLevelMenus(JMenu nextMenu, String menuPath, final String MENU_PATH_DELIMITER, Map<String, Action> ptiiMenuActionsMap) { int totMenuItems = nextMenu.getMenuComponentCount(); for (int n = 0; n < totMenuItems; n++) { Component nextComponent = nextMenu.getMenuComponent(n); if (nextComponent instanceof JMenuItem) { storePTIIMenuItems((JMenuItem) nextComponent, new StringBuffer(menuPath), MENU_PATH_DELIMITER, ptiiMenuActionsMap); }/*from w w w. jav a 2 s. c o m*/ // (if it's not an instanceof JMenuItem, it must // be a separator, and can therefore be ignored) } }
From source file:org.kepler.gui.MenuMapper.java
public static JMenuItem addMenuFor(String key, Action action, JComponent topLvlContainer, Map<String, JMenuItem> keplerMenuMap) { if (topLvlContainer == null) { if (isDebugging) { log.debug("NULL container received (eg JMenuBar) - returning NULL"); }/*ww w . j a v a 2 s. c o m*/ return null; } if (key == null) { if (isDebugging) { log.debug("NULL key received"); } return null; } key = key.trim(); if (key.length() < 1) { if (isDebugging) { log.debug("BLANK key received"); } return null; } if (action == null && key.indexOf(MENU_SEPARATOR_KEY) < 0) { if (isDebugging) { log.debug("NULL action received, but was not a separator: " + key); } return null; } if (keplerMenuMap.containsKey(key)) { if (isDebugging) { log.debug("Menu already added; skipping: " + key); } return null; } // split delimited parts and ensure menus all exist String[] menuLevel = key.split(MENU_PATH_DELIMITER); int totLevels = menuLevel.length; // create a menu for each "menuLevel" if it doesn't already exist final StringBuffer nextLevelBuff = new StringBuffer(); String prevLevelStr = null; JMenuItem leafMenuItem = null; for (int levelIdx = 0; levelIdx < totLevels; levelIdx++) { // save previous value prevLevelStr = nextLevelBuff.toString(); String nextLevelStr = menuLevel[levelIdx]; // get the index of the first MNEMONIC_SYMBOL int mnemonicIdx = nextLevelStr.indexOf(MNEMONIC_SYMBOL); char mnemonicChar = 0; // if an MNEMONIC_SYMBOL exists, remove all underscores. Then, idx // of // first underscore becomes idx of letter it used to precede - this // is the mnemonic letter if (mnemonicIdx > -1) { nextLevelStr = nextLevelStr.replaceAll(MNEMONIC_SYMBOL, ""); mnemonicChar = nextLevelStr.charAt(mnemonicIdx); } if (levelIdx != 0) { nextLevelBuff.append(MENU_PATH_DELIMITER); } nextLevelBuff.append(nextLevelStr); // don't add multiple separators together... if (nextLevelStr.indexOf(MENU_SEPARATOR_KEY) > -1) { if (separatorJustAdded == false) { // Check if we're at the top level, since this makes sense // only for // context menu - we can't add a separator to a JMenuBar if (levelIdx == 0) { if (topLvlContainer instanceof JContextMenu) { ((JContextMenu) topLvlContainer).addSeparator(); } } else { JMenu parent = (JMenu) keplerMenuMap.get(prevLevelStr); if (parent != null) { if (parent.getMenuComponentCount() < 1) { if (isDebugging) { log.debug("------ NOT adding separator to parent " + parent.getText() + ", since it does not contain any menu items"); } } else { if (isDebugging) { log.debug("------ adding separator to parent " + parent.getText()); } // add separator to parent parent.addSeparator(); separatorJustAdded = true; } } } } } else if (!keplerMenuMap.containsKey(nextLevelBuff.toString())) { // If menu has not already been created, we need // to create it and then add it to the parent level... JMenuItem menuItem = null; // if we're at a "leaf node" - need to create a JMenuItem if (levelIdx == totLevels - 1) { // save old display name to use as actionCommand on // menuitem, // since some parts of PTII still // use "if (actionCommand.equals("SaveAs")) {..." etc String oldDisplayName = (String) action.getValue(Action.NAME); // action.putValue(Action.NAME, nextLevelStr); if (mnemonicChar > 0) { action.putValue(GUIUtilities.MNEMONIC_KEY, new Integer(mnemonicChar)); } // Now we look to see if it's a checkbox // menu item, or just a regular one String menuItemType = (String) (action.getValue(MENUITEM_TYPE)); if (menuItemType != null && menuItemType == CHECKBOX_MENUITEM_TYPE) { menuItem = new JCheckBoxMenuItem(action); } else { menuItem = new JMenuItem(action); } // -------------------------------------------------------------- /** @todo - setting menu names - TEMPORARY FIX - FIXME */ // Currently, if we use the "proper" way of setting menu // names - // ie by using action.putValue(Action.NAME, "somename");, // then // the name appears on the port buttons on the toolbar, // making // them huge. As a temporary stop-gap, I am just setting the // new // display name using setText() instead of // action.putValue(.., // but this needs to be fixed elsewhere - we want to be able // to // use action.putValue(Action.NAME (ie uncomment the line // above // that reads: // action.putValue(Action.NAME, nextLevelStr); // and delete the line below that reads: // menuItem.setText(nextLevelStr); // otherwise this may bite us in future... menuItem.setText(nextLevelStr); // -------------------------------------------------------------- // set old display name as actionCommand on // menuitem, for ptii backward-compatibility menuItem.setActionCommand(oldDisplayName); // add JMenuItem to the Action, so it can be accessed by // Action code action.putValue(NEW_JMENUITEM_KEY, menuItem); leafMenuItem = menuItem; } else { // if we're *not* at a "leaf node" - need to create a JMenu menuItem = new JMenu(nextLevelStr); if (mnemonicChar > 0) { menuItem.setMnemonic(mnemonicChar); } } // level 0 is a special case, since the container (JMenuBar or // JContextMenu) is not a JMenu or a JMenuItem, so we can't // use the same code to add child to parent... if (levelIdx == 0) { if (topLvlContainer instanceof JMenuBar) { // this handles JMenuBar menus ((JMenuBar) topLvlContainer).add(menuItem); } else if (topLvlContainer instanceof JContextMenu) { // this handles popup context menus ((JContextMenu) topLvlContainer).add(menuItem); } // add to Map keplerMenuMap.put(nextLevelBuff.toString(), menuItem); separatorJustAdded = false; } else { JMenu parent = (JMenu) keplerMenuMap.get(prevLevelStr); if (parent != null) { // add to parent parent.add(menuItem); // add to Map keplerMenuMap.put(nextLevelBuff.toString(), menuItem); separatorJustAdded = false; } else { if (isDebugging) { log.debug("Parent menu is NULL" + prevLevelStr); } } } } } return leafMenuItem; }