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 / ComponentEvent.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  4.8 KB  |  144 lines

  1. /*
  2.  * @(#)ComponentEvent.java    1.17 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.Event;
  19. import java.awt.Component;
  20. import java.awt.Rectangle;
  21.  
  22. /**
  23.  * A low-level event which indicates that a component moved, changed
  24.  * size, or changed visibility (also, the root class for the other 
  25.  * component-level events).
  26.  * <P>
  27.  * Component events are provided for notification purposes ONLY;
  28.  * The AWT will automatically handle component moves and resizes
  29.  * internally so that GUI layout works properly regardless of
  30.  * whether a program is receiving these events or not.
  31.  * <P>
  32.  * In addition to serving as the base class for other component-related
  33.  * events (InputEvent, FocusEvent, WindowEvent, ContainerEvent),
  34.  * this class defines the events that indicate changes in
  35.  * a component's size, position, or visibility. 
  36.  * <P>
  37.  * This low-level event is generated by a component object (such as a 
  38.  * List) when the component is moved, resized, rendered invisible, or made
  39.  * visible again. The event is passed to every <code>ComponentListener</code>
  40.  * or <code>ComponentAdapter</code> object which registered to receive such
  41.  * events using the component's <code>addComponentListener</code> method.
  42.  * (<code>ComponentAdapter</code> objects implement the 
  43.  * <code>ComponentListener</code> interface.) Each such listener object 
  44.  * gets this <code>ComponentEvent</code> when the event occurs.
  45.  *
  46.  * @see ComponentAdapter
  47.  * @see ComponentListener
  48.  * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/componentlistener.html">Tutorial: Writing a Component Listener</a>
  49.  * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
  50.  *
  51.  * @version 1.17 03/18/98
  52.  * @author Carl Quinn
  53.  */
  54. public class ComponentEvent extends AWTEvent {
  55.  
  56.     /**
  57.      * The first number in the range of ids used for component events.
  58.      */
  59.     public static final int COMPONENT_FIRST        = 100;
  60.  
  61.     /**
  62.      * The last number in the range of ids used for component events.
  63.      */
  64.     public static final int COMPONENT_LAST        = 103;
  65.  
  66.    /**
  67.      * This event indicates that the component's position changed.
  68.      */
  69.     public static final int COMPONENT_MOVED    = COMPONENT_FIRST;
  70.  
  71.     /**
  72.      * This event indicates that the component's size changed.
  73.      */
  74.     public static final int COMPONENT_RESIZED    = 1 + COMPONENT_FIRST;
  75.  
  76.     /**
  77.      * This event indicates that the component was made visible.
  78.      */
  79.     public static final int COMPONENT_SHOWN    = 2 + COMPONENT_FIRST;
  80.  
  81.     /**
  82.      * This event indicates that the component was rendered invisible.
  83.      */
  84.     public static final int COMPONENT_HIDDEN    = 3 + COMPONENT_FIRST;
  85.  
  86.     /*
  87.      * JDK 1.1 serialVersionUID 
  88.      */
  89.     private static final long serialVersionUID = 8101406823902992965L;
  90.  
  91.     /**
  92.      * Constructs a ComponentEvent object.
  93.      *
  94.      * @param source the Component object that originated the event
  95.      * @param id     an integer indicating the type of event
  96.      */
  97.     public ComponentEvent(Component source, int id) {
  98.         super(source, id);
  99.     }
  100.  
  101.     /**
  102.      * Returns the originator of the event.
  103.      *
  104.      * @return the Component object that originated the event
  105.      */
  106.     public Component getComponent() {
  107. //        return (source instanceof Component) ? (Component)source : null;
  108.         return (Component)source; // cast should always be OK, type was checked in constructor
  109.     }
  110.  
  111.     /**
  112.      * Returns a parameter string identifying this event.
  113.      * This method is useful for event-logging and for debugging.
  114.      *
  115.      * @return a string identifying the event and its attributes
  116.      */
  117.     public String paramString() {
  118.         String typeStr;
  119.         Rectangle b = (source !=null
  120.                ? ((Component)source).getBounds()
  121.                : null);
  122.  
  123.         switch(id) {
  124.           case COMPONENT_SHOWN:
  125.               typeStr = "COMPONENT_SHOWN";
  126.               break;
  127.           case COMPONENT_HIDDEN:
  128.               typeStr = "COMPONENT_HIDDEN";
  129.               break;
  130.           case COMPONENT_MOVED:
  131.               typeStr = "COMPONENT_MOVED ("+ 
  132.                          b.x+","+b.y+" "+b.width+"x"+b.height+")";
  133.               break;
  134.           case COMPONENT_RESIZED:
  135.               typeStr = "COMPONENT_RESIZED ("+ 
  136.                          b.x+","+b.y+" "+b.width+"x"+b.height+")";
  137.               break;
  138.           default:
  139.               typeStr = "unknown type";
  140.         }
  141.         return typeStr;
  142.     }
  143. }
  144.