home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / MenuItem.java < prev    next >
Text File  |  1996-05-03  |  3KB  |  121 lines

  1. /*
  2.  * @(#)MenuItem.java    1.15 95/12/14 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.awt.peer.MenuItemPeer;
  22.  
  23. /**
  24.  * A String item that represents a choice in a menu.
  25.  *
  26.  * @version 1.15, 14 Dec 1995
  27.  * @author Sami Shaio
  28.  */
  29. public class MenuItem extends MenuComponent {
  30.     boolean enabled = true;
  31.     String label;
  32.  
  33.     /** 
  34.      * Constructs a new MenuItem with the specified label.
  35.      * @param label the label for this menu item. Note that "-" is
  36.      * reserved to mean a separator between menu items.
  37.      */
  38.     public MenuItem(String label) {
  39.     this.label = label;
  40.     }
  41.  
  42.     /**
  43.      * Creates the menu item's peer.  The peer allows us to modify the 
  44.      * appearance of the menu item without changing its functionality.
  45.      */
  46.     public synchronized void addNotify() {
  47.     if (peer == null) {
  48.         peer = Toolkit.getDefaultToolkit().createMenuItem(this);
  49.     }
  50.     }
  51.  
  52.     /**
  53.      * Gets the label for this menu item.
  54.      */
  55.     public String getLabel() {
  56.     return label;
  57.     }
  58.  
  59.     /**
  60.      * Sets the label to be the specified label.
  61.      * @param label the label for this menu item
  62.      */
  63.     public void setLabel(String label) {
  64.     this.label = label;
  65.     MenuItemPeer peer = (MenuItemPeer)this.peer;
  66.     if (peer != null) {
  67.         peer.setLabel(label);
  68.     }
  69.     }
  70.  
  71.     /**
  72.      * Checks whether the menu item is enabled.
  73.      */
  74.     public boolean isEnabled() {
  75.     return enabled;
  76.     }
  77.  
  78.     /**
  79.      * Makes this menu item selectable by the user.
  80.      */
  81.     public void enable() {
  82.     enabled = true;
  83.     MenuItemPeer peer = (MenuItemPeer)this.peer;
  84.     if (peer != null) {
  85.         peer.enable();
  86.     }
  87.     }
  88.  
  89.     /**
  90.      * Conditionally enables a component.
  91.      * @param cond enabled if true; disabled otherwise.
  92.      * @see #enable
  93.      * @see #disable
  94.      */
  95.     public void enable(boolean cond) {
  96.     if (cond) {
  97.         enable();
  98.     } else {
  99.         disable();
  100.     }
  101.     }
  102.  
  103.     /**
  104.      * Makes this menu item unselectable by the user.
  105.      */
  106.     public void disable() {
  107.     enabled = false;
  108.     MenuItemPeer peer = (MenuItemPeer)this.peer;
  109.     if (peer != null) {
  110.         peer.disable();
  111.     }
  112.     }
  113.  
  114.     /**
  115.      * Returns the String parameter of the menu item.
  116.      */
  117.     public String paramString() {
  118.     return "label=" + label;
  119.     }
  120. }
  121.