comparison 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
comparison
equal deleted inserted replaced
163:e5dd0081ccba 167:862b05e0334a
25 import dwtx.jface.operation.IRunnableWithProgress; 25 import dwtx.jface.operation.IRunnableWithProgress;
26 import dwtx.jface.operation.AccumulatingProgressMonitor; 26 import dwtx.jface.operation.AccumulatingProgressMonitor;
27 27
28 import dwt.dwthelper.utils; 28 import dwt.dwthelper.utils;
29 import dwt.dwthelper.Runnable; 29 import dwt.dwthelper.Runnable;
30 import tango.core.Thread; 30 import dwtx.dwtxhelper.JThread;
31 import tango.io.Stdout; 31 import tango.io.Stdout;
32 32
33 /** 33 /**
34 * Utility class for supporting modal operations. The runnable passed to the 34 * Utility class for supporting modal operations. The runnable passed to the
35 * <code>run</code> method is executed in a separate thread, depending on the 35 * <code>run</code> method is executed in a separate thread, depending on the
65 private static bool runInSeparateThread = true; 65 private static bool runInSeparateThread = true;
66 66
67 /** 67 /**
68 * Thread which runs the modal context. 68 * Thread which runs the modal context.
69 */ 69 */
70 private static class ModalContextThread : Thread { 70 private static class ModalContextThread : JThread {
71 /** 71 /**
72 * The operation to be run. 72 * The operation to be run.
73 */ 73 */
74 private IRunnableWithProgress runnable; 74 private IRunnableWithProgress runnable;
75 75
96 /** 96 /**
97 * The thread that forked this modal context thread. 97 * The thread that forked this modal context thread.
98 * 98 *
99 * @since 3.1 99 * @since 3.1
100 */ 100 */
101 private Thread callingThread; 101 private JThread callingThread;
102 102
103 /** 103 /**
104 * Creates a new modal context. 104 * Creates a new modal context.
105 * 105 *
106 * @param operation 106 * @param operation
111 * @param display 111 * @param display
112 * the display to be used to read and dispatch events 112 * the display to be used to read and dispatch events
113 */ 113 */
114 private this(IRunnableWithProgress operation, 114 private this(IRunnableWithProgress operation,
115 IProgressMonitor monitor, Display display) { 115 IProgressMonitor monitor, Display display) {
116 super(&run2); //$NON-NLS-1$ 116 super(); //$NON-NLS-1$
117 Assert.isTrue(monitor !is null && display !is null); 117 Assert.isTrue(monitor !is null && display !is null);
118 runnable = operation; 118 runnable = operation;
119 progressMonitor = new AccumulatingProgressMonitor(monitor, display); 119 progressMonitor = new AccumulatingProgressMonitor(monitor, display);
120 this.display = display; 120 this.display = display;
121 this.callingThread = Thread.getThis(); 121 this.callingThread = JThread.currentThread();
122 } 122 }
123 123
124 /* 124 /*
125 * (non-Javadoc) Method declared on Thread. 125 * (non-Javadoc) Method declared on Thread.
126 */ 126 */
127 public /+override+/ void run2() { 127 public override void run() {
128 try { 128 try {
129 if (runnable !is null) { 129 if (runnable !is null) {
130 runnable.run(progressMonitor); 130 runnable.run(progressMonitor);
131 } 131 }
132 /+ 132 /+
154 throwable = exception; 154 throwable = exception;
155 } 155 }
156 156
157 // Make sure that all events in the asynchronous event queue 157 // Make sure that all events in the asynchronous event queue
158 // are dispatched. 158 // are dispatched.
159 display.syncExec(new class() Runnable { 159 display.syncExec(dgRunnable( {
160 public void run() { 160 // do nothing
161 // do nothing 161 } ));
162 }
163 });
164 162
165 // Stop event dispatching 163 // Stop event dispatching
166 continueEventDispatching = false; 164 continueEventDispatching = false;
167 165
168 // Force the event loop to return from sleep () so that 166 // Force the event loop to return from sleep () so that
274 /** 272 /**
275 * Returns the currently active modal context thread, or null if no modal 273 * Returns the currently active modal context thread, or null if no modal
276 * context is active. 274 * context is active.
277 */ 275 */
278 private static ModalContextThread getCurrentModalContextThread() { 276 private static ModalContextThread getCurrentModalContextThread() {
279 Thread t = Thread.getThis(); 277 JThread t = JThread.currentThread();
280 if ( auto r = cast(ModalContextThread)t ) { 278 if ( auto r = cast(ModalContextThread)t ) {
281 return r; 279 return r;
282 } 280 }
283 return null; 281 return null;
284 } 282 }
305 * @param thread 303 * @param thread
306 * The thread to be checked 304 * The thread to be checked
307 * @return <code>true</code> if the given thread is running a modal 305 * @return <code>true</code> if the given thread is running a modal
308 * context, <code>false</code> if not 306 * context, <code>false</code> if not
309 */ 307 */
310 public static bool isModalContextThread(Thread thread) { 308 public static bool isModalContextThread(JThread thread) {
311 return (cast(ModalContextThread)thread) !is null; 309 return (cast(ModalContextThread)thread) !is null;
312 } 310 }
313 311
314 /** 312 /**
315 * Runs the given runnable in a modal context, passing it a progress 313 * Runs the given runnable in a modal context, passing it a progress
426 * @param listener 424 * @param listener
427 * @param switchingThread 425 * @param switchingThread
428 * the {@link Thread} being switched to 426 * the {@link Thread} being switched to
429 */ 427 */
430 static Exception invokeThreadListener(IThreadListener listener, 428 static Exception invokeThreadListener(IThreadListener listener,
431 Thread switchingThread) { 429 JThread switchingThread) {
432 try { 430 try {
433 listener.threadChange(switchingThread); 431 listener.threadChange(switchingThread);
434 // } catch (ThreadDeath e) { 432 // } catch (ThreadDeath e) {
435 // Make sure to propagate ThreadDeath, or threads will never 433 // Make sure to propagate ThreadDeath, or threads will never
436 // fully terminate 434 // fully terminate