Example usage for javax.swing JPopupMenu getInvoker

List of usage examples for javax.swing JPopupMenu getInvoker

Introduction

In this page you can find the example usage for javax.swing JPopupMenu getInvoker.

Prototype

public Component getInvoker() 

Source Link

Document

Returns the component which is the 'invoker' of this popup menu.

Usage

From source file:Main.java

/**
 * This method exists because popup menus can not be directly moved
 * (the have to be hidden and re-shown).
 *//*from ww w.j  a va2s .c  om*/
public static void setLocationOnScreen(JPopupMenu visibleMenu, int x, int y) {
    Point invokerLocation = visibleMenu.getInvoker().getLocationOnScreen();
    visibleMenu.setVisible(false);

    visibleMenu.show(visibleMenu.getInvoker(), x - invokerLocation.x, y - invokerLocation.y);
}

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 {/*from  ww  w . j av a 2  s.  c  o m*/
            menuContainer = null;
        }
    }
    if (menuContainer != null) {
        final JPopupMenu menu = (JPopupMenu) menuContainer;
        final Component invoker = menu.getInvoker();
        return invoker;
    } else {
        return null;
    }

}

From source file:Main.java

/**
 * Repaints UI tree recursively./*  ww w. j  a va2  s  . co  m*/
 *
 * @param c UI component.
 */
private static void updateComponentTreeUI0(Component c) {
    if (c instanceof JComponent) {
        JComponent jc = (JComponent) c;
        jc.invalidate();
        jc.validate();
        jc.repaint();
        JPopupMenu jpm = jc.getComponentPopupMenu();
        if (jpm != null && jpm.isVisible() && jpm.getInvoker() == jc) {
            updateComponentTreeUI(jpm);
        }
    }
    Component[] children = null;
    if (c instanceof JMenu) {
        children = ((JMenu) c).getMenuComponents();
    } else if (c instanceof java.awt.Container) {
        children = ((java.awt.Container) c).getComponents();
    }
    if (children != null) {
        for (int i = 0; i < children.length; i++)
            updateComponentTreeUI0(children[i]);
    }
}

From source file:Main.java

public static JPopupMenu createStdEditPopupMenu(final JTextComponent[] fields) {
    final JPopupMenu popupMenu = new JPopupMenu();

    /* text fields popup menu: "Cut" */
    final JMenuItem cutMenuItem = new JMenuItem("Cut", 't');
    cutMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).cut();
            }/*from w ww.  j  av a 2s .  c  o  m*/
        }
    });
    popupMenu.add(cutMenuItem);

    /* text fields popup menu: "Copy" */
    final JMenuItem copyMenuItem = new JMenuItem("Copy", 'C');
    copyMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).copy();
            }
        }
    });
    popupMenu.add(copyMenuItem);

    /* text fields popup menu: "Paste" */
    final JMenuItem pasteMenuItem = new JMenuItem("Paste", 'P');
    pasteMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).paste();
            }
        }
    });
    popupMenu.add(pasteMenuItem);
    popupMenu.addSeparator();

    /* text fields popup menu: "Select All" */
    final JMenuItem selectAllMenuItem = new JMenuItem("Select All", 'A');
    selectAllMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).selectAll();
            }
        }
    });
    popupMenu.add(selectAllMenuItem);

    /* add mouse listeners to the specified fields */
    for (final JTextComponent f : fields) {
        f.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                processMouseEvent(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                processMouseEvent(e);
            }

            private void processMouseEvent(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                    popupMenu.setInvoker(f);
                }
            }
        });
    }
    return popupMenu;
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(400, 300);// w w w.  j a  v a 2 s.c o  m
    jPopupMenu1.add(jMenuItem1);
    jTabbedPane1.addTab(null, jLabel1);
    jTabbedPane1.addTab(null, jLabel2);
    getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
    int tabCount = jTabbedPane1.getTabCount();
    for (int i = 0; i < tabCount; i++) {
        JLabel jLabel = new JLabel("Testing the tab" + (i + 1));
        jTabbedPane1.setTabComponentAt(i, jLabel);
        jLabel.setName(String.valueOf(i));
        jLabel.setComponentPopupMenu(jPopupMenu1);
    }
    jPopupMenu1.addPopupMenuListener(new PopupMenuListener() {

        @Override
        public void popupMenuCanceled(final PopupMenuEvent evt) {
        }

        @Override
        public void popupMenuWillBecomeInvisible(final PopupMenuEvent evt) {
        }

        @Override
        public void popupMenuWillBecomeVisible(final PopupMenuEvent evt) {
            JPopupMenu source = (JPopupMenu) evt.getSource();
            JLabel invoker = (JLabel) source.getInvoker();
            JLabel component = (JLabel) jTabbedPane1.getComponentAt(Integer.parseInt(invoker.getName()));
            jMenuItem1.setText(invoker.getText() + ":  " + component.getText());
        }
    });
}

