home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / PopupMenu.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  2.8 KB  |  99 lines

  1. /*
  2.  * @(#)PopupMenu.java    1.11 98/03/18
  3.  *
  4.  * Copyright 1996, 1997 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.
  23.  *
  24.  * @version    1.11 03/18/98
  25.  * @author     Amy Fowler
  26.  */
  27. public class PopupMenu extends Menu {
  28.  
  29.     private static final String base = "popup";
  30.     static int nameCounter = 0;
  31.  
  32.     /*
  33.      * JDK 1.1 serialVersionUID 
  34.      */
  35.     private static final long serialVersionUID = -4620452533522760060L;
  36.  
  37.     /**
  38.      * Creates a new popup menu.
  39.      */
  40.     public PopupMenu() {
  41.     this("");
  42.     }
  43.  
  44.     /**
  45.      * Creates a new popup menu with the specified name.
  46.      * @param title the title string for the popup menu
  47.      */
  48.     public PopupMenu(String label) {
  49.     super(label);
  50.     this.name = base + nameCounter++;
  51.     }
  52.  
  53.     /**
  54.      * Creates the popup menu's peer.  The peer allows us to change the 
  55.      * appearance of the popup menu without changing any of the popup menu's 
  56.      * functionality.
  57.      */
  58.     public synchronized void addNotify() {
  59.     if (peer == null) {
  60.         peer = Toolkit.getDefaultToolkit().createPopupMenu(this);
  61.     }
  62.     int nitems = getItemCount();
  63.     for (int i = 0 ; i < nitems ; i++) {
  64.         MenuItem mi = getItem(i);
  65.         mi.parent = this;
  66.         mi.addNotify();
  67.     }
  68.     }
  69.  
  70.    /**
  71.      * Shows the popup menu at the x, y position relative to an origin component.
  72.      * The origin component must be contained within the component hierarchy 
  73.      * of the popup menu's parent.  Both the origin and the parent must be 
  74.      * showing on the screen for this method to be valid.
  75.      * @param origin the component which defines the coordinate space
  76.      * @param x the x coordinate position to popup the menu
  77.      * @param y the y coordinate position to popup the menu
  78.      */
  79.     public void show(Component origin, int x, int y) {
  80.     Component p = (Component)parent;
  81.     if (p == null) {
  82.         throw new NullPointerException("parent is null");
  83.     }
  84.     if (p != origin &&
  85.         p instanceof Container && !((Container)p).isAncestorOf(origin)) {
  86.         throw new IllegalArgumentException("origin not in parent's hierarchy");
  87.     }
  88.     if (p.getPeer() == null || !p.isShowing()) {
  89.         throw new RuntimeException("parent not showing on screen");
  90.     }
  91.     if (peer == null) {
  92.         addNotify();
  93.     }
  94.     ((PopupMenuPeer)peer).show(new Event(origin, 0, Event.MOUSE_DOWN, x, y, 0, 0));
  95.     
  96.     }
  97.  
  98. }
  99.