1. /*
  2. * @(#)InvocationEvent.java 1.14 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.ActiveEvent;
  9. import java.awt.AWTEvent;
  10. /**
  11. * An event which executes the <code>run()</code> method on a <code>Runnable
  12. * </code> when dispatched by the AWT event dispatcher thread. This class can
  13. * be used as a reference implementation of <code>ActiveEvent</code> rather
  14. * than declaring a new class and defining <code>dispatch()</code>.<p>
  15. *
  16. * Instances of this class are placed on the <code>EventQueue</code> by calls
  17. * to <code>invokeLater</code> and <code>invokeAndWait</code>. Client code
  18. * can use this fact to write replacement functions for <code>invokeLater
  19. * </code> and <code>invokeAndWait</code> without writing special-case code
  20. * in any <code>AWTEventListener</code> objects.
  21. *
  22. * @author Fred Ecks
  23. * @author David Mendenhall
  24. * @version 1.14, 01/23/03
  25. *
  26. * @see java.awt.ActiveEvent
  27. * @see java.awt.EventQueue#invokeLater
  28. * @see java.awt.EventQueue#invokeAndWait
  29. * @see AWTEventListener
  30. *
  31. * @since 1.2
  32. */
  33. public class InvocationEvent extends AWTEvent implements ActiveEvent {
  34. /**
  35. * Marks the first integer id for the range of invocation event ids.
  36. */
  37. public static final int INVOCATION_FIRST = 1200;
  38. /**
  39. * The default id for all InvocationEvents.
  40. */
  41. public static final int INVOCATION_DEFAULT = INVOCATION_FIRST;
  42. /**
  43. * Marks the last integer id for the range of invocation event ids.
  44. */
  45. public static final int INVOCATION_LAST = INVOCATION_DEFAULT;
  46. /**
  47. * The Runnable whose run() method will be called.
  48. */
  49. protected Runnable runnable;
  50. /**
  51. * The (potentially null) Object whose notifyAll() method will be called
  52. * immediately after the Runnable.run() method returns.
  53. */
  54. protected Object notifier;
  55. /**
  56. * Set to true if dispatch() catches Exception and stores it in the
  57. * exception instance variable. If false, Exceptions are propagated up
  58. * to the EventDispatchThread's dispatch loop.
  59. */
  60. protected boolean catchExceptions;
  61. /**
  62. * The (potentially null) Exception thrown during execution of the
  63. * Runnable.run() method. This variable will also be null if a particular
  64. * instance does not catch exceptions.
  65. */
  66. private Exception exception = null;
  67. /**
  68. * The timestamp of when this event occurred.
  69. *
  70. * @serial
  71. * @see #getWhen
  72. */
  73. private long when;
  74. /*
  75. * JDK 1.1 serialVersionUID.
  76. */
  77. private static final long serialVersionUID = 436056344909459450L;
  78. /**
  79. * Constructs an <code>InvocationEvent</code> with the specified
  80. * source which will execute the runnable's <code>run</code>
  81. * method when dispatched.
  82. *
  83. * @param source the <code>Object</code> that originated the event
  84. * @param runnable the <code>Runnable</code> whose <code>run</code>
  85. * method will be executed
  86. */
  87. public InvocationEvent(Object source, Runnable runnable) {
  88. this(source, runnable, null, false);
  89. }
  90. /**
  91. * Constructs an <code>InvocationEvent</code> with the specified
  92. * source which will execute the runnable's <code>run</code>
  93. * method when dispatched. If notifier is non-<code>null</code>,
  94. * <code>notifyAll()</code> will be called on it
  95. * immediately after <code>run</code> returns.
  96. *
  97. * @param source the <code>Object</code> that originated
  98. * the event
  99. * @param runnable the <code>Runnable</code> whose
  100. * <code>run</code> method will be
  101. * executed
  102. * @param notifier the Object whose <code>notifyAll</code>
  103. * method will be called after
  104. * <code>Runnable.run</code> has returned
  105. * @param catchExceptions specifies whether <code>dispatch</code>
  106. * should catch Exception when executing
  107. * the <code>Runnable</code>'s <code>run</code>
  108. * method, or should instead propagate those
  109. * Exceptions to the EventDispatchThread's
  110. * dispatch loop
  111. */
  112. public InvocationEvent(Object source, Runnable runnable, Object notifier,
  113. boolean catchExceptions) {
  114. this(source, INVOCATION_DEFAULT, runnable, notifier, catchExceptions);
  115. }
  116. /**
  117. * Constructs an <code>InvocationEvent</code> with the specified
  118. * source and ID which will execute the runnable's <code>run</code>
  119. * method when dispatched. If notifier is non-<code>null</code>,
  120. * <code>notifyAll</code> will be called on it
  121. * immediately after <code>run</code> returns.
  122. * <p>Note that passing in an invalid <code>id</code> results in
  123. * unspecified behavior.
  124. *
  125. * @param source the <code>Object</code> that originated
  126. * the event
  127. * @param id the ID for the event
  128. * @param runnable the <code>Runnable</code> whose
  129. * <code>run</code> method will be executed
  130. * @param notifier the <code>Object whose <code>notifyAll</code>
  131. * method will be called after
  132. * <code>Runnable.run</code> has returned
  133. * @param catchExceptions specifies whether <code>dispatch</code>
  134. * should catch Exception when executing the
  135. * <code>Runnable</code>'s <code>run</code>
  136. * method, or should instead propagate those
  137. * Exceptions to the EventDispatchThread's
  138. * dispatch loop
  139. */
  140. protected InvocationEvent(Object source, int id, Runnable runnable,
  141. Object notifier, boolean catchExceptions) {
  142. super(source, id);
  143. this.runnable = runnable;
  144. this.notifier = notifier;
  145. this.catchExceptions = catchExceptions;
  146. this.when = System.currentTimeMillis();
  147. }
  148. /**
  149. * Executes the Runnable's <code>run()</code> method and notifies the
  150. * notifier (if any) when <code>run()</code> returns.
  151. */
  152. public void dispatch() {
  153. if (catchExceptions) {
  154. try {
  155. runnable.run();
  156. }
  157. catch (Exception e) {
  158. exception = e;
  159. }
  160. }
  161. else {
  162. runnable.run();
  163. }
  164. if (notifier != null) {
  165. synchronized (notifier) {
  166. notifier.notifyAll();
  167. }
  168. }
  169. }
  170. /**
  171. * Returns any Exception caught while executing the Runnable's <code>run()
  172. * </code> method.
  173. *
  174. * @return A reference to the Exception if one was thrown; null if no
  175. * Exception was thrown or if this InvocationEvent does not
  176. * catch exceptions
  177. */
  178. public Exception getException() {
  179. return (catchExceptions) ? exception : null;
  180. }
  181. /**
  182. * Returns the timestamp of when this event occurred.
  183. *
  184. * @return this event's timestamp
  185. * @since 1.4
  186. */
  187. public long getWhen() {
  188. return when;
  189. }
  190. /**
  191. * Returns a parameter string identifying this event.
  192. * This method is useful for event-logging and for debugging.
  193. *
  194. * @return A string identifying the event and its attributes
  195. */
  196. public String paramString() {
  197. String typeStr;
  198. switch(id) {
  199. case INVOCATION_DEFAULT:
  200. typeStr = "INVOCATION_DEFAULT";
  201. break;
  202. default:
  203. typeStr = "unknown type";
  204. }
  205. return typeStr + ",runnable=" + runnable + ",notifier=" + notifier +
  206. ",catchExceptions=" + catchExceptions + ",when=" + when;
  207. }
  208. }