blob: 6171b9b1862eb3e34f2597915b3869e27da1665f [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2012, 2017 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.r.ui.rhelp;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.statushandlers.StatusManager;
import org.eclipse.statet.ecommons.ui.util.UIAccess;
import org.eclipse.statet.ecommons.ui.util.UIAccess.CheckedRunnable;
import org.eclipse.statet.internal.r.ui.rhelp.RHelpUIServlet;
import org.eclipse.statet.internal.r.ui.rhelp.RHelpView;
import org.eclipse.statet.nico.ui.NicoUITools;
import org.eclipse.statet.r.console.core.RProcess;
import org.eclipse.statet.r.core.RCore;
import org.eclipse.statet.r.core.renv.IREnv;
import org.eclipse.statet.r.core.rhelp.IRHelpManager;
import org.eclipse.statet.r.ui.RUI;
public class OpenRHelpHandler extends AbstractHandler {
private final IREnv rEnv;
private final RProcess tool;
private final boolean reuse;
public OpenRHelpHandler(final IREnv rEnv, final RProcess tool, final boolean reuse) {
if (rEnv == null) {
throw new NullPointerException("rEnv"); //$NON-NLS-1$
}
this.rEnv= rEnv;
this.tool= tool;
this.reuse= reuse;
}
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
return execute(event.getParameter("url")); //$NON-NLS-1$
}
public IStatus execute(String url) {
final RProcess tool= this.tool;
final IRHelpManager rHelpManager= RCore.getRHelpManager();
if (url == null || url.isEmpty() || url.equals("about:blank")) { //$NON-NLS-1$
url= rHelpManager.getREnvHttpUrl(this.rEnv, RHelpUIServlet.BROWSE_TARGET);
}
else {
url= rHelpManager.toHttpUrl(url, this.rEnv, RHelpUIServlet.BROWSE_TARGET);
}
if (url == null) {
return null;
}
try {
final String urlToOpen= url;
UIAccess.checkedSyncExec(new CheckedRunnable() {
@Override
public void run() throws CoreException {
final RHelpView view= (RHelpView) NicoUITools.getView(RUI.R_HELP_VIEW_ID, tool, true);
view.openUrl(urlToOpen, (OpenRHelpHandler.this.reuse) ? view.findBrowserSession(urlToOpen) : null);
}
});
return Status.OK_STATUS;
}
catch (final CoreException e) {
final IStatus status= new Status(IStatus.ERROR, RUI.BUNDLE_ID, -1,
"An error occured when opening R help page in R help view.", e );
StatusManager.getManager().handle(status);
return status;
}
}
}