Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2b2fdfc397144d01e920704348aee9b2668a4ad2 (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
/*******************************************************************************
 * Copyright (c) 2002 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 * IBM - Initial API and implementation
 ******************************************************************************/

package org.eclipse.team.internal.ccvs.ui;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.team.core.TeamException;
import org.eclipse.ui.IMarkerResolutionGenerator;
import org.eclipse.ui.IWorkbenchWindow;

public abstract class CVSAbstractResolutionGenerator implements IMarkerResolutionGenerator {
	protected Shell getShell() {
		IWorkbenchWindow window = CVSUIPlugin.getPlugin().getWorkbench().getActiveWorkbenchWindow();
		if (window != null) {
			return window.getShell();
		} else {
			Display display = Display.getCurrent();
			return new Shell(display);
		}
	}
	
	protected void run(final IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException {
		final Exception[] exception = new Exception[] {null};
		Display.getDefault().syncExec(new Runnable() {
			public void run() {
				try {
					new ProgressMonitorDialog(getShell()).run(true, true, runnable);
				} catch (InterruptedException e) {
					exception[0] = e;
				} catch (InvocationTargetException e) {
					exception[0] = e;
				}
			}
		});
		if (exception[0] != null) {
			if (exception[0] instanceof InvocationTargetException) {
				throw (InvocationTargetException)exception[0];
			} else if (exception[0] instanceof InterruptedException) {
				throw (InterruptedException)exception[0];
			} else {
				throw new InvocationTargetException(exception[0]);
			}
		}
	}
	
	/**
	 * Shows the given errors to the user.
	 * 
	 * @param status  the status containing the error
	 * @param title  the title of the error dialog
	 * @param message  the message for the error dialog
	 * @param shell  the shell to open the error dialog in
	 */
	protected void handle(Throwable exception, String title, final String message) {		
		// Handle the target exception for InvocationTargetExceptions
		if (exception instanceof InvocationTargetException) {
			handle(((InvocationTargetException)exception).getTargetException(), title, message);
			return;
		}
		
		// Create a status to be displayed for the exception
		IStatus status = null;
		boolean log = false;
		boolean dialog = false;
		if (exception instanceof TeamException) {
			status = ((TeamException)exception).getStatus();
			log = false;
			dialog = true;
		} else if (exception instanceof CoreException) {
			status = ((CoreException)exception).getStatus();
			log = true;
			dialog = true;
		} else if (exception instanceof InterruptedException) {
			return;
		} else {
			status = new Status(IStatus.ERROR, CVSUIPlugin.ID, 1, Policy.bind("TeamAction.internal"), exception); //$NON-NLS-1$
			log = true;
			dialog = true;
		}
		
		// Display and/or log as appropriate
		if (status == null) return;
		if (!status.isOK()) {
			IStatus toShow = status;
			if (status.isMultiStatus()) {
				IStatus[] children = status.getChildren();
				if (children.length == 1) {
					toShow = children[0];
				}
			}
			if (title == null) title = status.getMessage();
			if (dialog) {
				final IStatus showStatus = toShow;
				final String displayTitle = title;
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						ErrorDialog.openError(getShell(), displayTitle, message, showStatus);
					}
				});
			}
			if (log) CVSUIPlugin.log(toShow);
		}
	}
}

Back to the top