blob: bfc0fa6e2dbfc0cd0c38cc7b62f83984e1be4381 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2009, 2018 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.internal.r.objectbrowser;
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.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.statet.ecommons.models.core.util.ElementPartition;
import org.eclipse.statet.ecommons.ui.util.UIAccess;
import org.eclipse.statet.nico.core.runtime.SubmitType;
import org.eclipse.statet.nico.core.runtime.ToolController;
import org.eclipse.statet.nico.core.runtime.ToolProcess;
import org.eclipse.statet.nico.ui.NicoUITools;
import org.eclipse.statet.r.console.core.RConsoleTool;
import org.eclipse.statet.r.core.data.CombinedRElement;
import org.eclipse.statet.r.core.model.RElementName;
import org.eclipse.statet.rj.data.RObject;
class PrintHandler extends AbstractHandler {
private final ObjectBrowserView view;
public PrintHandler(final ObjectBrowserView objectBrowserView) {
this.view = objectBrowserView;
}
private boolean isValidSelection(final ITreeSelection selection) {
if (selection == null || selection.size() != 1) {
return false;
}
final Object element = selection.getFirstElement();
if (element instanceof ElementPartition) {
final CombinedRElement rElement = ContentProvider.getCombinedRElement(element);
if (rElement == null || rElement.getRObjectType() != RObject.TYPE_LIST) {
return false;
}
}
return true;
}
@Override
public void setEnabled(final Object evaluationContext) {
final ToolProcess process = this.view.getTool();
setBaseEnabled(process != null && !process.isTerminated()
&& isValidSelection(this.view.getSelection()) );
}
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
if (!UIAccess.isOkToUse(this.view.getViewer())) {
return null;
}
final ITreeSelection selection = this.view.getSelection();
if (!isValidSelection(selection)) {
return null;
}
final TreePath treePath = selection.getPaths()[0];
final RElementName elementName = this.view.getFQElementName(treePath);
if (elementName != null) {
String cmd = RElementName.createDisplayName(elementName, RElementName.DISPLAY_FQN | RElementName.DISPLAY_EXACT);
if (treePath.getLastSegment() instanceof ElementPartition) {
final ElementPartition partition = (ElementPartition) treePath.getLastSegment();
cmd = cmd + '[' + (partition.getPartitionStart() + 1) + ':' + (partition.getPartitionStart() + partition.getPartitionLength()) + ']';
}
final ToolProcess process = this.view.getTool();
try {
final ToolController controller = NicoUITools.accessController(RConsoleTool.TYPE, process);
controller.submit(cmd, SubmitType.TOOLS);
}
catch (final CoreException e) {
}
}
return null;
}
}