/*******************************************************************************
* Copyright (c) 2012 VMWare, Inc.
* 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:
* VMWare, Inc. - initial API and implementation
*******************************************************************************/
package org.grails.ide.eclipse.ui.internal.utils;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
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;
/**
* @author Christian Dupuis
* @since 2.2.0
*/
@SuppressWarnings("restriction")
public class WebUiUtils {
public static void openUrl(String location) {
openUrl(location, 0);
}
private static void openUrl(String location, int customFlags) {
try {
URL url = null;
if (location != null) {
url = new URL(location);
}
if (WebBrowserPreference.getBrowserChoice() == WebBrowserPreference.EXTERNAL) {
try {
IWorkbenchBrowserSupport support = PlatformUI.getWorkbench()
.getBrowserSupport();
support.getExternalBrowser().openURL(url);
}
catch (Exception e) {
}
}
else {
IWebBrowser browser = null;
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.grails.ide.eclipse.ui-"
+ Calendar.getInstance().getTimeInMillis();
browser = WorkbenchBrowserSupport.getInstance().createBrowser(flags, generatedId,
null, null);
browser.openURL(url);
}
}
catch (PartInitException e) {
MessageDialog.openError(Display.getDefault().getActiveShell(), "Browser init error",
"Browser could not be initiated");
}
catch (MalformedURLException e) {
}
}
}