Mercurial > projects > dwt-addons
annotate dwtx/jface/operation/ModalContext.d @ 70:46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 01:36:46 +0200 |
parents | 84ce9636d109 |
children | 4878bef4a38e |
rev | line source |
---|---|
6 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
2 * Copyright (c) 2000, 2007 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> | |
43 */ | |
44 public class ModalContext { | |
45 | |
46 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
47 * 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
|
48 * default. |
6 | 49 */ |
65 | 50 private static bool debug_ = true; |
6 | 51 |
52 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
53 * 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
|
54 * global state. |
6 | 55 */ |
56 private static int modalLevel = 0; | |
57 | |
58 /** | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
59 * 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
|
60 * 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
|
61 * the calling thread. |
6 | 62 */ |
63 private static bool runInSeparateThread = true; | |
64 | |
65 /** | |
66 * Thread which runs the modal context. | |
67 */ | |
68 private static class ModalContextThread : Thread { | |
69 /** | |
70 * The operation to be run. | |
71 */ | |
72 private IRunnableWithProgress runnable; | |
73 | |
74 /** | |
75 * The exception thrown by the operation starter. | |
76 */ | |
77 private Exception throwable; | |
78 | |
79 /** | |
80 * The progress monitor used for progress and cancelation. | |
81 */ | |
82 private IProgressMonitor progressMonitor; | |
83 | |
84 /** | |
85 * The display used for event dispatching. | |
86 */ | |
87 private Display display; | |
88 | |
89 /** | |
90 * Indicates whether to continue event queue dispatching. | |
91 */ | |
92 private /+volatile+/ bool continueEventDispatching = true; | |
93 | |
94 /** | |
95 * The thread that forked this modal context thread. | |
96 * | |
97 * @since 3.1 | |
98 */ | |
99 private Thread callingThread; | |
100 | |
101 /** | |
102 * Creates a new modal context. | |
103 * | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
104 * @param operation |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
105 * the runnable to run |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
106 * @param monitor |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
107 * 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
|
108 * receive requests for cancelation |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
109 * @param display |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
110 * the display to be used to read and dispatch events |
6 | 111 */ |
112 private this(IRunnableWithProgress operation, | |
113 IProgressMonitor monitor, Display display) { | |
56 | 114 super(&run2); //$NON-NLS-1$ |
6 | 115 Assert.isTrue(monitor !is null && display !is null); |
116 runnable = operation; | |
117 progressMonitor = new AccumulatingProgressMonitor(monitor, display); | |
118 this.display = display; | |
119 this.callingThread = Thread.getThis(); | |
120 } | |
121 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
122 /* |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
123 * (non-Javadoc) Method declared on Thread. |
6 | 124 */ |
56 | 125 public /+override+/ void run2() { |
6 | 126 try { |
127 if (runnable !is null) { | |
128 runnable.run(progressMonitor); | |
129 } | |
130 /+ | |
131 } catch (InvocationTargetException e) { | |
132 throwable = e; | |
133 } catch (InterruptedException e) { | |
134 throwable = e; | |
135 } catch (RuntimeException e) { | |
136 throwable = e; | |
137 } catch (ThreadDeath e) { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
138 // 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
|
139 // fully terminate |
6 | 140 throw e; |
141 +/ | |
142 } catch (/+Error+/Exception e) { | |
143 throwable = e; | |
144 } finally { | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
145 // notify the operation of change of thread of control |
6 | 146 if ( auto tl = cast(IThreadListener)runnable ) { |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
147 auto exception = |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
148 invokeThreadListener(tl, callingThread); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
149 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
150 //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
|
151 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
|
152 throwable = exception; |
6 | 153 } |
154 | |
155 // Make sure that all events in the asynchronous event queue | |
156 // are dispatched. | |
157 display.syncExec(new class() Runnable { | |
158 public void run() { | |
159 // do nothing | |
160 } | |
161 }); | |
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. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
218 * |
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: | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
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> |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
257 * |
6 | 258 * </p> |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
259 * |
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() { | |
277 Thread t = Thread.getThis(); | |
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> |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
291 * |
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 */ |
308 public static bool isModalContextThread(Thread thread) { | |
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> |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
329 * |
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 } |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
376 |
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. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
423 * |
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 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
428 static Throwable invokeThreadListener(IThreadListener listener, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
429 Thread switchingThread) { |
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); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
432 } catch (ThreadDeath e) { |
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 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
435 throw e; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
436 } catch (Error e) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
437 return e; |
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. | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
475 * |
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. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
65
diff
changeset
|
489 * |
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 } |