1 /*
2 * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package java.awt.event;
27
28 import java.awt.AWTEvent;
29 import java.awt.Component;
30 import java.awt.Rectangle;
31
32 /**
33 * A low-level event which indicates that a component moved, changed
34 * size, or changed visibility (also, the root class for the other
35 * component-level events).
36 * <P>
37 * Component events are provided for notification purposes ONLY;
38 * The AWT will automatically handle component moves and resizes
39 * internally so that GUI layout works properly regardless of
40 * whether a program is receiving these events or not.
41 * <P>
42 * In addition to serving as the base class for other component-related
43 * events (InputEvent, FocusEvent, WindowEvent, ContainerEvent),
44 * this class defines the events that indicate changes in
45 * a component's size, position, or visibility.
46 * <P>
47 * This low-level event is generated by a component object (such as a
48 * List) when the component is moved, resized, rendered invisible, or made
49 * visible again. The event is passed to every <code>ComponentListener</code>
50 * or <code>ComponentAdapter</code> object which registered to receive such
51 * events using the component's <code>addComponentListener</code> method.
52 * (<code>ComponentAdapter</code> objects implement the
53 * <code>ComponentListener</code> interface.) Each such listener object
54 * gets this <code>ComponentEvent</code> when the event occurs.
55 * <p>
56 * An unspecified behavior will be caused if the {@code id} parameter
57 * of any particular {@code ComponentEvent} instance is not
58 * in the range from {@code COMPONENT_FIRST} to {@code COMPONENT_LAST}.
59 *
60 * @see ComponentAdapter
61 * @see ComponentListener
62 * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/componentlistener.html">Tutorial: Writing a Component Listener</a>
63 *
64 * @author Carl Quinn
65 * @since 1.1
66 */
67 public class ComponentEvent extends AWTEvent {
68
69 /**
70 * The first number in the range of ids used for component events.
71 */
72 public static final int COMPONENT_FIRST = 100;
73
74 /**
75 * The last number in the range of ids used for component events.
76 */
77 public static final int COMPONENT_LAST = 103;
78
79 /**
80 * This event indicates that the component's position changed.
81 */
82 public static final int COMPONENT_MOVED = COMPONENT_FIRST;
83
84 /**
85 * This event indicates that the component's size changed.
86 */
87 public static final int COMPONENT_RESIZED = 1 + COMPONENT_FIRST;
88
89 /**
90 * This event indicates that the component was made visible.
91 */
92 public static final int COMPONENT_SHOWN = 2 + COMPONENT_FIRST;
93
94 /**
95 * This event indicates that the component was rendered invisible.
96 */
97 public static final int COMPONENT_HIDDEN = 3 + COMPONENT_FIRST;
98
99 /*
100 * JDK 1.1 serialVersionUID
101 */
102 private static final long serialVersionUID = 8101406823902992965L;
103
104 /**
105 * Constructs a <code>ComponentEvent</code> object.
106 * <p> This method throws an
107 * <code>IllegalArgumentException</code> if <code>source</code>
108 * is <code>null</code>.
109 *
110 * @param source The <code>Component</code> that originated the event
111 * @param id An integer indicating the type of event.
112 * For information on allowable values, see
113 * the class description for {@link ComponentEvent}
114 * @throws IllegalArgumentException if <code>source</code> is null
115 * @see #getComponent()
116 * @see #getID()
117 */
118 public ComponentEvent(Component source, int id) {
119 super(source, id);
120 }
121
122 /**
123 * Returns the originator of the event.
124 *
125 * @return the <code>Component</code> object that originated
126 * the event, or <code>null</code> if the object is not a
127 * <code>Component</code>.
128 */
129 public Component getComponent() {
130 return (source instanceof Component) ? (Component)source : null;
131 }
132
133 /**
134 * Returns a parameter string identifying this event.
135 * This method is useful for event-logging and for debugging.
136 *
137 * @return a string identifying the event and its attributes
138 */
139 public String paramString() {
140 String typeStr;
141 Rectangle b = (source !=null
142 ? ((Component)source).getBounds()
143 : null);
144
145 switch(id) {
146 case COMPONENT_SHOWN:
147 typeStr = "COMPONENT_SHOWN";
148 break;
149 case COMPONENT_HIDDEN:
150 typeStr = "COMPONENT_HIDDEN";
151 break;
152 case COMPONENT_MOVED:
153 typeStr = "COMPONENT_MOVED ("+
154 b.x+","+b.y+" "+b.width+"x"+b.height+")";
155 break;
156 case COMPONENT_RESIZED:
157 typeStr = "COMPONENT_RESIZED ("+
158 b.x+","+b.y+" "+b.width+"x"+b.height+")";
159 break;
160 default:
161 typeStr = "unknown type";
162 }
163 return typeStr;
164 }
165 }