blob: 090ba017f559c919f9514ec56c255c9c589d9a6f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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
26package java.awt.event;
27
28import java.awt.Window;
29import sun.awt.AppContext;
30import sun.awt.SunToolkit;
31
32/**
33 * A low-level event that indicates that a window has changed its status. This
34 * low-level event is generated by a Window object when it is opened, closed,
35 * activated, deactivated, iconified, or deiconified, or when focus is
36 * transfered into or out of the Window.
37 * <P>
38 * The event is passed to every <code>WindowListener</code>
39 * or <code>WindowAdapter</code> object which registered to receive such
40 * events using the window's <code>addWindowListener</code> method.
41 * (<code>WindowAdapter</code> objects implement the
42 * <code>WindowListener</code> interface.) Each such listener object
43 * gets this <code>WindowEvent</code> when the event occurs.
44 *
45 * @author Carl Quinn
46 * @author Amy Fowler
47 *
48 * @see WindowAdapter
49 * @see WindowListener
50 * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/windowlistener.html">Tutorial: Writing a Window Listener</a>
51 *
52 * @since JDK1.1
53 */
54public class WindowEvent extends ComponentEvent {
55
56 /**
57 * The first number in the range of ids used for window events.
58 */
59 public static final int WINDOW_FIRST = 200;
60
61 /**
62 * The window opened event. This event is delivered only
63 * the first time a window is made visible.
64 */
65 public static final int WINDOW_OPENED = WINDOW_FIRST; // 200
66
67 /**
68 * The "window is closing" event. This event is delivered when
69 * the user attempts to close the window from the window's system menu.
70 * If the program does not explicitly hide or dispose the window
71 * while processing this event, the window close operation will be
72 * cancelled.
73 */
74 public static final int WINDOW_CLOSING = 1 + WINDOW_FIRST; //Event.WINDOW_DESTROY
75
76 /**
77 * The window closed event. This event is delivered after
78 * the window has been closed as the result of a call to dispose.
79 */
80 public static final int WINDOW_CLOSED = 2 + WINDOW_FIRST;
81
82 /**
83 * The window iconified event. This event is delivered when
84 * the window has been changed from a normal to a minimized state.
85 * For many platforms, a minimized window is displayed as
86 * the icon specified in the window's iconImage property.
87 * @see java.awt.Frame#setIconImage
88 */
89 public static final int WINDOW_ICONIFIED = 3 + WINDOW_FIRST; //Event.WINDOW_ICONIFY
90
91 /**
92 * The window deiconified event type. This event is delivered when
93 * the window has been changed from a minimized to a normal state.
94 */
95 public static final int WINDOW_DEICONIFIED = 4 + WINDOW_FIRST; //Event.WINDOW_DEICONIFY
96
97 /**
98 * The window-activated event type. This event is delivered when the Window
99 * becomes the active Window. Only a Frame or a Dialog can be the active
100 * Window. The native windowing system may denote the active Window or its
101 * children with special decorations, such as a highlighted title bar. The
102 * active Window is always either the focused Window, or the first Frame or
103 * Dialog that is an owner of the focused Window.
104 */
105 public static final int WINDOW_ACTIVATED = 5 + WINDOW_FIRST;
106
107 /**
108 * The window-deactivated event type. This event is delivered when the
109 * Window is no longer the active Window. Only a Frame or a Dialog can be
110 * the active Window. The native windowing system may denote the active
111 * Window or its children with special decorations, such as a highlighted
112 * title bar. The active Window is always either the focused Window, or the
113 * first Frame or Dialog that is an owner of the focused Window.
114 */
115 public static final int WINDOW_DEACTIVATED = 6 + WINDOW_FIRST;
116
117 /**
118 * The window-gained-focus event type. This event is delivered when the
119 * Window becomes the focused Window, which means that the Window, or one
120 * of its subcomponents, will receive keyboard events.
121 */
122 public static final int WINDOW_GAINED_FOCUS = 7 + WINDOW_FIRST;
123
124 /**
125 * The window-lost-focus event type. This event is delivered when a Window
126 * is no longer the focused Window, which means keyboard events will no
127 * longer be delivered to the Window or any of its subcomponents.
128 */
129 public static final int WINDOW_LOST_FOCUS = 8 + WINDOW_FIRST;
130
131 /**
132 * The window-state-changed event type. This event is delivered
133 * when a Window's state is changed by virtue of it being
134 * iconified, maximized etc.
135 * @since 1.4
136 */
137 public static final int WINDOW_STATE_CHANGED = 9 + WINDOW_FIRST;
138
139 /**
140 * The last number in the range of ids used for window events.
141 */
142 public static final int WINDOW_LAST = WINDOW_STATE_CHANGED;
143
144 /**
145 * The other Window involved in this focus or activation change. For a
146 * WINDOW_ACTIVATED or WINDOW_GAINED_FOCUS event, this is the Window that
147 * lost activation or focus. For a WINDOW_DEACTIVATED or WINDOW_LOST_FOCUS
148 * event, this is the Window that gained activation or focus. For any other
149 * type of WindowEvent, or if the focus or activation change occurs with a
150 * native application, a Java application in a different VM, or with no
151 * other Window, null is returned.
152 *
153 * @see #getOppositeWindow
154 * @since 1.4
155 */
156 transient Window opposite;
157
158 /**
159 * TBS
160 */
161 int oldState;
162 int newState;
163
164
165 /*
166 * JDK 1.1 serialVersionUID
167 */
168 private static final long serialVersionUID = -1567959133147912127L;
169
170
171 /**
172 * Constructs a <code>WindowEvent</code> object.
173 * <p>Note that passing in an invalid <code>id</code> results in
174 * unspecified behavior. This method throws an
175 * <code>IllegalArgumentException</code> if <code>source</code>
176 * is <code>null</code>.
177 *
178 * @param source the <code>Window</code> object
179 * that originated the event
180 * @param id an integer indicating the type of event.
181 * @param opposite the other window involved in the focus or activation
182 * change, or <code>null</code>
183 * @param oldState previous state of the window for window state
184 * change event
185 * @param newState new state of the window for window state change event
186 * @throws IllegalArgumentException if <code>source</code> is null
187 * @since 1.4
188 */
189 public WindowEvent(Window source, int id, Window opposite,
190 int oldState, int newState)
191 {
192 super(source, id);
193 this.opposite = opposite;
194 this.oldState = oldState;
195 this.newState = newState;
196 }
197
198 /**
199 * Constructs a <code>WindowEvent</code> object with the
200 * specified opposite <code>Window</code>. The opposite
201 * <code>Window</code> is the other <code>Window</code>
202 * involved in this focus or activation change.
203 * For a <code>WINDOW_ACTIVATED</code> or
204 * <code>WINDOW_GAINED_FOCUS</code> event, this is the
205 * <code>Window</code> that lost activation or focus.
206 * For a <code>WINDOW_DEACTIVATED</code> or
207 * <code>WINDOW_LOST_FOCUS</code> event, this is the
208 * <code>Window</code> that gained activation or focus.
209 * If this focus change occurs with a native application, with a
210 * Java application in a different VM, or with no other
211 * <code>Window</code>, then the opposite Window is <code>null</code>.
212 * <p>Note that passing in an invalid <code>id</code> results in
213 * unspecified behavior. This method throws an
214 * <code>IllegalArgumentException</code> if <code>source</code>
215 * is <code>null</code>.
216 *
217 * @param source the <code>Window</code> object that
218 * originated the event
219 * @param id <code>WINDOW_ACTIVATED</code>,
220 * <code>WINDOW_DEACTIVATED</code>,
221 * <code>WINDOW_GAINED_FOCUS</code>,
222 * or <code>WINDOW_LOST_FOCUS</code>. It is
223 * expected that this constructor will not be used for
224 * other <code>WindowEvent</code> types because the
225 * opposite <code>Window</code> of such events
226 * will always be <code>null</code>
227 * @param opposite the other <code>Window</code> involved in the
228 * focus or activation change, or <code>null</code>
229 * @throws IllegalArgumentException if <code>source</code> is null
230 * @since 1.4
231 */
232 public WindowEvent(Window source, int id, Window opposite) {
233 this(source, id, opposite, 0, 0);
234 }
235
236 /**
237 * Constructs a <code>WindowEvent</code> object with the specified
238 * previous and new window states.
239 * <p>Note that passing in an invalid <code>id</code> results in
240 * unspecified behavior. This method throws an
241 * <code>IllegalArgumentException</code> if <code>source</code>
242 * is <code>null</code>.
243 *
244 * @param source the <code>Window</code> object
245 * that originated the event
246 * @param id <code>WINDOW_STATE_CHANGED</code> event type.
247 * It is expected that this constructor will not
248 * be used for other <code>WindowEvent</code>
249 * types, because the previous and new window
250 * states are meaningless for other event types.
251 * @param oldState an integer representing the previous window state
252 * @param newState an integer representing the new window state
253 * @throws IllegalArgumentException if <code>source</code> is null
254 * @since 1.4
255 */
256 public WindowEvent(Window source, int id, int oldState, int newState) {
257 this(source, id, null, oldState, newState);
258 }
259
260 /**
261 * Constructs a <code>WindowEvent</code> object.
262 * <p>Note that passing in an invalid <code>id</code> results in
263 * unspecified behavior. This method throws an
264 * <code>IllegalArgumentException</code> if <code>source</code>
265 * is <code>null</code>.
266 *
267 * @param source the <code>Window</code> object that originated the event
268 * @param id an integer indicating the type of event
269 * @throws IllegalArgumentException if <code>source</code> is null
270 */
271 public WindowEvent(Window source, int id) {
272 this(source, id, null, 0, 0);
273 }
274
275 /**
276 * Returns the originator of the event.
277 *
278 * @return the Window object that originated the event
279 */
280 public Window getWindow() {
281 return (source instanceof Window) ? (Window)source : null;
282 }
283
284 /**
285 * Returns the other Window involved in this focus or activation change.
286 * For a WINDOW_ACTIVATED or WINDOW_GAINED_FOCUS event, this is the Window
287 * that lost activation or focus. For a WINDOW_DEACTIVATED or
288 * WINDOW_LOST_FOCUS event, this is the Window that gained activation or
289 * focus. For any other type of WindowEvent, or if the focus or activation
290 * change occurs with a native application, with a Java application in a
291 * different VM or context, or with no other Window, null is returned.
292 *
293 * @return the other Window involved in the focus or activation change, or
294 * null
295 * @since 1.4
296 */
297 public Window getOppositeWindow() {
298 if (opposite == null) {
299 return null;
300 }
301
302 return (SunToolkit.targetToAppContext(opposite) ==
303 AppContext.getAppContext())
304 ? opposite
305 : null;
306 }
307
308 /**
309 * For <code>WINDOW_STATE_CHANGED</code> events returns the
310 * previous state of the window. The state is
311 * represented as a bitwise mask.
312 * <ul>
313 * <li><code>NORMAL</code>
314 * <br>Indicates that no state bits are set.
315 * <li><code>ICONIFIED</code>
316 * <li><code>MAXIMIZED_HORIZ</code>
317 * <li><code>MAXIMIZED_VERT</code>
318 * <li><code>MAXIMIZED_BOTH</code>
319 * <br>Concatenates <code>MAXIMIZED_HORIZ</code>
320 * and <code>MAXIMIZED_VERT</code>.
321 * </ul>
322 *
323 * @return a bitwise mask of the previous window state
324 * @see java.awt.Frame#getExtendedState()
325 * @since 1.4
326 */
327 public int getOldState() {
328 return oldState;
329 }
330
331 /**
332 * For <code>WINDOW_STATE_CHANGED</code> events returns the
333 * new state of the window. The state is
334 * represented as a bitwise mask.
335 * <ul>
336 * <li><code>NORMAL</code>
337 * <br>Indicates that no state bits are set.
338 * <li><code>ICONIFIED</code>
339 * <li><code>MAXIMIZED_HORIZ</code>
340 * <li><code>MAXIMIZED_VERT</code>
341 * <li><code>MAXIMIZED_BOTH</code>
342 * <br>Concatenates <code>MAXIMIZED_HORIZ</code>
343 * and <code>MAXIMIZED_VERT</code>.
344 * </ul>
345 *
346 * @return a bitwise mask of the new window state
347 * @see java.awt.Frame#getExtendedState()
348 * @since 1.4
349 */
350 public int getNewState() {
351 return newState;
352 }
353
354 /**
355 * Returns a parameter string identifying this event.
356 * This method is useful for event-logging and for debugging.
357 *
358 * @return a string identifying the event and its attributes
359 */
360 public String paramString() {
361 String typeStr;
362 switch(id) {
363 case WINDOW_OPENED:
364 typeStr = "WINDOW_OPENED";
365 break;
366 case WINDOW_CLOSING:
367 typeStr = "WINDOW_CLOSING";
368 break;
369 case WINDOW_CLOSED:
370 typeStr = "WINDOW_CLOSED";
371 break;
372 case WINDOW_ICONIFIED:
373 typeStr = "WINDOW_ICONIFIED";
374 break;
375 case WINDOW_DEICONIFIED:
376 typeStr = "WINDOW_DEICONIFIED";
377 break;
378 case WINDOW_ACTIVATED:
379 typeStr = "WINDOW_ACTIVATED";
380 break;
381 case WINDOW_DEACTIVATED:
382 typeStr = "WINDOW_DEACTIVATED";
383 break;
384 case WINDOW_GAINED_FOCUS:
385 typeStr = "WINDOW_GAINED_FOCUS";
386 break;
387 case WINDOW_LOST_FOCUS:
388 typeStr = "WINDOW_LOST_FOCUS";
389 break;
390 case WINDOW_STATE_CHANGED:
391 typeStr = "WINDOW_STATE_CHANGED";
392 break;
393 default:
394 typeStr = "unknown type";
395 }
396 typeStr += ",opposite=" + getOppositeWindow()
397 + ",oldState=" + oldState + ",newState=" + newState;
398
399 return typeStr;
400 }
401}