blob: a844c026c4fc221fc5462c0dda966c6ed6f62cfb [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2013, 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.ltk.ui.sourceediting.actions;
import static org.eclipse.statet.jcommons.lang.ObjectUtils.nonNullAssert;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.text.ITextOperationTarget;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
import org.eclipse.statet.jcommons.lang.Nullable;
import org.eclipse.statet.ecommons.ui.util.UIAccess;
import org.eclipse.statet.ecommons.ui.workbench.WorkbenchUIUtils;
import org.eclipse.statet.ltk.ui.sourceediting.ISourceEditor;
@NonNullByDefault
public class SourceEditorOperationHandler extends AbstractHandler {
private final @Nullable ISourceEditor editor;
private final int textOperation;
public SourceEditorOperationHandler(final ISourceEditor editor, final int viewerOperation) {
this.editor= nonNullAssert(editor);
this.textOperation= viewerOperation;
}
public SourceEditorOperationHandler(final int viewerOperation) {
this.editor= null;
this.textOperation= viewerOperation;
}
protected int getTextOperation() {
return this.textOperation;
}
protected @Nullable ISourceEditor getSourceEditor(final @Nullable Object context) {
if (this.editor != null) {
return this.editor;
}
final IWorkbenchPart part= WorkbenchUIUtils.getActivePart(context);
if (part == null) {
return null;
}
return part.getAdapter(ISourceEditor.class);
}
protected @Nullable ITextOperationTarget getOperationTarget(final ISourceEditor editor) {
ITextOperationTarget target= editor.getAdapter(ITextOperationTarget.class);
if (target == null) {
target= editor.getViewer().getTextOperationTarget();
}
return target;
}
@Override
public void setEnabled(final @Nullable Object evaluationContext) {
final ISourceEditor editor= getSourceEditor(evaluationContext);
if (editor == null || !UIAccess.isOkToUse(editor.getViewer())) {
setBaseEnabled(false);
return;
}
final ITextOperationTarget operationTarget= getOperationTarget(editor);
setBaseEnabled(operationTarget != null
&& operationTarget.canDoOperation(this.textOperation) );
}
@Override
public @Nullable Object execute(final ExecutionEvent event) throws ExecutionException {
final ISourceEditor editor= getSourceEditor(event.getApplicationContext());
if (editor == null || !UIAccess.isOkToUse(editor.getViewer())) {
return null;
}
final ITextOperationTarget operationTarget= getOperationTarget(editor);
if (operationTarget != null && operationTarget.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS)) {
operationTarget.doOperation(this.textOperation);
}
return null;
}
}