From source file:org.eclipse.jubula.rc.swing.listener.RecordActions.java

/**
 * select MenuItem/*from   w ww.  jav a2  s  .c o  m*/
 * @param mi JMenuItem
 */
protected void selectMenuItem(JMenuItem mi) {
    Component comp = mi;
    boolean isMenuBarItem = false;
    String logName = null;
    while (comp.getParent() != null) {
        if (comp.getParent() instanceof JPopupMenu) {
            JPopupMenu jpm = (JPopupMenu) comp.getParent();
            comp = jpm.getInvoker();
        } else {
            comp = comp.getParent();
        }

        if (comp instanceof JMenuBar) {
            isMenuBarItem = true;
            break;
        }
        if (comp instanceof JComponent && !(comp instanceof JMenu)) {
            break;
        }
    }
    IComponentIdentifier id = null;
    Action a = new Action();
    if (comp instanceof JComponent) {
        String pth = m_recordHelper.getPath(mi);
        List parValues = new LinkedList();
        parValues.add(pth);
        parValues.add(Constants.REC_OPERATOR);

        if (isMenuBarItem) {
            id = m_recordHelper.getMenuCompID();
            a = m_recordHelper.compSysToAction(id, "CompSystem.SelectMenuItem"); //$NON-NLS-1$
        } else {
            try {
                id = ComponentHandler.getIdentifier(comp);
                a = m_recordHelper.compSysToAction(id, "CompSystem.PopupSelectByTextPathNew"); //$NON-NLS-1$
                logName = createLogicalName(comp, id);
                parValues.add((new Integer(m_popupMouseBtn)).toString());
            } catch (NoIdentifierForComponentException nifce) {
                // no identifier for the component, log this as an error
                log.error("no identifier for '" + comp); //$NON-NLS-1$
            }
        }

        if (logName != null) {
            createCAP(a, id, parValues, logName);
        } else {
            createCAP(a, id, parValues);
        }
    }
}

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
 *//*  w w w. j  a  va2 s  .co m*/
private static Component getJMenu(JMenuItem item) {
    if (item.getParent() instanceof JPopupMenu) {
        JPopupMenu popup = (JPopupMenu) item.getParent();
        return popup.getInvoker();
    }
    return item.getParent();
}

From source file:org.eclipse.wb.internal.swing.utils.SwingImageUtils.java

private static void fetchMenuVisualData_JMenu_JPopupMenu(MenuVisualData menuData, Container menuObject)
        throws Exception {
    JPopupMenu popupMenu = menuObject instanceof JPopupMenu ? (JPopupMenu) menuObject
            : ((JMenu) menuObject).getPopupMenu();
    // image/*w  ww  . ja  v  a2s  . c o  m*/
    {
        prepareForPrinting(popupMenu);
        // OSX Java since jdk 1.6.0_20 requires menu invoker to be visible.
        // traverse parents until null or already visible and make sure that all are visible.
        // CHECK: it could flash on Windows.
        Point parentLocation = null;
        Component parent = popupMenu.getInvoker();
        while (parent != null && !parent.isShowing()) {
            Container parent2 = parent.getParent();
            if (parent2 != null) {
                parent = parent2;
            } else {
                break;
            }
        }
        if (parent != null) {
            parentLocation = parent.getLocation();
            prepareForPrinting(parent);
        }
        // fetch image
        try {
            Container popupMenuParent = popupMenu.getParent();
            if (popupMenuParent != null) {
                popupMenuParent.doLayout();
            }
            popupMenu.doLayout();
            menuData.m_menuImage = createComponentShot(popupMenu);
        } finally {
            setVisible(popupMenu, false);
            if (parent != null) {
                parent.setLocation(parentLocation);
                if (parent instanceof JPopupMenu) {
                    setVisible(parent, false);
                }
            }
        }
    }
    // bounds
    {
        org.eclipse.swt.graphics.Rectangle imageBounds = menuData.m_menuImage.getBounds();
        menuData.m_menuBounds = new Rectangle(0, 0, imageBounds.width, imageBounds.height);
    }
    // items
    fetchMenuVisualData_items(menuData, popupMenu);
}