Initial implementation of the new Debug Plugin
[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
21  * Window - Preferences - Java - Code Style - Code Templates
22  */
23 public class XDebugThread extends XDebugElement implements IThread, IDebugEventSetListener {
24         
25         /**
26          * Breakpoints this thread is suspended at or <code>null</code>
27          * 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 VM
43          */
44         public XDebugThread(XDebugTarget target) {
45                 super(target);
46                 DebugPlugin.getDefault().addDebugEventListener(this);
47         }
48         
49         /* (non-Javadoc)
50          * @see org.eclipse.debug.core.model.IThread#getStackFrames()
51          */
52         public IStackFrame[] getStackFrames() throws DebugException {
53                 if (isSuspended()) {
54                         if (fStackFrames==null)
55                         {
56 //                              XDebugCorePlugin.log(IStatus.INFO,"vor getStackFrames");
57                                 fStackFrames=((XDebugTarget) getDebugTarget()).getStackFrames();
58 //                              XDebugCorePlugin.log(IStatus.INFO,"nach getStackFrames");
59                         }
60                         return fStackFrames;
61                 } else {
62                         return new IStackFrame[0];
63                 }
64         }
65         /* (non-Javadoc)
66          * @see org.eclipse.debug.core.model.IThread#hasStackFrames()
67          */
68         public boolean hasStackFrames() throws DebugException {
69                 return isSuspended();
70         }
71         /* (non-Javadoc)
72          * @see org.eclipse.debug.core.model.IThread#getPriority()
73          */
74         public int getPriority() throws DebugException {
75                 return 0;
76         }
77         /* (non-Javadoc)
78          * @see org.eclipse.debug.core.model.IThread#getTopStackFrame()
79          */
80         public IStackFrame getTopStackFrame() throws DebugException {
81                 IStackFrame[] frames = getStackFrames();
82                 if (frames.length > 0) {
83                         return frames[0];
84                 }
85                 return null;
86         }
87         /* (non-Javadoc)
88          * @see org.eclipse.debug.core.model.IThread#getName()
89          */
90         public String getName() throws DebugException {
91 //              if (fStackFrames!=null) 
92 //                      return fStackFrames[0].getName();
93 //              else 
94                         return "Thread[1]";
95         }
96         /* (non-Javadoc)
97          * @see org.eclipse.debug.core.model.IThread#getBreakpoints()
98          */
99         public IBreakpoint[] getBreakpoints() {
100                 if (fBreakpoints == null) {
101                         return new IBreakpoint[0];
102                 }
103                 return fBreakpoints;
104         }
105         /**
106          * Sets the breakpoints this thread is suspended at, or <code>null</code>
107          * if none.
108          * 
109          * @param breakpoints the breakpoints this thread is suspended at, or <code>null</code>
110          * if none
111          */
112         protected void setBreakpoints(IBreakpoint[] breakpoints) {
113                 fBreakpoints = breakpoints;
114         }
115         /* (non-Javadoc)
116          * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
117          */
118         public boolean canResume() {
119                 return isSuspended();
120         }
121         /* (non-Javadoc)
122          * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
123          */
124         public boolean canSuspend() {
125                 return !isSuspended();
126         }
127         /* (non-Javadoc)
128          * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
129          */
130         public boolean isSuspended() {
131                 return getDebugTarget().isSuspended();
132         }
133         /* (non-Javadoc)
134          * @see org.eclipse.debug.core.model.ISuspendResume#resume()
135          */
136         public void resume() throws DebugException {
137                 fStackFrames=null;
138                 fBreakpoints=null;
139                 getDebugTarget().resume();
140         }
141         /* (non-Javadoc)
142          * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
143          */
144         public void suspend() throws DebugException {
145                 getDebugTarget().suspend();
146         }
147         /* (non-Javadoc)
148          * @see org.eclipse.debug.core.model.IStep#canStepInto()
149          */
150         public boolean canStepInto() {
151                 return isSuspended();
152         }
153         /* (non-Javadoc)
154          * @see org.eclipse.debug.core.model.IStep#canStepOver()
155          */
156         public boolean canStepOver() {
157                 return isSuspended();
158         }
159         /* (non-Javadoc)
160          * @see org.eclipse.debug.core.model.IStep#canStepReturn()
161          */
162         public boolean canStepReturn() {
163                 if (fStackFrames!=null)
164                         return (fStackFrames.length>1);
165                 else
166                         return false;
167         }
168         /* (non-Javadoc)
169          * @see org.eclipse.debug.core.model.IStep#isStepping()
170          */
171         public boolean isStepping() {
172                 return fStepping;
173         }
174         /* (non-Javadoc)
175          * @see org.eclipse.debug.core.model.IStep#stepInto()
176          */
177         public void stepInto() throws DebugException {
178                 fStackFrames=null;
179                 fBreakpoints=null;
180                 ((XDebugTarget)getDebugTarget()).step_into();
181         }
182         /* (non-Javadoc)
183          * @see org.eclipse.debug.core.model.IStep#stepOver()
184          */
185         public void stepOver() throws DebugException {
186                 fStackFrames=null;
187                 fBreakpoints=null;
188                 ((XDebugTarget)getDebugTarget()).step_over();
189         }
190         /* (non-Javadoc)
191          * @see org.eclipse.debug.core.model.IStep#stepReturn()
192          */
193         public void stepReturn() throws DebugException {
194                 fStackFrames=null;
195                 fBreakpoints=null;
196                 ((XDebugTarget)getDebugTarget()).step_out();
197
198         }
199         /* (non-Javadoc)
200          * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
201          */
202         public boolean canTerminate() {
203                 return !isTerminated();
204         }
205         /* (non-Javadoc)
206          * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
207          */
208         public boolean isTerminated() {
209                 return getDebugTarget().isTerminated();
210         }
211         /* (non-Javadoc)
212          * @see org.eclipse.debug.core.model.ITerminate#terminate()
213          */
214         public void terminate() throws DebugException {
215                 getDebugTarget().terminate();
216         }
217         
218         /**
219          * Sets whether this thread is stepping
220          * 
221          * @param stepping whether stepping
222          */
223         protected void setStepping(boolean stepping) {
224                 fStepping = stepping;
225         }
226
227         public void handleDebugEvents(DebugEvent[] events) {
228                 DebugEvent de=events[0];
229                 System.out.println(de.toString());
230                 
231         }
232
233         public void removeEventListeners() {
234                 DebugPlugin.getDefault().removeDebugEventListener(this);
235                 
236         }
237 }