blob: 3912877a0db4d04c2406cbda96dfbab6d113ad41 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2012, 2019 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 java.net.URI;
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.jcommons.lang.Nullable;
import org.eclipse.statet.ecommons.ui.mpbv.BrowserSession;
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.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.ui.RUI;
import org.eclipse.statet.rhelp.core.http.RHelpHttpService;
import org.eclipse.statet.rj.renv.core.REnv;
public class OpenRHelpHandler extends AbstractHandler {
private final REnv rEnv;
private final RProcess tool;
private final boolean reuse;
public OpenRHelpHandler(final REnv 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 @Nullable Object execute(final ExecutionEvent event) throws ExecutionException {
return execute(event.getParameter("url")); //$NON-NLS-1$
}
public @Nullable IStatus execute(final String urlString) {
final RProcess tool= this.tool;
final URI url;
if (urlString == null || urlString.isEmpty()
|| urlString.equals("about:blank")) { //$NON-NLS-1$
url= RCore.getRHelpHttpService().getREnvHttpUrl(
this.rEnv, RHelpHttpService.BROWSE_TARGET );
}
else {
url= RCore.getRHelpHttpService().toHttpUrl(
urlString, this.rEnv, RHelpHttpService.BROWSE_TARGET );
}
if (url == null) {
return null;
}
try {
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(url, (OpenRHelpHandler.this.reuse) ?
BrowserSession.findSessionByUrl(view.getSessions(), url) :
null );
}
});
return Status.OK_STATUS;
}
catch (final CoreException e) {
final IStatus status= new Status(IStatus.ERROR, RUI.BUNDLE_ID,
"An error occured when opening R help page in R help view.",
e );
StatusManager.getManager().handle(status);
return status;
}
}
}