jdk/src/share/classes/java/awt/event/InvocationEvent.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4264 40c232605c68
child 21246 9d1752954b0e
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4264
diff changeset
     2
 * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4264
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4264
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4264
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4264
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4264
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.ActiveEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.AWTEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * An event which executes the <code>run()</code> method on a <code>Runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * </code> when dispatched by the AWT event dispatcher thread. This class can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * be used as a reference implementation of <code>ActiveEvent</code> rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * than declaring a new class and defining <code>dispatch()</code>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Instances of this class are placed on the <code>EventQueue</code> by calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * to <code>invokeLater</code> and <code>invokeAndWait</code>. Client code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * can use this fact to write replacement functions for <code>invokeLater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * </code> and <code>invokeAndWait</code> without writing special-case code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * in any <code>AWTEventListener</code> objects.
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    42
 * <p>
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    43
 * An unspecified behavior will be caused if the {@code id} parameter
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    44
 * of any particular {@code InvocationEvent} instance is not
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
    45
 * in the range from {@code INVOCATION_FIRST} to {@code INVOCATION_LAST}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author      Fred Ecks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author      David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see         java.awt.ActiveEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see         java.awt.EventQueue#invokeLater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @see         java.awt.EventQueue#invokeAndWait
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @see         AWTEventListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since       1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class InvocationEvent extends AWTEvent implements ActiveEvent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Marks the first integer id for the range of invocation event ids.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static final int INVOCATION_FIRST = 1200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The default id for all InvocationEvents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final int INVOCATION_DEFAULT = INVOCATION_FIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Marks the last integer id for the range of invocation event ids.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static final int INVOCATION_LAST = INVOCATION_DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The Runnable whose run() method will be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    protected Runnable runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * The (potentially null) Object whose notifyAll() method will be called
4264
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
    81
     * immediately after the Runnable.run() method has returned or thrown an exception.
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
    82
     *
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
    83
     * @see #isDispatched
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected Object notifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
4264
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
    88
     * Indicates whether the <code>run()</code> method of the <code>runnable</code>
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
    89
     * was executed or not.
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
    90
     *
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
    91
     * @see #isDispatched
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
    92
     * @since 1.7
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
    93
     */
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
    94
    private volatile boolean dispatched = false;
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
    95
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
    96
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Set to true if dispatch() catches Throwable and stores it in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * exception instance variable. If false, Throwables are propagated up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * to the EventDispatchThread's dispatch loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected boolean catchExceptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * The (potentially null) Exception thrown during execution of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Runnable.run() method. This variable will also be null if a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * instance does not catch exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private Exception exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * The (potentially null) Throwable thrown during execution of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Runnable.run() method. This variable will also be null if a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * instance does not catch exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private Throwable throwable = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The timestamp of when this event occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @see #getWhen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private long when;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * JDK 1.1 serialVersionUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static final long serialVersionUID = 436056344909459450L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Constructs an <code>InvocationEvent</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * source which will execute the runnable's <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * method when dispatched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p>This is a convenience constructor.  An invocation of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <tt>InvocationEvent(source, runnable)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * behaves in exactly the same way as the invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <tt>{@link #InvocationEvent(Object, Runnable, Object, boolean) InvocationEvent}(source, runnable, null, false)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <p> This method throws an <code>IllegalArgumentException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * if <code>source</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   141
     * @param source    The <code>Object</code> that originated the event
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   142
     * @param runnable  The <code>Runnable</code> whose <code>run</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *                  method will be executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @throws IllegalArgumentException if <code>source</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   146
     * @see #getSource()
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   147
     * @see #InvocationEvent(Object, Runnable, Object, boolean)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public InvocationEvent(Object source, Runnable runnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this(source, runnable, null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Constructs an <code>InvocationEvent</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * source which will execute the runnable's <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * method when dispatched.  If notifier is non-<code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <code>notifyAll()</code> will be called on it
4264
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   158
     * immediately after <code>run</code> has returned or thrown an exception.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <p>An invocation of the form <tt>InvocationEvent(source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * runnable, notifier, catchThrowables)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * behaves in exactly the same way as the invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <tt>{@link #InvocationEvent(Object, int, Runnable, Object, boolean) InvocationEvent}(source, InvocationEvent.INVOCATION_DEFAULT, runnable, notifier, catchThrowables)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <p>This method throws an <code>IllegalArgumentException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * if <code>source</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   166
     * @param source            The <code>Object</code> that originated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *                          the event
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   168
     * @param runnable          The <code>Runnable</code> whose
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *                          <code>run</code> method will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *                          executed
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   171
     * @param notifier          The {@code Object} whose <code>notifyAll</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *                          method will be called after
4264
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   173
     *                          <code>Runnable.run</code> has returned or
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   174
     *                          thrown an exception
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   175
     * @param catchThrowables   Specifies whether <code>dispatch</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *                          should catch Throwable when executing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *                          the <code>Runnable</code>'s <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *                          method, or should instead propagate those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *                          Throwables to the EventDispatchThread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *                          dispatch loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @throws IllegalArgumentException if <code>source</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   183
     * @see #getSource()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see     #InvocationEvent(Object, int, Runnable, Object, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public InvocationEvent(Object source, Runnable runnable, Object notifier,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                           boolean catchThrowables) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        this(source, INVOCATION_DEFAULT, runnable, notifier, catchThrowables);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Constructs an <code>InvocationEvent</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * source and ID which will execute the runnable's <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * method when dispatched.  If notifier is non-<code>null</code>,
4264
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   195
     * <code>notifyAll</code> will be called on it immediately after
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   196
     * <code>run</code> has returned or thrown an exception.
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   197
     * <p>This method throws an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <code>IllegalArgumentException</code> if <code>source</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   201
     * @param source            The <code>Object</code> that originated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *                          the event
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   203
     * @param id     An integer indicating the type of event.
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   204
     *                     For information on allowable values, see
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   205
     *                     the class description for {@link InvocationEvent}
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   206
     * @param runnable          The <code>Runnable</code> whose
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *                          <code>run</code> method will be executed
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   208
     * @param notifier          The <code>Object</code> whose <code>notifyAll</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *                          method will be called after
4264
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   210
     *                          <code>Runnable.run</code> has returned or
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   211
     *                          thrown an exception
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   212
     * @param catchThrowables   Specifies whether <code>dispatch</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *                          should catch Throwable when executing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *                          <code>Runnable</code>'s <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *                          method, or should instead propagate those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *                          Throwables to the EventDispatchThread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *                          dispatch loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @throws IllegalArgumentException if <code>source</code> is null
440
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   219
     * @see #getSource()
a3dac373f62d 6520716: event classes lack info about parameters
dav
parents: 2
diff changeset
   220
     * @see #getID()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    protected InvocationEvent(Object source, int id, Runnable runnable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                              Object notifier, boolean catchThrowables) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        super(source, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        this.runnable = runnable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        this.notifier = notifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        this.catchExceptions = catchThrowables;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        this.when = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Executes the Runnable's <code>run()</code> method and notifies the
4264
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   233
     * notifier (if any) when <code>run()</code> has returned or thrown an exception.
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   234
     *
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   235
     * @see #isDispatched
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public void dispatch() {
4264
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   238
        try {
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   239
            if (catchExceptions) {
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   240
                try {
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   241
                    runnable.run();
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   242
                }
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   243
                catch (Throwable t) {
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   244
                    if (t instanceof Exception) {
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   245
                        exception = (Exception) t;
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   246
                    }
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   247
                    throwable = t;
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   248
                }
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   249
            }
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   250
            else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                runnable.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
4264
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   253
        } finally {
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   254
            dispatched = true;
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   255
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   256
            if (notifier != null) {
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   257
                synchronized (notifier) {
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   258
                    notifier.notifyAll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Returns any Exception caught while executing the Runnable's <code>run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * </code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @return  A reference to the Exception if one was thrown; null if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *          Exception was thrown or if this InvocationEvent does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *          catch exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public Exception getException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return (catchExceptions) ? exception : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Returns any Throwable caught while executing the Runnable's <code>run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * </code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return  A reference to the Throwable if one was thrown; null if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *          Throwable was thrown or if this InvocationEvent does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *          catch Throwables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public Throwable getThrowable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return (catchExceptions) ? throwable : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Returns the timestamp of when this event occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return this event's timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public long getWhen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return when;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
4264
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   300
     * Returns {@code true} if the event is dispatched or any exception is
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   301
     * thrown while dispatching, {@code false} otherwise. The method should
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   302
     * be called by a waiting thread that calls the {@code notifier.wait()} method.
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   303
     * Since spurious wakeups are possible (as explained in {@link Object#wait()}),
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   304
     * this method should be used in a waiting loop to ensure that the event
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   305
     * got dispatched:
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   306
     * <pre>
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   307
     *     while (!event.isDispatched()) {
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   308
     *         notifier.wait();
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   309
     *     }
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   310
     * </pre>
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   311
     * If the waiting thread wakes up without dispatching the event,
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   312
     * the {@code isDispatched()} method returns {@code false}, and
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   313
     * the {@code while} loop executes once more, thus, causing
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   314
     * the awakened thread to revert to the waiting mode.
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   315
     * <p>
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   316
     * If the {@code notifier.notifyAll()} happens before the waiting thread
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   317
     * enters the {@code notifier.wait()} method, the {@code while} loop ensures
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   318
     * that the waiting thread will not enter the {@code notifier.wait()} method.
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   319
     * Otherwise, there is no guarantee that the waiting thread will ever be woken
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   320
     * from the wait.
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   321
     *
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   322
     * @return {@code true} if the event has been dispatched, or any exception
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   323
     * has been thrown while dispatching, {@code false} otherwise
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   324
     * @see #dispatch
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   325
     * @see #notifier
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   326
     * @see #catchExceptions
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   327
     * @since 1.7
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   328
     */
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   329
    public boolean isDispatched() {
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   330
        return dispatched;
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   331
    }
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   332
40c232605c68 6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents: 440
diff changeset
   333
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Returns a parameter string identifying this event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * This method is useful for event-logging and for debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @return  A string identifying the event and its attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        String typeStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        switch(id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            case INVOCATION_DEFAULT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                typeStr = "INVOCATION_DEFAULT";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                typeStr = "unknown type";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return typeStr + ",runnable=" + runnable + ",notifier=" + notifier +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            ",catchExceptions=" + catchExceptions + ",when=" + when;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
}