src/java.desktop/share/classes/java/awt/PopupMenu.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 136      * Shows the popup menu at the x, y position relative to an origin
 137      * component.
 138      * The origin component must be contained within the component
 139      * hierarchy of the popup menu's parent.  Both the origin and the parent
 140      * must be showing on the screen for this method to be valid.
 141      * <p>
 142      * If this <code>PopupMenu</code> is being used as a <code>Menu</code>
 143      * (i.e., it has a non-<code>Component</code> parent),
 144      * then you cannot call this method on the <code>PopupMenu</code>.
 145      *
 146      * @param origin the component which defines the coordinate space
 147      * @param x the x coordinate position to popup the menu
 148      * @param y the y coordinate position to popup the menu
 149      * @exception NullPointerException  if the parent is <code>null</code>
 150      * @exception IllegalArgumentException  if this <code>PopupMenu</code>
 151      *                has a non-<code>Component</code> parent
 152      * @exception IllegalArgumentException if the origin is not in the
 153      *                parent's hierarchy
 154      * @exception RuntimeException if the parent is not showing on screen
 155      */
 156     @SuppressWarnings("deprecation")
 157     public void show(Component origin, int x, int y) {
 158         // Use localParent for thread safety.
 159         MenuContainer localParent = parent;
 160         if (localParent == null) {
 161             throw new NullPointerException("parent is null");
 162         }
 163         if (!(localParent instanceof Component)) {
 164             throw new IllegalArgumentException(
 165                 "PopupMenus with non-Component parents cannot be shown");
 166         }
 167         Component compParent = (Component)localParent;
 168         //Fixed 6278745: Incorrect exception throwing in PopupMenu.show() method
 169         //Exception was not thrown if compParent was not equal to origin and
 170         //was not Container
 171         if (compParent != origin) {
 172             if (compParent instanceof Container) {
 173                 if (!((Container)compParent).isAncestorOf(origin)) {
 174                     throw new IllegalArgumentException("origin not in parent's hierarchy");
 175                 }
 176             } else {
 177                 throw new IllegalArgumentException("origin not in parent's hierarchy");
 178             }
 179         }
 180         if (compParent.getPeer() == null || !compParent.isShowing()) {
 181             throw new RuntimeException("parent not showing on screen");
 182         }
 183         if (peer == null) {
 184             addNotify();
 185         }
 186         synchronized (getTreeLock()) {
 187             if (peer != null) {
 188                 ((PopupMenuPeer)peer).show(
 189                     new Event(origin, 0, Event.MOUSE_DOWN, x, y, 0, 0));
 190             }
 191         }
 192     }
 193 
 194 
 195 /////////////////
 196 // Accessibility support
 197 ////////////////
 198 
 199     /**
 200      * Gets the <code>AccessibleContext</code> associated with this


   1 /*
   2  * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 136      * Shows the popup menu at the x, y position relative to an origin
 137      * component.
 138      * The origin component must be contained within the component
 139      * hierarchy of the popup menu's parent.  Both the origin and the parent
 140      * must be showing on the screen for this method to be valid.
 141      * <p>
 142      * If this <code>PopupMenu</code> is being used as a <code>Menu</code>
 143      * (i.e., it has a non-<code>Component</code> parent),
 144      * then you cannot call this method on the <code>PopupMenu</code>.
 145      *
 146      * @param origin the component which defines the coordinate space
 147      * @param x the x coordinate position to popup the menu
 148      * @param y the y coordinate position to popup the menu
 149      * @exception NullPointerException  if the parent is <code>null</code>
 150      * @exception IllegalArgumentException  if this <code>PopupMenu</code>
 151      *                has a non-<code>Component</code> parent
 152      * @exception IllegalArgumentException if the origin is not in the
 153      *                parent's hierarchy
 154      * @exception RuntimeException if the parent is not showing on screen
 155      */

 156     public void show(Component origin, int x, int y) {
 157         // Use localParent for thread safety.
 158         MenuContainer localParent = parent;
 159         if (localParent == null) {
 160             throw new NullPointerException("parent is null");
 161         }
 162         if (!(localParent instanceof Component)) {
 163             throw new IllegalArgumentException(
 164                 "PopupMenus with non-Component parents cannot be shown");
 165         }
 166         Component compParent = (Component)localParent;
 167         //Fixed 6278745: Incorrect exception throwing in PopupMenu.show() method
 168         //Exception was not thrown if compParent was not equal to origin and
 169         //was not Container
 170         if (compParent != origin) {
 171             if (compParent instanceof Container) {
 172                 if (!((Container)compParent).isAncestorOf(origin)) {
 173                     throw new IllegalArgumentException("origin not in parent's hierarchy");
 174                 }
 175             } else {
 176                 throw new IllegalArgumentException("origin not in parent's hierarchy");
 177             }
 178         }
 179         if (compParent.peer == null || !compParent.isShowing()) {
 180             throw new RuntimeException("parent not showing on screen");
 181         }
 182         if (peer == null) {
 183             addNotify();
 184         }
 185         synchronized (getTreeLock()) {
 186             if (peer != null) {
 187                 ((PopupMenuPeer)peer).show(
 188                     new Event(origin, 0, Event.MOUSE_DOWN, x, y, 0, 0));
 189             }
 190         }
 191     }
 192 
 193 
 194 /////////////////
 195 // Accessibility support
 196 ////////////////
 197 
 198     /**
 199      * Gets the <code>AccessibleContext</code> associated with this