Mercurial > projects > dwt-addons
annotate dwtx/jface/operation/ModalContext.d @ 167:862b05e0334a
Add a wrapper for Thread
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 09 Sep 2008 15:59:16 +0200 |
parents | 7ffeace6c47f |
children |
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; | |
167 | 30 import dwtx.dwtxhelper.JThread; |
6 | 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 */ | |
167 | 70 private static class ModalContextThread : JThread { |
6 | 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 */ | |
167 | 101 private JThread callingThread; |
6 | 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) { | |
167 | 116 super(); //$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; | |
167 | 121 this.callingThread = JThread.currentThread(); |
6 | 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 */ |
167 | 127 public override void run() { |
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. | |
167 | 159 display.syncExec(dgRunnable( { |
160 // do nothing | |
161 } )); | |
6 | 162 |
163 // Stop event dispatching | |
164 continueEventDispatching = false; | |
165 | |
166 // Force the event loop to return from sleep () so that | |
167 // it stops event dispatching. | |
168 display.asyncExec(null); | |
169 } | |
170 } | |
171 | |
172 /** | |
173 * Processes events or waits until this modal context thread terminates. | |
174 */ | |
175 public void block() { | |
176 if (display is Display.getCurrent()) { | |
177 while (continueEventDispatching) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
178 // Run the event loop. Handle any uncaught exceptions caused |
6 | 179 // by UI events. |
180 try { | |
181 if (!display.readAndDispatch()) { | |
182 display.sleep(); | |
183 } | |
184 } | |
185 /+ | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
186 // 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
|
187 // We must |
6 | 188 // propagate it in order for it to properly terminate. |
189 catch (ThreadDeath e) { | |
190 throw (e); | |
191 } | |
192 +/ | |
193 // For all other exceptions, log the problem. | |
194 catch (Exception e) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
195 Policy |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
196 .getLog() |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
197 .log( |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
198 new Status( |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
199 IStatus.ERROR, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
200 Policy.JFACE, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
201 "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
|
202 e)); |
6 | 203 } |
204 } | |
205 } else { | |
206 try { | |
207 join(); | |
208 } catch (Exception e) { | |
209 throwable = e; | |
210 } | |
211 } | |
212 } | |
213 } | |
214 | |
215 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
216 * 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
|
217 * around, the second progress monitor. |
71 | 218 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
219 * @param monitor1 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
220 * the first progress monitor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
221 * @param monitor2 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
222 * the second progress monitor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
223 * @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
|
224 * around, the second |
6 | 225 * @see ProgressMonitorWrapper |
226 */ | |
227 public static bool canProgressMonitorBeUsed(IProgressMonitor monitor1, | |
228 IProgressMonitor monitor2) { | |
229 if (monitor1 is monitor2) { | |
230 return true; | |
231 } | |
232 | |
233 while ( cast(ProgressMonitorWrapper)monitor1 ) { | |
234 monitor1 = (cast(ProgressMonitorWrapper)monitor1) | |
235 .getWrappedProgressMonitor(); | |
236 if (monitor1 is monitor2) { | |
237 return true; | |
238 } | |
239 } | |
240 return false; | |
241 } | |
242 | |
243 /** | |
244 * Checks with the given progress monitor and throws | |
245 * <code>InterruptedException</code> if it has been canceled. | |
246 * <p> | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
247 * 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
|
248 * that a request to cancel will be honored. |
6 | 249 * </p> |
250 * <p> | |
251 * Convenience for: | |
71 | 252 * |
6 | 253 * <pre> |
254 * if (monitor.isCanceled()) | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
255 * throw new InterruptedException(); |
6 | 256 * </pre> |
71 | 257 * |
6 | 258 * </p> |
71 | 259 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
260 * @param monitor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
261 * the progress monitor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
262 * @exception InterruptedException |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
263 * if cancelling the operation has been requested |
6 | 264 * @see IProgressMonitor#isCanceled() |
265 */ | |
266 public static void checkCanceled(IProgressMonitor monitor) { | |
267 if (monitor.isCanceled()) { | |
268 throw new InterruptedException(); | |
269 } | |
270 } | |
271 | |
272 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
273 * 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
|
274 * context is active. |
6 | 275 */ |
276 private static ModalContextThread getCurrentModalContextThread() { | |
167 | 277 JThread t = JThread.currentThread(); |
6 | 278 if ( auto r = cast(ModalContextThread)t ) { |
279 return r; | |
280 } | |
281 return null; | |
282 } | |
283 | |
284 /** | |
285 * Returns the modal nesting level. | |
286 * <p> | |
287 * 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
|
288 * <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
|
289 * of another call to <code>ModalContext.run</code>. |
6 | 290 * </p> |
71 | 291 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
292 * @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
|
293 * 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
|
294 * <code>ModalContext.run</code> |
6 | 295 */ |
296 public static int getModalLevel() { | |
297 return modalLevel; | |
298 } | |
299 | |
300 /** | |
301 * Returns whether the given thread is running a modal context. | |
302 * | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
303 * @param thread |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
304 * The thread to be checked |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
305 * @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
|
306 * context, <code>false</code> if not |
6 | 307 */ |
167 | 308 public static bool isModalContextThread(JThread thread) { |
6 | 309 return (cast(ModalContextThread)thread) !is null; |
310 } | |
311 | |
312 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
313 * 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
|
314 * monitor. |
6 | 315 * <p> |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
316 * 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
|
317 * given runnable. |
6 | 318 * </p> |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
319 * <p> |
6 | 320 * If the supplied operation implements <code>IThreadListener</code>, it |
321 * 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
|
322 * 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
|
323 * 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
|
324 * 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
|
325 * 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
|
326 * 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
|
327 * 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
|
328 * </p> |
71 | 329 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
330 * @param operation |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
331 * the runnable to run |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
332 * @param fork |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
333 * <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
|
334 * 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
|
335 * @param monitor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
336 * 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
|
337 * requests for cancelation |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
338 * @param display |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
339 * 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
|
340 * @exception InvocationTargetException |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
341 * 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
|
342 * should wrap it inside an |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
343 * <code>InvocationTargetException</code>; runtime |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
344 * 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
|
345 * <code>InvocationTargetException</code> by this method |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
346 * @exception InterruptedException |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
347 * 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
|
348 * <code>IProgressMonitor.isCanceled()</code>, it should |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
349 * exit by throwing <code>InterruptedException</code>; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
350 * this method propagates the exception |
6 | 351 */ |
352 public static void run(IRunnableWithProgress operation, bool fork, | |
353 IProgressMonitor monitor, Display display) { | |
354 Assert.isTrue(operation !is null && monitor !is null); | |
355 | |
356 modalLevel++; | |
357 try { | |
358 if (monitor !is null) { | |
359 monitor.setCanceled(false); | |
360 } | |
361 // Is the runnable supposed to be execute in the same thread. | |
362 if (!fork || !runInSeparateThread) { | |
363 runInCurrentThread(operation, monitor); | |
364 } else { | |
365 ModalContextThread t = getCurrentModalContextThread(); | |
366 if (t !is null) { | |
367 Assert.isTrue(canProgressMonitorBeUsed(monitor, | |
368 t.progressMonitor)); | |
369 runInCurrentThread(operation, monitor); | |
370 } else { | |
371 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
|
372 Exception listenerException = null; |
6 | 373 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
|
374 listenerException = invokeThreadListener(tl, t); |
6 | 375 } |
71 | 376 |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
377 if(listenerException is null){ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
378 t.start(); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
379 t.block(); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
380 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
381 else { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
382 if(t.throwable is null) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
383 t.throwable = listenerException; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
384 } |
6 | 385 Exception throwable = t.throwable; |
386 if (throwable !is null) { | |
387 if (debug_ | |
388 && !(cast(InterruptedException)throwable ) | |
389 && !(cast(OperationCanceledException)throwable )) { | |
390 Stderr.formatln("Exception in modal context operation:"); //$NON-NLS-1$ | |
391 ExceptionPrintStackTrace(throwable); | |
392 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
|
393 // 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
|
394 // throwable, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
395 // 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
|
396 // other thread). |
6 | 397 ExceptionPrintStackTrace( new InvocationTargetException(null)); |
398 } | |
399 if (cast(InvocationTargetException)throwable ) { | |
400 throw cast(InvocationTargetException) throwable; | |
401 } else if (cast(InterruptedException)throwable ) { | |
402 throw cast(InterruptedException) throwable; | |
403 } else if (cast(OperationCanceledException)throwable ) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
404 // See 1GAN3L5: ITPUI:WIN2000 - ModalContext |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
405 // converts OperationCancelException into |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
406 // InvocationTargetException |
6 | 407 throw new InterruptedException(throwable |
408 .msg); | |
409 } else { | |
410 throw new InvocationTargetException(throwable); | |
411 } | |
412 } | |
413 } | |
414 } | |
415 } finally { | |
416 modalLevel--; | |
417 } | |
418 } | |
419 | |
420 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
421 * 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
|
422 * return them. |
71 | 423 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
424 * @param listener |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
425 * @param switchingThread |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
426 * the {@link Thread} being switched to |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
427 */ |
71 | 428 static Exception invokeThreadListener(IThreadListener listener, |
167 | 429 JThread switchingThread) { |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
430 try { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
431 listener.threadChange(switchingThread); |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
432 // } catch (ThreadDeath e) { |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
433 // 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
|
434 // fully terminate |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
435 // throw e; |
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
436 // } catch (Error e) { |
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
437 // return e; |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
438 }catch (RuntimeException e) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
439 return e; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
440 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
441 return null; |
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 |
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 * 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
|
446 * InterruptedException or InvocationTargetException |
6 | 447 */ |
448 private static void runInCurrentThread(IRunnableWithProgress runnable, | |
449 IProgressMonitor progressMonitor) { | |
450 try { | |
451 if (runnable !is null) { | |
452 runnable.run(progressMonitor); | |
453 } | |
454 } catch (InvocationTargetException e) { | |
455 throw e; | |
456 } catch (InterruptedException e) { | |
457 throw e; | |
458 } catch (OperationCanceledException e) { | |
459 throw new InterruptedException(); | |
460 /+ | |
461 } catch (ThreadDeath e) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
462 // 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
|
463 // terminate |
6 | 464 throw e; |
465 +/ | |
466 } catch (RuntimeException e) { | |
467 throw new InvocationTargetException(e); | |
468 } catch (/+Error+/Exception e) { | |
469 throw new InvocationTargetException(e); | |
470 } | |
471 } | |
472 | |
473 /** | |
474 * Sets whether ModalContext is running in debug mode. | |
71 | 475 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
476 * @param debugMode |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
477 * <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
|
478 * for normal mode (the default) |
6 | 479 */ |
480 public static void setDebugMode(bool debugMode) { | |
481 debug_ = debugMode; | |
482 } | |
483 | |
484 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
485 * 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
|
486 * <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
|
487 * 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
|
488 * this method to disallow event processing temporarily. |
71 | 489 * |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
490 * @param allowReadAndDispatch |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
491 * <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
|
492 * 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
|
493 * 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
|
494 * ModalContext. |
6 | 495 * @since 3.2 |
496 */ | |
497 public static void setAllowReadAndDispatch(bool allowReadAndDispatch) { | |
498 // use a separate thread if and only if it is OK to spin the event loop | |
499 runInSeparateThread = allowReadAndDispatch; | |
500 } | |
501 } |