Java JPopupMenu showModal(javax.swing.JPopupMenu popupMenu, java.awt.Component invoker, java.awt.Point pt)

Here you can find the source of showModal(javax.swing.JPopupMenu popupMenu, java.awt.Component invoker, java.awt.Point pt)

Description

show Modal

License

Open Source License

Declaration

public static void showModal(javax.swing.JPopupMenu popupMenu, java.awt.Component invoker, java.awt.Point pt) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006, 2015, Carnegie Mellon University. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Products derived from the software may not be called "Alice", nor may
 *    "Alice" appear in their name, without prior written permission of
 *    Carnegie Mellon University./*w w w  . j a  v  a  2  s  . c  o  m*/
 *
 * 4. All advertising materials mentioning features or use of this software must
 *    display the following acknowledgement: "This product includes software
 *    developed by Carnegie Mellon University"
 *
 * 5. The gallery of art assets and animations provided with this software is
 *    contributed by Electronic Arts Inc. and may be used for personal,
 *    non-commercial, and academic use only. Redistributions of any program
 *    source code that utilizes The Sims 2 Assets must also retain the copyright
 *    notice, list of conditions and the disclaimer contained in
 *    The Alice 3.0 Art Gallery License.
 *
 * DISCLAIMER:
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
 * ANY AND ALL EXPRESS, STATUTORY OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,  FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHORS, COPYRIGHT OWNERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING FROM OR OTHERWISE RELATING TO
 * THE USE OF OR OTHER DEALINGS WITH THE SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

public class Main {
    public static void showModal(javax.swing.JPopupMenu popupMenu, java.awt.Component invoker, java.awt.Point pt) {
        java.awt.Component root = javax.swing.SwingUtilities.getRoot(invoker);
        final javax.swing.JLayeredPane layeredPane;
        if (root instanceof javax.swing.JFrame) {
            javax.swing.JFrame window = (javax.swing.JFrame) root;
            layeredPane = window.getLayeredPane();
        } else if (root instanceof javax.swing.JDialog) {
            javax.swing.JDialog window = (javax.swing.JDialog) root;
            layeredPane = window.getLayeredPane();
        } else if (root instanceof javax.swing.JWindow) {
            javax.swing.JWindow window = (javax.swing.JWindow) root;
            layeredPane = window.getLayeredPane();
        } else {
            layeredPane = null;
        }
        if (layeredPane != null) {
            class EventConsumer extends javax.swing.JComponent {
                private java.awt.event.MouseListener mouseAdapter = new java.awt.event.MouseListener() {
                    @Override
                    public void mouseEntered(java.awt.event.MouseEvent e) {
                    }

                    @Override
                    public void mouseExited(java.awt.event.MouseEvent e) {
                    }

                    @Override
                    public void mousePressed(java.awt.event.MouseEvent e) {
                    }

                    @Override
                    public void mouseReleased(java.awt.event.MouseEvent e) {
                        EventConsumer.this.removeFromParentJustInCaseAnExceptionWasThrownSomewhere();
                    }

                    @Override
                    public void mouseClicked(java.awt.event.MouseEvent e) {
                    }
                };

                private void removeFromParentJustInCaseAnExceptionWasThrownSomewhere() {
                    java.awt.Container parent = this.getParent();
                    if (parent != null) {
                        parent.remove(this);
                    }
                }

                @Override
                public void addNotify() {
                    super.addNotify();
                    this.setLocation(0, 0);
                    java.awt.Component parent = this.getParent();
                    if (parent != null) {
                        this.setSize(parent.getSize());
                    }
                    this.addMouseListener(this.mouseAdapter);
                }

                @Override
                public void removeNotify() {
                    this.removeMouseListener(this.mouseAdapter);
                    super.removeNotify();
                }

            }
            final EventConsumer eventConsumer = new EventConsumer();
            popupMenu.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
                @Override
                public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent e) {
                    layeredPane.add(eventConsumer, new Integer(javax.swing.JLayeredPane.MODAL_LAYER));
                }

                @Override
                public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent e) {
                    layeredPane.remove(eventConsumer);
                }

                @Override
                public void popupMenuCanceled(javax.swing.event.PopupMenuEvent e) {
                }
            });
        }

        int x;
        int y;
        if (pt != null) {
            x = pt.x;
            y = pt.y;
        } else {
            if (invoker != null) {
                //todo
                //if( invoker.getComponentOrientation().isLeftToRight() ) {
                x = 0;
                y = invoker.getHeight();
            } else {
                x = 0;
                y = 0;
            }
        }
        popupMenu.show(invoker, x, y);
    }
}

Related

  1. setLocationOnScreen(JPopupMenu visibleMenu, int x, int y)
  2. setPopupMenu(final JComponent component, final JPopupMenu popup)
  3. setPopupMenu(final JComponent component, final JPopupMenu popup)
  4. showButtonPopupMenu(AbstractButton button, JPopupMenu popup)
  5. showMenu(JPopupMenu menu, JButton button)
  6. showPopMenuWithParentWidth(JPopupMenu popup, Component parentComponent, int x, int y)
  7. showPopup(JPopupMenu popup, MouseEvent event)
  8. showPopupCloseMenu(JPopupMenu popup, Component parentComponent)
  9. showPopupMenu(final JPopupMenu popup, final Component component, int x, int y)