KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > awt > EventQueue


1 /*
2  * EventQueue.java
3  *
4  * Created on 5. Juli 2006, 15:24
5  */

6 /*
7  * Copyright 2006 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.awt;
23
24 import java.lang.reflect.*;
25 import java.util.logging.*;
26
27 /**
28  * Subclasses {@link java.awt.EventQueue} in order to provide utility methods
29  * for dealing with the AWT Event Queue.
30  *
31  * @author Christian Schlichtherle
32  * @since TrueZIP 6.1
33  * @version @version@
34  */

35 public class EventQueue extends java.awt.EventQueue JavaDoc {
36
37     private static final byte RESET = 0, CANCELLED = 1, STARTED = 2, DONE = 3;
38
39     /**
40      * Equivalent to {@link #invokeAndWaitUninterruptibly(Runnable, long)
41      * invokeAndWaitUninterruptibly(task, 0)}, but cannot throw an
42      * <code>EventDispatchTimeoutException</code>.
43      */

44     public static final void invokeAndWaitUninterruptibly(Runnable JavaDoc task)
45     throws InvocationTargetException {
46         try {
47             invokeAndWait(task, false, 0);
48         } catch (EventDispatchTimeoutException cannotHappen) {
49             throw new AssertionError JavaDoc(cannotHappen);
50         } catch (InterruptedException JavaDoc cannotHappen) {
51             throw new AssertionError JavaDoc(cannotHappen);
52         }
53     }
54
55     /**
56      * Invokes the given <code>task</code> on the AWT Event Dispatching Thread
57      * (EDT) and waits until it's finished.
58      * If this method is called on the EDT itself, it will just invoke the
59      * given <code>task</code>.
60      * <p>
61      * If the current thread gets interrupted while waiting for the EDT to
62      * finish the task, then waiting is continued normally, but the current
63      * thread's interrupt status is {@link Thread#interrupt() set} upon return.
64      *
65      * @param task The {@link Runnable} whose <code>run</code>
66      * method should be executed synchronously in the EDT.
67      * @param startTimeout If positive, then this parameter specifies the
68      * maximum time to wait before the EDT starts to process
69      * <code>task</code> in milliseconds.
70      * @throws IllegalArgumentException If <code>startTimeout</code> is
71      * negative.
72      * @throws EventDispatchTimeoutException If <code>startTimeout</code> is
73      * positive and waiting for the EDT to start processing the
74      * <code>task</code> timed out.
75      * The task has been cancelled, i.e. it will not be executed.
76      * @throws InvocationTargetException If an exception is thrown when
77      * running <code>task</code>.
78      * <code>getCause()</code> yields the cause of this exception,
79      * which must be a {@link RuntimeException} or an {@link Error}.
80      * @see Thread#interrupted
81      */

82     public static final void invokeAndWaitUninterruptibly(
83             Runnable JavaDoc task,
84             long startTimeout)
85     throws EventDispatchTimeoutException,
86             InvocationTargetException {
87         try {
88             invokeAndWait(task, false, startTimeout);
89         } catch (InterruptedException JavaDoc cannotHappen) {
90             throw new AssertionError JavaDoc(cannotHappen);
91         }
92     }
93
94     /*public static final void invokeAndWaitInterruptibly(
95             Runnable task)
96     throws InterruptedException,
97             InvocationTargetException {
98         try {
99             invokeAndWait(task, true, 0);
100         } catch (EventDispatchTimeoutException cannotHappen) {
101             throw new AssertionError(cannotHappen);
102         }
103     }*/

104
105     /*public static final void invokeAndWaitInterruptibly(
106             Runnable task,
107             long startTimeout)
108     throws EventDispatchTimeoutException,
109             InterruptedException,
110             InvocationTargetException {
111         invokeAndWait(task, true, startTimeout);
112     }*/

113
114     public static void invokeAndWait(
115             final Runnable JavaDoc task,
116             final boolean interruptibly,
117             final long startTimeout)
118     throws EventDispatchTimeoutException,
119             InterruptedException JavaDoc,
120             InvocationTargetException {
121         if (startTimeout < 0)
122             throw new IllegalArgumentException JavaDoc("Timeout must not be negative!");
123
124         if (isDispatchThread()) {
125             try {
126                 task.run();
127             } catch (Throwable JavaDoc throwable) {
128                 throw new InvocationTargetException(throwable);
129             }
130         } else { // !isDispatchThread()
131
class MonitoredAction implements Runnable JavaDoc {
132                 Thread JavaDoc edt;
133                 Throwable JavaDoc throwable;
134                 byte status = RESET;
135
136                 public void run() {
137                     assert isDispatchThread();
138                     if (start()) {
139                         try {
140                             task.run();
141                             finished(null);
142                         } catch (Throwable JavaDoc throwable) {
143                             finished(throwable);
144                         }
145                     }
146                 }
147
148                 private synchronized boolean start() {
149                     if (status == CANCELLED)
150                         return false;
151
152                     edt = Thread.currentThread();
153                     status = STARTED;
154                     notify();
155                     return true;
156                 }
157
158                 private synchronized void finished(final Throwable JavaDoc t) {
159                     throwable = t;
160                     status = DONE;
161                     Thread.interrupted(); // clear status
162
notify();
163                 }
164             }
165
166             final MonitoredAction action = new MonitoredAction();
167             invokeLater(action);
168             synchronized (action) {
169                 InterruptedException JavaDoc interrupted = null;
170                 while (action.status < DONE) {
171                     try {
172                         action.wait(action.status < STARTED ? startTimeout : 0);
173                         if (action.status < STARTED) {
174                             action.status = CANCELLED;
175                             throw new EventDispatchTimeoutException(startTimeout);
176                         }
177                     } catch (InterruptedException JavaDoc ex) {
178                         interrupted = ex;
179                         if (interruptibly)
180                             break;
181                     }
182                 }
183                 if (interrupted != null) {
184                     if (interruptibly) {
185                         if (action.status >= STARTED)
186                             action.edt.interrupt();
187                         throw interrupted;
188                     } else {
189                         Thread.currentThread().interrupt();
190                     }
191                 }
192                 if (action.throwable != null)
193                     throw new InvocationTargetException(action.throwable);
194             }
195         }
196     }
197 }
198
Popular Tags