KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > TerminateActionDelegate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.actions;
12
13
14 import org.eclipse.debug.core.DebugEvent;
15 import org.eclipse.debug.core.DebugException;
16 import org.eclipse.debug.core.DebugPlugin;
17 import org.eclipse.debug.core.ILaunch;
18 import org.eclipse.debug.core.ILaunchManager;
19 import org.eclipse.debug.core.model.IDebugTarget;
20 import org.eclipse.debug.core.model.IProcess;
21 import org.eclipse.debug.core.model.ITerminate;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25
26 public class TerminateActionDelegate extends AbstractListenerActionDelegate {
27
28     /**
29      * @see AbstractDebugActionDelegate#doAction(Object)
30      */

31     protected void doAction(Object JavaDoc element) throws DebugException {
32         if (element instanceof ITerminate) {
33             if (element instanceof IProcess) {
34                 killTargets((IProcess) element);
35             }
36             ((ITerminate)element).terminate();
37         }
38     }
39     
40     private void killTargets(IProcess process) throws DebugException {
41         ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
42         ILaunch[] launches = launchManager.getLaunches();
43
44         for (int i = 0; i < launches.length; i++) {
45             ILaunch launch = launches[i];
46             IProcess[] processes = launch.getProcesses();
47             for (int j = 0; j < processes.length; j++) {
48                 IProcess process2 = processes[j];
49                 if (process2.equals(process)) {
50                     IDebugTarget[] debugTargets = launch.getDebugTargets();
51                     for (int k = 0; k < debugTargets.length; k++) {
52                         IDebugTarget target = debugTargets[k];
53                         if (target.canTerminate()) {
54                             target.terminate();
55                         }
56                     }
57                     return; // all possible targets have been terminated for the launch.
58
}
59             }
60         }
61     }
62
63     /**
64      * @see AbstractDebugActionDelegate#isRunInBackground()
65      */

66     protected boolean isRunInBackground() {
67         return true;
68     }
69
70     /**
71      * @see AbstractDebugActionDelegate#isEnabledFor(Object)
72      */

73     protected boolean isEnabledFor(Object JavaDoc element) {
74         return element instanceof ITerminate && ((ITerminate)element).canTerminate();
75     }
76     
77     /**
78      * @see AbstractDebugActionDelegate#getStatusMessage()
79      */

80     protected String JavaDoc getStatusMessage() {
81         return ActionMessages.TerminateActionDelegate_Exceptions_occurred_attempting_to_terminate__2; //$NON-NLS-1$
82
}
83
84     /**
85      * @see AbstractDebugActionDelegate#getErrorDialogMessage()
86      */

87     protected String JavaDoc getErrorDialogMessage() {
88         return ActionMessages.TerminateActionDelegate_Terminate_failed__1; //$NON-NLS-1$
89
}
90     
91     /**
92      * @see ListenerActionDelegate#doHandleDebugEvent(DebugEvent)
93      */

94     protected void doHandleDebugEvent(DebugEvent event) {
95         if (event.getKind() == DebugEvent.TERMINATE || event.getKind() == DebugEvent.CREATE) {
96             update(getAction(), getSelection());
97         }
98     }
99
100     /* (non-Javadoc)
101      * @see org.eclipse.debug.internal.ui.actions.AbstractDebugActionDelegate#update(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
102      */

103     protected void update(IAction action, ISelection s) {
104         // ignore non-structured selections (for example, text selection in console)
105
if (s instanceof IStructuredSelection) {
106             super.update(action, s);
107         }
108     }
109     
110     
111 }
112
Popular Tags