AWTEvent.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* AWTEvent.java -- the root event in AWT
  2. Copyright (C) 1999, 2000, 2002, 2005 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt;
  32. import java.awt.event.ActionEvent;
  33. import java.awt.event.AdjustmentEvent;
  34. import java.awt.event.ComponentEvent;
  35. import java.awt.event.ContainerEvent;
  36. import java.awt.event.FocusEvent;
  37. import java.awt.event.InputMethodEvent;
  38. import java.awt.event.InvocationEvent;
  39. import java.awt.event.ItemEvent;
  40. import java.awt.event.KeyEvent;
  41. import java.awt.event.MouseEvent;
  42. import java.awt.event.PaintEvent;
  43. import java.awt.event.TextEvent;
  44. import java.awt.event.WindowEvent;
  45. import java.util.EventObject;
  46. /**
  47. * AWTEvent is the root event class for all AWT events in the JDK 1.1 event
  48. * model. It supersedes the Event class from JDK 1.0. Subclasses outside of
  49. * the java.awt package should have IDs greater than RESERVED_ID_MAX.
  50. *
  51. * <p>Event masks defined here are used by components in
  52. * <code>enableEvents</code> to select event types not selected by registered
  53. * listeners. Event masks are appropriately set when registering on
  54. * components.
  55. *
  56. * @author Warren Levy (warrenl@cygnus.com)
  57. * @author Aaron M. Renn (arenn@urbanophile.com)
  58. * @since 1.1
  59. * @status updated to 1.4
  60. */
  61. public abstract class AWTEvent extends EventObject
  62. {
  63. /**
  64. * Compatible with JDK 1.1+.
  65. */
  66. private static final long serialVersionUID = -1825314779160409405L;
  67. /**
  68. * The ID of the event.
  69. *
  70. * @see #getID()
  71. * @see #AWTEvent(Object, int)
  72. * @serial the identifier number of this event
  73. */
  74. protected int id;
  75. /**
  76. * Indicates if the event has been consumed. False mean it is passed to
  77. * the peer, true means it has already been processed. Semantic events
  78. * generated by low-level events always have the value true.
  79. *
  80. * @see #consume()
  81. * @see #isConsumed()
  82. * @serial whether the event has been consumed
  83. */
  84. protected boolean consumed;
  85. /**
  86. * Used for implementing a simple linked list in EventQueue.
  87. */
  88. transient AWTEvent queueNext;
  89. /**
  90. * Who knows? It's in the serial version.
  91. *
  92. * @serial No idea what this is for.
  93. */
  94. byte[] bdata;
  95. /**
  96. * Indicates if this event is dispatched by the KeyboardFocusManager.
  97. */
  98. boolean isFocusManagerEvent = false;
  99. /** Mask for selecting component events. */
  100. public static final long COMPONENT_EVENT_MASK = 0x00001;
  101. /** Mask for selecting container events. */
  102. public static final long CONTAINER_EVENT_MASK = 0x00002;
  103. /** Mask for selecting component focus events. */
  104. public static final long FOCUS_EVENT_MASK = 0x00004;
  105. /** Mask for selecting keyboard events. */
  106. public static final long KEY_EVENT_MASK = 0x00008;
  107. /** Mask for mouse button events. */
  108. public static final long MOUSE_EVENT_MASK = 0x00010;
  109. /** Mask for mouse motion events. */
  110. public static final long MOUSE_MOTION_EVENT_MASK = 0x00020;
  111. /** Mask for window events. */
  112. public static final long WINDOW_EVENT_MASK = 0x00040;
  113. /** Mask for action events. */
  114. public static final long ACTION_EVENT_MASK = 0x00080;
  115. /** Mask for adjustment events. */
  116. public static final long ADJUSTMENT_EVENT_MASK = 0x00100;
  117. /** Mask for item events. */
  118. public static final long ITEM_EVENT_MASK = 0x00200;
  119. /** Mask for text events. */
  120. public static final long TEXT_EVENT_MASK = 0x00400;
  121. /**
  122. * Mask for input method events.
  123. * @since 1.3
  124. */
  125. public static final long INPUT_METHOD_EVENT_MASK = 0x00800;
  126. /**
  127. * Mask if input methods are enabled. Package visible only.
  128. */
  129. static final long INPUT_ENABLED_EVENT_MASK = 0x01000;
  130. /**
  131. * Mask for paint events.
  132. * @since 1.3
  133. */
  134. public static final long PAINT_EVENT_MASK = 0x02000;
  135. /**
  136. * Mask for invocation events.
  137. * @since 1.3
  138. */
  139. public static final long INVOCATION_EVENT_MASK = 0x04000;
  140. /**
  141. * Mask for hierarchy events.
  142. * @since 1.3
  143. */
  144. public static final long HIERARCHY_EVENT_MASK = 0x08000;
  145. /**
  146. * Mask for hierarchy bounds events.
  147. * @since 1.3
  148. */
  149. public static final long HIERARCHY_BOUNDS_EVENT_MASK = 0x10000;
  150. /**
  151. * Mask for mouse wheel events.
  152. * @since 1.4
  153. */
  154. public static final long MOUSE_WHEEL_EVENT_MASK = 0x20000;
  155. /**
  156. * Mask for window state events.
  157. * @since 1.4
  158. */
  159. public static final long WINDOW_STATE_EVENT_MASK = 0x40000;
  160. /**
  161. * Mask for window focus events.
  162. * @since 1.4
  163. */
  164. public static final long WINDOW_FOCUS_EVENT_MASK = 0x80000;
  165. /**
  166. * This is the highest number for event ids that are reserved for use by
  167. * the AWT system itself. Subclasses outside of java.awt should use higher
  168. * ids.
  169. */
  170. public static final int RESERVED_ID_MAX = 1999;
  171. /**
  172. * Initializes a new AWTEvent from the old Java 1.0 event object.
  173. *
  174. * @param event the old-style event
  175. * @throws NullPointerException if event is null
  176. */
  177. public AWTEvent(Event event)
  178. {
  179. this(event.target, event.id);
  180. consumed = event.consumed;
  181. }
  182. /**
  183. * Create an event on the specified source object and id.
  184. *
  185. * @param source the object that caused the event
  186. * @param id the event id
  187. * @throws IllegalArgumentException if source is null
  188. */
  189. public AWTEvent(Object source, int id)
  190. {
  191. super(source);
  192. this.id = id;
  193. }
  194. /**
  195. * Retarget the event, such as converting a heavyweight component to a
  196. * lightweight child of the original. This is not for general use, but
  197. * is for event targeting systems like KeyboardFocusManager.
  198. *
  199. * @param source the new source
  200. */
  201. public void setSource(Object source)
  202. {
  203. this.source = source;
  204. }
  205. /**
  206. * Returns the event type id.
  207. *
  208. * @return the id number of this event
  209. */
  210. public int getID()
  211. {
  212. return id;
  213. }
  214. /**
  215. * Create a string that represents this event in the format
  216. * <code>classname[eventstring] on sourcecomponentname</code>.
  217. *
  218. * @return a string representing this event
  219. */
  220. public String toString ()
  221. {
  222. String src;
  223. if (source instanceof Component)
  224. src = ((Component) source).getName();
  225. else if (source instanceof MenuComponent)
  226. src = ((MenuComponent) source).getName();
  227. else if (source != null)
  228. src = source.toString();
  229. else
  230. src = "null";
  231. String string = getClass ().getName () + "[" + paramString () + "] on "
  232. + src;
  233. return string;
  234. }
  235. /**
  236. * Returns a string representation of the state of this event. It may be
  237. * empty, but must not be null; it is implementation defined.
  238. *
  239. * @return a string representation of this event
  240. */
  241. public String paramString()
  242. {
  243. return "";
  244. }
  245. /**
  246. * Consumes this event so that it will not be processed in the default
  247. * manner.
  248. */
  249. protected void consume()
  250. {
  251. consumed = true;
  252. }
  253. /**
  254. * Tests whether not not this event has been consumed. A consumed event
  255. * is not processed in the default manner.
  256. *
  257. * @return true if this event has been consumed
  258. */
  259. protected boolean isConsumed()
  260. {
  261. return consumed;
  262. }
  263. /**
  264. * Converts an event id to the appropriate event mask.
  265. *
  266. * @param id the event id
  267. *
  268. * @return the event mask for the specified id
  269. */
  270. static long eventIdToMask(int id)
  271. {
  272. long mask = 0;
  273. switch (id)
  274. {
  275. case ActionEvent.ACTION_PERFORMED:
  276. mask = ACTION_EVENT_MASK;
  277. break;
  278. case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
  279. mask = ADJUSTMENT_EVENT_MASK;
  280. break;
  281. case ComponentEvent.COMPONENT_MOVED:
  282. case ComponentEvent.COMPONENT_RESIZED:
  283. case ComponentEvent.COMPONENT_SHOWN:
  284. case ComponentEvent.COMPONENT_HIDDEN:
  285. mask = COMPONENT_EVENT_MASK;
  286. break;
  287. case ContainerEvent.COMPONENT_ADDED:
  288. case ContainerEvent.COMPONENT_REMOVED:
  289. mask = CONTAINER_EVENT_MASK;
  290. break;
  291. case FocusEvent.FOCUS_GAINED:
  292. case FocusEvent.FOCUS_LOST:
  293. mask = FOCUS_EVENT_MASK;
  294. break;
  295. case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
  296. case InputMethodEvent.CARET_POSITION_CHANGED:
  297. mask = INPUT_METHOD_EVENT_MASK;
  298. break;
  299. case InvocationEvent.INVOCATION_DEFAULT:
  300. mask = INVOCATION_EVENT_MASK;
  301. break;
  302. case ItemEvent.ITEM_STATE_CHANGED:
  303. mask = ITEM_EVENT_MASK;
  304. break;
  305. case KeyEvent.KEY_TYPED:
  306. case KeyEvent.KEY_PRESSED:
  307. case KeyEvent.KEY_RELEASED:
  308. mask = KEY_EVENT_MASK;
  309. break;
  310. case MouseEvent.MOUSE_CLICKED:
  311. case MouseEvent.MOUSE_PRESSED:
  312. case MouseEvent.MOUSE_RELEASED:
  313. mask = MOUSE_EVENT_MASK;
  314. break;
  315. case MouseEvent.MOUSE_MOVED:
  316. case MouseEvent.MOUSE_ENTERED:
  317. case MouseEvent.MOUSE_EXITED:
  318. case MouseEvent.MOUSE_DRAGGED:
  319. mask = MOUSE_MOTION_EVENT_MASK;
  320. break;
  321. case MouseEvent.MOUSE_WHEEL:
  322. mask = MOUSE_WHEEL_EVENT_MASK;
  323. break;
  324. case PaintEvent.PAINT:
  325. case PaintEvent.UPDATE:
  326. mask = PAINT_EVENT_MASK;
  327. break;
  328. case TextEvent.TEXT_VALUE_CHANGED:
  329. mask = TEXT_EVENT_MASK;
  330. break;
  331. case WindowEvent.WINDOW_OPENED:
  332. case WindowEvent.WINDOW_CLOSING:
  333. case WindowEvent.WINDOW_CLOSED:
  334. case WindowEvent.WINDOW_ICONIFIED:
  335. case WindowEvent.WINDOW_DEICONIFIED:
  336. case WindowEvent.WINDOW_ACTIVATED:
  337. case WindowEvent.WINDOW_DEACTIVATED:
  338. mask = WINDOW_EVENT_MASK;
  339. break;
  340. case WindowEvent.WINDOW_GAINED_FOCUS:
  341. case WindowEvent.WINDOW_LOST_FOCUS:
  342. mask = WINDOW_FOCUS_EVENT_MASK;
  343. break;
  344. case WindowEvent.WINDOW_STATE_CHANGED:
  345. mask = WINDOW_STATE_EVENT_MASK;
  346. break;
  347. default:
  348. mask = 0;
  349. }
  350. return mask;
  351. }
  352. } // class AWTEvent