KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > console > ConsoleTerminateAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.views.console;
12
13 import org.eclipse.debug.core.DebugException;
14 import org.eclipse.debug.core.DebugPlugin;
15 import org.eclipse.debug.core.ILaunch;
16 import org.eclipse.debug.core.ILaunchManager;
17 import org.eclipse.debug.core.model.IDebugTarget;
18 import org.eclipse.debug.core.model.IProcess;
19 import org.eclipse.debug.internal.ui.DebugPluginImages;
20 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
21 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
22 import org.eclipse.jface.action.Action;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.texteditor.IUpdate;
25
26 /**
27  * ConsoleTerminateAction
28  */

29 public class ConsoleTerminateAction extends Action implements IUpdate {
30
31     private ProcessConsole fConsole;
32
33     /**
34      * Creates a terminate action for the console
35      */

36     public ConsoleTerminateAction(ProcessConsole console) {
37         super(ConsoleMessages.ConsoleTerminateAction_0);
38         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.CONSOLE_TERMINATE_ACTION);
39         fConsole = console;
40         setToolTipText(ConsoleMessages.ConsoleTerminateAction_1);
41         setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_TERMINATE));
42         setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_TERMINATE));
43         setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_TERMINATE));
44         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.CONSOLE_TERMINATE_ACTION);
45         update();
46     }
47
48     /* (non-Javadoc)
49      * @see org.eclipse.ui.texteditor.IUpdate#update()
50      */

51     public void update() {
52         IProcess process = fConsole.getProcess();
53         setEnabled(process.canTerminate());
54     }
55     
56     /* (non-Javadoc)
57      * @see org.eclipse.jface.action.IAction#run()
58      */

59     public void run() {
60         try {
61             IProcess process = fConsole.getProcess();
62             killTargets(process);
63             process.terminate();
64         } catch (DebugException e) {
65             // TODO: report exception
66
}
67     }
68     
69     private void killTargets(IProcess process) throws DebugException {
70         ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
71         ILaunch[] launches = launchManager.getLaunches();
72
73         for (int i = 0; i < launches.length; i++) {
74             ILaunch launch = launches[i];
75             IProcess[] processes = launch.getProcesses();
76             for (int j = 0; j < processes.length; j++) {
77                 IProcess process2 = processes[j];
78                 if (process2.equals(process)) {
79                     IDebugTarget[] debugTargets = launch.getDebugTargets();
80                     for (int k = 0; k < debugTargets.length; k++) {
81                         IDebugTarget target = debugTargets[k];
82                         if (target.canTerminate()) {
83                             target.terminate();
84                         }
85                     }
86                     return; // all possible targets have been terminated for the launch.
87
}
88             }
89         }
90     }
91
92     public void dispose() {
93         fConsole = null;
94     }
95
96 }
97
Popular Tags