KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > commands > StepCommand


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.core.commands;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.debug.core.IRequest;
20 import org.eclipse.debug.core.commands.IEnabledStateRequest;
21 import org.eclipse.debug.core.model.IStackFrame;
22 import org.eclipse.debug.core.model.IStep;
23
24 /**
25  * Common function for step commands.
26  *
27  * @since 3.3
28  */

29 public abstract class StepCommand extends DebugCommand {
30
31     protected void doExecute(Object JavaDoc[] targets, IProgressMonitor monitor, IRequest request) throws CoreException {
32         for (int i = 0; i < targets.length; i++) {
33             step(targets[i]);
34         }
35     }
36
37     protected abstract void step(Object JavaDoc target) throws CoreException ;
38     
39     protected boolean isExecutable(Object JavaDoc[] targets, IProgressMonitor monitor, IEnabledStateRequest collector) throws CoreException {
40         if (isThreadCompatible(targets)) {
41             for (int i = 0; i < targets.length; i++) {
42                 if (!isSteppable(targets[i])) {
43                     return false;
44                 }
45             }
46             return true;
47         } else {
48             return false;
49         }
50     }
51     
52     protected abstract boolean isSteppable(Object JavaDoc target) throws CoreException;
53     
54     protected boolean isThreadCompatible(Object JavaDoc[] targets) {
55         if (targets.length == 1) {
56             return true;
57         }
58         // check if frames from same thread
59
Set JavaDoc threads = new HashSet JavaDoc(targets.length);
60         for (int i = 0; i < targets.length; i++) {
61             Object JavaDoc object = targets[i];
62             IStackFrame frame = null;
63             if (object instanceof IStackFrame) {
64                 frame = (IStackFrame) object;
65             } else if (object instanceof IAdaptable) {
66                 frame = (IStackFrame)((IAdaptable)object).getAdapter(IStackFrame.class);
67             }
68             if (frame != null) {
69                 if (!threads.add(frame.getThread())) {
70                     return false;
71                 }
72             }
73         }
74         return true;
75     }
76     
77     protected Object JavaDoc getTarget(Object JavaDoc element) {
78         if (element instanceof IStep) {
79             return element;
80         } else if (element instanceof IAdaptable) {
81             return ((IAdaptable) element).getAdapter(IStep.class);
82         }
83         return null;
84     }
85
86 }
87
Popular Tags