001 /*
002 * Copyright 1999-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.event;
027
028 import java.awt.AWTEvent;
029 import java.awt.Component;
030 import java.awt.Container;
031
032 /**
033 * An event which indicates a change to the <code>Component</code>
034 * hierarchy to which a <code>Component</code> belongs.
035 * <ul>
036 * <li>Hierarchy Change Events (HierarchyListener)
037 * <ul>
038 * <li> addition of an ancestor
039 * <li> removal of an ancestor
040 * <li> hierarchy made displayable
041 * <li> hierarchy made undisplayable
042 * <li> hierarchy shown on the screen (both visible and displayable)
043 * <li> hierarchy hidden on the screen (either invisible or undisplayable)
044 * </ul>
045 * <li>Ancestor Reshape Events (HierarchyBoundsListener)
046 * <ul>
047 * <li> an ancestor was resized
048 * <li> an ancestor was moved
049 * </ul>
050 * </ul>
051 * <p>
052 * Hierarchy events are provided for notification purposes ONLY.
053 * The AWT will automatically handle changes to the hierarchy internally so
054 * that GUI layout and displayability works properly regardless of whether a
055 * program is receiving these events or not.
056 * <p>
057 * This event is generated by a Container object (such as a Panel) when the
058 * Container is added, removed, moved, or resized, and passed down the
059 * hierarchy. It is also generated by a Component object when that object's
060 * <code>addNotify</code>, <code>removeNotify</code>, <code>show</code>, or
061 * <code>hide</code> method is called. ANCESTOR_MOVED and ANCESTOR_RESIZED
062 * events are dispatched to every <code>HierarchyBoundsListener</code> or
063 * <code>HierarchyBoundsAdapter</code> object which registered to receive
064 * such events using the Component's <code>addHierarchyBoundsListener</code>
065 * method. (<code>HierarchyBoundsAdapter</code> objects implement the <code>
066 * HierarchyBoundsListener</code> interface.) HIERARCHY_CHANGED events are
067 * dispatched to every <code>HierarchyListener</code> object which registered
068 * to receive such events using the Component's <code>addHierarchyListener
069 * </code> method. Each such listener object gets this <code>HierarchyEvent
070 * </code> when the event occurs.
071 *
072 * @author David Mendenhall
073 * @version 1.20, 05/05/07
074 * @see HierarchyListener
075 * @see HierarchyBoundsAdapter
076 * @see HierarchyBoundsListener
077 * @since 1.3
078 */
079 public class HierarchyEvent extends AWTEvent {
080 /*
081 * serialVersionUID
082 */
083 private static final long serialVersionUID = -5337576970038043990L;
084
085 /**
086 * Marks the first integer id for the range of hierarchy event ids.
087 */
088 public static final int HIERARCHY_FIRST = 1400; // 1300 used by sun.awt.windows.ModalityEvent
089
090 /**
091 * The event id indicating that modification was made to the
092 * entire hierarchy tree.
093 */
094 public static final int HIERARCHY_CHANGED = HIERARCHY_FIRST;
095
096 /**
097 * The event id indicating an ancestor-Container was moved.
098 */
099 public static final int ANCESTOR_MOVED = 1 + HIERARCHY_FIRST;
100
101 /**
102 * The event id indicating an ancestor-Container was resized.
103 */
104 public static final int ANCESTOR_RESIZED = 2 + HIERARCHY_FIRST;
105
106 /**
107 * Marks the last integer id for the range of ancestor event ids.
108 */
109 public static final int HIERARCHY_LAST = ANCESTOR_RESIZED;
110
111 /**
112 * Indicates that the <code>HIERARCHY_CHANGED</code> event
113 * was generated by a reparenting operation.
114 */
115 public static final int PARENT_CHANGED = 0x1;
116
117 /**
118 * Indicates that the <code>HIERARCHY_CHANGED</code> event
119 * was generated due to a change in the displayability
120 * of the hierarchy. To discern the
121 * current displayability of the hierarchy, call
122 * <code>Component.isDisplayable</code>. Displayability changes occur
123 * in response to explicit or implicit calls to
124 * <code>Component.addNotify</code> and
125 * <code>Component.removeNotify</code>.
126 *
127 * @see java.awt.Component#isDisplayable()
128 * @see java.awt.Component#addNotify()
129 * @see java.awt.Component#removeNotify()
130 */
131 public static final int DISPLAYABILITY_CHANGED = 0x2;
132
133 /**
134 * Indicates that the <code>HIERARCHY_CHANGED</code> event
135 * was generated due to a change in the showing state
136 * of the hierarchy. To discern the
137 * current showing state of the hierarchy, call
138 * <code>Component.isShowing</code>. Showing state changes occur
139 * when either the displayability or visibility of the
140 * hierarchy occurs. Visibility changes occur in response to explicit
141 * or implicit calls to <code>Component.show</code> and
142 * <code>Component.hide</code>.
143 *
144 * @see java.awt.Component#isShowing()
145 * @see java.awt.Component#addNotify()
146 * @see java.awt.Component#removeNotify()
147 * @see java.awt.Component#show()
148 * @see java.awt.Component#hide()
149 */
150 public static final int SHOWING_CHANGED = 0x4;
151
152 Component changed;
153 Container changedParent;
154 long changeFlags;
155
156 /**
157 * Constructs an <code>HierarchyEvent</code> object to identify a
158 * change in the <code>Component</code> hierarchy.
159 * <p>Note that passing in an invalid <code>id</code> results in
160 * unspecified behavior. This method throws an
161 * <code>IllegalArgumentException</code> if <code>source</code>
162 * is <code>null</code>.
163 *
164 * @param source the <code>Component</code> object that
165 * originated the event
166 * @param id an integer indicating the type of event
167 * @param changed the <code>Component</code> at the top of
168 * the hierarchy which was changed
169 * @param changedParent the parent of <code>changed</code>; this
170 * may be the parent before or after the
171 * change, depending on the type of change
172 * @throws IllegalArgumentException if <code>source</code> is null
173 */
174 public HierarchyEvent(Component source, int id, Component changed,
175 Container changedParent) {
176 super (source, id);
177 this .changed = changed;
178 this .changedParent = changedParent;
179 }
180
181 /**
182 * Constructs an <code>HierarchyEvent</code> object to identify
183 * a change in the <code>Component</code> hierarchy.
184 * <p>Note that passing in an invalid <code>id</code> results in
185 * unspecified behavior. This method throws an
186 * <code>IllegalArgumentException</code> if <code>source</code>
187 * is <code>null</code>.
188 *
189 * @param source the <code>Component</code> object that
190 * originated the event
191 * @param id an integer indicating the type of event
192 * @param changed the <code>Component</code> at the top
193 * of the hierarchy which was changed
194 * @param changedParent the parent of <code>changed</code>; this
195 * may be the parent before or after the
196 * change, depending on the type of change
197 * @param changeFlags a bitmask which indicates the type(s) of
198 * <code>HIERARCHY_CHANGED</code> events
199 * represented in this event object
200 * @throws IllegalArgumentException if <code>source</code> is null
201 */
202 public HierarchyEvent(Component source, int id, Component changed,
203 Container changedParent, long changeFlags) {
204 super (source, id);
205 this .changed = changed;
206 this .changedParent = changedParent;
207 this .changeFlags = changeFlags;
208 }
209
210 /**
211 * Returns the originator of the event.
212 *
213 * @return the <code>Component</code> object that originated
214 * the event, or <code>null</code> if the object is not a
215 * <code>Component</code>.
216 */
217 public Component getComponent() {
218 return (source instanceof Component) ? (Component) source
219 : null;
220 }
221
222 /**
223 * Returns the Component at the top of the hierarchy which was
224 * changed.
225 *
226 * @return the changed Component
227 */
228 public Component getChanged() {
229 return changed;
230 }
231
232 /**
233 * Returns the parent of the Component returned by <code>
234 * getChanged()</code>. For a HIERARCHY_CHANGED event where the
235 * change was of type PARENT_CHANGED via a call to <code>
236 * Container.add</code>, the parent returned is the parent
237 * after the add operation. For a HIERARCHY_CHANGED event where
238 * the change was of type PARENT_CHANGED via a call to <code>
239 * Container.remove</code>, the parent returned is the parent
240 * before the remove operation. For all other events and types,
241 * the parent returned is the parent during the operation.
242 *
243 * @return the parent of the changed Component
244 */
245 public Container getChangedParent() {
246 return changedParent;
247 }
248
249 /**
250 * Returns a bitmask which indicates the type(s) of
251 * HIERARCHY_CHANGED events represented in this event object.
252 * The bits have been bitwise-ored together.
253 *
254 * @return the bitmask, or 0 if this is not an HIERARCHY_CHANGED
255 * event
256 */
257 public long getChangeFlags() {
258 return changeFlags;
259 }
260
261 /**
262 * Returns a parameter string identifying this event.
263 * This method is useful for event-logging and for debugging.
264 *
265 * @return a string identifying the event and its attributes
266 */
267 public String paramString() {
268 String typeStr;
269 switch (id) {
270 case ANCESTOR_MOVED:
271 typeStr = "ANCESTOR_MOVED (" + changed + ","
272 + changedParent + ")";
273 break;
274 case ANCESTOR_RESIZED:
275 typeStr = "ANCESTOR_RESIZED (" + changed + ","
276 + changedParent + ")";
277 break;
278 case HIERARCHY_CHANGED: {
279 typeStr = "HIERARCHY_CHANGED (";
280 boolean first = true;
281 if ((changeFlags & PARENT_CHANGED) != 0) {
282 first = false;
283 typeStr += "PARENT_CHANGED";
284 }
285 if ((changeFlags & DISPLAYABILITY_CHANGED) != 0) {
286 if (first) {
287 first = false;
288 } else {
289 typeStr += ",";
290 }
291 typeStr += "DISPLAYABILITY_CHANGED";
292 }
293 if ((changeFlags & SHOWING_CHANGED) != 0) {
294 if (first) {
295 first = false;
296 } else {
297 typeStr += ",";
298 }
299 typeStr += "SHOWING_CHANGED";
300 }
301 if (!first) {
302 typeStr += ",";
303 }
304 typeStr += changed + "," + changedParent + ")";
305 break;
306 }
307 default:
308 typeStr = "unknown type";
309 }
310 return typeStr;
311 }
312 }
|