home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / menu.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  4.6 KB  |  168 lines

  1. /*
  2.  * @(#)Menu.java    1.15 95/08/29 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.util.Vector;
  22. import java.awt.peer.MenuPeer;
  23.  
  24. /**
  25.  * A Menu that is a component of a menu bar.
  26.  *
  27.  * @version 1.15, 08/29/95
  28.  * @author Sami Shaio
  29.  */
  30. public class Menu extends MenuItem implements MenuContainer {
  31.     Vector        items = new Vector();
  32.     boolean        tearOff;
  33.     boolean        isHelpMenu;
  34.  
  35.     /** 
  36.      * Constructs a new Menu with the specified label.  This menu will not
  37.      * be able to be torn off.  Torn off means that the menu will
  38.      * still appear on screen after the the mouse button has been released.
  39.      * @param label the label to be added to this menu 
  40.      */
  41.     public Menu(String label) {
  42.     this(label, false);
  43.     }
  44.  
  45.     /** 
  46.      * Constructs a new Menu with the specified label. The menu will be able
  47.      * to be torn off if tearOff is true.  Torn off means that the menu will
  48.      * still appear on screen after the the mouse button has been released.
  49.      * @param label the label to be added to this menu
  50.      * @param tearOff the boolean indicating whether or not the menu will be
  51.      * able to be torn off.
  52.      */
  53.     public Menu(String label, boolean tearOff) {
  54.     super(label);
  55.     this.tearOff = tearOff;
  56.     }
  57.  
  58.     /**
  59.      * Creates the menu's peer.  The peer allows us to modify the 
  60.      * appearance of the menu without changing its functionality.
  61.      */
  62.     public synchronized void addNotify() {
  63.     peer = Toolkit.getDefaultToolkit().createMenu(this);
  64.  
  65.     int nitems = countItems();
  66.     for (int i = 0 ; i < nitems ; i++) {
  67.         MenuItem mi = getItem(i);
  68.         mi.parent = this;
  69.         mi.addNotify();
  70.     }
  71.     super.addNotify();
  72.     }
  73.  
  74.     /**
  75.      * Removes the menu's peer.  The peer allows us to modify the appearance
  76.      * of the menu without changing its functionality.
  77.      */
  78.     public synchronized void removeNotify() {
  79.     int nitems = countItems();
  80.     for (int i = 0 ; i < nitems ; i++) {
  81.         getItem(i).removeNotify();
  82.     }
  83.     super.removeNotify();
  84.     }
  85.  
  86.     /**
  87.      * Returns true if this is a tear-off menu.  Torn off means that the menu 
  88.      * will still appear on screen after the the mouse button has been 
  89.      * released.
  90.      */
  91.     public boolean isTearOff() {
  92.     return tearOff;
  93.     }
  94.  
  95.     /** 
  96.       * Returns the number of elements in this menu.
  97.       */
  98.     public int countItems() {
  99.     return items.size();
  100.     }
  101.  
  102.     /**
  103.      * Returns the item located at the specified index of this menu.
  104.      * @param index the position of the item to be returned
  105.      */
  106.     public MenuItem getItem(int index) {
  107.     return (MenuItem)items.elementAt(index);
  108.     }
  109.  
  110.     /**
  111.      * Adds the specified item to this menu.
  112.      * @param mi the item to be added
  113.      */
  114.     public synchronized MenuItem add(MenuItem mi) {
  115.     if (mi.parent != null) {
  116.         mi.parent.remove(mi);
  117.     }
  118.     items.addElement(mi);
  119.     mi.parent = this;
  120.     if (peer != null) {
  121.         mi.addNotify();
  122.         ((MenuPeer)peer).addItem(mi);
  123.     }
  124.     return mi;
  125.     }
  126.  
  127.     /**
  128.      * Add an item with with the specified label to this menu.
  129.      * @param label the text on the item
  130.      */
  131.     public void add(String label) {
  132.     add(new MenuItem(label));
  133.     }
  134.  
  135.     /**
  136.      * Adds a separator line, or a hypen, to the menu at the current position.
  137.      */
  138.     public void addSeparator() {
  139.     add("-");
  140.     }
  141.  
  142.     /**
  143.      * Deletes the item at the specified index from this menu.
  144.      * @param index the position of the item to be removed 
  145.      */
  146.     public synchronized void remove(int index) {
  147.     MenuItem mi = getItem(index);
  148.     items.removeElementAt(index);
  149.     MenuPeer peer = (MenuPeer)this.peer;
  150.     if (peer != null) {
  151.         mi.removeNotify();
  152.         mi.parent = null;
  153.         peer.delItem(index);
  154.     }
  155.     }
  156.  
  157.     /**
  158.      * Deletes the specified item from this menu.
  159.      * @param item the item to be removed from the menu
  160.      */
  161.     public synchronized void remove(MenuComponent item) {
  162.     int index = items.indexOf(item);
  163.     if (index >= 0) {
  164.         remove(index);
  165.     }
  166.     }
  167. }
  168.