| /*=============================================================================# |
| # Copyright (c) 2007, 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.nico.ui.actions; |
| |
| import org.eclipse.core.commands.ExecutionEvent; |
| import org.eclipse.swt.widgets.Display; |
| |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| import org.eclipse.statet.jcommons.lang.Nullable; |
| import org.eclipse.statet.jcommons.ts.core.ToolProvider; |
| |
| import org.eclipse.statet.nico.core.runtime.ToolController; |
| import org.eclipse.statet.nico.core.runtime.ToolProcess; |
| |
| |
| /** |
| * Handler to cancel tool tasks. |
| */ |
| @NonNullByDefault |
| public class CancelHandler extends AbstractToolHandler<ToolProcess> { |
| |
| |
| public static final String MENU_ID= "org.eclipse.statet.nico.menus.Cancel"; //$NON-NLS-1$ |
| |
| public static final String PAR_OPTIONS= "options"; //$NON-NLS-1$ |
| |
| |
| private final int options; |
| |
| |
| public CancelHandler(final ToolProvider toolProvider, final int options) { |
| super(null, null, toolProvider, null); |
| |
| this.options= options; |
| init(); |
| } |
| |
| public CancelHandler(final ToolProvider toolProvider) { |
| this(toolProvider, 0); |
| } |
| |
| |
| @Override |
| protected @Nullable Object execute(final ToolProcess tool, final ExecutionEvent event) { |
| final String optionsParameter= event.getParameter(PAR_OPTIONS); |
| int options= this.options; |
| if (optionsParameter != null) { |
| try { |
| options= Integer.decode(optionsParameter); |
| } |
| catch (final NumberFormatException e) { |
| } |
| } |
| |
| final ToolController controller= (tool != null) ? tool.getController() : null; |
| if (controller == null) { |
| return null; |
| } |
| |
| if (!controller.cancelTask(options)) { |
| Display.getCurrent().beep(); |
| } |
| |
| return null; |
| } |
| |
| } |