KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > event > ComponentEvent


1 /*
2  * @(#)ComponentEvent.java 1.27 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.event;
9
10 import java.awt.AWTEvent JavaDoc;
11 import java.awt.Event JavaDoc;
12 import java.awt.Component JavaDoc;
13 import java.awt.Rectangle JavaDoc;
14
15 /**
16  * A low-level event which indicates that a component moved, changed
17  * size, or changed visibility (also, the root class for the other
18  * component-level events).
19  * <P>
20  * Component events are provided for notification purposes ONLY;
21  * The AWT will automatically handle component moves and resizes
22  * internally so that GUI layout works properly regardless of
23  * whether a program is receiving these events or not.
24  * <P>
25  * In addition to serving as the base class for other component-related
26  * events (InputEvent, FocusEvent, WindowEvent, ContainerEvent),
27  * this class defines the events that indicate changes in
28  * a component's size, position, or visibility.
29  * <P>
30  * This low-level event is generated by a component object (such as a
31  * List) when the component is moved, resized, rendered invisible, or made
32  * visible again. The event is passed to every <code>ComponentListener</code>
33  * or <code>ComponentAdapter</code> object which registered to receive such
34  * events using the component's <code>addComponentListener</code> method.
35  * (<code>ComponentAdapter</code> objects implement the
36  * <code>ComponentListener</code> interface.) Each such listener object
37  * gets this <code>ComponentEvent</code> when the event occurs.
38  *
39  * @see ComponentAdapter
40  * @see ComponentListener
41  * @see <a HREF="http://java.sun.com/docs/books/tutorial/post1.0/ui/componentlistener.html">Tutorial: Writing a Component 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  * @author Carl Quinn
45  * @version 1.27 12/19/03
46  * @since 1.1
47  */

48 public class ComponentEvent extends AWTEvent JavaDoc {
49
50     /**
51      * The first number in the range of ids used for component events.
52      */

53     public static final int COMPONENT_FIRST = 100;
54
55     /**
56      * The last number in the range of ids used for component events.
57      */

58     public static final int COMPONENT_LAST = 103;
59
60    /**
61      * This event indicates that the component's position changed.
62      */

63     public static final int COMPONENT_MOVED = COMPONENT_FIRST;
64
65     /**
66      * This event indicates that the component's size changed.
67      */

68     public static final int COMPONENT_RESIZED = 1 + COMPONENT_FIRST;
69
70     /**
71      * This event indicates that the component was made visible.
72      */

73     public static final int COMPONENT_SHOWN = 2 + COMPONENT_FIRST;
74
75     /**
76      * This event indicates that the component was rendered invisible.
77      */

78     public static final int COMPONENT_HIDDEN = 3 + COMPONENT_FIRST;
79
80     /*
81      * JDK 1.1 serialVersionUID
82      */

83     private static final long serialVersionUID = 8101406823902992965L;
84
85     /**
86      * Constructs a <code>ComponentEvent</code> object.
87      * <p>Note that passing in an invalid <code>id</code> results in
88      * unspecified behavior. This method throws an
89      * <code>IllegalArgumentException</code> if <code>source</code>
90      * is <code>null</code>.
91      *
92      * @param source the <code>Component</code> that originated the event
93      * @param id an integer indicating the type of event
94      * @throws IllegalArgumentException if <code>source</code> is null
95      */

96     public ComponentEvent(Component JavaDoc source, int id) {
97         super(source, id);
98     }
99
100     /**
101      * Returns the originator of the event.
102      *
103      * @return the <code>Component</code> object that originated
104      * the event, or <code>null</code> if the object is not a
105      * <code>Component</code>.
106      */

107     public Component JavaDoc getComponent() {
108         return (source instanceof Component JavaDoc) ? (Component JavaDoc)source : null;
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 JavaDoc paramString() {
118         String JavaDoc typeStr;
119         Rectangle JavaDoc b = (source !=null
120                ? ((Component JavaDoc)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
Popular Tags