Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1296154750c5da32bddde4ca03841147bf6f2dfa (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*******************************************************************************
 * Copyright (c) 2011 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.commons.workbench.browser;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.mylyn.commons.core.CoreUtil;
import org.eclipse.mylyn.commons.core.ExtensionPointReader;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.commons.workbench.EditorHandle;
import org.eclipse.mylyn.commons.workbench.WorkbenchUtil;
import org.eclipse.mylyn.internal.commons.workbench.CommonsWorkbenchPlugin;
import org.eclipse.mylyn.internal.commons.workbench.Messages;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.ui.internal.browser.WebBrowserPreference;
import org.eclipse.ui.internal.browser.WorkbenchBrowserSupport;

/**
 * Provides a utilities for opening locations in a browser.
 * 
 * @author Steffen Pingel
 */
public class BrowserUtil {

	static class BrowserHandle extends EditorHandle {

		private final IWebBrowser browser;

		public BrowserHandle(IWebBrowser browser) {
			super(Status.OK_STATUS);
			this.browser = browser;
		}

		@Override
		public Object getAdapter(@SuppressWarnings("rawtypes")
		Class adapter) {
			if (adapter == IWebBrowser.class) {
				return browser;
			}
			return super.getAdapter(adapter);
		}

	}

	static class UrlHandlerInitializer {

		private static List<AbstractUrlHandler> handlers;

		static {
			ExtensionPointReader<AbstractUrlHandler> reader = new ExtensionPointReader<AbstractUrlHandler>(
					CommonsWorkbenchPlugin.ID_PLUGIN, "urlHandlers", "handler", AbstractUrlHandler.class); //$NON-NLS-1$ //$NON-NLS-2$
			IStatus status = reader.read();
			if (!status.isOK()) {
				StatusHandler.log(status);
			}
			handlers = reader.getItems();
		}

	}

	/**
	 * Opens <code>location</code> in a rich editor if applicable or web-browser according to the workbench preferences.
	 * 
	 * @param location
	 *            the url to open
	 * @see #openUrl(IWorkbenchPage, String, int)
	 */
	public static void openUrl(String location) {
		openUrl(location, SWT.NONE);
	}

	/**
	 * Opens <code>location</code> in a rich editor if applicable or in a web-browser according to the workbench
	 * preferences.
	 * 
	 * @param location
	 *            the url to open
	 * @param customFlags
	 *            additional flags that are passed to {@link IWorkbenchBrowserSupport}, pass
	 *            {@link IWorkbenchBrowserSupport#AS_EXTERNAL} to force opening external browser
	 * @see #openUrl(IWorkbenchPage, String, int)
	 */
	public static void openUrl(String location, int customFlags) {
		IWorkbenchPage page = null;
		if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null) {
			page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		}
		openUrl(page, location, customFlags);
	}

	/**
	 * Opens <code>location</code> in a rich editor if applicable or in a web-browser according to the workbench
	 * preferences.
	 * 
	 * @param page
	 *            the workbench page to open the editor in
	 * @param location
	 *            the url to open
	 * @param customFlags
	 *            additional flags that are passed to {@link IWorkbenchBrowserSupport}, pass
	 *            {@link IWorkbenchBrowserSupport#AS_EXTERNAL} to force opening external browser
	 * @return a handle that describes the editor or browser that was opened; if {@link EditorHandle#getStatus()}
	 *         returns an error status the operation was not successful
	 */
	public static EditorHandle openUrl(IWorkbenchPage page, String location, int customFlags) {
		try {
			return openUrlInternal(page, location, customFlags);
		} catch (PartInitException e) {
			Status status = new Status(IStatus.ERROR, CommonsWorkbenchPlugin.ID_PLUGIN,
					Messages.WorkbenchUtil_Browser_Initialization_Failed, e);
			CommonsWorkbenchPlugin.getDefault().getLog().log(status);
			if (!CoreUtil.TEST_MODE) {
				MessageDialog.openError(WorkbenchUtil.getShell(), Messages.WorkbenchUtil_Open_Location_Title,
						status.getMessage());
			}
			return new EditorHandle(status);
		} catch (MalformedURLException e) {
			if (location != null && location.trim().equals("")) { //$NON-NLS-1$
				Status status = new Status(IStatus.WARNING, CommonsWorkbenchPlugin.ID_PLUGIN,
						Messages.WorkbenchUtil_No_URL_Error, e);
				if (!CoreUtil.TEST_MODE) {
					MessageDialog.openWarning(WorkbenchUtil.getShell(), Messages.WorkbenchUtil_Open_Location_Title,
							status.getMessage());
				} else {
					CommonsWorkbenchPlugin.getDefault().getLog().log(status);
				}
				return new EditorHandle(status);
			} else {
				Status status = new Status(IStatus.ERROR, CommonsWorkbenchPlugin.ID_PLUGIN, NLS.bind(
						Messages.WorkbenchUtil_Invalid_URL_Error, location), e);
				if (!CoreUtil.TEST_MODE) {
					MessageDialog.openError(WorkbenchUtil.getShell(), Messages.WorkbenchUtil_Open_Location_Title,
							status.getMessage());
				} else {
					CommonsWorkbenchPlugin.getDefault().getLog().log(status);
				}
				return new EditorHandle(status);
			}
		}
	}

	private static EditorHandle openUrlInternal(IWorkbenchPage page, String location, int customFlags)
			throws MalformedURLException, PartInitException {
		if (location != null && (customFlags & IWorkbenchBrowserSupport.AS_EXTERNAL) == 0) {
			// delegate to handler
			if (page != null) {
				EditorHandle handle = openUrlByHandler(page, location, customFlags);
				if (handle != null) {
					return handle;
				}
			}
		}

		URL url = null;
		if (location != null) {
			url = new URL(location);
		}

		if (WebBrowserPreference.getBrowserChoice() == WebBrowserPreference.EXTERNAL
				|| (customFlags & IWorkbenchBrowserSupport.AS_EXTERNAL) != 0) {
			// open in external browser
			IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
			final IWebBrowser browser = support.getExternalBrowser();
			browser.openURL(url);
			return new BrowserHandle(browser);
		} else {
			// open in internal browser
			int flags = customFlags;
			if (WorkbenchBrowserSupport.getInstance().isInternalWebBrowserAvailable()) {
				flags |= IWorkbenchBrowserSupport.AS_EDITOR | IWorkbenchBrowserSupport.LOCATION_BAR
						| IWorkbenchBrowserSupport.NAVIGATION_BAR;
			} else {
				flags |= IWorkbenchBrowserSupport.AS_EXTERNAL | IWorkbenchBrowserSupport.LOCATION_BAR
						| IWorkbenchBrowserSupport.NAVIGATION_BAR;
			}
			String generatedId = "org.eclipse.mylyn.web.browser-" + Calendar.getInstance().getTimeInMillis(); //$NON-NLS-1$
			IWebBrowser browser = WorkbenchBrowserSupport.getInstance().createBrowser(flags, generatedId, null, null);
			browser.openURL(url);
			return new BrowserHandle(browser);
		}
	}

	private static EditorHandle openUrlByHandler(final IWorkbenchPage page, final String location, final int customFlags) {
		for (final AbstractUrlHandler handler : UrlHandlerInitializer.handlers) {
			final AtomicReference<EditorHandle> result = new AtomicReference<EditorHandle>();
			SafeRunnable.run(new ISafeRunnable() {
				public void run() throws Exception {
					result.set(handler.openUrl(page, location, customFlags));
				}

				public void handleException(Throwable exception) {
					CommonsWorkbenchPlugin.getDefault()
							.getLog()
							.log(new Status(IStatus.ERROR, CommonsWorkbenchPlugin.ID_PLUGIN, NLS.bind(
									"Unexpected error in {0} while opening URL ''{1}''", handler.getClass(), location))); //$NON-NLS-1$
				}
			});

			if (result.get() != null) {
				// success
				return result.get();
			}
		}
		return null;
	}

}

Back to the top