EventQueue.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation
  2. This file is part of GNU Classpath.
  3. GNU Classpath is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. GNU Classpath is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with GNU Classpath; see the file COPYING. If not, write to the
  13. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA.
  15. Linking this library statically or dynamically with other modules is
  16. making a combined work based on this library. Thus, the terms and
  17. conditions of the GNU General Public License cover the whole
  18. combination.
  19. As a special exception, the copyright holders of this library give you
  20. permission to link this library with independent modules to produce an
  21. executable, regardless of the license terms of these independent
  22. modules, and to copy and distribute the resulting executable under
  23. terms of your choice, provided that you also meet, for each linked
  24. independent module, the terms and conditions of the license of that
  25. module. An independent module is a module which is not derived from
  26. or based on this library. If you modify this library, you may extend
  27. this exception to your version of the library, but you are not
  28. obligated to do so. If you do not wish to do so, delete this
  29. exception statement from your version. */
  30. package java.awt;
  31. import java.awt.event.*;
  32. import java.util.EmptyStackException;
  33. import java.lang.reflect.InvocationTargetException;
  34. /* Written using on-line Java 2 Platform Standard Edition v1.3 API
  35. * Specification, as well as "The Java Class Libraries", 2nd edition
  36. * (Addison-Wesley, 1998).
  37. * Status: Believed complete, but untested. Check FIXME's.
  38. */
  39. /**
  40. * This class manages a queue of <code>AWTEvent</code> objects that
  41. * are posted to it. The AWT system uses only one event queue for all
  42. * events.
  43. *
  44. * @author Bryce McKinlay
  45. * @author Aaron M. Renn (arenn@urbanophile.com)
  46. */
  47. public class EventQueue
  48. {
  49. private static final int INITIAL_QUEUE_DEPTH = 8;
  50. private AWTEvent[] queue = new AWTEvent[INITIAL_QUEUE_DEPTH];
  51. private int next_in = 0; // Index where next event will be added to queue
  52. private int next_out = 0; // Index of next event to be removed from queue
  53. private EventQueue next;
  54. private EventQueue prev;
  55. private EventDispatchThread dispatchThread = new EventDispatchThread(this);
  56. /**
  57. * Initializes a new instance of <code>EventQueue</code>.
  58. */
  59. public EventQueue()
  60. {
  61. }
  62. /**
  63. * Returns the next event in the queue. This method will block until
  64. * an event is available or until the thread is interrupted.
  65. *
  66. * @return The next event in the queue.
  67. *
  68. * @exception InterruptedException If this thread is interrupted while
  69. * waiting for an event to be posted to the queue.
  70. */
  71. public synchronized AWTEvent getNextEvent()
  72. throws InterruptedException
  73. {
  74. if (next != null)
  75. return next.getNextEvent();
  76. while (next_in == next_out)
  77. wait();
  78. AWTEvent res = queue[next_out];
  79. if (++next_out == queue.length)
  80. next_out = 0;
  81. return res;
  82. }
  83. /**
  84. * Returns the next event in the queue without removing it from the queue.
  85. * This method will block until an event is available or until the thread
  86. * is interrupted.
  87. *
  88. * @return The next event in the queue.
  89. * @specnote Does not block. Returns null if there are no events on the
  90. * queue.
  91. */
  92. public synchronized AWTEvent peekEvent()
  93. {
  94. if (next != null)
  95. return next.peekEvent();
  96. if (next_in != next_out)
  97. return queue[next_out];
  98. else return null;
  99. }
  100. /**
  101. * Returns the next event in the queue that has the specified id
  102. * without removing it from the queue.
  103. * This method will block until an event is available or until the thread
  104. * is interrupted.
  105. *
  106. * @param id The event id to return.
  107. *
  108. * @return The next event in the queue.
  109. *
  110. * @specnote Does not block. Returns null if there are no matching events
  111. * on the queue.
  112. */
  113. public synchronized AWTEvent peekEvent(int id)
  114. {
  115. if (next != null)
  116. return next.peekEvent(id);
  117. int i = next_out;
  118. while (i != next_in)
  119. {
  120. AWTEvent qevt = queue[i];
  121. if (qevt.id == id)
  122. return qevt;
  123. }
  124. return null;
  125. }
  126. /**
  127. * Posts a new event to the queue.
  128. *
  129. * @param event The event to post to the queue.
  130. *
  131. * @exception NullPointerException If event is null.
  132. */
  133. public synchronized void postEvent(AWTEvent evt)
  134. {
  135. if (next != null)
  136. {
  137. next.postEvent(evt);
  138. return;
  139. }
  140. // FIXME: Security checks?
  141. /* Check for any events already on the queue with the same source
  142. and ID. */
  143. int i = next_out;
  144. while (i != next_in)
  145. {
  146. AWTEvent qevt = queue[i];
  147. Object src;
  148. if (qevt.id == evt.id
  149. && (src = qevt.getSource()) == evt.getSource()
  150. && src instanceof Component)
  151. {
  152. /* If there are, call coalesceEvents on the source component
  153. to see if they can be combined. */
  154. Component srccmp = (Component) src;
  155. AWTEvent coalesced_evt = srccmp.coalesceEvents(qevt, evt);
  156. if (coalesced_evt != null)
  157. {
  158. /* Yes. Replace the existing event with the combined event. */
  159. queue[i] = coalesced_evt;
  160. return;
  161. }
  162. break;
  163. }
  164. if (++i == queue.length)
  165. i = 0;
  166. }
  167. queue[next_in] = evt;
  168. if (++next_in == queue.length)
  169. next_in = 0;
  170. if (next_in == next_out)
  171. {
  172. /* Queue is full. Extend it. */
  173. AWTEvent[] oldQueue = queue;
  174. queue = new AWTEvent[queue.length * 2];
  175. int len = oldQueue.length - next_out;
  176. System.arraycopy(oldQueue, next_out, queue, 0, len);
  177. if (next_out != 0)
  178. System.arraycopy(oldQueue, 0, queue, len, next_out);
  179. next_out = 0;
  180. next_in = oldQueue.length;
  181. }
  182. notify();
  183. }
  184. /**
  185. * Causes runnable to have its run method called in the dispatch thread of the
  186. * EventQueue. This will happen after all pending events are processed. The
  187. * call blocks until this has happened. This method will throw an Error if
  188. * called from the event dispatcher thread.
  189. *
  190. * @exception InterruptedException If another thread has interrupted
  191. * this thread.
  192. * @exception InvocationTargetException If an exception is thrown when running
  193. * runnable.
  194. *
  195. * @since 1.2
  196. */
  197. public static void invokeAndWait(Runnable runnable)
  198. throws InterruptedException, InvocationTargetException
  199. {
  200. EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
  201. Thread current = Thread.currentThread();
  202. if (current == eq.dispatchThread)
  203. throw new Error("Can't call invokeAndWait from event dispatch thread");
  204. InvocationEvent ie =
  205. new InvocationEvent(eq, runnable, current, true);
  206. synchronized (current)
  207. {
  208. eq.postEvent(ie);
  209. current.wait();
  210. }
  211. Exception exception;
  212. if ((exception = ie.getException()) != null)
  213. throw new InvocationTargetException(exception);
  214. }
  215. /** @since JDK1.2 */
  216. public static void invokeLater(Runnable runnable)
  217. {
  218. EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
  219. InvocationEvent ie =
  220. new InvocationEvent(eq, runnable, null, false);
  221. eq.postEvent(ie);
  222. }
  223. public static boolean isDispatchThread()
  224. {
  225. EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
  226. return (Thread.currentThread() == eq.dispatchThread);
  227. }
  228. /** Allows a custom EventQueue implementation to replace this one.
  229. * All pending events are transferred to the new queue. Calls to postEvent,
  230. * getNextEvent, and peekEvent are forwarded to the pushed queue until it
  231. * is removed with a pop().
  232. *
  233. * @exception NullPointerException if newEventQueue is null.
  234. */
  235. public synchronized void push(EventQueue newEventQueue)
  236. {
  237. int i = next_out;
  238. while (i != next_in)
  239. {
  240. newEventQueue.postEvent(queue[i]);
  241. next_out = i;
  242. if (++i == queue.length)
  243. i = 0;
  244. }
  245. next = newEventQueue;
  246. newEventQueue.prev = this;
  247. }
  248. /** Transfer any pending events from this queue back to the parent queue that
  249. * was previously push()ed. Event dispatch from this queue is suspended.
  250. *
  251. * @exception EmptyStackException If no previous push was made on this
  252. * EventQueue.
  253. */
  254. protected void pop() throws EmptyStackException
  255. {
  256. if (prev == null)
  257. throw new EmptyStackException();
  258. // Don't synchronize both this and prev at the same time, or deadlock could
  259. // occur.
  260. synchronized (prev)
  261. {
  262. prev.next = null;
  263. }
  264. synchronized (this)
  265. {
  266. int i = next_out;
  267. while (i != next_in)
  268. {
  269. prev.postEvent(queue[i]);
  270. next_out = i;
  271. if (++i == queue.length)
  272. i = 0;
  273. }
  274. }
  275. }
  276. /**
  277. * Dispatches an event. The manner in which the event is dispatched depends
  278. * upon the type of the event and the type of the event's source object.
  279. *
  280. * @exception NullPointerException If event is null.
  281. */
  282. protected void dispatchEvent(AWTEvent evt)
  283. {
  284. if (evt instanceof ActiveEvent)
  285. {
  286. ActiveEvent active_evt = (ActiveEvent) evt;
  287. active_evt.dispatch();
  288. }
  289. else
  290. {
  291. Object source = evt.getSource();
  292. if (source instanceof Component)
  293. {
  294. Component srccmp = (Component) source;
  295. srccmp.dispatchEvent(evt);
  296. }
  297. else if (source instanceof MenuComponent)
  298. {
  299. MenuComponent srccmp = (MenuComponent) source;
  300. srccmp.dispatchEvent(evt);
  301. }
  302. }
  303. }
  304. /**
  305. * Returns the timestamp of the most recent event that had a timestamp, or
  306. * the initialization time of the event queue if no events have been fired.
  307. * At present, only <code>InputEvent</code>s, <code>ActionEvent</code>s,
  308. * <code>InputMethodEvent</code>s, and <code>InvocationEvent</code>s have
  309. * timestamps, but this may be added to other events in future versions.
  310. * If this is called by the event dispatching thread, it can be any
  311. * (sequential) value, but to other threads, the safest bet is to return
  312. * System.currentTimeMillis().
  313. *
  314. * @return the most recent timestamp
  315. * @see InputEvent#getWhen()
  316. * @see ActionEvent#getWhen()
  317. * @see InvocationEvent#getWhen()
  318. * @see InputMethodEvent#getWhen()
  319. * @since 1.4
  320. */
  321. public static long getMostRecentEventTime()
  322. {
  323. // XXX For now, this ONLY does the current time.
  324. return System.currentTimeMillis();
  325. }
  326. } // class EventQueue