13
13
*******************************************************************************/
14
14
package org .eclipse .debug .internal .ui .commands .actions ;
15
15
16
+ import java .util .Arrays ;
17
+
18
+ import org .eclipse .core .runtime .ILog ;
19
+ import org .eclipse .core .runtime .Status ;
20
+ import org .eclipse .debug .core .DebugException ;
16
21
import org .eclipse .debug .core .DebugPlugin ;
17
22
import org .eclipse .debug .core .ILaunch ;
18
23
import org .eclipse .debug .core .ILaunchManager ;
22
27
import org .eclipse .debug .internal .ui .IInternalDebugUIConstants ;
23
28
import org .eclipse .debug .internal .ui .actions .ActionMessages ;
24
29
import org .eclipse .debug .ui .actions .DebugCommandAction ;
30
+ import org .eclipse .debug .ui .contexts .DebugContextEvent ;
25
31
import org .eclipse .jface .resource .ImageDescriptor ;
26
32
import org .eclipse .jface .viewers .ISelection ;
27
33
import org .eclipse .jface .viewers .StructuredSelection ;
34
+ import org .eclipse .swt .widgets .Event ;
28
35
import org .eclipse .ui .IWorkbenchPart ;
29
36
import org .eclipse .ui .IWorkbenchWindow ;
30
37
@@ -49,9 +56,7 @@ public void dispose() {
49
56
private void attachSelfToLaunchManager () {
50
57
ILaunchManager launchManager = getLaunchManager ();
51
58
launchManager .addLaunchListener (this );
52
- // heuristic... rather than updating all the time, just assume there's
53
- // something that's not terminated.
54
- setEnabled (launchManager .getLaunches ().length > 0 );
59
+ setEnabled (canTerminate ());
55
60
}
56
61
57
62
private ILaunchManager getLaunchManager () {
@@ -103,12 +108,12 @@ protected Class<ITerminateHandler> getCommandType() {
103
108
104
109
@ Override
105
110
public void launchesTerminated (ILaunch [] launches ) {
106
- setEnabled (getLaunchManager (). getLaunches (). length > 0 );
111
+ setEnabled (canTerminate () );
107
112
}
108
113
109
114
@ Override
110
115
public void launchesAdded (ILaunch [] launches ) {
111
- setEnabled (true );
116
+ setEnabled (canTerminate () );
112
117
}
113
118
114
119
@ Override
@@ -117,7 +122,7 @@ public void launchesChanged(ILaunch[] launches) {
117
122
118
123
@ Override
119
124
public void launchesRemoved (ILaunch [] launches ) {
120
- setEnabled (getLaunchManager (). getLaunches (). length > 0 );
125
+ setEnabled (canTerminate () );
121
126
}
122
127
123
128
@ Override
@@ -136,4 +141,32 @@ public void init(IWorkbenchWindow window) {
136
141
super .init (window );
137
142
attachSelfToLaunchManager ();
138
143
}
144
+
145
+ @ Override
146
+ protected boolean getInitialEnablement () {
147
+ return canTerminate ();
148
+ }
149
+
150
+ private boolean canTerminate () {
151
+ return Arrays .stream (getLaunchManager ().getLaunches ()).anyMatch (ILaunch ::canTerminate );
152
+ }
153
+
154
+ @ Override
155
+ public void runWithEvent (Event event ) {
156
+ for (ILaunch l : getLaunchManager ().getLaunches ()) {
157
+ try {
158
+ if (l .canTerminate ()) {
159
+ l .terminate ();
160
+ }
161
+ } catch (DebugException e ) {
162
+ ILog .get ().log (Status .warning ("Unable to terminate launch: " + e .getMessage (), e )); //$NON-NLS-1$
163
+ }
164
+ }
165
+ setEnabled (canTerminate ());
166
+ }
167
+
168
+ @ Override
169
+ public void debugContextChanged (DebugContextEvent event ) {
170
+ setEnabled (canTerminate ());
171
+ }
139
172
}
0 commit comments