Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0cf2c6a585d47323336d5b250b76c49e77df1bed (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
/*******************************************************************************
 * Copyright (c) 2006, 2015 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.core.commands;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.IRequest;
import org.eclipse.debug.core.commands.AbstractDebugCommand;
import org.eclipse.debug.core.commands.IEnabledStateRequest;
import org.eclipse.debug.core.model.IStackFrame;
import org.eclipse.debug.core.model.IStep;
import org.eclipse.debug.core.model.IThread;

/**
 * Common function for step commands.
 *
 * @since 3.3
 */
public abstract class StepCommand extends AbstractDebugCommand {

	@Override
	protected void doExecute(Object[] targets, IProgressMonitor monitor, IRequest request) throws CoreException {
		for (int i = 0; i < targets.length; i++) {
			step(targets[i]);
		}
	}

	protected abstract void step(Object target) throws CoreException ;

	@Override
	protected boolean isExecutable(Object[] targets, IProgressMonitor monitor, IEnabledStateRequest collector) throws CoreException {
		if (isThreadCompatible(targets)) {
			for (int i = 0; i < targets.length; i++) {
				if (!isSteppable(targets[i])) {
					return false;
				}
			}
			return true;
		} else {
			return false;
		}
	}

	protected abstract boolean isSteppable(Object target) throws CoreException;

	protected boolean isThreadCompatible(Object[] targets) {
		if (targets.length == 1) {
			return true;
		}
		// check if frames from same thread
		Set<IThread> threads = new HashSet<IThread>(targets.length);
		for (int i = 0; i < targets.length; i++) {
			Object object = targets[i];
			IStackFrame frame = null;
			if (object instanceof IStackFrame) {
				frame = (IStackFrame) object;
			} else if (object instanceof IAdaptable) {
				frame = ((IAdaptable)object).getAdapter(IStackFrame.class);
			}
			if (frame != null) {
				if (!threads.add(frame.getThread())) {
					return false;
				}
			}
		}
		return true;
	}

	@Override
	protected Object getTarget(Object element) {
		return getAdapter(element, IStep.class);
	}

}

Back to the top