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

  1. /*
  2.  * @(#)Menu.java    1.38 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.MenuPeer;
  27. import java.awt.event.KeyEvent;
  28.  
  29. /**
  30.  * A <code>Menu</code> object is a pull-down menu component 
  31.  * that is deployed from a menu bar. 
  32.  * <p>
  33.  * A menu can optionally be a <i>tear-off</i> menu. A tear-off menu 
  34.  * can be opened and dragged away from its parent menu bar or menu. 
  35.  * It remains on the screen after the mouse button has been released. 
  36.  * The mechanism for tearing off a menu is platform dependent, since 
  37.  * the look and feel of the tear-off menu is determined by its peer.
  38.  * On platforms that do not support tear-off menus, the tear-off
  39.  * property is ignored.
  40.  * <p>
  41.  * Each item in a menu must belong to the <code>MenuItem</code> 
  42.  * class. It can be an instance of <code>MenuItem</code>, a submenu 
  43.  * (an instance of <code>Menu</code>), or a check box (an instance of 
  44.  * <code>CheckboxMenuItem</code>).
  45.  *
  46.  * @version 1.38, 06/17/97
  47.  * @author Sami Shaio
  48.  * @see     java.awt.MenuItem
  49.  * @see     java.awt.CheckboxMenuItem
  50.  * @since   JDK1.0
  51.  */
  52. public class Menu extends MenuItem implements MenuContainer {
  53.     Vector        items = new Vector();
  54.     boolean        tearOff;
  55.     boolean        isHelpMenu;
  56.  
  57.     private static final String base = "menu";
  58.     private static int nameCounter = 0;
  59.  
  60.     /*
  61.      * JDK 1.1 serialVersionUID 
  62.      */
  63.      private static final long serialVersionUID = -8809584163345499784L;
  64.  
  65.     /** 
  66.      * Constructs a new menu with an empty label. This menu is not
  67.      * a tear-off menu.
  68.      * @since      JDK1.1
  69.      */
  70.     public Menu() {
  71.     this("", false);
  72.     }
  73.  
  74.     /** 
  75.      * Constructs a new menu with the specified label. This menu is not
  76.      * a tear-off menu.
  77.      * @param       label the menu's label in the menu bar, or in 
  78.      *                   another menu of which this menu is a submenu.
  79.      * @since       JDK1.0
  80.      */
  81.     public Menu(String label) {
  82.     this(label, false);
  83.     }
  84.  
  85.     /** 
  86.      * Constructs a new menu with the specified label. If the 
  87.      * value of <code>tearOff</code> is <code>true</code>,
  88.      * the menu can be torn off.
  89.      * <p>
  90.      * Tear-off functionality may not be supported by all 
  91.      * implementations of AWT.  If a particular implementation doesn't 
  92.      * support tear-off menus, this value is silently ignored.
  93.      * @param       label the menu's label in the menu bar, or in 
  94.      *                   another menu of which this menu is a submenu.
  95.      * @param       tearOff   if <code>true</code>, the menu 
  96.      *                   is a tear-off menu.
  97.      * @since       JDK1.0.
  98.      */
  99.     public Menu(String label, boolean tearOff) {
  100.     super(label);
  101.     this.name = base + nameCounter++;
  102.     this.tearOff = tearOff;
  103.     }
  104.  
  105.     /**
  106.      * Creates the menu's peer.  The peer allows us to modify the 
  107.      * appearance of the menu without changing its functionality.
  108.      */
  109.     public void addNotify() {
  110.     if (peer == null) {
  111.         peer = Toolkit.getDefaultToolkit().createMenu(this);
  112.     }
  113.     int nitems = getItemCount();
  114.     for (int i = 0 ; i < nitems ; i++) {
  115.         MenuItem mi = getItem(i);
  116.         mi.parent = this;
  117.         mi.addNotify();
  118.     }
  119.     }
  120.  
  121.     /**
  122.      * Removes the menu's peer.  The peer allows us to modify the appearance
  123.      * of the menu without changing its functionality.
  124.      */
  125.     public void removeNotify() {
  126.     int nitems = getItemCount();
  127.     for (int i = 0 ; i < nitems ; i++) {
  128.         getItem(i).removeNotify();
  129.     }
  130.     super.removeNotify();
  131.     }
  132.  
  133.     /**
  134.      * Indicates whether this menu is a tear-off menu.  
  135.      * <p>
  136.      * Tear-off functionality may not be supported by all 
  137.      * implementations of AWT.  If a particular implementation doesn't 
  138.      * support tear-off menus, this value is silently ignored.
  139.      * @return      <code>true</code> if this is a tear-off menu; 
  140.      *                         <code>false</code> otherwise.
  141.      * @since       JDK1.0
  142.      */
  143.     public boolean isTearOff() {
  144.     return tearOff;
  145.     }
  146.  
  147.     /** 
  148.       * Get the number of items in this menu.
  149.       * @return     the number of items in this menu.
  150.       * @since      JDK1.1
  151.       */
  152.     public int getItemCount() {
  153.     return countItems();
  154.     }
  155.  
  156.     /** 
  157.      * @deprecated As of JDK version 1.1,
  158.      * replaced by <code>getItemCount()</code>.
  159.      */
  160.     public int countItems() {
  161.     return items.size();
  162.     }
  163.  
  164.     /**
  165.      * Gets the item located at the specified index of this menu.
  166.      * @param     index the position of the item to be returned.
  167.      * @return    the item located at the specified index.
  168.      * @since     JDK1.0
  169.      */
  170.     public MenuItem getItem(int index) {
  171.     return (MenuItem)items.elementAt(index);
  172.     }
  173.  
  174.     /**
  175.      * Adds the specified menu item to this menu. If the 
  176.      * menu item has been part of another menu, remove it  
  177.      * from that menu. 
  178.      * @param       mi   the menu item to be added.
  179.      * @return      the menu item added.
  180.      * @see         java.awt.Menu#insert(java.lang.String, int)
  181.      * @see         java.awt.Menu#insert(java.awt.MenuItem, int)
  182.      * @since       JDK1.0
  183.      */
  184.     public synchronized MenuItem add(MenuItem mi) {
  185.     if (mi.parent != null) {
  186.         mi.parent.remove(mi);
  187.     }
  188.     items.addElement(mi);
  189.     mi.parent = this;
  190.         MenuPeer peer = (MenuPeer)this.peer;
  191.     if (peer != null) {
  192.         mi.addNotify();
  193.         peer.addItem(mi);
  194.     }
  195.     return mi;
  196.     }
  197.  
  198.     /**
  199.      * Adds an item with the specified label to this menu. 
  200.      * @param       label   the text on the item.
  201.      * @see         java.awt.Menu#insert(java.lang.String, int)
  202.      * @see         java.awt.Menu#insert(java.awt.MenuItem, int)
  203.      * @since       JDK1.0
  204.      */
  205.     public void add(String label) {
  206.     add(new MenuItem(label));
  207.     }
  208.  
  209.     /**
  210.      * Inserts a menu item into this menu 
  211.      * at the specified position.
  212.      * @param         menuitem  the menu item to be inserted.
  213.      * @param         index     the position at which the menu  
  214.      *                          item should be inserted.
  215.      * @see           java.awt.Menu#add(java.lang.String)
  216.      * @see           java.awt.Menu#add(java.awt.MenuItem)
  217.      * @exception     IllegalArgumentException if the value of
  218.      *                    <code>index</code> is less than zero.
  219.      * @since         JDK1.1
  220.      */
  221.  
  222.     public synchronized void insert(MenuItem menuitem, int index) {
  223.     if (index < 0) {
  224.         throw new IllegalArgumentException("index less than zero.");
  225.     }
  226.  
  227.         int nitems = getItemCount();
  228.     Vector tempItems = new Vector();
  229.  
  230.     /* Remove the item at index, nitems-index times 
  231.        storing them in a temporary vector in the
  232.        order they appear on the menu.
  233.        */
  234.     for (int i = index ; i < nitems; i++) {
  235.         tempItems.addElement(getItem(index));
  236.         remove(index);
  237.     }
  238.  
  239.     add(menuitem);
  240.  
  241.     /* Add the removed items back to the menu, they are
  242.        already in the correct order in the temp vector.
  243.        */
  244.     for (int i = 0; i < tempItems.size()  ; i++) {
  245.         add((MenuItem)tempItems.elementAt(i));
  246.     }
  247.     }
  248.  
  249.     /**
  250.      * Inserts a menu item with the specified label into this menu 
  251.      * at the specified position.
  252.      * @param       label the text on the item.
  253.      * @param       index the position at which the menu item 
  254.      *                      should be inserted.
  255.      * @see         java.awt.Menu#add(java.lang.String)
  256.      * @see         java.awt.Menu#add(java.awt.MenuItem)
  257.      * @since       JDK1.1
  258.      */
  259.  
  260.     public void insert(String label, int index) {
  261.         insert(new MenuItem(label), index);
  262.     }
  263.       
  264.     /**
  265.      * Adds a separator line, or a hypen, to the menu at the current position.
  266.      * @see         java.awt.Menu#insertSeparator(int)
  267.      * @since       JDK1.0
  268.      */
  269.     public void addSeparator() {
  270.     add("-");
  271.     }
  272.  
  273.     /**
  274.      * Inserts a separator at the specified position.
  275.      * @param       index the position at which the 
  276.      *                       menu separator should be inserted.
  277.      * @exception   IllegalArgumentException if the value of 
  278.      *                       <code>index</code> is less than 0.
  279.      * @see         java.awt.Menu#addSeparator
  280.      * @since       JDK1.1
  281.      */
  282.  
  283.     public void insertSeparator(int index) {
  284.     if (index < 0) {
  285.         throw new IllegalArgumentException("index less than zero.");
  286.     }
  287.  
  288.         int nitems = getItemCount();
  289.     Vector tempItems = new Vector();
  290.  
  291.     /* Remove the item at index, nitems-index times 
  292.        storing them in a temporary vector in the
  293.        order they appear on the menu.
  294.        */
  295.     for (int i = index ; i < nitems; i++) {
  296.         tempItems.addElement(getItem(index));
  297.         remove(index);
  298.     }
  299.  
  300.     addSeparator();
  301.  
  302.     /* Add the removed items back to the menu, they are
  303.        already in the correct order in the temp vector.
  304.        */
  305.     for (int i = 0; i < tempItems.size()  ; i++) {
  306.         add((MenuItem)tempItems.elementAt(i));
  307.     }
  308.     }
  309.  
  310.     /**
  311.      * Removes the menu item at the specified index from this menu.
  312.      * @param       index the position of the item to be removed. 
  313.      * @since       JDK1.0 
  314.      */
  315.     public synchronized void remove(int index) {
  316.     MenuItem mi = getItem(index);
  317.     items.removeElementAt(index);
  318.         MenuPeer peer = (MenuPeer)this.peer;
  319.     if (peer != null) {
  320.         mi.removeNotify();
  321.         mi.parent = null;
  322.         peer.delItem(index);
  323.     }
  324.     }
  325.  
  326.     /**
  327.      * Removes the specified menu item from this menu.
  328.      * @param       item the item to be removed from the menu
  329.      * @since       JDK1.0
  330.      */
  331.     public synchronized void remove(MenuComponent item) {
  332.     int index = items.indexOf(item);
  333.     if (index >= 0) {
  334.         remove(index);
  335.     }
  336.     }
  337.  
  338.     /**
  339.      * Removes all items from this menu.
  340.      * @since       JDK1.0.
  341.      */
  342.     public synchronized void removeAll() {
  343.         int nitems = getItemCount();
  344.     for (int i = 0 ; i < nitems ; i++) {
  345.         remove(0);
  346.     }
  347.     }
  348.  
  349.     /*
  350.      * Post an ActionEvent to the target of the MenuPeer 
  351.      * associated with the specified keyboard event (on 
  352.      * keydown).  Returns true if there is an associated 
  353.      * keyboard event.
  354.      */
  355.     boolean handleShortcut(KeyEvent e) {
  356.         int nitems = getItemCount();
  357.         for (int i = 0 ; i < nitems ; i++) {
  358.             MenuItem mi = getItem(i);
  359.             if (mi.handleShortcut(e)) {
  360.                 return true;
  361.             }
  362.         }
  363.         return false;
  364.     }
  365.  
  366.     MenuItem getShortcutMenuItem(MenuShortcut s) {
  367.     int nitems = getItemCount();
  368.     for (int i = 0 ; i < nitems ; i++) {
  369.             MenuItem mi = getItem(i).getShortcutMenuItem(s);
  370.             if (mi != null) {
  371.                 return mi;
  372.             }
  373.     }
  374.         return null;
  375.     }
  376.  
  377.     synchronized Enumeration shortcuts() {
  378.         Vector shortcuts = new Vector();
  379.         int nitems = getItemCount();
  380.     for (int i = 0 ; i < nitems ; i++) {
  381.             MenuItem mi = getItem(i);
  382.             if (mi instanceof Menu) {
  383.                 Enumeration e = ((Menu)mi).shortcuts();
  384.                 while (e.hasMoreElements()) {
  385.                     shortcuts.addElement(e.nextElement());
  386.                 }
  387.             } else {
  388.                 MenuShortcut ms = mi.getShortcut();
  389.                 if (ms != null) {
  390.                     shortcuts.addElement(ms);
  391.                 }
  392.             }
  393.     }
  394.         return shortcuts.elements();
  395.     }
  396.  
  397.     void deleteShortcut(MenuShortcut s) {
  398.     int nitems = getItemCount();
  399.     for (int i = 0 ; i < nitems ; i++) {
  400.         getItem(i).deleteShortcut();
  401.     }
  402.     }
  403.  
  404.  
  405.     /* Serialization support.  A MenuContainer is responsible for
  406.      * restoring the parent fields of its children. 
  407.      */
  408.  
  409.     private int menuSerializedDataVersion = 1;
  410.  
  411.     private void writeObject(java.io.ObjectOutputStream s)
  412.       throws java.lang.ClassNotFoundException,
  413.          java.io.IOException 
  414.     {
  415.       s.defaultWriteObject();
  416.     }
  417.  
  418.     private void readObject(java.io.ObjectInputStream s)
  419.       throws java.lang.ClassNotFoundException,
  420.          java.io.IOException 
  421.     {
  422.       s.defaultReadObject();
  423.       for(int i = 0; i < items.size(); i++) {
  424.     MenuItem item = (MenuItem)items.elementAt(i);
  425.     item.parent = this;
  426.       }
  427.     }
  428.  
  429.     /**
  430.      * Gets the parameter string representing the state of this menu. 
  431.      * This string is useful for debugging.
  432.      * @since      JDK1.0nu.
  433.      */
  434.     public String paramString() {
  435.         String str = ",tearOff=" + tearOff+",isHelpMenu=" + isHelpMenu;
  436.         return super.paramString() + str;
  437.     }
  438. }
  439.  
  440.