/* * Copyright 2000-2020 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.teamcity.ui.actions; import jetbrains.buildServer.util.StringUtil; import jetbrains.teamcity.Activator; import jetbrains.teamcity.Constants.Preferences; import jetbrains.teamcity.SharedImages; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jface.action.Action; import org.eclipse.jface.operation.IRunnableWithProgress; 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 java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URL; public abstract class OpenURLAction extends Action { private static IWebBrowser ourBrowser; private static String ourBrowserType; public OpenURLAction() { setImageDescriptor(SharedImages.getImageDescriptorByPath(SharedImages.WEB_BROWSER_IMG)); } public OpenURLAction(String name) { this(); setText(name); } @Override public void run() { String url = getURL(); if (url != null) { URL httpURL; try { httpURL = new URL(url); } catch (MalformedURLException e1) { return; } final URL toOpen = httpURL; final IRunnableWithProgress runnable = new IRunnableWithProgress() { public void run(IProgressMonitor monitor) { if (monitor == null) { monitor = new NullProgressMonitor(); } monitor.beginTask(Messages.getString("OpenURLAction.task.name"), IProgressMonitor.UNKNOWN); //$NON-NLS-1$ try { final String option = Activator.getDefault().getPreferenceStore().getString(Preferences.BROWSER_OPTION); if (ourBrowser != null && StringUtil.emptyIfNull(ourBrowserType).equals(option)) { ourBrowser.openURL(toOpen); } else { IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport(); if (support != null) { if (Preferences.BROWSER_EXTERNAL.equals(option)) { ourBrowser = support.getExternalBrowser(); } else if (Preferences.BROWSER_INTERNAL.equals(option)) { ourBrowser = support.createBrowser(IWorkbenchBrowserSupport.AS_EDITOR | IWorkbenchBrowserSupport.STATUS | IWorkbenchBrowserSupport.NAVIGATION_BAR, getURL(), null, null); } else { ourBrowser = support.createBrowser(IWorkbenchBrowserSupport.AS_EDITOR | IWorkbenchBrowserSupport.STATUS | IWorkbenchBrowserSupport.NAVIGATION_BAR, getURL(), null, null); } ourBrowserType = option; ourBrowser.openURL(toOpen); } } } catch (PartInitException ignored) { } finally { monitor.done(); } } }; Display.getCurrent().asyncExec(new Runnable() { public void run() { try { runnable.run(new NullProgressMonitor()); } catch (InvocationTargetException ignored) { } catch (InterruptedException ignored) { } } }); } } protected abstract String getURL(); }