1. /*
  2. * @(#)AWTEvent.java 1.38 00/02/11
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.awt;
  11. import java.util.EventObject;
  12. import java.awt.event.*;
  13. /**
  14. * The root event class for all AWT events.
  15. * This class and its subclasses supercede the original
  16. * java.awt.Event class.
  17. * Subclasses of this root AWTEvent class defined outside of the
  18. * java.awt.event package should define event ID values greater than
  19. * the value defined by RESERVED_ID_MAX.
  20. * <p>
  21. * The event masks defined in this class are needed by Component subclasses
  22. * which are using Component.enableEvents() to select for event types not
  23. * selected by registered listeners. If a listener is registered on a
  24. * component, the appropriate event mask is already set internally by the
  25. * component.
  26. * <p>
  27. * The masks are also used to specify to which types of events an
  28. * AWTEventListener should listen. The masks are bitwise-ORed together
  29. * and passed to Toolkit.addAWTEventListener.
  30. *
  31. * @see Component#enableEvents
  32. * @see Toolkit#addAWTEventListener
  33. *
  34. * @see java.awt.event.ActionEvent
  35. * @see java.awt.event.AdjustmentEvent
  36. * @see java.awt.event.ComponentEvent
  37. * @see java.awt.event.ContainerEvent
  38. * @see java.awt.event.FocusEvent
  39. * @see java.awt.event.InputMethodEvent
  40. * @see java.awt.event.InvocationEvent
  41. * @see java.awt.event.ItemEvent
  42. * @see java.awt.event.HierarchyEvent
  43. * @see java.awt.event.KeyEvent
  44. * @see java.awt.event.MouseEvent
  45. * @see java.awt.event.PaintEvent
  46. * @see java.awt.event.TextEvent
  47. * @see java.awt.event.WindowEvent
  48. *
  49. * @author Carl Quinn
  50. * @author Amy Fowler
  51. * @version 1.38 02/11/00
  52. * @since 1.1
  53. */
  54. public abstract class AWTEvent extends EventObject {
  55. private transient long data;
  56. /*
  57. * This field contains the event's event id
  58. * @serial
  59. * @see getID()
  60. * @see AWTEvent())
  61. */
  62. protected int id;
  63. // This field controls whether or not the event is sent back
  64. // down to the peer once the source has processed it -
  65. // false means it's sent to the peer, true means it's not.
  66. // Semantic events always have a 'true' value since they were
  67. // generated by the peer in response to a low-level event.
  68. /*
  69. * @serial
  70. * @see consume()
  71. * @see isConsumed()
  72. */
  73. protected boolean consumed = false;
  74. /**
  75. * The event mask for selecting component events.
  76. */
  77. public final static long COMPONENT_EVENT_MASK = 0x01;
  78. /**
  79. * The event mask for selecting container events.
  80. */
  81. public final static long CONTAINER_EVENT_MASK = 0x02;
  82. /**
  83. * The event mask for selecting focus events.
  84. */
  85. public final static long FOCUS_EVENT_MASK = 0x04;
  86. /**
  87. * The event mask for selecting key events.
  88. */
  89. public final static long KEY_EVENT_MASK = 0x08;
  90. /**
  91. * The event mask for selecting mouse events.
  92. */
  93. public final static long MOUSE_EVENT_MASK = 0x10;
  94. /**
  95. * The event mask for selecting mouse motion events.
  96. */
  97. public final static long MOUSE_MOTION_EVENT_MASK = 0x20;
  98. /**
  99. * The event mask for selecting window events.
  100. */
  101. public final static long WINDOW_EVENT_MASK = 0x40;
  102. /**
  103. * The event mask for selecting action events.
  104. */
  105. public final static long ACTION_EVENT_MASK = 0x80;
  106. /**
  107. * The event mask for selecting adjustment events.
  108. */
  109. public final static long ADJUSTMENT_EVENT_MASK = 0x100;
  110. /**
  111. * The event mask for selecting item events.
  112. */
  113. public final static long ITEM_EVENT_MASK = 0x200;
  114. /**
  115. * The event mask for selecting text events.
  116. */
  117. public final static long TEXT_EVENT_MASK = 0x400;
  118. /**
  119. * The event mask for selecting input method events.
  120. */
  121. public final static long INPUT_METHOD_EVENT_MASK = 0x800;
  122. /**
  123. * The pseudo event mask for enabling input methods.
  124. * We're using one bit in the eventMask so we don't need
  125. * a separate field inputMethodsEnabled.
  126. */
  127. final static long INPUT_METHODS_ENABLED_MASK = 0x1000;
  128. /**
  129. * The event mask for selecting paint events.
  130. */
  131. public final static long PAINT_EVENT_MASK = 0x2000;
  132. /**
  133. * The event mask for selecting invocation events.
  134. */
  135. public final static long INVOCATION_EVENT_MASK = 0x4000;
  136. /**
  137. * The event mask for selecting hierarchy events.
  138. */
  139. public final static long HIERARCHY_EVENT_MASK = 0x8000;
  140. /**
  141. * The event mask for selecting hierarchy bounds events.
  142. */
  143. public final static long HIERARCHY_BOUNDS_EVENT_MASK = 0x10000;
  144. /**
  145. * The maximum value for reserved AWT event IDs. Programs defining
  146. * their own event IDs should use IDs greater than this value.
  147. */
  148. public final static int RESERVED_ID_MAX = 1999;
  149. /*
  150. * JDK 1.1 serialVersionUID
  151. */
  152. private static final long serialVersionUID = -1825314779160409405L;
  153. static {
  154. /* ensure that the necessary native libraries are loaded */
  155. Toolkit.loadLibraries();
  156. initIDs();
  157. }
  158. /**
  159. * Initialize JNI field and method IDs for fields that may be
  160. accessed from C.
  161. */
  162. private static native void initIDs();
  163. /**
  164. * Constructs an AWTEvent object from the parameters of a 1.0-style event.
  165. * @param event the old-style event
  166. */
  167. public AWTEvent(Event event) {
  168. this(event.target, event.id);
  169. }
  170. /**
  171. * Constructs an AWTEvent object with the specified source object and type.
  172. * @param source the object where the event originated
  173. * @id the event type
  174. */
  175. public AWTEvent(Object source, int id) {
  176. super(source);
  177. this.id = id;
  178. switch(id) {
  179. case ActionEvent.ACTION_PERFORMED:
  180. case ItemEvent.ITEM_STATE_CHANGED:
  181. case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
  182. case TextEvent.TEXT_VALUE_CHANGED:
  183. consumed = true;
  184. break;
  185. default:
  186. }
  187. }
  188. /**
  189. * Returns the event type.
  190. */
  191. public int getID() {
  192. return id;
  193. }
  194. public String toString() {
  195. String srcName = null;
  196. if (source instanceof Component) {
  197. srcName = ((Component)source).getName();
  198. } else if (source instanceof MenuComponent) {
  199. srcName = ((MenuComponent)source).getName();
  200. }
  201. return getClass().getName() + "[" + paramString() + "] on " +
  202. (srcName != null? srcName : source);
  203. }
  204. /**
  205. * Returns a string representing the state of this event. This
  206. * method is intended to be used only for debugging purposes, and the
  207. * content and format of the returned string may vary between
  208. * implementations. The returned string may be empty but may not be
  209. * <code>null</code>.
  210. *
  211. * @return a string representation of this event.
  212. */
  213. public String paramString() {
  214. return "";
  215. }
  216. protected void consume() {
  217. switch(id) {
  218. case KeyEvent.KEY_PRESSED:
  219. case KeyEvent.KEY_RELEASED:
  220. case MouseEvent.MOUSE_PRESSED:
  221. case MouseEvent.MOUSE_RELEASED:
  222. case MouseEvent.MOUSE_MOVED:
  223. case MouseEvent.MOUSE_DRAGGED:
  224. case MouseEvent.MOUSE_ENTERED:
  225. case MouseEvent.MOUSE_EXITED:
  226. case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
  227. case InputMethodEvent.CARET_POSITION_CHANGED:
  228. consumed = true;
  229. break;
  230. default:
  231. // event type cannot be consumed
  232. }
  233. }
  234. protected boolean isConsumed() {
  235. return consumed;
  236. }
  237. /* Converts a new event to an old one (used for compatibility).
  238. * If the new event cannot be converted (because no old equivelent
  239. * exists) then this returns null.
  240. *
  241. * Note: this method is here instead of in each individual new
  242. * event class in java.awt.event because we don't want to make
  243. * it public and it needs to be called from java.awt.
  244. */
  245. Event convertToOld() {
  246. Object src = getSource();
  247. int newid = id;
  248. switch(id) {
  249. case KeyEvent.KEY_PRESSED:
  250. case KeyEvent.KEY_RELEASED:
  251. KeyEvent ke = (KeyEvent)this;
  252. if (ke.isActionKey()) {
  253. newid = (id == KeyEvent.KEY_PRESSED?
  254. Event.KEY_ACTION : Event.KEY_ACTION_RELEASE);
  255. }
  256. int keyCode = ke.getKeyCode();
  257. if (keyCode == KeyEvent.VK_SHIFT ||
  258. keyCode == KeyEvent.VK_CONTROL ||
  259. keyCode == KeyEvent.VK_ALT) {
  260. return null; // suppress modifier keys in old event model.
  261. }
  262. // no mask for button1 existed in old Event - strip it out
  263. return new Event(src, ke.getWhen(), newid, 0, 0,
  264. Event.getOldEventKey(ke),
  265. (ke.getModifiers() & ~InputEvent.BUTTON1_MASK));
  266. case MouseEvent.MOUSE_PRESSED:
  267. case MouseEvent.MOUSE_RELEASED:
  268. case MouseEvent.MOUSE_MOVED:
  269. case MouseEvent.MOUSE_DRAGGED:
  270. case MouseEvent.MOUSE_ENTERED:
  271. case MouseEvent.MOUSE_EXITED:
  272. MouseEvent me = (MouseEvent)this;
  273. // no mask for button1 existed in old Event - strip it out
  274. Event olde = new Event(src, me.getWhen(), newid,
  275. me.getX(), me.getY(), 0,
  276. (me.getModifiers() & ~InputEvent.BUTTON1_MASK));
  277. olde.clickCount = me.getClickCount();
  278. return olde;
  279. case FocusEvent.FOCUS_GAINED:
  280. return new Event(src, Event.GOT_FOCUS, null);
  281. case FocusEvent.FOCUS_LOST:
  282. return new Event(src, Event.LOST_FOCUS, null);
  283. case WindowEvent.WINDOW_CLOSING:
  284. case WindowEvent.WINDOW_ICONIFIED:
  285. case WindowEvent.WINDOW_DEICONIFIED:
  286. return new Event(src, newid, null);
  287. case ComponentEvent.COMPONENT_MOVED:
  288. if (src instanceof Frame || src instanceof Dialog) {
  289. Point p = ((Component)src).getLocation();
  290. return new Event(src, 0, Event.WINDOW_MOVED, p.x, p.y, 0, 0);
  291. }
  292. break;
  293. case ActionEvent.ACTION_PERFORMED:
  294. ActionEvent ae = (ActionEvent)this;
  295. String cmd;
  296. if (src instanceof Button) {
  297. cmd = ((Button)src).getLabel();
  298. } else if (src instanceof MenuItem) {
  299. cmd = ((MenuItem)src).getLabel();
  300. } else {
  301. cmd = ae.getActionCommand();
  302. }
  303. return new Event(src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd);
  304. case ItemEvent.ITEM_STATE_CHANGED:
  305. ItemEvent ie = (ItemEvent)this;
  306. Object arg;
  307. if (src instanceof List) {
  308. newid = (ie.getStateChange() == ItemEvent.SELECTED?
  309. Event.LIST_SELECT : Event.LIST_DESELECT);
  310. arg = ie.getItem();
  311. } else {
  312. newid = Event.ACTION_EVENT;
  313. if (src instanceof Choice) {
  314. arg = ie.getItem();
  315. } else { // Checkbox
  316. arg = new Boolean(ie.getStateChange() == ItemEvent.SELECTED);
  317. }
  318. }
  319. return new Event(src, newid, arg);
  320. case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
  321. AdjustmentEvent aje = (AdjustmentEvent)this;
  322. switch(aje.getAdjustmentType()) {
  323. case AdjustmentEvent.UNIT_INCREMENT:
  324. newid = Event.SCROLL_LINE_DOWN;
  325. break;
  326. case AdjustmentEvent.UNIT_DECREMENT:
  327. newid = Event.SCROLL_LINE_UP;
  328. break;
  329. case AdjustmentEvent.BLOCK_INCREMENT:
  330. newid = Event.SCROLL_PAGE_DOWN;
  331. break;
  332. case AdjustmentEvent.BLOCK_DECREMENT:
  333. newid = Event.SCROLL_PAGE_UP;
  334. break;
  335. case AdjustmentEvent.TRACK:
  336. newid = Event.SCROLL_ABSOLUTE;
  337. break;
  338. default:
  339. return null;
  340. }
  341. return new Event(src, newid, new Integer(aje.getValue()));
  342. default:
  343. }
  344. return null;
  345. }
  346. /**
  347. * "Moves" any private data into "other." The data is copied, and
  348. * any references data structures in this object are set to null.
  349. */
  350. void movePrivateDataInto(AWTEvent other) {
  351. other.data = this.data;
  352. data = 0;
  353. }
  354. /**
  355. * Copies all private data from this event into that.
  356. * Space is allocated for the copied data that will be
  357. * freed when the that is finalized. Upon completion,
  358. * this event is not changed.
  359. */
  360. void copyPrivateDataInto(AWTEvent that) {
  361. // currently, the only private data is native
  362. copyDataFieldInto(that);
  363. }
  364. /**
  365. * Copies the "data" instance variable from this event
  366. * into that. Space is allocated for the data in that
  367. * and must be freed at some point (normally in finalize()).
  368. */
  369. private native void copyDataFieldInto(AWTEvent that);
  370. /**
  371. * frees any native data held by this object. Normally called
  372. * when the object is finalized.
  373. */
  374. private native void freeNativeData();
  375. protected void finalize() throws Throwable {
  376. freeNativeData();
  377. super.finalize();
  378. }
  379. } // class AWTEvent