home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / MenuBar.java < prev    next >
Text File  |  1997-10-01  |  10KB  |  337 lines

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