231119a64b47abeb6869b89b9d273c7e8f117b3b
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / model / PHPThread.java
1 /**********************************************************************
2  Copyright (c) 2000, 2002 IBM Corp. and others.
3  All rights reserved. This program and the accompanying materials
4  are made available under the terms of the Common Public License v1.0
5  which accompanies this distribution, and is available at
6  http://www.eclipse.org/legal/cpl-v10.html
7
8  Contributors:
9  IBM Corporation - Initial implementation
10  Vicente Fernando - www.alfersoft.com.ar
11  **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.core.model;
13
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15
16 import org.eclipse.debug.core.DebugEvent;
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.ILaunch;
20 import org.eclipse.debug.core.model.IBreakpoint;
21 import org.eclipse.debug.core.model.IDebugTarget;
22 import org.eclipse.debug.core.model.IStackFrame;
23 import org.eclipse.debug.core.model.IThread;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.ui.model.IWorkbenchAdapter;
26
27 public class PHPThread implements IThread {
28
29         private PHPStackFrame[] frames;
30
31         private IDebugTarget target;
32
33         private String name;
34
35         private int id;
36
37         private class State {
38                 private boolean isSuspended = false;
39
40                 private boolean isTerminated = false;
41
42                 private boolean isStepping = false;
43
44                 boolean isSuspended() {
45                         return isSuspended;
46                 }
47
48                 boolean isTerminated() {
49                         return isTerminated;
50                 }
51
52                 boolean isStepping() {
53                         return isStepping;
54                 }
55
56                 void setSuspended(boolean suspended) {
57                         if (isTerminated())
58                                 throw new IllegalStateException();
59                         if (suspended && isStepping())
60                                 throw new IllegalStateException();
61                         isSuspended = suspended;
62                 }
63
64                 void setStepping(boolean stepping) {
65                         if (stepping && !isSuspended())
66                                 throw new IllegalStateException();
67                         if (isTerminated())
68                                 throw new IllegalStateException();
69                         isStepping = stepping;
70                 }
71
72                 void setTerminated(boolean terminated) {
73                         isTerminated = terminated;
74                 }
75         }
76
77         private final State state = new State();
78
79         public PHPThread(IDebugTarget target, int id) {
80                 this.target = target;
81                 this.setId(id);
82         }
83
84         public IStackFrame[] getStackFrames() throws DebugException {
85                 return frames;
86         }
87
88         public int getStackFramesSize() {
89                 return frames.length;
90         }
91
92         public boolean hasStackFrames() {
93                 if (frames == null) {
94                         return false;
95                 }
96                 return frames.length > 0;
97         }
98
99         public int getPriority() throws DebugException {
100                 return 0;
101         }
102
103         public IStackFrame getTopStackFrame() throws DebugException {
104                 if (frames == null || frames.length == 0) {
105                         return null;
106                 }
107                 return (IStackFrame) frames[0];
108         }
109
110         public IBreakpoint[] getBreakpoints() {
111                 return null;
112         }
113
114         public String getModelIdentifier() {
115                 return this.getDebugTarget().getModelIdentifier();
116         }
117
118         public IDebugTarget getDebugTarget() {
119                 return target;
120         }
121
122         public void setDebugTarget(IDebugTarget target) {
123                 this.target = target;
124         }
125
126         public ILaunch getLaunch() {
127                 return this.getDebugTarget().getLaunch();
128         }
129
130         public synchronized boolean canResume() {
131                 return isSuspended();
132         }
133
134         public synchronized boolean canSuspend() {
135                 return !isSuspended();
136         }
137
138         public synchronized boolean isSuspended() {
139                 return state.isSuspended;
140         }
141
142         protected void prepareForResume() {
143                 state.setSuspended(false);
144                 this.frames = null;
145                 DebugEvent ev = new DebugEvent(this, DebugEvent.RESUME,
146                                 DebugEvent.CLIENT_REQUEST);
147                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
148         }
149
150         public synchronized void resume() throws DebugException {
151                 if (!isSuspended())
152                         return;
153                 this.prepareForResume();
154                 ((PHPDebugTarget) this.getDebugTarget()).getPHPDBGProxy().resume();
155         }
156
157         /*
158          * public void doSuspend(SuspensionPoint suspensionPoint) { //
159          * this.getPHPDebuggerProxy().readFrames(this);
160          * this.createName(suspensionPoint) ; this.suspend() ; }
161          */
162
163         public synchronized void suspend() throws DebugException {
164                 if (isSuspended())
165                         return;
166                 state.setSuspended(true);
167                 state.setStepping(false);
168                 getDebugTarget().suspend();
169                 DebugEvent ev = new DebugEvent(this, DebugEvent.SUSPEND,
170                                 DebugEvent.BREAKPOINT);
171                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
172         }
173
174         public boolean canStepInto() {
175                 return isSuspended() && isStepping() && this.hasStackFrames();
176         }
177
178         public boolean canStepOver() {
179                 return isSuspended() && isStepping() && this.hasStackFrames();
180         }
181
182         public boolean canStepReturn() {
183                 return isSuspended() && isStepping() && this.hasStackFrames();
184         }
185
186         public boolean isStepping() {
187                 return state.isStepping();
188         }
189
190         public void stepInto() throws DebugException {
191                 try { state.setStepping(true); }
192                 catch (IllegalStateException x) {
193                         throw new DebugException(PHPeclipsePlugin.error(x));
194                 }
195                 this.frames = null;
196                 frames[0].stepInto();
197         }
198
199         public void stepOver() throws DebugException {
200                 state.setStepping(true);
201                 this.frames = null;
202                 frames[0].stepOver();
203         }
204
205         public void stepReturn() throws DebugException {
206         }
207
208         public boolean canTerminate() {
209                 return !isTerminated();
210         }
211
212         public boolean isTerminated() {
213                 return state.isTerminated();
214         }
215
216         public synchronized void terminate() throws DebugException {
217                 if (isTerminated())
218                         return;
219                 state.setTerminated(true);
220                 this.frames = null;
221                 getDebugTarget().terminate();
222         }
223
224         public Object getAdapter(Class arg0) {
225                 if (IWorkbenchAdapter.class.equals(arg0)) {
226                         return new IWorkbenchAdapter() {
227                                 public Object[] getChildren(Object o) {
228                                         Object[] children = null;
229                                         try {
230                                                 IStackFrame[] frames = getStackFrames();
231                                                 if (null != frames) {
232                                                         children = new Object[frames.length];
233                                                         for (int i = 0; i < frames.length; ++i)
234                                                                 children[i] = frames[i];
235                                                 }
236                                         } catch (DebugException x) {
237                                                 PHPeclipsePlugin.log("Unable to get stack frames.", x);
238                                         }
239                                         return children;
240                                 }
241
242                                 public ImageDescriptor getImageDescriptor(Object object) {
243                                         return null;
244                                 }
245
246                                 public String getLabel(Object o) {
247                                         throw new UnsupportedOperationException();
248                                 }
249
250                                 public Object getParent(Object o) {
251                                         return getDebugTarget();
252                                 }
253                         };
254                 }
255                 return null;
256         }
257
258         public void setStackFrames(PHPStackFrame[] frames) {
259                 this.frames = frames;
260         }
261
262         public String getName() {
263                 String name = this.name;
264                 if (isSuspended())
265                         name = name + " (suspended)";
266                 return name;
267         }
268
269         public void setName(String name) {
270                 this.name = name;
271         }
272
273         /*
274          * protected void createName(SuspensionPoint suspensionPoint) { this.name =
275          * "PHP Thread - " + this.getId() ; if (suspensionPoint != null) { this.name += " (" +
276          * suspensionPoint + ")" ; } }
277          */
278
279         public int getId() {
280                 return id;
281         }
282
283         public void setId(int id) {
284                 this.id = id;
285         }
286
287 }