| /*=============================================================================# |
| # Copyright (c) 2013, 2021 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 org.eclipse.core.commands.ExecutionEvent; |
| import org.eclipse.core.commands.ExecutionException; |
| import org.eclipse.jface.text.ITextOperationTarget; |
| import org.eclipse.swt.widgets.Display; |
| |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| import org.eclipse.statet.jcommons.lang.Nullable; |
| |
| import org.eclipse.statet.ltk.ui.sourceediting.SourceEditor; |
| |
| |
| @NonNullByDefault |
| public class SourceEditorOperationHandler extends AbstractSourceEditorHandler { |
| |
| |
| private final int textOperation; |
| |
| |
| public SourceEditorOperationHandler(final int textOperation) { |
| super(); |
| this.textOperation= textOperation; |
| } |
| |
| public SourceEditorOperationHandler(final SourceEditor editor, final int textOperation) { |
| super(editor); |
| this.textOperation= textOperation; |
| } |
| |
| |
| protected final int getTextOperation() { |
| return this.textOperation; |
| } |
| |
| protected @Nullable ITextOperationTarget getOperationTarget(final SourceEditor 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 SourceEditor editor= getSourceEditor(evaluationContext); |
| if (editor == null || !isSupported(editor)) { |
| 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 SourceEditor editor= getSourceEditor(event.getApplicationContext()); |
| if (editor == null || !isSupported(editor)) { |
| return null; |
| } |
| |
| final ITextOperationTarget operationTarget= getOperationTarget(editor); |
| if (operationTarget != null && operationTarget.canDoOperation(this.textOperation)) { |
| operationTarget.doOperation(this.textOperation); |
| return null; |
| } |
| Display.getCurrent().beep(); |
| return null; |
| } |
| |
| } |