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

  1. /*
  2.  * @(#)MenuComponent.java    1.26 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.awt.peer.MenuComponentPeer;
  25. import java.awt.event.ActionEvent;
  26.  
  27. /**
  28.  * The abstract class <code>MenuComponent</code> is the superclass 
  29.  * of all menu-related components. In this respect, the class
  30.  * <code>MenuComponent</code> is analogous to the abstract superclass
  31.  * <code>Component</code> for AWT components.
  32.  * <p>
  33.  * Menu components receive and process AWT events, just as components do,
  34.  * through the method <code>processEvent</code>.
  35.  *
  36.  * @version     1.26, 06/17/97
  37.  * @author     Arthur van Hoff
  38.  * @since       JDK1.0
  39.  */
  40. public abstract class MenuComponent implements java.io.Serializable {
  41.     transient MenuComponentPeer peer;
  42.     transient MenuContainer parent;
  43.     Font font;
  44.     String name;
  45.  
  46.     boolean newEventsOnly = false;
  47.  
  48.     /*
  49.      * Internal constants for serialization 
  50.      */
  51.     final static String actionListenerK = Component.actionListenerK;
  52.     final static String itemListenerK = Component.itemListenerK;
  53.  
  54.     /*
  55.      * JDK 1.1 serialVersionUID 
  56.      */
  57.     private static final long serialVersionUID = -4536902356223894379L;
  58.  
  59.     /**
  60.      * Gets the name of the menu component.
  61.      * @return        the name of the menu component.
  62.      * @see           java.awt.MenuComponent#setName(java.lang.String)
  63.      * @since         JDK1.1
  64.      */
  65.     public String getName() {
  66.         return name;
  67.     }
  68.  
  69.     /**
  70.      * Sets the name of the component to the specified string.
  71.      * @param         name    the name of the menu component.
  72.      * @see           java.awt.MenuComponent#getName
  73.      * @since         JDK1.1
  74.      */
  75.     public void setName(String name) {
  76.         this.name = name;
  77.     }
  78.  
  79.     /**
  80.      * Returns the parent container for this menu component.
  81.      * @return    the menu component containing this menu component, 
  82.      *                 or <code>null</code> if this menu component 
  83.      *                 is the outermost component, the menu bar itself.
  84.      * @since     JDK1.0
  85.      */
  86.     public MenuContainer getParent() {
  87.     return parent;
  88.     }
  89.  
  90.     /**
  91.      * @deprecated As of JDK version 1.1,
  92.      * programs should not directly manipulate peers.
  93.      */
  94.     public MenuComponentPeer getPeer() {
  95.     return peer;
  96.     }
  97.  
  98.     /**
  99.      * Gets the font used for this menu component.
  100.      * @return   the font used in this menu component, if there is one; 
  101.      *                  <code>null</code> otherwise.
  102.      * @see     java.awt.MenuComponent#setFont
  103.      * @since   JDK1.0
  104.      */
  105.     public Font getFont() {
  106.     Font font = this.font;
  107.     if (font != null) {
  108.         return font;
  109.     }
  110.     MenuContainer parent = this.parent;
  111.     if (parent != null) {
  112.         return parent.getFont();
  113.     }
  114.     return null;
  115.     }
  116.  
  117.     /**
  118.      * Sets the font to be used for this menu component to the specified 
  119.      * font. This font is also used by all subcomponents of this menu 
  120.      * component, unless those subcomponents specify a different font. 
  121.      * @param     f   the font to be set.
  122.      * @see       java.awt.MenuComponent#getFont
  123.      * @since     JDK1.0
  124.      */
  125.     public void setFont(Font f) {
  126.     font = f;
  127.     }
  128.  
  129.     /**
  130.      * Removes the menu component's peer.  The peer allows us to modify the
  131.      * appearance of the menu component without changing the functionality of
  132.      * the menu component.
  133.      */
  134.     public void removeNotify() {
  135.     MenuComponentPeer p = (MenuComponentPeer)this.peer;
  136.     if (p != null) {
  137.         this.peer = null;
  138.         p.dispose();
  139.     }
  140.     }
  141.  
  142.     /**
  143.      * Posts the specified event to the menu.
  144.      * This method is part of the Java 1.0 event system
  145.      * and it is maintained only for backwards compatibility.
  146.      * Its use is discouraged, and it may not be supported
  147.      * in the future.
  148.      * @param evt the event which is to take place
  149.      * @deprecated As of JDK version 1.1,
  150.      * replaced by <code>dispatchEvent(AWTEvent)</code>.
  151.      * @since JDK1.0
  152.      */
  153.     public boolean postEvent(Event evt) {
  154.     MenuContainer parent = this.parent;
  155.     if (parent != null) {
  156.         parent.postEvent(evt);
  157.     }
  158.     return false;
  159.     }
  160.  
  161.     /*
  162.      * Delivers an event to this component or one of its sub components.
  163.      * @param e the event
  164.      */
  165.     public final void dispatchEvent(AWTEvent e) {
  166.         dispatchEventImpl(e);
  167.     }
  168.  
  169.     void dispatchEventImpl(AWTEvent e) {
  170.         if (newEventsOnly || 
  171.             (parent != null && parent instanceof MenuComponent &&
  172.              ((MenuComponent)parent).newEventsOnly)) {
  173.             if (eventEnabled(e)) {
  174.                 processEvent(e);
  175.             } else if (e instanceof ActionEvent && parent != null) {
  176.                 ((MenuComponent)parent).dispatchEvent(new ActionEvent(parent, 
  177.                                          e.getID(),
  178.                                          ((ActionEvent)e).getActionCommand()));
  179.             }
  180.                 
  181.         } else { // backward compatibility
  182.             Event olde = e.convertToOld();
  183.             if (olde != null) {
  184.                 postEvent(olde);
  185.             }
  186.         }
  187.     }
  188.  
  189.     // REMIND: remove when filtering is done at lower level
  190.     boolean eventEnabled(AWTEvent e) {
  191.         return false;
  192.     }        
  193.     /** 
  194.      * Processes events occurring on this menu component.  
  195.      * @param e the event
  196.      * @since JDK1.1
  197.      */   
  198.     protected void processEvent(AWTEvent e) {
  199.     }
  200.  
  201.     /**
  202.      * Returns the parameter string representing the state of this  
  203.      * menu component. This string is useful for debugging. 
  204.      * @return     the parameter string of this menu component.
  205.      * @since      JDK1.0
  206.      */
  207.     protected String paramString() {
  208.     return (name != null? name : "");
  209.     }
  210.  
  211.     /**
  212.      * Returns a representation of this menu component as a string. 
  213.      * @return  a string representation of this menu component.
  214.      * @since     JDK1.0
  215.      */
  216.     public String toString() {
  217.     return getClass().getName() + "[" + paramString() + "]";
  218.     }
  219. }
  220.