home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / awt / menuba~2.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  4.0 KB  |  164 lines

  1. /*
  2.  * @(#)MenuBar.java    1.15 95/09/20 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994,1995 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.MenuBarPeer;
  23.  
  24. /**
  25.  * A class that encapsulates the platform's concept of a menu bar bound
  26.  * to a Frame. In order to associate the MenuBar with an actual Frame,
  27.  * the Frame.setMenuBar() method should be called.
  28.  *
  29.  * @see Frame#setMenuBar
  30.  *
  31.  * @version 1.15, 09/20/95
  32.  * @author Sami Shaio
  33.  *
  34.  */
  35. public class MenuBar extends MenuComponent implements MenuContainer {
  36.     Vector menus = new Vector();
  37.     Menu helpMenu;
  38.  
  39.     /**
  40.      * Creates a new menu bar.
  41.      */
  42.     public MenuBar() {
  43.     }
  44.  
  45.     /**
  46.      * Creates the menu bar's peer.  The peer allows us to change the 
  47.      * appearance of the menu bar without changing any of the menu bar's 
  48.      * functionality.
  49.      */
  50.     public synchronized void addNotify() {
  51.     peer = Toolkit.getDefaultToolkit().createMenuBar(this);
  52.  
  53.     int nmenus = countMenus();
  54.     for (int i = 0 ; i < nmenus ; i++) {
  55.         getMenu(i).addNotify();
  56.     }
  57.     }
  58.  
  59.     /**
  60.      * Removes the menu bar's peer.  The peer allows us to change the 
  61.      * appearance of the menu bar without changing any of the menu bar's 
  62.      * functionality.
  63.      */
  64.     public void removeNotify() {
  65.     int nmenus = countMenus();
  66.     for (int i = 0 ; i < nmenus ; i++) {
  67.         getMenu(i).removeNotify();
  68.     }
  69.     super.removeNotify();
  70.     }
  71.  
  72.     /**
  73.      * Gets the help menu on the menu bar.
  74.      */
  75.     public Menu getHelpMenu() {
  76.     return helpMenu;
  77.     }
  78.  
  79.     /**
  80.      * Sets the help menu to the specified menu on the menu bar.
  81.      * @param m the menu to be set
  82.      */
  83.     public synchronized void setHelpMenu(Menu m) {
  84.     if (helpMenu != null) {
  85.         helpMenu.removeNotify();
  86.         helpMenu.parent = null;
  87.     }
  88.  
  89.     helpMenu = m;
  90.     if (m != null) {
  91.         m.isHelpMenu = true;
  92.         MenuBarPeer peer = (MenuBarPeer)this.peer;
  93.         if (peer != null) {
  94.         if (m.peer == null) {
  95.             m.addNotify();
  96.         }
  97.         peer.addHelpMenu(m);
  98.         }
  99.     }
  100.     }
  101.  
  102.     /**
  103.      * Adds the specified menu to the menu bar.
  104.      * @param m the menu to be added to the menu bar
  105.      */
  106.     public synchronized Menu add(Menu m) {
  107.     if (m.parent != null) {
  108.         m.parent.remove(m);
  109.     }
  110.     menus.addElement(m);
  111.     m.parent = this;
  112.  
  113.     MenuBarPeer peer = (MenuBarPeer)this.peer;
  114.     if (peer != null) {
  115.         if (m.peer == null) {
  116.         m.addNotify();
  117.         }
  118.         peer.addMenu(m);
  119.     }
  120.     return m;
  121.     }
  122.  
  123.     /**
  124.      * Removes the menu located at the specified index from the menu bar.
  125.      * @param index the position of the menu to be removed
  126.      */
  127.     public synchronized void remove(int index) {
  128.     MenuBarPeer peer = (MenuBarPeer)this.peer;
  129.     if (peer != null) {
  130.         Menu m = getMenu(index);
  131.         m.removeNotify();
  132.         m.parent = null;
  133.         peer.delMenu(index);
  134.     }
  135.     menus.removeElementAt(index);
  136.     }
  137.  
  138.     /**
  139.      * Removes the specified menu from the menu bar.
  140.      * @param m the menu to be removed
  141.      */
  142.     public synchronized void remove(MenuComponent m) {
  143.     int index = menus.indexOf(m);
  144.     if (index >= 0) {
  145.         remove(index);
  146.     }
  147.     }
  148.  
  149.     /**
  150.      * Counts the number of menus on the menu bar.
  151.      */
  152.     public int countMenus() {
  153.     return menus.size();
  154.     }
  155.  
  156.     /**
  157.      * Gets the specified menu.
  158.      * @param i the menu to be returned
  159.      */
  160.     public Menu getMenu(int i) {
  161.     return (Menu)menus.elementAt(i);
  162.     }
  163. }
  164.