KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > context > TerminateAdapter


1 /*******************************************************************************
2  * Copyright (c) 2006 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.context;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.debug.core.DebugException;
19 import org.eclipse.debug.core.DebugPlugin;
20 import org.eclipse.debug.core.ILaunch;
21 import org.eclipse.debug.core.ILaunchManager;
22 import org.eclipse.debug.core.model.IDebugTarget;
23 import org.eclipse.debug.core.model.IProcess;
24 import org.eclipse.debug.core.model.ITerminate;
25 import org.eclipse.debug.internal.ui.actions.provisional.IAsynchronousTerminateAdapter;
26 import org.eclipse.debug.internal.ui.actions.provisional.IBooleanRequestMonitor;
27 import org.eclipse.debug.internal.ui.viewers.provisional.IAsynchronousRequestMonitor;
28 import org.eclipse.debug.ui.IDebugUIConstants;
29
30 /**
31  * Default terminate adapter for standard debug model.
32  *
33  * @since 3.2
34  */

35 public class TerminateAdapter extends StandardActionAdapter implements IAsynchronousTerminateAdapter {
36
37     /* (non-Javadoc)
38      * @see org.eclipse.debug.internal.ui.actions.provisional.IAsynchronousTerminateAdapter#canTerminate(java.lang.Object, org.eclipse.debug.internal.ui.actions.provisional.IBooleanRequestMonitor)
39      */

40     public void canTerminate(final Object JavaDoc element, final IBooleanRequestMonitor requestMonitor) {
41         Job job = new Job("canTerminate") { //$NON-NLS-1$
42
protected IStatus run(IProgressMonitor monitor) {
43                 ITerminate terminate = getTarget(element);
44                 if (terminate != null)
45                     requestMonitor.setResult(terminate.canTerminate());
46                 else
47                     requestMonitor.setResult(false);
48                 requestMonitor.done();
49                 return Status.OK_STATUS;
50             }
51         };
52         job.setSystem(true);
53         job.setRule(createUpdateSchedulingRule());
54         job.schedule();
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.debug.internal.ui.actions.provisional.IAsynchronousTerminateAdapter#isTerminated(java.lang.Object, org.eclipse.debug.internal.ui.actions.provisional.IBooleanRequestMonitor)
59      */

60     public void isTerminated(final Object JavaDoc element, final IBooleanRequestMonitor requestMonitor) {
61         Job job = new Job("isTerminated") { //$NON-NLS-1$
62
protected IStatus run(IProgressMonitor monitor) {
63                 ITerminate terminate = getTarget(element);
64                 if (terminate != null)
65                     requestMonitor.setResult(terminate.isTerminated());
66                 else
67                     requestMonitor.setResult(false);
68                 requestMonitor.done();
69                 return Status.OK_STATUS;
70             }
71         };
72         job.setSystem(true);
73         job.setRule(createUpdateSchedulingRule());
74         job.schedule();
75     }
76
77     /* (non-Javadoc)
78      * @see org.eclipse.debug.internal.ui.actions.provisional.IAsynchronousTerminateAdapter#terminate(java.lang.Object, org.eclipse.debug.internal.ui.viewers.provisional.IAsynchronousRequestMonitor)
79      */

80     public void terminate(final Object JavaDoc element, final IAsynchronousRequestMonitor requestMonitor) {
81         Job job = new Job("terminate") { //$NON-NLS-1$
82
protected IStatus run(IProgressMonitor monitor) {
83                 ITerminate terminate = getTarget(element);
84                 if (terminate != null) {
85                     try {
86                         if (element instanceof IProcess) {
87                             killTargets((IProcess) element);
88                         }
89                         ((ITerminate) element).terminate();
90                     } catch (DebugException e) {
91                         requestMonitor.setStatus(e.getStatus());
92                     }
93                 } else {
94                     requestMonitor.setStatus(new Status(IStatus.ERROR, IDebugUIConstants.PLUGIN_ID, IDebugUIConstants.INTERNAL_ERROR, "element must be an instance of or adapt to ITerminate", //$NON-NLS-1$
95
null));
96                 }
97                 requestMonitor.done();
98                 return Status.OK_STATUS;
99             }
100         };
101         job.setSystem(true);
102         job.schedule();
103     }
104
105     private void killTargets(IProcess process) throws DebugException {
106         ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
107         ILaunch[] launches = launchManager.getLaunches();
108
109         for (int i = 0; i < launches.length; i++) {
110             ILaunch launch = launches[i];
111             IProcess[] processes = launch.getProcesses();
112             for (int j = 0; j < processes.length; j++) {
113                 IProcess process2 = processes[j];
114                 if (process2.equals(process)) {
115                     IDebugTarget[] debugTargets = launch.getDebugTargets();
116                     for (int k = 0; k < debugTargets.length; k++) {
117                         IDebugTarget target = debugTargets[k];
118                         if (target.canTerminate()) {
119                             target.terminate();
120                         }
121                     }
122                     return; // all possible targets have been terminated for the
123
// launch.
124
}
125             }
126         }
127     }
128
129     private ITerminate getTarget(Object JavaDoc element) {
130         if (element instanceof ITerminate) {
131             return (ITerminate) element;
132         } else if (element instanceof IAdaptable) {
133             return (ITerminate) ((IAdaptable) element).getAdapter(ITerminate.class);
134         }
135         return null;
136     }
137
138 }
139
Popular Tags