Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2e8dfe012058f957da81b32ef2e88bb882e1bfd1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
 * Copyright (c) 2006, 2018 Wind River Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *     Marc Khouzam (Ericsson) - Added support for multi-selection (Bug 330974)
 *     John Dallaway - Report command execution error (Bug 539455)
 *******************************************************************************/
package org.eclipse.cdt.dsf.debug.ui.actions;

import org.eclipse.cdt.debug.core.CDebugUtils;
import org.eclipse.cdt.dsf.concurrent.DsfExecutor;
import org.eclipse.cdt.dsf.concurrent.ImmediateDataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.ImmediateRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.Immutable;
import org.eclipse.cdt.dsf.debug.service.IMultiRunControl;
import org.eclipse.cdt.dsf.internal.ui.DsfUIPlugin;
import org.eclipse.cdt.dsf.service.DsfServicesTracker;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.debug.core.commands.IDebugCommandRequest;
import org.eclipse.debug.core.commands.IEnabledStateRequest;
import org.eclipse.debug.core.commands.ISuspendHandler;

/**
 * 
 * @since 1.0
 */
@Immutable
public class DsfSuspendCommand implements ISuspendHandler {
    private final DsfExecutor fExecutor;
    private final DsfServicesTracker fTracker;
    
    public DsfSuspendCommand(DsfSession session) {
        fExecutor = session.getExecutor();
        fTracker = new DsfServicesTracker(DsfUIPlugin.getBundleContext(), session.getId());
    }    

    public void dispose() {
        fTracker.dispose();
    }

    @Override
	public void canExecute(final IEnabledStateRequest request) {
        if (request.getElements().length == 1) {
			canExecuteSingle(request);
            return;
        }
        
   		// Handle multi-selection
        fExecutor.submit(new DsfCommandRunnable(fTracker, request.getElements(), request) { 
            @Override public void doExecute() {
    			final IMultiRunControl multiRun = fTracker.getService(IMultiRunControl.class);
    			if (multiRun == null) {
    				// No multi run control service: multi selection not allowed
    				request.setEnabled(false);
    				request.done();
    				return;
    			}
    			
    			// Check if some of the selections can be suspended
    			multiRun.canSuspendSome(
    					getContexts(),
    					new ImmediateDataRequestMonitor<Boolean>() {
    						@Override
    						protected void handleCompleted() {
    							request.setEnabled(isSuccess() && getData());
    							request.done();
    						}
    					});
            }
        });
    }
    
    private void canExecuteSingle(final IEnabledStateRequest request) {
        fExecutor.submit(new DsfCommandRunnable(fTracker, request.getElements()[0], request) { 
            @Override public void doExecute() {
                getRunControl().canSuspend(
                    getContext(),
                    new ImmediateDataRequestMonitor<Boolean>() {
                        @Override
                        protected void handleCompleted() {
                            request.setEnabled(isSuccess() && getData());
                            request.done();
                        }
                    });
            }
        });
    }

    @Override
	public boolean execute(final IDebugCommandRequest request) {
   		if (request.getElements().length == 1) {
   			executeSingle(request);
   			return false;
        }
  		
   		// Handle multi-selection
   		fExecutor.submit(new DsfCommandRunnable(fTracker, request.getElements(), request) { 
            @Override public void doExecute() {
    			final IMultiRunControl multiRun = fTracker.getService(IMultiRunControl.class);
    			if (multiRun == null) {
    				// No multi run control service: multi selection not allowed
    				request.done();
    				return;
    			}

    			multiRun.suspend(getContexts(), new ImmediateRequestMonitor() {
                    @Override
                    protected void handleError() {
                        super.handleError();
                        CDebugUtils.error(getStatus(), DsfSuspendCommand.this);
                    }
                });
    		}
    	});
   		return false;
    }
    
	private void executeSingle(IDebugCommandRequest request) {
        fExecutor.submit(new DsfCommandRunnable(fTracker, request.getElements()[0], request) { 
            @Override public void doExecute() {
                getRunControl().suspend(getContext(), new ImmediateRequestMonitor() {
                    @Override
                    protected void handleError() {
                        super.handleError();
                        CDebugUtils.error(getStatus(), DsfSuspendCommand.this);
                    }
                });
            }
        });
    }
}

Back to the top