Java Swing Menu getOperationMenu(MenuElement currentMenu, String path)

Here you can find the source of getOperationMenu(MenuElement currentMenu, String path)

Description

get Operation Menu

License

Open Source License

Declaration

private static Object[] getOperationMenu(MenuElement currentMenu,
            String path) 

Method Source Code

//package com.java2s;
/*//w w w  .  j  a v a2 s .co  m
 * #%L
 * The AIBench Workbench Plugin
 * %%
 * Copyright (C) 2006 - 2016 Daniel Glez-Pe?a and Florentino Fdez-Riverola
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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 Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JMenu;
import javax.swing.JMenuBar;

import javax.swing.JPopupMenu;
import javax.swing.MenuElement;

public class Main {
    private static Object[] getOperationMenu(MenuElement currentMenu,
            String path) {

        if (path == null || path.equals(""))
            return new Object[] { currentMenu, path };

        ArrayList<JMenu> menus = new ArrayList<JMenu>();

        //populate menus
        if ((Object) currentMenu instanceof JMenuBar) {
            JMenuBar bar = (JMenuBar) currentMenu;
            int count = bar.getMenuCount();

            for (int i = 0; i < count; i++) {
                menus.add(bar.getMenu(i));
            }
        } else if ((Object) currentMenu instanceof JPopupMenu) {
            JPopupMenu bar = (JPopupMenu) currentMenu;
            int count = bar.getSubElements().length;

            for (int i = 0; i < count; i++) {
                if ((JMenu) bar.getSubElements()[i] instanceof JMenu) {
                    menus.add((JMenu) bar.getSubElements()[i]);
                }
            }
        } else {
            // is a instance of JMenu
            JMenu menu = (JMenu) currentMenu;
            int count = menu.getMenuComponentCount();

            for (int i = 0; i < count; i++) {
                Object comp = menu.getMenuComponent(i);
                if (comp instanceof JMenu) {
                    menus.add((JMenu) comp);
                }
            }
        }

        //get the token
        StringTokenizer tk = new StringTokenizer(path, "/");
        String pathElement = null;
        if (tk.hasMoreTokens()) {
            pathElement = tk.nextToken();

            for (int i = 0; i < menus.size(); i++) {

                //throwing out @ char
                String noAt = pathElement;
                if (pathElement.indexOf("@") != -1) {
                    noAt = pathElement
                            .substring(pathElement.indexOf("@") + 1);
                }
                if (menus.get(i).getText() != null
                        && menus.get(i).getText().equals(noAt)) {
                    if (tk.hasMoreTokens()) {
                        return getOperationMenu(menus.get(i),
                                path.substring(pathElement.length() + 1));
                    } else {
                        return getOperationMenu(menus.get(i), "");
                    }
                }
            }
        }
        return new Object[] { currentMenu, path };
    }
}

Related

  1. fixIconTextGap(JComponent menu)
  2. getAllMenuSubElements( MenuElement root)
  3. getMenuFontHeight()
  4. getMenuKeyStroke(String stroke)
  5. getMenuSectionLabel(String string)
  6. getParsedElements(MenuElement[] elements)
  7. getTheMenuBar(String[][] arr)
  8. hasIcons(MenuElement menuElement)
  9. installMenuKeyListener(Component cmp, MenuKeyListener keyListener)