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

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