Example usage for javax.swing JMenuItem getParent

List of usage examples for javax.swing JMenuItem getParent

Introduction

In this page you can find the example usage for javax.swing JMenuItem getParent.

Prototype

public Container getParent() 

Source Link

Document

Gets the parent of this component.

Usage

From source file:Main.java

static Component getInvoker(final JMenuItem menuItem) {
    MenuContainer menuContainer = menuItem.getParent();
    while (menuContainer != null && !(menuContainer instanceof JPopupMenu)) {
        if (menuContainer instanceof MenuItem) {
            menuContainer = ((MenuItem) menuContainer).getParent();
        } else {/*w  ww.j  a v a2 s . c om*/
            menuContainer = null;
        }
    }
    if (menuContainer != null) {
        final JPopupMenu menu = (JPopupMenu) menuContainer;
        final Component invoker = menu.getInvoker();
        return invoker;
    } else {
        return null;
    }

}

From source file:edu.ku.brc.af.core.TaskMgr.java

/**
 * @param parent/*www  .ja  va2  s  .  co m*/
 * @param menuItemDesc
 * @param menuPath
 * @param currIndex
 */
public static void buildMenuTree(final MenuElement parent, final MenuItemDesc menuItemDesc,
        final String[] menuPath, final int currIndex) {
    if (currIndex == menuPath.length) {
        MenuElement me = menuItemDesc.getMenuItem();
        if (parent instanceof JMenuBar) {
            ((JMenuBar) parent).add((JMenu) me);

        } else if (parent instanceof JMenu) {
            Container menuComp = ((JMenu) parent).getPopupMenu();
            boolean found = false;
            int insertPos = menuComp.getComponentCount(); // last position
            JMenu menu = (JMenu) parent;

            if (menuItemDesc.getPosition() == MenuItemDesc.Position.Top) {
                insertPos = 0;
                //log.debug(String.format("0 Inserted: %s - %d", ((JMenuItem)me).getText(), insertPos));

            } else if (menuItemDesc.getPosition() == MenuItemDesc.Position.Bottom) {
                //log.debug(String.format("1 Inserted: %s - %d", ((JMenuItem)me).getText(),insertPos));
            }

            if (menuItemDesc.getPosition() == MenuItemDesc.Position.Before
                    || menuItemDesc.getPosition() == MenuItemDesc.Position.After) {
                int inx = 0;
                for (int i = 0; i < menuComp.getComponentCount(); i++) {
                    Component c = menuComp.getComponent(i);
                    if (c instanceof JMenuItem
                            && ((JMenuItem) c).getText().equals(menuItemDesc.getPosMenuItemName())) {
                        insertPos = inx + 1;
                        found = true;
                        break;
                    }
                    inx++;
                }
            }

            if (menuItemDesc.getSepPosition() == MenuItemDesc.Position.Before) {
                menu.add(new JPopupMenu.Separator(), insertPos);
                insertPos++;
            }

            if (insertPos == -1) {
                menu.add((JMenuItem) me);
            } else {
                menu.add((JMenuItem) me, insertPos);
                //log.debug(String.format("2 Inserted: %s - %d", ((JMenuItem)me).getText(),insertPos));
                found = true;
            }

            insertPos++;

            if (menuItemDesc.getSepPosition() == MenuItemDesc.Position.After) {
                if (found) {
                    menu.add(new JPopupMenu.Separator(), insertPos);
                    //log.debug(String.format("3 Inserted: Sep - %d", insertPos));

                }
            }
        } else if (parent instanceof JMenuItem) {
            JMenuItem mi = (JMenuItem) parent;
            JPopupMenu menu = (JPopupMenu) mi.getParent();

            int pos = 0;
            for (int i = 0; i < menu.getComponentCount(); i++) {
                if (mi == menu.getComponent(i)) {
                    pos = i;
                    break;
                }
            }
            //log.debug(String.format("4 Inserted: %s - %d", ((JMenuItem)me).getText(), menuItemDesc.getPosition() == MenuItemDesc.Position.After ? pos + 1 : pos));
            menu.insert((JMenuItem) me,
                    menuItemDesc.getPosition() == MenuItemDesc.Position.After ? pos + 1 : pos);
        }

    } else {
        String label = getResourceString(menuPath[currIndex]);

        MenuElement menuElement = getMenuByName(parent, label);

        /*log.debug(menuPath[currIndex]+" -> "+label+ " "+menuElement);
        if (parent instanceof JMenuItem) log.debug(((JMenuItem)parent).getText());
        else if (parent instanceof JMenu) log.debug(((JMenu)parent).getText());
        else if (parent instanceof JMenuBar) log.debug("MenuBar");
        */

        if (menuElement == null) {
            log.error("Couldn't find menu element [" + label + "]");//$NON-NLS-1$ //$NON-NLS-2$
            //UIRegistry.showError("Couldn't find menu element ["+label+"]"); //$NON-NLS-1$ //$NON-NLS-2$
        }
        buildMenuTree(menuElement, menuItemDesc, menuPath, currIndex + 1);
    }
}

From source file:org.eclipse.wb.internal.swing.model.component.exposed.SwingHierarchyProvider.java

/**
 * There are no good way to find {@link JMenu} parent of {@link JMenuItem}, see
 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4103931
 */// ww  w.ja  va 2s.  c  o m
private static Component getJMenu(JMenuItem item) {
    if (item.getParent() instanceof JPopupMenu) {
        JPopupMenu popup = (JPopupMenu) item.getParent();
        return popup.getInvoker();
    }
    return item.getParent();
}