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

  1. /*
  2.  * @(#)ContainerEvent.java    1.6 98/03/18
  3.  *
  4.  * Copyright 1996, 1997 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.  
  15. package java.awt.event;
  16.  
  17. import java.awt.AWTEvent;
  18. import java.awt.Container;
  19. import java.awt.Component;
  20.  
  21. /**
  22.  * A low-level event which indicates that a container's contents
  23.  * changed because a component was added or removed.
  24.  * <P>
  25.  * Container events are provided for notification purposes ONLY;
  26.  * The AWT will automatically handle changes to the containers
  27.  * contents internally so that the program works properly regardless of
  28.  * whether the program is receiving these events or not.
  29.  * <P>
  30.  * This low-level event is generated by a container object (such as a 
  31.  * Panel) when a component is added to it or removed from it. 
  32.  * The event is passed to every <code>ContainerListener</code>
  33.  * or <code>ContainerAdapter</code> object which registered to receive such
  34.  * events using the component's <code>addContainerListener</code> method.
  35.  * (<code>ContainerAdapter</code> objects implement the 
  36.  * <code>ContainerListener</code> interface.) Each such listener object 
  37.  * gets this <code>ContainerEvent</code> when the event occurs.
  38.  *
  39.  * @see ContainerAdapter
  40.  * @see ContainerListener
  41.  * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/containerlistener.html">Tutorial: Writing a Container Listener</a>
  42.  * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
  43.  *
  44.  * @version 1.6 03/18/98
  45.  * @author Tim Prinzing
  46.  * @author Amy Fowler
  47.  */
  48. public class ContainerEvent extends ComponentEvent {
  49.  
  50.     /**
  51.      * The first number in the range of ids used for container events.
  52.      */
  53.     public static final int CONTAINER_FIRST        = 300;
  54.  
  55.     /**
  56.      * The last number in the range of ids used for container events.
  57.      */
  58.     public static final int CONTAINER_LAST        = 301;
  59.  
  60.    /**
  61.      * This event indicates that a component was added to the container.
  62.      */
  63.     public static final int COMPONENT_ADDED    = CONTAINER_FIRST;
  64.  
  65.     /**
  66.      * This event indicates that a component was removed from the container.
  67.      */
  68.     public static final int COMPONENT_REMOVED = 1 + CONTAINER_FIRST;
  69.  
  70.     Component child;
  71.  
  72.     /*
  73.      * JDK 1.1 serialVersionUID 
  74.      */
  75.     private static final long serialVersionUID = -4114942250539772041L;
  76.  
  77.     /**
  78.      * Constructs a ContainerEvent object.
  79.      * 
  80.      * @param source the Component object (container) that originated the event
  81.      * @param id     an integer indicating the type of event
  82.      * @param child  the component that was added or removed
  83.      */
  84.     public ContainerEvent(Component source, int id, Component child) {
  85.         super(source, id);
  86.         this.child = child;
  87.     }
  88.  
  89.     /**
  90.      * Returns the originator of the event.
  91.      *
  92.      * @return the Container object that originated the event
  93.      */
  94.     public Container getContainer() {
  95.         return (Container)source; // cast should always be OK, type was checked in constructor
  96.     }
  97.  
  98.     /**
  99.      * Returns the component that was affected by the event.
  100.      *
  101.      * @return the Component object that was added or removed
  102.      */
  103.     public Component getChild() {
  104.         return child;
  105.     }
  106.  
  107.     /**
  108.      * Returns a parameter string identifying this event.
  109.      * This method is useful for event-logging and for debugging.
  110.      *
  111.      * @return a string identifying the event and its attributes
  112.      */
  113.     public String paramString() {
  114.         String typeStr;
  115.         switch(id) {
  116.           case COMPONENT_ADDED:
  117.               typeStr = "COMPONENT_ADDED";
  118.               break;
  119.           case COMPONENT_REMOVED:
  120.               typeStr = "COMPONENT_REMOVED";
  121.               break;
  122.           default:
  123.               typeStr = "unknown type";
  124.         }
  125.         return typeStr + ",child="+child.getName();
  126.     }
  127. }
  128.