Mercurial > projects > dwt-addons
annotate dwtx/jface/operation/ModalContext.d @ 90:7ffeace6c47f
Update 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 06 Jul 2008 23:30:07 +0200 |
parents | 5df4896124c7 |
children | 862b05e0334a |
rev | line source |
---|---|
6 | 1 /******************************************************************************* |
90 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
6 | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.operation.ModalContext; | |
14 | |
15 import dwt.widgets.Display; | |
16 import dwtx.core.runtime.Assert; | |
17 import dwtx.core.runtime.IProgressMonitor; | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
18 import dwtx.core.runtime.IStatus; |
6 | 19 import dwtx.core.runtime.OperationCanceledException; |
20 import dwtx.core.runtime.ProgressMonitorWrapper; | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
21 import dwtx.core.runtime.Status; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
22 import dwtx.jface.util.Policy; |
6 | 23 |
24 import dwtx.jface.operation.IThreadListener; | |
25 import dwtx.jface.operation.IRunnableWithProgress; | |
26 import dwtx.jface.operation.AccumulatingProgressMonitor; | |
27 | |
28 import dwt.dwthelper.utils; | |
29 import dwt.dwthelper.Runnable; | |
30 import tango.core.Thread; | |
31 import tango.io.Stdout; | |
32 | |
33 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
34 * Utility class for supporting modal operations. The runnable passed to the |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
35 * <code>run</code> method is executed in a separate thread, depending on the |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
36 * value of the passed fork argument. If the runnable is executed in a separate |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
37 * thread then the current thread either waits until the new thread ends or, if |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
38 * the current thread is the UI thread, it polls the DWT event queue and |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
39 * dispatches each event. |
6 | 40 * <p> |
41 * This class is not intended to be subclassed. | |
42 * </p> | |
90 | 43 * @noinstantiate This class is not intended to be instantiated by clients. |
44 * @noextend This class is not intended to be subclassed by clients. | |
6 | 45 */ |
46 public class ModalContext { | |
47 | |
48 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
49 * Indicated whether ModalContext is in debug mode; <code>false</code> by |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
50 * default. |
6 | 51 */ |
65 | 52 private static bool debug_ = true; |
6 | 53 |
54 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
55 * The number of nested modal runs, or 0 if not inside a modal run. This is |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
56 * global state. |
6 | 57 */ |
58 private static int modalLevel = 0; | |
59 | |
60 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
61 * Indicates whether operations should be run in a separate thread. Defaults |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
62 * to true. For internal debugging use, set to false to run operations in |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
63 * the calling thread. |
6 | 64 */ |
65 private static bool runInSeparateThread = true; | |
66 | |
67 /** | |
68 * Thread which runs the modal context. | |
69 */ | |
70 private static class ModalContextThread : Thread { | |
71 /** | |
72 * The operation to be run. | |
73 */ | |
74 private IRunnableWithProgress runnable; | |
75 | |
76 /** | |
77 * The exception thrown by the operation starter. | |
78 */ | |
79 private Exception throwable; | |
80 | |
81 /** | |
82 * The progress monitor used for progress and cancelation. | |
83 */ | |
84 private IProgressMonitor progressMonitor; | |
85 | |
86 /** | |
87 * The display used for event dispatching. | |
88 */ | |
89 private Display display; | |
90 | |
91 /** | |
92 * Indicates whether to continue event queue dispatching. | |
93 */ | |
94 private /+volatile+/ bool continueEventDispatching = true; | |
95 | |
96 /** | |
97 * The thread that forked this modal context thread. | |
98 * | |
99 * @since 3.1 | |
100 */ | |
101 private Thread callingThread; | |
102 | |
103 /** | |
104 * Creates a new modal context. | |
105 * | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
106 * @param operation |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
107 * the runnable to run |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
108 * @param monitor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
109 * the progress monitor to use to display progress and |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
110 * receive requests for cancelation |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
111 * @param display |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
112 * the display to be used to read and dispatch events |
6 | 113 */ |
114 private this(IRunnableWithProgress operation, | |
115 IProgressMonitor monitor, Display display) { | |
56 | 116 super(&run2); //$NON-NLS-1$ |
6 | 117 Assert.isTrue(monitor !is null && display !is null); |
118 runnable = operation; | |
119 progressMonitor = new AccumulatingProgressMonitor(monitor, display); | |
120 this.display = display; | |
121 this.callingThread = Thread.getThis(); | |
122 } | |
123 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
124 /* |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
125 * (non-Javadoc) Method declared on Thread. |
6 | 126 */ |
56 | 127 public /+override+/ void run2() { |
6 | 128 try { |
129 if (runnable !is null) { | |
130 runnable.run(progressMonitor); | |
131 } | |
132 /+ | |
133 } catch (InvocationTargetException e) { | |
134 throwable = e; | |
135 } catch (InterruptedException e) { | |
136 throwable = e; | |
137 } catch (RuntimeException e) { | |
138 throwable = e; | |
139 } catch (ThreadDeath e) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
140 // Make sure to propagate ThreadDeath, or threads will never |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
141 // fully terminate |
6 | 142 throw e; |
143 +/ | |
144 } catch (/+Error+/Exception e) { | |
145 throwable = e; | |
146 } finally { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
147 // notify the operation of change of thread of control |
6 | 148 if ( auto tl = cast(IThreadListener)runnable ) { |
71 | 149 auto exception = |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
150 invokeThreadListener(tl, callingThread); |
71 | 151 |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
152 //Forward it if we don't already have one |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
153 if(exception !is null && throwable is null) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
154 throwable = exception; |
6 | 155 } |
156 | |
157 // Make sure that all events in the asynchronous event queue | |
158 // are dispatched. | |
159 display.syncExec(new class() Runnable { | |
160 public void run() { | |
161 // do nothing | |
162 } | |
163 }); | |
164 | |
165 // Stop event dispatching | |
166 continueEventDispatching = false; | |
167 | |
168 // Force the event loop to return from sleep () so that | |
169 // it stops event dispatching. | |
170 display.asyncExec(null); | |
171 } | |
172 } | |
173 | |
174 /** | |
175 * Processes events or waits until this modal context thread terminates. | |
176 */ | |
177 public void block() { | |
178 if (display is Display.getCurrent()) { | |
179 while (continueEventDispatching) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
180 // Run the event loop. Handle any uncaught exceptions caused |
6 | 181 // by UI events. |
182 try { | |
183 if (!display.readAndDispatch()) { | |
184 display.sleep(); | |
185 } | |
186 } | |
187 /+ | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
188 // ThreadDeath is a normal error when the thread is dying. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
189 // We must |
6 | 190 // propagate it in order for it to properly terminate. |
191 catch (ThreadDeath e) { | |
192 throw (e); | |
193 } | |
194 +/ | |
195 // For all other exceptions, log the problem. | |
196 catch (Exception e) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
197 Policy |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
198 .getLog() |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
199 .log( |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
200 new Status( |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
201 IStatus.ERROR, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
202 Policy.JFACE, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
203 "Unhandled event loop exception during blocked modal context.",//$NON-NLS-1$ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
204 e)); |
6 | 205 } |
206 } | |
207 } else { | |
208 try { | |
209 join(); | |
210 } catch (Exception e) { | |
211 throwable = e; | |
212 } | |
213 } | |
214 } | |
215 } | |
216 | |
217 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
218 * Returns whether the first progress monitor is the same as, or a wrapper |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
219 * around, the second progress monitor. |
71 | 220 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
221 * @param monitor1 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
222 * the first progress monitor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
223 * @param monitor2 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
224 * the second progress monitor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
225 * @return <code>true</code> if the first is the same as, or a wrapper |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
226 * around, the second |
6 | 227 * @see ProgressMonitorWrapper |
228 */ | |
229 public static bool canProgressMonitorBeUsed(IProgressMonitor monitor1, | |
230 IProgressMonitor monitor2) { | |
231 if (monitor1 is monitor2) { | |
232 return true; | |
233 } | |
234 | |
235 while ( cast(ProgressMonitorWrapper)monitor1 ) { | |
236 monitor1 = (cast(ProgressMonitorWrapper)monitor1) | |
237 .getWrappedProgressMonitor(); | |
238 if (monitor1 is monitor2) { | |
239 return true; | |
240 } | |
241 } | |
242 return false; | |
243 } | |
244 | |
245 /** | |
246 * Checks with the given progress monitor and throws | |
247 * <code>InterruptedException</code> if it has been canceled. | |
248 * <p> | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
249 * Code in a long-running operation should call this method regularly so |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
250 * that a request to cancel will be honored. |
6 | 251 * </p> |
252 * <p> | |
253 * Convenience for: | |
71 | 254 * |
6 | 255 * <pre> |
256 * if (monitor.isCanceled()) | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
257 * throw new InterruptedException(); |
6 | 258 * </pre> |
71 | 259 * |
6 | 260 * </p> |
71 | 261 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
262 * @param monitor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
263 * the progress monitor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
264 * @exception InterruptedException |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
265 * if cancelling the operation has been requested |
6 | 266 * @see IProgressMonitor#isCanceled() |
267 */ | |
268 public static void checkCanceled(IProgressMonitor monitor) { | |
269 if (monitor.isCanceled()) { | |
270 throw new InterruptedException(); | |
271 } | |
272 } | |
273 | |
274 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
275 * Returns the currently active modal context thread, or null if no modal |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
276 * context is active. |
6 | 277 */ |
278 private static ModalContextThread getCurrentModalContextThread() { | |
279 Thread t = Thread.getThis(); | |
280 if ( auto r = cast(ModalContextThread)t ) { | |
281 return r; | |
282 } | |
283 return null; | |
284 } | |
285 | |
286 /** | |
287 * Returns the modal nesting level. | |
288 * <p> | |
289 * The modal nesting level increases by one each time the | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
290 * <code>ModalContext.run</code> method is called within the dynamic scope |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
291 * of another call to <code>ModalContext.run</code>. |
6 | 292 * </p> |
71 | 293 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
294 * @return the modal nesting level, or <code>0</code> if this method is |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
295 * called outside the dynamic scope of any invocation of |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
296 * <code>ModalContext.run</code> |
6 | 297 */ |
298 public static int getModalLevel() { | |
299 return modalLevel; | |
300 } | |
301 | |
302 /** | |
303 * Returns whether the given thread is running a modal context. | |
304 * | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
305 * @param thread |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
306 * The thread to be checked |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
307 * @return <code>true</code> if the given thread is running a modal |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
308 * context, <code>false</code> if not |
6 | 309 */ |
310 public static bool isModalContextThread(Thread thread) { | |
311 return (cast(ModalContextThread)thread) !is null; | |
312 } | |
313 | |
314 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
315 * Runs the given runnable in a modal context, passing it a progress |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
316 * monitor. |
6 | 317 * <p> |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
318 * The modal nesting level is increased by one from the perspective of the |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
319 * given runnable. |
6 | 320 * </p> |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
321 * <p> |
6 | 322 * If the supplied operation implements <code>IThreadListener</code>, it |
323 * will be notified of any thread changes required to execute the operation. | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
324 * Specifically, the operation will be notified of the thread that will call |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
325 * its <code>run</code> method before it is called, and will be notified |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
326 * of the change of control back to the thread calling this method when the |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
327 * operation completes. These thread change notifications give the operation |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
328 * an opportunity to transfer any thread-local state to the execution thread |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
329 * before control is transferred to the new thread. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
330 * </p> |
71 | 331 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
332 * @param operation |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
333 * the runnable to run |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
334 * @param fork |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
335 * <code>true</code> if the runnable should run in a separate |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
336 * thread, and <code>false</code> if in the same thread |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
337 * @param monitor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
338 * the progress monitor to use to display progress and receive |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
339 * requests for cancelation |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
340 * @param display |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
341 * the display to be used to read and dispatch events |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
342 * @exception InvocationTargetException |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
343 * if the run method must propagate a checked exception, it |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
344 * should wrap it inside an |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
345 * <code>InvocationTargetException</code>; runtime |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
346 * exceptions and errors are automatically wrapped in an |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
347 * <code>InvocationTargetException</code> by this method |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
348 * @exception InterruptedException |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
349 * if the operation detects a request to cancel, using |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
350 * <code>IProgressMonitor.isCanceled()</code>, it should |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
351 * exit by throwing <code>InterruptedException</code>; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
352 * this method propagates the exception |
6 | 353 */ |
354 public static void run(IRunnableWithProgress operation, bool fork, | |
355 IProgressMonitor monitor, Display display) { | |
356 Assert.isTrue(operation !is null && monitor !is null); | |
357 | |
358 modalLevel++; | |
359 try { | |
360 if (monitor !is null) { | |
361 monitor.setCanceled(false); | |
362 } | |
363 // Is the runnable supposed to be execute in the same thread. | |
364 if (!fork || !runInSeparateThread) { | |
365 runInCurrentThread(operation, monitor); | |
366 } else { | |
367 ModalContextThread t = getCurrentModalContextThread(); | |
368 if (t !is null) { | |
369 Assert.isTrue(canProgressMonitorBeUsed(monitor, | |
370 t.progressMonitor)); | |
371 runInCurrentThread(operation, monitor); | |
372 } else { | |
373 t = new ModalContextThread(operation, monitor, display); | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
374 Exception listenerException = null; |
6 | 375 if ( auto tl = cast(IThreadListener)operation ) { |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
376 listenerException = invokeThreadListener(tl, t); |
6 | 377 } |
71 | 378 |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
379 if(listenerException is null){ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
380 t.start(); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
381 t.block(); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
382 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
383 else { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
384 if(t.throwable is null) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
385 t.throwable = listenerException; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
386 } |
6 | 387 Exception throwable = t.throwable; |
388 if (throwable !is null) { | |
389 if (debug_ | |
390 && !(cast(InterruptedException)throwable ) | |
391 && !(cast(OperationCanceledException)throwable )) { | |
392 Stderr.formatln("Exception in modal context operation:"); //$NON-NLS-1$ | |
393 ExceptionPrintStackTrace(throwable); | |
394 Stderr.formatln("Called from:"); //$NON-NLS-1$ | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
395 // Don't create the InvocationTargetException on the |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
396 // throwable, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
397 // otherwise it will print its stack trace (from the |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
398 // other thread). |
6 | 399 ExceptionPrintStackTrace( new InvocationTargetException(null)); |
400 } | |
401 if (cast(InvocationTargetException)throwable ) { | |
402 throw cast(InvocationTargetException) throwable; | |
403 } else if (cast(InterruptedException)throwable ) { | |
404 throw cast(InterruptedException) throwable; | |
405 } else if (cast(OperationCanceledException)throwable ) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
406 // See 1GAN3L5: ITPUI:WIN2000 - ModalContext |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
407 // converts OperationCancelException into |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
408 // InvocationTargetException |
6 | 409 throw new InterruptedException(throwable |
410 .msg); | |
411 } else { | |
412 throw new InvocationTargetException(throwable); | |
413 } | |
414 } | |
415 } | |
416 } | |
417 } finally { | |
418 modalLevel--; | |
419 } | |
420 } | |
421 | |
422 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
423 * Invoke the ThreadListener if there are any errors or RuntimeExceptions |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
424 * return them. |
71 | 425 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
426 * @param listener |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
427 * @param switchingThread |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
428 * the {@link Thread} being switched to |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
429 */ |
71 | 430 static Exception invokeThreadListener(IThreadListener listener, |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
431 Thread switchingThread) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
432 try { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
433 listener.threadChange(switchingThread); |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
434 // } catch (ThreadDeath e) { |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
435 // Make sure to propagate ThreadDeath, or threads will never |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
436 // fully terminate |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
437 // throw e; |
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
438 // } catch (Error e) { |
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
439 // return e; |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
440 }catch (RuntimeException e) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
441 return e; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
442 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
443 return null; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
444 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
445 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
446 /** |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
447 * Run a runnable. Convert all thrown exceptions to either |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
448 * InterruptedException or InvocationTargetException |
6 | 449 */ |
450 private static void runInCurrentThread(IRunnableWithProgress runnable, | |
451 IProgressMonitor progressMonitor) { | |
452 try { | |
453 if (runnable !is null) { | |
454 runnable.run(progressMonitor); | |
455 } | |
456 } catch (InvocationTargetException e) { | |
457 throw e; | |
458 } catch (InterruptedException e) { | |
459 throw e; | |
460 } catch (OperationCanceledException e) { | |
461 throw new InterruptedException(); | |
462 /+ | |
463 } catch (ThreadDeath e) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
464 // Make sure to propagate ThreadDeath, or threads will never fully |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
465 // terminate |
6 | 466 throw e; |
467 +/ | |
468 } catch (RuntimeException e) { | |
469 throw new InvocationTargetException(e); | |
470 } catch (/+Error+/Exception e) { | |
471 throw new InvocationTargetException(e); | |
472 } | |
473 } | |
474 | |
475 /** | |
476 * Sets whether ModalContext is running in debug mode. | |
71 | 477 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
478 * @param debugMode |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
479 * <code>true</code> for debug mode, and <code>false</code> |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
480 * for normal mode (the default) |
6 | 481 */ |
482 public static void setDebugMode(bool debugMode) { | |
483 debug_ = debugMode; | |
484 } | |
485 | |
486 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
487 * Sets whether ModalContext may process events (by calling |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
488 * <code>Display.readAndDispatch()</code>) while running operations. By |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
489 * default, ModalContext will process events while running operations. Use |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
490 * this method to disallow event processing temporarily. |
71 | 491 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
492 * @param allowReadAndDispatch |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
493 * <code>true</code> (the default) if events may be processed |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
494 * while running an operation, <code>false</code> if |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
495 * Display.readAndDispatch() should not be called from |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
496 * ModalContext. |
6 | 497 * @since 3.2 |
498 */ | |
499 public static void setAllowReadAndDispatch(bool allowReadAndDispatch) { | |
500 // use a separate thread if and only if it is OK to spin the event loop | |
501 runInSeparateThread = allowReadAndDispatch; | |
502 } | |
503 } |