Skip to main content
summaryrefslogtreecommitdiffstats
blob: b2df5b22edbabe1f703fd0fa9dc61994e8471520 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers 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
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylyn.commons.core.IStatusHandler;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.tasks.ui.util.WebBrowserDialog;
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
 * @author Mik Kersten
 * @author Rob Elves
 */
public class RepositoryAwareStatusHandler implements IStatusHandler {

	protected static final String ERROR_MESSAGE = "Please report the following error at:\n"
			+ "http://bugs.eclipse.org/bugs/enter_bug.cgi?product=Mylyn\n\n"
			+ "Or via the popup menu in the Error Log view (see Window -> Show View)";

	/**
	 * Used to ensure that only one dialog is open.
	 */
	private static boolean errorDialogOpen = false;

	private static RepositoryAwareStatusHandler instance;

	public synchronized static RepositoryAwareStatusHandler getInstance() {
		if (instance == null) {
			new RepositoryAwareStatusHandler();
		}
		return instance;
	}

	public RepositoryAwareStatusHandler() {
		instance = this;
	}

	private MessageDialog createDialog(Shell shell, String title, String message, int type) {
		return new MessageDialog(shell, title, null, message, type, new String[] { IDialogConstants.OK_LABEL }, 0);
	}

	public void displayStatus(final String title, final IStatus status) {

		if (status.getCode() == RepositoryStatus.ERROR_INTERNAL) {
			StatusHandler.log(status);
			fail(status, true);
			return;
		}

		if (Platform.isRunning()) {
			try {
				PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
					public void run() {
						Shell shell = null;
						if (PlatformUI.getWorkbench() != null
								&& PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null) {
							shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
						}

						if (status instanceof RepositoryStatus && ((RepositoryStatus) status).isHtmlMessage()) {
							WebBrowserDialog.openAcceptAgreement(shell, title, status.getMessage(),
									((RepositoryStatus) status).getHtmlMessage());
							return;
						}

						switch (status.getSeverity()) {
						case IStatus.CANCEL:
						case IStatus.INFO:
							createDialog(shell, title, status.getMessage(), MessageDialog.INFORMATION).open();
							break;
						case IStatus.WARNING:
							createDialog(shell, title, status.getMessage(), MessageDialog.WARNING).open();
							break;
						case IStatus.ERROR:
						default:
							createDialog(shell, title, status.getMessage(), MessageDialog.ERROR).open();
							break;
						}
					}
				});
			} catch (Throwable t) {
				status.getException().printStackTrace();
			}
		}
	}

	public void fail(final IStatus status, boolean informUser) {
		if (informUser && Platform.isRunning()) {
			try {
				PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
					public void run() {
						Shell shell = null;
						if (PlatformUI.getWorkbench() != null
								&& PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null) {
							shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
						}

						// ensure that only one dialog can be open at a time
						synchronized (shell) {
							try {
								if (!errorDialogOpen) {
									errorDialogOpen = true;
									ErrorDialog.openError(shell, "Mylyn Error", ERROR_MESSAGE, status);
								}
							} finally {
								errorDialogOpen = false;
							}
						}
					}
				});
			} catch (Throwable t) {
				status.getException().printStackTrace();
			}
		}
	}

}

Back to the top