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