Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a930543a4685902634b6b546bb41e50434d4cd54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.util;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.swt.custom.BusyIndicator;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;

import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.operation.ModalContext;

/**
 * A runnable context that shows the busy cursor instead of a progress
 * monitor. Note, that the UI thread is blocked even if the runnable
 * is executed in a separate thread by passing <code>fork= true</code>
 * to the context's run method. Furthermore this context doesn't provide
 * any UI to cancel the operation.
 */
public class BusyIndicatorRunnableContext implements IRunnableContext {

	private static class BusyRunnable implements Runnable {
		
		private static class ThreadContext extends Thread {
			IRunnableWithProgress fRunnable;
			Throwable fThrowable;
			
			public ThreadContext(IRunnableWithProgress runnable) {
				this(runnable, "BusyCursorRunnableContext-Thread"); //$NON-NLS-1$
			}			
			protected ThreadContext(IRunnableWithProgress runnable, String name) {
				super(name);
				fRunnable= runnable;
			}
			public void run() {
				try {
					fRunnable.run(new NullProgressMonitor());
				} catch (InvocationTargetException e) {
					fThrowable= e;
				} catch (InterruptedException e) {
					fThrowable= e;
				} catch (ThreadDeath e) {
					fThrowable= e;
					throw e;
				} catch (RuntimeException e) {
					fThrowable= e;
				} catch (Error e) {
					fThrowable= e;
				}
			}
			void sync() {
				try {
					join();
				} catch (InterruptedException e) {
					// ok to ignore exception
				}
			}
		}
		
		public Throwable fThrowable;
		private boolean fFork;
		private IRunnableWithProgress fRunnable;
		public BusyRunnable(boolean fork, IRunnableWithProgress runnable) {
			fFork= fork;
			fRunnable= runnable;
		}
		public void run() {
			try {
				internalRun(fFork, fRunnable);
			} catch (InvocationTargetException e) {
				fThrowable= e;
			} catch (InterruptedException e) {
				fThrowable= e;
			}
		}
		private void internalRun(boolean fork, final IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException {
			Thread thread= Thread.currentThread();
			// Do not spawn another thread if we are already in a modal context
			// thread or inside a busy context thread.
			if (thread instanceof ThreadContext || ModalContext.isModalContextThread(thread))
				fork= false;
				
			if (fork) {
				final ThreadContext t= new ThreadContext(runnable);
				t.start();
				t.sync();
				// Check if the separate thread was terminated by an exception
				Throwable throwable= t.fThrowable;
				if (throwable != null) {
					if (throwable instanceof InvocationTargetException) {
						throw (InvocationTargetException) throwable;
					} else if (throwable instanceof InterruptedException) {
						throw (InterruptedException) throwable;
					} else if (throwable instanceof OperationCanceledException) {
						throw new InterruptedException();
					} else {
						throw new InvocationTargetException(throwable);
					}
				}
			} else {
				try {
					runnable.run(new NullProgressMonitor());
				} catch (OperationCanceledException e) {
					throw new InterruptedException();
				}	
			}
		}
	}

	/* (non-Javadoc)
	 * Method declared on IRunnableContext.
	 */
	public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException {
		BusyRunnable busyRunnable= new BusyRunnable(fork, runnable);
		BusyIndicator.showWhile(null, busyRunnable);
		Throwable throwable= busyRunnable.fThrowable;
		if (throwable instanceof InvocationTargetException) {
			throw (InvocationTargetException)throwable;
		} else if (throwable instanceof InterruptedException) {
			throw (InterruptedException)throwable;
		}
	}	
}

Back to the top