InvocationEvent.java 8.44 KB
Newer Older
Tom Tromey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/* InvocationEvent.java -- call a runnable when dispatched
   Copyright (C) 1999, 2002, 2004, 2005  Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.awt.event;

import java.awt.AWTEvent;
import java.awt.ActiveEvent;
import java.awt.EventQueue;

/**
 * This event executes {@link Runnable#run()} of a target object when it is
 * dispatched. This class is used by calls to <code>invokeLater</code> and
 * <code>invokeAndWait</code>, so client code can use this fact to avoid
 * writing special-casing AWTEventListener objects.
 *
 * @author Aaron M. Renn (arenn@urbanophile.com)
 * @see ActiveEvent
 * @see EventQueue#invokeLater(Runnable)
 * @see EventQueue#invokeAndWait(Runnable)
 * @see AWTEventListener
 * @since 1.2
 * @status updated to 1.4
 */
public class InvocationEvent extends AWTEvent implements ActiveEvent
{
  /**
   * Compatible with JDK 1.2+.
   */
  private static final long serialVersionUID = 436056344909459450L;

  /** This is the first id in the range of event ids used by this class. */
  public static final int INVOCATION_FIRST = 1200;

  /** This is the default id for this event type. */
  public static final int INVOCATION_DEFAULT = 1200;

  /** This is the last id in the range of event ids used by this class. */
  public static final int INVOCATION_LAST = 1200;

  /**
   * This is the <code>Runnable</code> object to call when dispatched.
   *
   * @serial the runnable to execute
   */
  protected Runnable runnable;

  /**
   * This is the object to call <code>notifyAll()</code> on when
   * the call to <code>run()</code> returns, or <code>null</code> if no
   * object is to be notified.
   *
   * @serial the object to notify
   */
  protected Object notifier;

  /**
   * This variable is set to <code>true</code> if exceptions are caught
   * and stored in a variable during the call to <code>run()</code>, otherwise
   * exceptions are ignored and propagate up.
   *
   * @serial true to catch exceptions
   */
  protected boolean catchExceptions;

  /**
   * This is the caught exception thrown in the <code>run()</code> method. It
   * is null if exceptions are ignored, the run method hasn't completed, or
   * there were no exceptions.
   *
   * @serial the caught exception, if any
   */
  private Exception exception;

  /**
110 111 112 113 114 115 116
   * This is the caught Throwable thrown in the <code>run()</code> method.
   * It is null if throwables are ignored, the run method hasn't completed, 
   * or there were no throwables thrown.
   */
  private Throwable throwable;
  
  /**
Tom Tromey committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
   * The timestamp when this event was created.
   *
   * @see #getWhen()
   * @serial the timestamp
   * @since 1.4
   */
  private final long when = EventQueue.getMostRecentEventTime();

  /**
   * Initializes a new instance of <code>InvocationEvent</code> with the
   * specified source and runnable.
   *
   * @param source the source of the event
   * @param runnable the <code>Runnable</code> object to invoke
   * @throws IllegalArgumentException if source is null
   */
  public InvocationEvent(Object source, Runnable runnable)
  {
    this(source, INVOCATION_DEFAULT, runnable, null, false);
  }

  /**
   * Initializes a new instance of <code>InvocationEvent</code> with the
   * specified source, runnable, and notifier.  It will also catch exceptions
   * if specified. If notifier is non-null, this will call notifyAll() on
   * the object when the runnable is complete. If catchExceptions is true,
   * this traps any exception in the runnable, otherwise it lets the exception
   * propagate up the Event Dispatch thread.
   *
   * @param source the source of the event
   * @param runnable the <code>Runnable</code> object to invoke
   * @param notifier the object to notify, or null
   * @param catchExceptions true to catch exceptions from the runnable
   */
  public InvocationEvent(Object source, Runnable runnable, Object notifier,
                         boolean catchExceptions)
  {
    this(source, INVOCATION_DEFAULT, runnable, notifier, catchExceptions);
  }

  /**
   * Initializes a new instance of <code>InvocationEvent</code> with the
   * specified source, runnable, and notifier.  It will also catch exceptions
   * if specified. If notifier is non-null, this will call notifyAll() on
   * the object when the runnable is complete. If catchExceptions is true,
   * this traps any exception in the runnable, otherwise it lets the exception
   * propagate up the Event Dispatch thread. Note that an invalid id leads to
   * unspecified results.
   *
   * @param source the source of the event
   * @param id the event id
   * @param runnable the <code>Runnable</code> object to invoke
   * @param notifier the object to notify, or null
   * @param catchExceptions true to catch exceptions from the runnable
   */
  protected InvocationEvent(Object source, int id, Runnable runnable,
                            Object notifier, boolean catchExceptions)
  {
    super(source, id);
    this.runnable = runnable;
    this.notifier = notifier;
    this.catchExceptions = catchExceptions;
  }

  /**
   * This method calls the <code>run()</code> method of the runnable, traps
   * exceptions if instructed to do so, and calls <code>notifyAll()</code>
   * on any notifier if all worked successfully.
   */
  public void dispatch()
  {
    if (catchExceptions)
      try
        {
          runnable.run();
        }
193
      catch (Throwable t)
Tom Tromey committed
194
        {
195 196 197
          throwable = t;
          if (t instanceof Exception)
            exception = (Exception)t;
Tom Tromey committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        }
    else
      runnable.run();

    Object o = notifier;
    if (o != null)
      synchronized(o)
	{
	  o.notifyAll();
	}
  }

  /**
   * This method returns the exception that occurred during the execution of
   * the runnable, or <code>null</code> if not exception was thrown or
   * exceptions were not caught.
   *
   * @return the exception thrown by the runnable
   */
  public Exception getException()
  {
    return exception;
  }

  /**
223 224 225 226 227 228 229 230 231 232 233 234
   * Returns a throwable caught while executing the Runnable's run() method.
   * Null if none was thrown or if this InvocationEvent doesn't catch
   * throwables.
   * @return the caught Throwable
   * @since 1.5
   */
  public Throwable getThrowable()
  {
    return throwable;
  }
  
  /**
Tom Tromey committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
   * Gets the timestamp of when this event was created.
   *
   * @return the timestamp of this event
   * @since 1.4
   */
  public long getWhen()
  {
    return when;
  }

  /**
   * This method returns a string identifying this event. This is formatted as:
   * <code>"INVOCATION_DEFAULT,runnable=" + runnable + ",notifier=" + notifier
   * + ",catchExceptions=" + catchExceptions + ",when=" + getWhen()</code>.
   *
   * @return a string identifying this event
   */
  public String paramString()
  {
    return (id == INVOCATION_DEFAULT ? "INVOCATION_DEFAULT,runnable="
            : "unknown type,runnable=") + runnable + ",notifier=" + notifier
      + ",catchExceptions=" + catchExceptions + ",when=" + when;
  }
} // class InvocationEvent