home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / PopupMenu.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  4.1 KB  |  137 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)PopupMenu.java    1.17 98/07/21
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. import java.awt.peer.PopupMenuPeer;
  18.  
  19.  
  20. /**
  21.  * A class that implements a menu which can be dynamically popped up
  22.  * at a specified position within a component.<p>
  23.  * As the inheritance hierarchy implies, a PopupMenu can be used anywhere
  24.  * a Menu can be used. However, if you use a PopupMenu like a Menu (e.g.,
  25.  * you add it to a MenuBar), then you <b>cannot</b> call <code>show</code>
  26.  * on that PopupMenu.
  27.  *
  28.  * @version    1.17 07/21/98
  29.  * @author     Amy Fowler
  30.  */
  31. public class PopupMenu extends Menu {
  32.  
  33.     private static final String base = "popup";
  34.     static int nameCounter = 0;
  35.  
  36.     /*
  37.      * JDK 1.1 serialVersionUID
  38.      */
  39.     private static final long serialVersionUID = -4620452533522760060L;
  40.  
  41.     /**
  42.      * Creates a new popup menu.
  43.      */
  44.     public PopupMenu() {
  45.     this("");
  46.     }
  47.  
  48.     /**
  49.      * Creates a new popup menu with the specified name.
  50.      *
  51.      * @param label a non-null string specifying the popup menu's label 
  52.      */
  53.     public PopupMenu(String label) {
  54.     super(label);
  55.     }
  56.  
  57.     /**
  58.      * Construct a name for this MenuComponent.  Called by getName() when
  59.      * the name is null.
  60.      */
  61.     String constructComponentName() {
  62.         synchronized (getClass()) {
  63.         return base + nameCounter++;
  64.     }
  65.     }
  66.  
  67.     /**
  68.      * Creates the popup menu's peer.
  69.      * The peer allows us to change the appearance of the popup menu without
  70.      * changing any of the popup menu's functionality.
  71.      */
  72.     public void addNotify() {
  73.         synchronized (getTreeLock()) {
  74.         // If our parent is not a Component, then this PopupMenu is
  75.         // really just a plain, old Menu.
  76.         if (parent != null && !(parent instanceof Component)) {
  77.             super.addNotify();
  78.         }
  79.         else {
  80.             if (peer == null)
  81.             peer = Toolkit.getDefaultToolkit().createPopupMenu(this);
  82.         int nitems = getItemCount();
  83.         for (int i = 0 ; i < nitems ; i++) {
  84.             MenuItem mi = getItem(i);
  85.             mi.parent = this;
  86.             mi.addNotify();
  87.         }
  88.         }
  89.     }
  90.     }
  91.  
  92.    /**
  93.      * Shows the popup menu at the x, y position relative to an origin component.
  94.      * The origin component must be contained within the component
  95.      * hierarchy of the popup menu's parent.  Both the origin and the parent 
  96.      * must be showing on the screen for this method to be valid.<p>
  97.      * If this PopupMenu is being used as a Menu (i.e., it has a non-Component
  98.      * parent), then you cannot call this method on the PopupMenu.
  99.      * 
  100.      * @param origin the component which defines the coordinate space
  101.      * @param x the x coordinate position to popup the menu
  102.      * @param y the y coordinate position to popup the menu
  103.      * @exception IllegalArgumentException  if this PopupMenu has a non-
  104.      *            Component parent
  105.      */
  106.     public void show(Component origin, int x, int y) {
  107.         // Use localParent for thread safety.
  108.         MenuContainer localParent = parent;
  109.     if (localParent == null) {
  110.         throw new NullPointerException("parent is null");
  111.     }
  112.         if (!(localParent instanceof Component)) {
  113.         throw new IllegalArgumentException(
  114.             "PopupMenus with non-Component parents cannot be shown");
  115.     }
  116.         Component compParent = (Component)localParent;
  117.     if (compParent != origin &&
  118.         compParent instanceof Container &&
  119.         !((Container)compParent).isAncestorOf(origin)) {
  120.             throw new IllegalArgumentException(
  121.             "origin not in parent's hierarchy");
  122.     }
  123.     if (compParent.getPeer() == null || !compParent.isShowing()) {
  124.         throw new RuntimeException("parent not showing on screen");
  125.     }
  126.     if (peer == null) {
  127.         addNotify();
  128.     }
  129.     synchronized (getTreeLock()) {
  130.         if (peer != null) {
  131.             ((PopupMenuPeer)peer).show(
  132.             new Event(origin, 0, Event.MOUSE_DOWN, x, y, 0, 0));
  133.         }
  134.     }
  135.     }
  136. }
  137.