Java JMenuItem recursiveUpdateOrInsertMenuItem(MenuElement menu, JMenuItem menuItem, boolean hideOnUpdate)

Here you can find the source of recursiveUpdateOrInsertMenuItem(MenuElement menu, JMenuItem menuItem, boolean hideOnUpdate)

Description

recursive Update Or Insert Menu Item

License

Open Source License

Parameter

Parameter Description
menu a parameter
menuItem a parameter

Return

True if another item was found with the same actionCommand

Declaration

private static boolean recursiveUpdateOrInsertMenuItem(MenuElement menu, JMenuItem menuItem,
        boolean hideOnUpdate) 

Method Source Code

//package com.java2s;
/**//from   w w w.j av a  2 s  .c  o m
 * OrbisGIS is a GIS application dedicated to scientific spatial simulation.
 * This cross-platform GIS is developed at French IRSTV institute and is able to
 * manipulate and create vector and raster spatial information.
 *
 * OrbisGIS is distributed under GPL 3 license. It is produced by the "Atelier SIG"
 * team of the IRSTV Institute <http://www.irstv.fr/> CNRS FR 2488.
 *
 * Copyright (C) 2007-2012 IRSTV (FR CNRS 2488)
 *
 * This file is part of OrbisGIS.
 *
 * OrbisGIS is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * OrbisGIS is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * OrbisGIS. If not, see <http://www.gnu.org/licenses/>.
 *
 * For more information, please consult: <http://www.orbisgis.org/>
 * or contact directly:
 * info_at_ orbisgis.org
 */

import java.awt.event.ActionListener;

import javax.swing.JMenuItem;

import javax.swing.MenuElement;

public class Main {
    /**
     * @param menu
     * @param menuItem
     * @return True if another item was found with the same actionCommand 
     */
    private static boolean recursiveUpdateOrInsertMenuItem(MenuElement menu, JMenuItem menuItem,
            boolean hideOnUpdate) {
        boolean updated = false;
        for (MenuElement menuEl : menu.getSubElements()) {
            if (menuEl instanceof JMenuItem) {
                JMenuItem subMenuItem = (JMenuItem) menuEl;
                String actionCommand = subMenuItem.getActionCommand();
                if (actionCommand.equals(menuItem.getActionCommand())) {
                    if (hideOnUpdate) {
                        subMenuItem.setVisible(false);
                    } else {
                        for (ActionListener listener : menuItem.getActionListeners()) {
                            subMenuItem.addActionListener(listener);
                        }
                    }
                    updated = true;
                }
            } else {
                updated = updated || recursiveUpdateOrInsertMenuItem(menuEl, menuItem, hideOnUpdate);
            }
        }
        return updated;
    }
}

Related

  1. getMenuItemParent(JMenuItem menuItem)
  2. getTopLevelMenu(JMenuItem menuitem)
  3. layoutMenuItem(JMenuItem menuItem, FontMetrics fm, String text, FontMetrics fmAccel, String acceleratorText, Icon icon, Icon checkIcon, Icon arrowIcon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewRect, Rectangle iconRect, Rectangle textRect, Rectangle acceleratorRect, Rectangle checkIconRect, Rectangle arrowIconRect, int textIconGap, int menuItemGap)
  4. linkMenuItem(final JMenuItem master, final JMenuItem slave)
  5. pintaBarraMenu(Graphics g, JMenuItem menuItem, Color bgColor)
  6. replaceMenuItem( @Nonnull JMenuItem orginalMenuItem, @Nonnull JMenuItem replacementMenuItem)
  7. setAccelerator(JMenuItem menuItem, int key, int mask)
  8. setCtrlAccelerator(final JMenuItem jmi, final char accelerator)
  9. setCtrlAccelerator(JMenuItem jmi, char accelerator)