| /*=============================================================================# |
| # Copyright (c) 2000, 2021 IBM Corporation 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. |
| # |
| # SPDX-License-Identifier: EPL-2.0 |
| # |
| # Contributors: |
| # IBM Corporation - org.eclipse.ui.console: initial API and implementation |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.nico.ui.console; |
| |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.debug.core.DebugException; |
| import org.eclipse.debug.core.DebugPlugin; |
| import org.eclipse.debug.core.ILaunch; |
| import org.eclipse.debug.core.ILaunchManager; |
| import org.eclipse.debug.core.model.IDebugTarget; |
| import org.eclipse.debug.core.model.IProcess; |
| import org.eclipse.debug.internal.ui.IDebugHelpContextIds; |
| import org.eclipse.debug.internal.ui.views.console.ConsoleMessages; |
| import org.eclipse.debug.ui.DebugUITools; |
| import org.eclipse.jface.action.Action; |
| import org.eclipse.jface.resource.ImageRegistry; |
| import org.eclipse.ui.IWorkbenchPage; |
| import org.eclipse.ui.PlatformUI; |
| import org.eclipse.ui.console.IConsole; |
| import org.eclipse.ui.statushandlers.StatusManager; |
| import org.eclipse.ui.texteditor.IUpdate; |
| |
| import org.eclipse.statet.ecommons.ui.util.UIAccess; |
| |
| import org.eclipse.statet.internal.nico.ui.Messages; |
| import org.eclipse.statet.internal.nico.ui.NicoUIPlugin; |
| import org.eclipse.statet.nico.ui.NicoUI; |
| import org.eclipse.statet.nico.ui.NicoUITools; |
| |
| |
| public class TerminateToolAction extends Action implements IUpdate { |
| |
| |
| private IProcess process; |
| |
| |
| /** |
| * Creates a terminate action for the console |
| */ |
| public TerminateToolAction(final IProcess process) { |
| super(ConsoleMessages.ConsoleTerminateAction_0); |
| PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.CONSOLE_TERMINATE_ACTION); |
| this.process= process; |
| setToolTipText(ConsoleMessages.ConsoleTerminateAction_1); |
| final ImageRegistry registry= NicoUIPlugin.getInstance().getImageRegistry(); |
| setImageDescriptor(registry.getDescriptor(NicoUI.LOCTOOL_TERMINATE_IMAGE_ID)); |
| setDisabledImageDescriptor(registry.getDescriptor(NicoUI.LOCTOOLD_TERMINATE_IMAGE_ID)); |
| update(); |
| } |
| |
| |
| @Override |
| public void update() { |
| setEnabled(this.process.canTerminate()); |
| } |
| |
| @Override |
| public void run() { |
| try { |
| // killTargets(fProcess); |
| this.process.terminate(); |
| |
| final IWorkbenchPage page= UIAccess.getActiveWorkbenchPage(false); |
| final IConsole console= DebugUITools.getConsole(this.process); |
| if (console instanceof NIConsole) { |
| NicoUITools.showConsole((NIConsole) console, page, true); |
| } |
| } catch (final DebugException e) { |
| final int severity= e.getStatus().getSeverity(); |
| StatusManager.getManager().handle(new Status(severity, NicoUI.BUNDLE_ID, -1, Messages.TerminateToolAction_error_message, e), |
| (severity >= IStatus.ERROR) ? StatusManager.LOG | StatusManager.SHOW : StatusManager.LOG); |
| } |
| } |
| |
| private void killTargets(final IProcess process) throws DebugException { |
| final ILaunchManager launchManager= DebugPlugin.getDefault().getLaunchManager(); |
| final ILaunch[] launches= launchManager.getLaunches(); |
| |
| for (int i= 0; i < launches.length; i++) { |
| final ILaunch launch= launches[i]; |
| final IProcess[] processes= launch.getProcesses(); |
| for (int j= 0; j < processes.length; j++) { |
| final IProcess process2= processes[j]; |
| if (process2.equals(process)) { |
| final IDebugTarget[] debugTargets= launch.getDebugTargets(); |
| for (int k= 0; k < debugTargets.length; k++) { |
| final IDebugTarget target= debugTargets[k]; |
| if (target.canTerminate()) { |
| target.terminate(); |
| } |
| } |
| return; // all possible targets have been terminated for the launch. |
| } |
| } |
| } |
| } |
| |
| public void dispose() { |
| this.process= null; |
| } |
| |
| } |