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