1. /*
  2. * @(#)WindowEvent.java 1.30 03/01/23
  3. *
  4. * Copyright 2003 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.30, 01/23/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. *
  139. * @param source the <code>Window</code> object
  140. * that originated the event
  141. * @param id an integer indicating the type of event.
  142. * @param opposite the other window involved in the focus or activation
  143. * change, or <code>null</code>
  144. * @param oldState previous state of the window for window state
  145. * change event
  146. * @param newState new state of the window for window state change event
  147. * @since 1.4
  148. */
  149. public WindowEvent(Window source, int id, Window opposite,
  150. int oldState, int newState)
  151. {
  152. super(source, id);
  153. this.opposite = opposite;
  154. this.oldState = oldState;
  155. this.newState = newState;
  156. }
  157. /**
  158. * Constructs a <code>WindowEvent</code> object with the
  159. * specified opposite <code>Window</code>. The opposite
  160. * <code>Window</code> is the other <code>Window</code>
  161. * involved in this focus or activation change.
  162. * For a <code>WINDOW_ACTIVATED</code> or
  163. * <code>WINDOW_GAINED_FOCUS</code> event, this is the
  164. * <code>Window</code> that lost activation or focus.
  165. * For a <code>WINDOW_DEACTIVATED</code> or
  166. * <code>WINDOW_LOST_FOCUS</code> event, this is the
  167. * <code>Window</code> that gained activation or focus.
  168. * If this focus change occurs with a native application, with a
  169. * Java application in a different VM, or with no other
  170. * <code>Window</code>, then the opposite Window is <code>null</code>.
  171. * <p>Note that passing in an invalid <code>id</code> results in
  172. * unspecified behavior.
  173. *
  174. * @param source the <code>Window</code> object that
  175. * originated the event
  176. * @param id <code>WINDOW_ACTIVATED</code>,
  177. * <code>WINDOW_DEACTIVATED</code>,
  178. * <code>WINDOW_GAINED_FOCUS</code>,
  179. * or <code>WINDOW_LOST_FOCUS</code>. It is
  180. * expected that this constructor will not be used for
  181. * other <code>WindowEvent</code> types because the
  182. * opposite <code>Window</code> of such events
  183. * will always be <code>null</code>
  184. * @param opposite the other <code>Window</code> involved in the
  185. * focus or activation change, or <code>null</code>
  186. */
  187. public WindowEvent(Window source, int id, Window opposite) {
  188. this(source, id, opposite, 0, 0);
  189. }
  190. /**
  191. * Constructs a <code>WindowEvent</code> object with the specified
  192. * previous and new window states.
  193. *
  194. * @param source the <code>Window</code> object
  195. * that originated the event
  196. * @param id <code>WINDOW_STATE_CHANGED</code> event type.
  197. * It is expected that this constructor will not
  198. * be used for other <code>WindowEvent</code>
  199. * types, because the previous and new window
  200. * states are meaningless for other event types.
  201. * @param oldState an integer representing the previous window state
  202. * @param newState an integer representing the new window state
  203. * @since 1.4
  204. */
  205. public WindowEvent(Window source, int id, int oldState, int newState) {
  206. this(source, id, null, oldState, newState);
  207. }
  208. /**
  209. * Constructs a <code>WindowEvent</code> object.
  210. * <p>Note that passing in an invalid <code>id</code> results in
  211. * unspecified behavior.
  212. *
  213. * @param source the <code>Window</code> object that originated the event
  214. * @param id an integer indicating the type of event
  215. */
  216. public WindowEvent(Window source, int id) {
  217. this(source, id, null, 0, 0);
  218. }
  219. /**
  220. * Returns the originator of the event.
  221. *
  222. * @return the Window object that originated the event
  223. */
  224. public Window getWindow() {
  225. return (source instanceof Window) ? (Window)source : null;
  226. }
  227. /**
  228. * Returns the other Window involved in this focus or activation change.
  229. * For a WINDOW_ACTIVATED or WINDOW_GAINED_FOCUS event, this is the Window
  230. * that lost activation or focus. For a WINDOW_DEACTIVATED or
  231. * WINDOW_LOST_FOCUS event, this is the Window that gained activation or
  232. * focus. For any other type of WindowEvent, or if the focus or activation
  233. * change occurs with a native application, with a Java application in a
  234. * different VM or context, or with no other Window, null is returned.
  235. *
  236. * @return the other Window involved in the focus or activation change, or
  237. * null
  238. * @since 1.4
  239. */
  240. public Window getOppositeWindow() {
  241. if (opposite == null) {
  242. return null;
  243. }
  244. return (SunToolkit.targetToAppContext(opposite) ==
  245. AppContext.getAppContext())
  246. ? opposite
  247. : null;
  248. }
  249. /**
  250. * For <code>WINDOW_STATE_CHANGED</code> events returns the
  251. * previous state of the window. The state is
  252. * represented as a bitwise mask.
  253. * <ul>
  254. * <li><code>NORMAL</code>
  255. * <br>Indicates that no state bits are set.
  256. * <li><code>ICONIFIED</code>
  257. * <li><code>MAXIMIZED_HORIZ</code>
  258. * <li><code>MAXIMIZED_VERT</code>
  259. * <li><code>MAXIMIZED_BOTH</code>
  260. * <br>Concatenates <code>MAXIMIZED_HORIZ</code>
  261. * and <code>MAXIMIZED_VERT</code>.
  262. * </ul>
  263. *
  264. * @return a bitwise mask of the previous window state
  265. * @see java.awt.Frame#getExtendedState()
  266. * @since 1.4
  267. */
  268. public int getOldState() {
  269. return oldState;
  270. }
  271. /**
  272. * For <code>WINDOW_STATE_CHANGED</code> events returns the
  273. * new state of the window. The state is
  274. * represented as a bitwise mask.
  275. * <ul>
  276. * <li><code>NORMAL</code>
  277. * <br>Indicates that no state bits are set.
  278. * <li><code>ICONIFIED</code>
  279. * <li><code>MAXIMIZED_HORIZ</code>
  280. * <li><code>MAXIMIZED_VERT</code>
  281. * <li><code>MAXIMIZED_BOTH</code>
  282. * <br>Concatenates <code>MAXIMIZED_HORIZ</code>
  283. * and <code>MAXIMIZED_VERT</code>.
  284. * </ul>
  285. *
  286. * @return a bitwise mask of the new window state
  287. * @see java.awt.Frame#getExtendedState()
  288. * @since 1.4
  289. */
  290. public int getNewState() {
  291. return newState;
  292. }
  293. /**
  294. * Returns a parameter string identifying this event.
  295. * This method is useful for event-logging and for debugging.
  296. *
  297. * @return a string identifying the event and its attributes
  298. */
  299. public String paramString() {
  300. String typeStr;
  301. switch(id) {
  302. case WINDOW_OPENED:
  303. typeStr = "WINDOW_OPENED";
  304. break;
  305. case WINDOW_CLOSING:
  306. typeStr = "WINDOW_CLOSING";
  307. break;
  308. case WINDOW_CLOSED:
  309. typeStr = "WINDOW_CLOSED";
  310. break;
  311. case WINDOW_ICONIFIED:
  312. typeStr = "WINDOW_ICONIFIED";
  313. break;
  314. case WINDOW_DEICONIFIED:
  315. typeStr = "WINDOW_DEICONIFIED";
  316. break;
  317. case WINDOW_ACTIVATED:
  318. typeStr = "WINDOW_ACTIVATED";
  319. break;
  320. case WINDOW_DEACTIVATED:
  321. typeStr = "WINDOW_DEACTIVATED";
  322. break;
  323. case WINDOW_GAINED_FOCUS:
  324. typeStr = "WINDOW_GAINED_FOCUS";
  325. break;
  326. case WINDOW_LOST_FOCUS:
  327. typeStr = "WINDOW_LOST_FOCUS";
  328. break;
  329. case WINDOW_STATE_CHANGED:
  330. typeStr = "WINDOW_STATE_CHANGED";
  331. break;
  332. default:
  333. typeStr = "unknown type";
  334. }
  335. return typeStr + ",opposite=" + getOppositeWindow()
  336. + ",oldState=" + oldState + ",newState=" + newState;
  337. }
  338. }