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