1. /*
  2. * @(#)InvocationEvent.java 1.5 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.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>EventQueueListener</code> objects.
  21. *
  22. * @see java.awt.ActiveEvent
  23. * @see java.awt.EventQueue#invokeLater
  24. * @see java.awt.EventQueue#invokeAndWait
  25. * @see EventQueueListener
  26. *
  27. * @version 1.5, 11/29/01
  28. * @author Fred Ecks
  29. * @author David Mendenhall
  30. */
  31. public class InvocationEvent extends AWTEvent implements ActiveEvent {
  32. /**
  33. * Marks the first integer id for the range of invoke event ids.
  34. */
  35. public static final int INVOCATION_FIRST = 1200;
  36. /**
  37. * The default id for all InvocationEvents.
  38. */
  39. public static final int INVOCATION_DEFAULT = INVOCATION_FIRST;
  40. /**
  41. * Marks the last integer id for the range of invoke event ids.
  42. */
  43. public static final int INVOCATION_LAST = INVOCATION_DEFAULT;
  44. /**
  45. * The Runnable whose run() method will be called.
  46. */
  47. protected Runnable runnable;
  48. /**
  49. * The (potentially null) Object whose notifyAll() method will be called
  50. * immediately after the Runnable.run() method returns.
  51. */
  52. protected Object notifier;
  53. /**
  54. * Set to true if dispatch() catches Exception and stores it in the
  55. * exception instance variable. If false, Exceptions are propagated up
  56. * to the EventDispatchThread's dispatch loop.
  57. */
  58. protected boolean catchExceptions;
  59. /**
  60. * The (potentially null) Exception thrown during execution of the
  61. * Runnable.run() method. This variable will also be null if a particular
  62. * instance does not catch exceptions.
  63. */
  64. private Exception exception = null;
  65. /**
  66. * Constructs an InvocationEvent with the specified source which will
  67. * execute the runnable's <code>run()</code> method when dispatched.
  68. *
  69. * @param source the Object that originated the event
  70. * @param runnable the Runnable whose run() method will be executed
  71. */
  72. public InvocationEvent(Object source, Runnable runnable) {
  73. this(source, runnable, null, false);
  74. }
  75. /**
  76. * Constructs an InvocationEvent with the specified source which will
  77. * execute the runnable's <code>run()</code> method when dispatched. If
  78. * notifier is non-null, <code>notifyAll()</code> will be called on it
  79. * immediately after <code>run()</code> returns.
  80. *
  81. * @param source the Object that originated the event
  82. * @param runnable the Runnable whose run() method will be
  83. * executed
  84. * @param notifier the Object whose notifyAll() method will be
  85. * called after Runnable.run() has returned
  86. * @param catchExceptions specifies whether dispatch() should catch
  87. * Exception when executing the Runnable's run()
  88. * method, or should instead propagate those
  89. * Exceptions to the EventDispatchThread's
  90. * dispatch loop
  91. */
  92. public InvocationEvent(Object source, Runnable runnable, Object notifier,
  93. boolean catchExceptions) {
  94. this(source, INVOCATION_DEFAULT, runnable, notifier, catchExceptions);
  95. }
  96. /**
  97. * Constructs an InvocationEvent with the specified source and ID which
  98. * will execute the runnable's <code>run()</code> method when dispatched.
  99. * If notifier is non-null, <code>notifyAll()</code> will be called on it
  100. * immediately after <code>run()</code> returns.
  101. *
  102. * @param source the Object that originated the event
  103. * @param id the ID for the Event
  104. * @param runnable the Runnable whose run() method will be
  105. * executed
  106. * @param notifier the Object whose notifyAll() method will be
  107. * called after Runnable.run() has returned
  108. * @param catchExceptions specifies whether dispatch() should catch
  109. * Exception when executing the Runnable's run()
  110. * method, or should instead propagate those
  111. * Exceptions to the EventDispatchThread's
  112. * dispatch loop
  113. */
  114. protected InvocationEvent(Object source, int id, Runnable runnable,
  115. Object notifier, boolean catchExceptions) {
  116. super(source, id);
  117. this.runnable = runnable;
  118. this.notifier = notifier;
  119. this.catchExceptions = catchExceptions;
  120. }
  121. /**
  122. * Executes the Runnable's <code>run()</code> method and notifies the
  123. * notifier (if any) when <code>run()</code> returns.
  124. */
  125. public void dispatch() {
  126. if (catchExceptions) {
  127. try {
  128. runnable.run();
  129. }
  130. catch (Exception e) {
  131. exception = e;
  132. }
  133. }
  134. else {
  135. runnable.run();
  136. }
  137. if (notifier != null) {
  138. synchronized (notifier) {
  139. notifier.notifyAll();
  140. }
  141. }
  142. }
  143. /**
  144. * Returns any Exception caught while executing the Runnable's <code>run()
  145. * </code> method.
  146. *
  147. * @return A reference to the Exception if one was thrown; null if no
  148. * Exception was thrown or if this InvocationEvent does not
  149. * catch exceptions
  150. */
  151. public Exception getException() {
  152. return (catchExceptions) ? exception : null;
  153. }
  154. /**
  155. * Returns a parameter string identifying this event.
  156. * This method is useful for event-logging and for debugging.
  157. *
  158. * @return A string identifying the event and its attributes
  159. */
  160. public String paramString() {
  161. String typeStr;
  162. switch(id) {
  163. case INVOCATION_DEFAULT:
  164. typeStr = "INVOCATION_DEFAULT";
  165. break;
  166. default:
  167. typeStr = "unknown type";
  168. }
  169. return typeStr + ",runnable=" + runnable + ",notifier=" + notifier +
  170. ",catchExceptions=" + catchExceptions;
  171. }
  172. }