KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > util > BusyIndicatorRunnableContext


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
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  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.util;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.swt.custom.BusyIndicator;
16
17 import org.eclipse.core.runtime.NullProgressMonitor;
18 import org.eclipse.core.runtime.OperationCanceledException;
19
20 import org.eclipse.jface.operation.IRunnableContext;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.jface.operation.ModalContext;
23
24 /**
25  * A runnable context that shows the busy cursor instead of a progress
26  * monitor. Note, that the UI thread is blocked even if the runnable
27  * is executed in a separate thread by passing <code>fork= true</code>
28  * to the context's run method. Furthermore this context doesn't provide
29  * any UI to cancel the operation.
30  */

31 public class BusyIndicatorRunnableContext implements IRunnableContext {
32
33     private static class BusyRunnable implements Runnable JavaDoc {
34         
35         private static class ThreadContext extends Thread JavaDoc {
36             IRunnableWithProgress fRunnable;
37             Throwable JavaDoc fThrowable;
38             
39             public ThreadContext(IRunnableWithProgress runnable) {
40                 this(runnable, "BusyCursorRunnableContext-Thread"); //$NON-NLS-1$
41
}
42             protected ThreadContext(IRunnableWithProgress runnable, String JavaDoc name) {
43                 super(name);
44                 fRunnable= runnable;
45             }
46             public void run() {
47                 try {
48                     fRunnable.run(new NullProgressMonitor());
49                 } catch (InvocationTargetException JavaDoc e) {
50                     fThrowable= e;
51                 } catch (InterruptedException JavaDoc e) {
52                     fThrowable= e;
53                 } catch (ThreadDeath JavaDoc e) {
54                     fThrowable= e;
55                     throw e;
56                 } catch (RuntimeException JavaDoc e) {
57                     fThrowable= e;
58                 } catch (Error JavaDoc e) {
59                     fThrowable= e;
60                 }
61             }
62             void sync() {
63                 try {
64                     join();
65                 } catch (InterruptedException JavaDoc e) {
66                     // ok to ignore exception
67
}
68             }
69         }
70         
71         public Throwable JavaDoc fThrowable;
72         private boolean fFork;
73         private IRunnableWithProgress fRunnable;
74         public BusyRunnable(boolean fork, IRunnableWithProgress runnable) {
75             fFork= fork;
76             fRunnable= runnable;
77         }
78         public void run() {
79             try {
80                 internalRun(fFork, fRunnable);
81             } catch (InvocationTargetException JavaDoc e) {
82                 fThrowable= e;
83             } catch (InterruptedException JavaDoc e) {
84                 fThrowable= e;
85             }
86         }
87         private void internalRun(boolean fork, final IRunnableWithProgress runnable) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
88             Thread JavaDoc thread= Thread.currentThread();
89             // Do not spawn another thread if we are already in a modal context
90
// thread or inside a busy context thread.
91
if (thread instanceof ThreadContext || ModalContext.isModalContextThread(thread))
92                 fork= false;
93                 
94             if (fork) {
95                 final ThreadContext t= new ThreadContext(runnable);
96                 t.start();
97                 t.sync();
98                 // Check if the separate thread was terminated by an exception
99
Throwable JavaDoc throwable= t.fThrowable;
100                 if (throwable != null) {
101                     if (throwable instanceof InvocationTargetException JavaDoc) {
102                         throw (InvocationTargetException JavaDoc) throwable;
103                     } else if (throwable instanceof InterruptedException JavaDoc) {
104                         throw (InterruptedException JavaDoc) throwable;
105                     } else if (throwable instanceof OperationCanceledException) {
106                         throw new InterruptedException JavaDoc();
107                     } else {
108                         throw new InvocationTargetException JavaDoc(throwable);
109                     }
110                 }
111             } else {
112                 try {
113                     runnable.run(new NullProgressMonitor());
114                 } catch (OperationCanceledException e) {
115                     throw new InterruptedException JavaDoc();
116                 }
117             }
118         }
119     }
120
121     /* (non-Javadoc)
122      * Method declared on IRunnableContext.
123      */

124     public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
125         BusyRunnable busyRunnable= new BusyRunnable(fork, runnable);
126         BusyIndicator.showWhile(null, busyRunnable);
127         Throwable JavaDoc throwable= busyRunnable.fThrowable;
128         if (throwable instanceof InvocationTargetException JavaDoc) {
129             throw (InvocationTargetException JavaDoc)throwable;
130         } else if (throwable instanceof InterruptedException JavaDoc) {
131             throw (InterruptedException JavaDoc)throwable;
132         }
133     }
134 }
135
Popular Tags