5c7b9360b9230fcf1cac11ad530519feec0d0dac
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / php / model / XDebugThread.java
1 /*
2  * Created on 23.11.2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */
7 package net.sourceforge.phpeclipse.xdebug.php.model;
8
9 import org.eclipse.debug.core.DebugEvent;
10 import org.eclipse.debug.core.DebugException;
11 import org.eclipse.debug.core.DebugPlugin;
12 import org.eclipse.debug.core.IDebugEventSetListener;
13 import org.eclipse.debug.core.model.IBreakpoint;
14 import org.eclipse.debug.core.model.IStackFrame;
15 import org.eclipse.debug.core.model.IThread;
16
17 /**
18  * @author Axel
19  * 
20  * TODO To change the template for this generated type comment go to Window -
21  * Preferences - Java - Code Style - Code Templates
22  */
23 public class XDebugThread extends XDebugElement implements IThread,
24                 IDebugEventSetListener {
25
26         /**
27          * Breakpoints this thread is suspended at or <code>null</code> if none.
28          */
29
30         private IStackFrame[] fStackFrames = null;
31
32         private IBreakpoint[] fBreakpoints;
33
34         /**
35          * Whether this thread is stepping
36          */
37         private boolean fStepping = false;
38
39         /**
40          * Constructs a new thread for the given target
41          * 
42          * @param target
43          *            VM
44          */
45         public XDebugThread(XDebugTarget target) {
46                 super(target);
47                 DebugPlugin.getDefault().addDebugEventListener(this);
48         }
49
50         /*
51          * (non-Javadoc)
52          * 
53          * @see org.eclipse.debug.core.model.IThread#getStackFrames()
54          */
55         public IStackFrame[] getStackFrames() throws DebugException {
56                 if (isSuspended()) {
57                         if (fStackFrames == null) {
58                                 // XDebugCorePlugin.log(IStatus.INFO,"vor getStackFrames");
59                                 fStackFrames = ((XDebugTarget) getDebugTarget())
60                                                 .getStackFrames();
61                                 // XDebugCorePlugin.log(IStatus.INFO,"nach getStackFrames");
62                         }
63                         return fStackFrames;
64                 } else {
65                         return new IStackFrame[0];
66                 }
67         }
68
69         /*
70          * (non-Javadoc)
71          * 
72          * @see org.eclipse.debug.core.model.IThread#hasStackFrames()
73          */
74         public boolean hasStackFrames() throws DebugException {
75                 return isSuspended();
76         }
77
78         /*
79          * (non-Javadoc)
80          * 
81          * @see org.eclipse.debug.core.model.IThread#getPriority()
82          */
83         public int getPriority() throws DebugException {
84                 return 0;
85         }
86
87         /*
88          * (non-Javadoc)
89          * 
90          * @see org.eclipse.debug.core.model.IThread#getTopStackFrame()
91          */
92         public IStackFrame getTopStackFrame() throws DebugException {
93                 IStackFrame[] frames = getStackFrames();
94                 if (frames.length > 0) {
95                         return frames[0];
96                 }
97                 return null;
98         }
99
100         /*
101          * (non-Javadoc)
102          * 
103          * @see org.eclipse.debug.core.model.IThread#getName()
104          */
105         public String getName() throws DebugException {
106                 // if (fStackFrames!=null)
107                 // return fStackFrames[0].getName();
108                 // else
109                 return "Thread[1]";
110         }
111
112         /*
113          * (non-Javadoc)
114          * 
115          * @see org.eclipse.debug.core.model.IThread#getBreakpoints()
116          */
117         public IBreakpoint[] getBreakpoints() {
118                 if (fBreakpoints == null) {
119                         return new IBreakpoint[0];
120                 }
121                 return fBreakpoints;
122         }
123
124         /**
125          * Sets the breakpoints this thread is suspended at, or <code>null</code>
126          * if none.
127          * 
128          * @param breakpoints
129          *            the breakpoints this thread is suspended at, or
130          *            <code>null</code> if none
131          */
132         protected void setBreakpoints(IBreakpoint[] breakpoints) {
133                 fBreakpoints = breakpoints;
134         }
135
136         /*
137          * (non-Javadoc)
138          * 
139          * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
140          */
141         public boolean canResume() {
142                 return isSuspended();
143         }
144
145         /*
146          * (non-Javadoc)
147          * 
148          * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
149          */
150         public boolean canSuspend() {
151                 return !isSuspended();
152         }
153
154         /*
155          * (non-Javadoc)
156          * 
157          * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
158          */
159         public boolean isSuspended() {
160                 return getDebugTarget().isSuspended();
161         }
162
163         /*
164          * (non-Javadoc)
165          * 
166          * @see org.eclipse.debug.core.model.ISuspendResume#resume()
167          */
168         public void resume() throws DebugException {
169                 fStackFrames = null;
170                 fBreakpoints = null;
171                 getDebugTarget().resume();
172         }
173
174         /*
175          * (non-Javadoc)
176          * 
177          * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
178          */
179         public void suspend() throws DebugException {
180                 getDebugTarget().suspend();
181         }
182
183         /*
184          * (non-Javadoc)
185          * 
186          * @see org.eclipse.debug.core.model.IStep#canStepInto()
187          */
188         public boolean canStepInto() {
189                 return isSuspended();
190         }
191
192         /*
193          * (non-Javadoc)
194          * 
195          * @see org.eclipse.debug.core.model.IStep#canStepOver()
196          */
197         public boolean canStepOver() {
198                 return isSuspended();
199         }
200
201         /*
202          * (non-Javadoc)
203          * 
204          * @see org.eclipse.debug.core.model.IStep#canStepReturn()
205          */
206         public boolean canStepReturn() {
207                 if (fStackFrames != null)
208                         return (fStackFrames.length > 1);
209                 else
210                         return false;
211         }
212
213         /*
214          * (non-Javadoc)
215          * 
216          * @see org.eclipse.debug.core.model.IStep#isStepping()
217          */
218         public boolean isStepping() {
219                 return fStepping;
220         }
221
222         /*
223          * (non-Javadoc)
224          * 
225          * @see org.eclipse.debug.core.model.IStep#stepInto()
226          */
227         public void stepInto() throws DebugException {
228                 fStackFrames = null;
229                 fBreakpoints = null;
230                 ((XDebugTarget) getDebugTarget()).step_into();
231         }
232
233         /*
234          * (non-Javadoc)
235          * 
236          * @see org.eclipse.debug.core.model.IStep#stepOver()
237          */
238         public void stepOver() throws DebugException {
239                 fStackFrames = null;
240                 fBreakpoints = null;
241                 ((XDebugTarget) getDebugTarget()).step_over();
242         }
243
244         /*
245          * (non-Javadoc)
246          * 
247          * @see org.eclipse.debug.core.model.IStep#stepReturn()
248          */
249         public void stepReturn() throws DebugException {
250                 fStackFrames = null;
251                 fBreakpoints = null;
252                 ((XDebugTarget) getDebugTarget()).step_out();
253
254         }
255
256         /*
257          * (non-Javadoc)
258          * 
259          * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
260          */
261         public boolean canTerminate() {
262                 return !isTerminated();
263         }
264
265         /*
266          * (non-Javadoc)
267          * 
268          * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
269          */
270         public boolean isTerminated() {
271                 return getDebugTarget().isTerminated();
272         }
273
274         /*
275          * (non-Javadoc)
276          * 
277          * @see org.eclipse.debug.core.model.ITerminate#terminate()
278          */
279         public void terminate() throws DebugException {
280                 getDebugTarget().terminate();
281         }
282
283         /**
284          * Sets whether this thread is stepping
285          * 
286          * @param stepping
287          *            whether stepping
288          */
289         protected void setStepping(boolean stepping) {
290                 fStepping = stepping;
291         }
292
293         public void handleDebugEvents(DebugEvent[] events) {
294                 DebugEvent de = events[0];
295                 System.out.println(de.toString());
296
297         }
298
299         public void removeEventListeners() {
300                 DebugPlugin.getDefault().removeDebugEventListener(this);
301
302         }
303 }