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