home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / PopupMenu.java < prev    next >
Text File  |  1997-05-20  |  3KB  |  107 lines

  1. /*
  2.  * @(#)PopupMenu.java    1.9 97/01/27
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.awt;
  24.  
  25. import java.awt.peer.PopupMenuPeer;
  26.  
  27.  
  28. /**
  29.  * A class that implements a menu which can be dynamically popped up
  30.  * at a specified position within a component.
  31.  *
  32.  * @version    1.9 01/27/97
  33.  * @author     Amy Fowler
  34.  */
  35. public class PopupMenu extends Menu {
  36.  
  37.     private static final String base = "popup";
  38.     static int nameCounter = 0;
  39.  
  40.     /*
  41.      * JDK 1.1 serialVersionUID 
  42.      */
  43.     private static final long serialVersionUID = -4620452533522760060L;
  44.  
  45.     /**
  46.      * Creates a new popup menu.
  47.      */
  48.     public PopupMenu() {
  49.     this("");
  50.     }
  51.  
  52.     /**
  53.      * Creates a new popup menu with the specified name.
  54.      * @param title the title string for the popup menu
  55.      */
  56.     public PopupMenu(String label) {
  57.     super(label);
  58.     this.name = base + nameCounter++;
  59.     }
  60.  
  61.     /**
  62.      * Creates the popup menu's peer.  The peer allows us to change the 
  63.      * appearance of the popup menu without changing any of the popup menu's 
  64.      * functionality.
  65.      */
  66.     public synchronized void addNotify() {
  67.     if (peer == null) {
  68.         peer = Toolkit.getDefaultToolkit().createPopupMenu(this);
  69.     }
  70.     int nitems = getItemCount();
  71.     for (int i = 0 ; i < nitems ; i++) {
  72.         MenuItem mi = getItem(i);
  73.         mi.parent = this;
  74.         mi.addNotify();
  75.     }
  76.     }
  77.  
  78.    /**
  79.      * Shows the popup menu at the x, y position relative to an origin component.
  80.      * The origin component must be contained within the component hierarchy 
  81.      * of the popup menu's parent.  Both the origin and the parent must be 
  82.      * showing on the screen for this method to be valid.
  83.      * @param origin the component which defines the coordinate space
  84.      * @param x the x coordinate position to popup the menu
  85.      * @param y the y coordinate position to popup the menu
  86.      */
  87.     public void show(Component origin, int x, int y) {
  88.     Component p = (Component)parent;
  89.     if (p == null) {
  90.         throw new NullPointerException("parent is null");
  91.     }
  92.     if (p != origin &&
  93.         p instanceof Container && !((Container)p).isAncestorOf(origin)) {
  94.         throw new IllegalArgumentException("origin not in parent's hierarchy");
  95.     }
  96.     if (p.getPeer() == null || !p.isShowing()) {
  97.         throw new RuntimeException("parent not showing on screen");
  98.     }
  99.     if (peer == null) {
  100.         addNotify();
  101.     }
  102.     ((PopupMenuPeer)peer).show(new Event(origin, 0, Event.MOUSE_DOWN, x, y, 0, 0));
  103.     
  104.     }
  105.  
  106. }
  107.