blob: 39e1f036aa7f9685571114235f918bab27c8cbc8 [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 org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.statet.ecommons.ui.util.UIAccess;
import org.eclipse.statet.ecommons.ui.workbench.WorkbenchUIUtils;
import org.eclipse.statet.ltk.ui.sourceediting.ISourceEditor;
public class SourceEditorOperationHandler extends AbstractHandler {
private final int viewerOperation;
public SourceEditorOperationHandler(final int viewerOperation) {
this.viewerOperation= viewerOperation;
}
protected ISourceEditor getSourceEditor(final Object context) {
final IWorkbenchPart part= WorkbenchUIUtils.getActivePart(context);
if (part == null) {
return null;
}
return part.getAdapter(ISourceEditor.class);
}
@Override
public void setEnabled(final Object evaluationContext) {
final ISourceEditor editor= getSourceEditor(evaluationContext);
if (editor == null) {
setBaseEnabled(false);
return;
}
final SourceViewer viewer= editor.getViewer();
setBaseEnabled(UIAccess.isOkToUse(viewer)
&& viewer.canDoOperation(this.viewerOperation) );
}
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
final ISourceEditor editor= getSourceEditor(event.getApplicationContext());
if (editor == null) {
return null;
}
final SourceViewer viewer= editor.getViewer();
if (UIAccess.isOkToUse(viewer)) {
viewer.doOperation(this.viewerOperation);
}
return null;
}
}