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

  1. /*
  2.  * @(#)MenuBar.java    1.36 98/03/18
  3.  *
  4.  * Copyright 1995-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. package java.awt;
  15.  
  16. import java.util.Vector;
  17. import java.util.Enumeration;
  18. import java.awt.peer.MenuBarPeer;
  19. import java.awt.event.KeyEvent;
  20.  
  21. /**
  22.  * The <code>MenuBar</code> class encapsulates the platform's 
  23.  * concept of a menu bar bound to a frame. In order to associate 
  24.  * the menu bar with a <code>Frame</code> object, call the 
  25.  * frame's <code>setMenuBar</code> method.
  26.  * <p>
  27.  * <A NAME="mbexample"></A><!-- target for cross references -->
  28.  * This is what a menu bar might look like:
  29.  * <p>
  30.  * <img src="images-awt/MenuBar-1.gif" 
  31.  * ALIGN=center HSPACE=10 VSPACE=7>
  32.  * <p>
  33.  * A menu bar handles keyboard shortcuts for menu items, passing them 
  34.  * along to its child menus. 
  35.  * (Keyboard shortcuts, which are optional, provide the user with
  36.  * an alternative to the mouse for invoking a menu item and the
  37.  * action that is associated with it.)
  38.  * Each menu item can maintain an instance of <code>MenuShortcut</code>. 
  39.  * The <code>MenuBar</code> class defines several methods, 
  40.  * <A HREF="#shortcuts"><code>shortCuts</code></A> and 
  41.  * <A HREF="#getShortcutMenuItem"><code>getShortcutMenuItem</code></A> 
  42.  * that retrieve information about the shortcuts a given
  43.  * menu bar is managing.
  44.  *
  45.  * @version 1.36, 03/18/98
  46.  * @author Sami Shaio
  47.  * @see        java.awt.Frame
  48.  * @see        java.awt.Frame#setMenuBar(java.awt.MenuBar)
  49.  * @see        java.awt.Menu
  50.  * @see        java.awt.MenuItem
  51.  * @see        java.awt.MenuShortcut
  52.  * @since      JDK1.0
  53.  */
  54. public class MenuBar extends MenuComponent implements MenuContainer {
  55.  
  56.     static {
  57.         initIDs();
  58.     }
  59.  
  60.     Vector menus = new Vector();
  61.     Menu helpMenu;
  62.  
  63.     private static final String base = "menubar";
  64.     private static int nameCounter = 0;
  65.  
  66.     /*
  67.      * JDK 1.1 serialVersionUID 
  68.      */
  69.      private static final long serialVersionUID = -4930327919388951260L;
  70.  
  71.     /**
  72.      * Creates a new menu bar.
  73.      */
  74.     public MenuBar() {
  75.         this.name = base + nameCounter++;
  76.     }
  77.  
  78.     /**
  79.      * Creates the menu bar's peer.  The peer allows us to change the 
  80.      * appearance of the menu bar without changing any of the menu bar's 
  81.      * functionality.
  82.      */
  83.     public void addNotify() {
  84.     peer = Toolkit.getDefaultToolkit().createMenuBar(this);
  85.  
  86.     int nmenus = getMenuCount();
  87.     for (int i = 0 ; i < nmenus ; i++) {
  88.         getMenu(i).addNotify();
  89.     }
  90.     }
  91.  
  92.     /**
  93.      * Removes the menu bar's peer.  The peer allows us to change the 
  94.      * appearance of the menu bar without changing any of the menu bar's 
  95.      * functionality.
  96.      */
  97.     public void removeNotify() {
  98.     int nmenus = getMenuCount();
  99.     for (int i = 0 ; i < nmenus ; i++) {
  100.         getMenu(i).removeNotify();
  101.     }
  102.     super.removeNotify();
  103.     }
  104.  
  105.     /**
  106.      * Gets the help menu on the menu bar.
  107.      * @return    the help menu on this menu bar.
  108.      */
  109.     public Menu getHelpMenu() {
  110.     return helpMenu;
  111.     }
  112.  
  113.     /**
  114.      * Sets the help menu on this menu bar to be the specified menu.
  115.      * @param     m    the menu to be set as the help menu.
  116.      */
  117.     public synchronized void setHelpMenu(Menu m) {
  118.     if (helpMenu == m) {
  119.         return;
  120.     }
  121.     if (helpMenu != null) {
  122.         helpMenu.removeNotify();
  123.         helpMenu.parent = null;
  124.     }
  125.     if (m.parent != this) {
  126.         add(m);
  127.     }
  128.     helpMenu = m;
  129.     if (m != null) {
  130.         m.isHelpMenu = true;
  131.         m.parent = this;
  132.         MenuBarPeer peer = (MenuBarPeer)this.peer;
  133.         if (peer != null) {
  134.         if (m.peer == null) {
  135.             m.addNotify();
  136.         }
  137.         peer.addHelpMenu(m);
  138.         }
  139.     }
  140.     }
  141.  
  142.     /**
  143.      * Adds the specified menu to the menu bar.
  144.      * @param        m   the menu to be added.
  145.      * @return       the menu added.
  146.      * @see          java.awt.MenuBar#remove(int)
  147.      * @see          java.awt.MenuBar#remove(java.awt.MenuComponent)
  148.      */
  149.     public synchronized Menu add(Menu m) {
  150.     if (m.parent != null) {
  151.         m.parent.remove(m);
  152.     }
  153.     menus.addElement(m);
  154.     m.parent = this;
  155.  
  156.     MenuBarPeer peer = (MenuBarPeer)this.peer;
  157.     if (peer != null) {
  158.         if (m.peer == null) {
  159.         m.addNotify();
  160.         }
  161.         peer.addMenu(m);
  162.     }
  163.     return m;
  164.     }
  165.  
  166.     /**
  167.      * Removes the menu located at the specified 
  168.      * index from this menu bar. 
  169.      * @param        index   the position of the menu to be removed.
  170.      * @see          java.awt.MenuBar#add(java.awt.Menu)
  171.      */
  172.     public synchronized void remove(int index) {
  173.     MenuBarPeer peer = (MenuBarPeer)this.peer;
  174.     if (peer != null) {
  175.         Menu m = getMenu(index);
  176.         m.removeNotify();
  177.         m.parent = null;
  178.         peer.delMenu(index);
  179.     }
  180.     menus.removeElementAt(index);
  181.     }
  182.  
  183.     /**
  184.      * Removes the specified menu component from this menu bar.
  185.      * @param        m the menu component to be removed.
  186.      * @see          java.awt.MenuBar#add(java.awt.Menu)
  187.      */
  188.     public synchronized void remove(MenuComponent m) {
  189.     int index = menus.indexOf(m);
  190.     if (index >= 0) {
  191.         remove(index);
  192.     }
  193.     }
  194.  
  195.     /**
  196.      * Gets the number of menus on the menu bar.
  197.      * @return     the number of menus on the menu bar.
  198.      * @since      JDK1.1
  199.      */
  200.     public int getMenuCount() {
  201.     return countMenus();
  202.     }
  203.  
  204.     /**
  205.      * @deprecated As of JDK version 1.1,
  206.      * replaced by <code>getMenuCount()</code>.
  207.      */
  208.     public int countMenus() {
  209.     return menus.size();
  210.     }
  211.  
  212.     /**
  213.      * Gets the specified menu.
  214.      * @param      i the index position of the menu to be returned.
  215.      * @return     the menu at the specified index of this menu bar.
  216.      */
  217.     public Menu getMenu(int i) {
  218.     return (Menu)menus.elementAt(i);
  219.     }
  220.  
  221.     /** 
  222.      * Gets an enumeration of all menu shortcuts this menu bar 
  223.      * is managing.  
  224.      * @return      an enumeration of menu shortcuts that this
  225.      *                      menu bar is managing.
  226.      * @see         java.awt.MenuShortcut
  227.      * @since       JDK1.1
  228.      */
  229.     public synchronized Enumeration shortcuts() {
  230.         Vector shortcuts = new Vector();
  231.     int nmenus = getMenuCount();
  232.     for (int i = 0 ; i < nmenus ; i++) {
  233.             Enumeration e = getMenu(i).shortcuts();
  234.             while (e.hasMoreElements()) {
  235.                 shortcuts.addElement(e.nextElement());
  236.             }
  237.     }
  238.         return shortcuts.elements();
  239.     }
  240.  
  241.     /**
  242.      * Gets the instance of <code>MenuItem</code> associated 
  243.      * with the specified <code>MenuShortcut</code> object,
  244.      * or <code>null</code> if none has been specified.
  245.      * @param        s the specified menu shortcut.
  246.      * @see          java.awt.MenuItem
  247.      * @see          java.awt.MenuShortcut
  248.      * @since        JDK1.1
  249.      */
  250.      public MenuItem getShortcutMenuItem(MenuShortcut s) {
  251.     int nmenus = getMenuCount();
  252.     for (int i = 0 ; i < nmenus ; i++) {
  253.             MenuItem mi = getMenu(i).getShortcutMenuItem(s);
  254.             if (mi != null) {
  255.                 return mi;
  256.             }
  257.     }
  258.         return null;  // MenuShortcut wasn't found
  259.      }
  260.  
  261.     /*
  262.      * Post an ACTION_EVENT to the target of the MenuPeer 
  263.      * associated with the specified keyboard event (on 
  264.      * keydown).  Returns true if there is an associated 
  265.      * keyboard event.
  266.      */
  267.     boolean handleShortcut(KeyEvent e) {
  268.         // Is it a key event?
  269.         int id = e.getID();
  270.         if (id != KeyEvent.KEY_PRESSED && id != KeyEvent.KEY_RELEASED) {
  271.             return false;
  272.         }
  273.  
  274.         // Is the accelerator modifier key pressed?
  275.         int accelKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  276.         if ((e.getModifiers() & accelKey) == 0) {
  277.             return false;
  278.         }
  279.  
  280.         // Pass MenuShortcut on to child menus.
  281.     int nmenus = getMenuCount();
  282.     for (int i = 0 ; i < nmenus ; i++) {
  283.         Menu m = getMenu(i);
  284.             if (m.handleShortcut(e)) {
  285.                 return true;
  286.             }
  287.         }
  288.         return false;
  289.     }
  290.  
  291.     /**
  292.      * Deletes the specified menu shortcut.
  293.      * @param     s the menu shortcut to delete.
  294.      * @since     JDK1.1
  295.      */
  296.     public void deleteShortcut(MenuShortcut s) {
  297.     int nmenus = getMenuCount();
  298.     for (int i = 0 ; i < nmenus ; i++) {
  299.         getMenu(i).deleteShortcut(s);
  300.         }
  301.     }
  302.  
  303.     /* Serialization support.  Restore the (transient) parent 
  304.      * fields of Menubar menus here.
  305.      */
  306.  
  307.     private int menuBarSerializedDataVersion = 1;
  308.  
  309.     private void writeObject(java.io.ObjectOutputStream s)
  310.       throws java.lang.ClassNotFoundException,
  311.          java.io.IOException 
  312.     {
  313.       s.defaultWriteObject();
  314.     }
  315.  
  316.     private void readObject(java.io.ObjectInputStream s)
  317.       throws java.lang.ClassNotFoundException,
  318.          java.io.IOException 
  319.     {
  320.       s.defaultReadObject();
  321.       for (int i = 0; i < menus.size(); i++) {
  322.     Menu m = (Menu)menus.elementAt(i);
  323.     m.parent = this;
  324.       }
  325.     }
  326.  
  327.     /**
  328.      * Initialize JNI field and method IDs
  329.      */
  330.     private static native void initIDs();
  331. }
  332.