Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ae58ac288efbfebfaca6f909f1164b7c66be1980 (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
/**
 * StartDebugCommandHandler.java
 * Created on Jun 29, 2012
 *
 * Copyright (c) 2012, 2013 Wind River Systems, Inc.
 *
 * The right to copy, distribute, modify, or otherwise make use
 * of this software may be licensed only pursuant to the terms
 * of an applicable Wind River license agreement.
 */
package org.eclipse.tcf.te.tcf.ui.handler;

import java.util.Iterator;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.tcf.te.core.async.AsyncCallbackCollector;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.runtime.services.interfaces.IDebugService;
import org.eclipse.tcf.te.runtime.services.interfaces.IUIService;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.steps.StartDebuggerStep.IDelegate;
import org.eclipse.tcf.te.ui.async.UICallbackInvocationDelegate;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.part.EditorPart;

/**
 * Start debugger command handler implementation.
 */
public class StartDebugCommandHandler extends AbstractHandler {

	/* (non-Javadoc)
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// Get the active part
		IWorkbenchPart part = HandlerUtil.getActivePart(event);
		// Get the current selection
		ISelection selection = HandlerUtil.getCurrentSelection(event);

		// If the handler is invoked from an editor part, ignore the selection and
		// construct an artificial selection from the active editor input.
		if (part instanceof EditorPart) {
			IEditorInput input = ((EditorPart)part).getEditorInput();
			Object element = input != null ? input.getAdapter(Object.class) : null;
			if (element != null) {
				selection = new StructuredSelection(element);
			}
		}

		// If the selection is not empty, iterate over the selection and execute
		// the operation for each peer model node in the selection.
		if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
			// Create the collector keeping track of the callbacks for each peer model
			// node within the selection
			final AsyncCallbackCollector collector = new AsyncCallbackCollector(new Callback() {
				@Override
				protected void internalDone(Object caller, IStatus status) {
					// Signal that all operations completed
				}
			}, new UICallbackInvocationDelegate());

			Iterator<?> iterator = ((IStructuredSelection)selection).iterator();
			while (iterator.hasNext()) {
				final Object element = iterator.next();
				if (element instanceof IPeerModel) {
					startDebugger((IPeerModel)element, new AsyncCallbackCollector.SimpleCollectorCallback(collector));
				}
			}

			// Mark the collector initialization done
			collector.initDone();
		}

		return null;
	}

	/**
	 * Starts the debugger for the given peer model node.
	 *
	 * @param peerModel The peer model node. Must not be <code>null</code>.
	 * @param callback The callback. Must not be <code>null</code>.
	 */
	public void startDebugger(final IPeerModel peerModel, final ICallback callback) {
		Assert.isNotNull(peerModel);
		Assert.isNotNull(callback);

		IDebugService dbgService = ServiceManager.getInstance().getService(peerModel, IDebugService.class, false);
		if (dbgService != null) {
			final IProgressMonitor monitor = new NullProgressMonitor();
			IPropertiesContainer props = new PropertiesContainer();
			dbgService.attach(peerModel, props, monitor, new Callback() {
				@Override
                protected void internalDone(Object caller, IStatus status) {
					// Check if there is a delegate registered
					IUIService uiService = ServiceManager.getInstance().getService(peerModel, IUIService.class, false);
					IDelegate delegate = uiService != null ? uiService.getDelegate(peerModel, IDelegate.class) : null;

					if (delegate != null) {
						delegate.postAttachDebugger(peerModel, monitor, callback);
					} else {
						callback.done(caller, status);
					}
				}
			});
		}
	}
}

Back to the top