Skip to main content
summaryrefslogtreecommitdiffstats
blob: 94e50024220f5c37b338797736e91b2d7cca9e06 (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
/*******************************************************************************
 * Copyright (c) 2004, 2005 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
 *******************************************************************************/
/*
 *  $RCSfile: UIRunner.java,v $
 *  $Revision: 1.4 $  $Date: 2005/08/24 20:39:05 $ 
 */
package org.eclipse.jem.internal.proxy.core;

import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;

import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
 

/**
 * The actual implementation of IUIRunner to run the build under
 * IProgressService control if in the UI thread.
 * 
 * <package-protected> because should only be used within here.
 * 
 * @since 1.0.0
 */
class UIRunner implements IUIRunner {
	
	/*
	 * Special class that takes a progress monitor 1 and only handles isCanceled from it,
	 * but everything else is forwarded to progress monitor 2. This allows the pm that
	 * is sent into handleBuild to signal a cancel even though the progress service
	 * sends in its own pm. 
	 * 
	 * @since 1.0.0
	 */
	private static class UIRunnerProgressMonitor extends ProgressMonitorWrapper {
		private IProgressMonitor pmcancel;
		
		public UIRunnerProgressMonitor(IProgressMonitor pmcancel, IProgressMonitor pmmain) {
			super(pmmain);
			this.pmcancel = pmcancel;
		}
		
		
		/* (non-Javadoc)
		 * @see org.eclipse.core.runtime.IProgressMonitor#isCanceled()
		 */
		public boolean isCanceled() {
			return pmcancel.isCanceled() || super.isCanceled();
		}
		
		/* (non-Javadoc)
		 * @see org.eclipse.core.runtime.IProgressMonitor#setCanceled(boolean)
		 */
		public void setCanceled(boolean value) {
			super.setCanceled(value);
			pmcancel.setCanceled(value);	// Cancel it too now.
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jem.internal.proxy.core.IUIRunner#handleBuild(org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void handleBuild(final IProgressMonitor pm) throws CoreException {
		if (!PlatformUI.isWorkbenchRunning() || Display.getCurrent() == null) {
			ProxyLaunchSupport.runBuild(pm);
		} else {
			pm.beginTask("", 100); //$NON-NLS-1$
			try {
				PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {

					public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
						IProgressMonitor uipm = new UIRunnerProgressMonitor(pm, monitor); 
						try {
							ProxyLaunchSupport.runBuild(uipm);
						} catch (CoreException e) {
							throw new InvocationTargetException(e);
						}
						if (uipm.isCanceled()) {
							pm.setCanceled(true);	// Make sure that cancel got through (could of come from monitor instead).
							throw new InterruptedException();
						}
					}
				});
			} catch (InvocationTargetException e) {
				if (e.getCause() instanceof CoreException)
					throw (CoreException) e.getCause();
				ProxyPlugin.getPlugin().getLogger().log(e.getCause(), Level.WARNING);
			} catch (InterruptedException e) {
				// It was canceled, launch will be canceled too. pm is already marked canceled and caller can check that
			}
			pm.done();
		}
	}
}

Back to the top