efc55ea8947de959fca19559609e91204f3d5871
[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 extends PHPDebugElement implements IThread {
28
29         private PHPStackFrame[] frames;
30
31         private PHPDebugTarget 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 (PHPDebugTarget target, int id) {
80                 super (target);
81                 this.target = target;
82                 this.setId(id);
83         }
84
85         public IStackFrame[] getStackFrames() throws DebugException {
86                 return frames;
87         }
88
89         public int getStackFramesSize() {
90                 return frames.length;
91         }
92
93         public boolean hasStackFrames() {
94                 if (frames == null) {
95                         return false;
96                 }
97                 return frames.length > 0;
98         }
99
100         public int getPriority() throws DebugException {
101                 return 0;
102         }
103
104         public IStackFrame getTopStackFrame() throws DebugException {
105                 if (frames == null || frames.length == 0) {
106                         return null;
107                 }
108                 return (IStackFrame) frames[0];
109         }
110
111         public IBreakpoint[] getBreakpoints() {
112                 return null;
113         }
114
115         public String getModelIdentifier() {
116                 return this.getDebugTarget().getModelIdentifier();
117         }
118
119         public IDebugTarget getDebugTarget() {
120                 return target;
121         }
122
123         public void setDebugTarget(PHPDebugTarget target) {
124                 this.target = target;
125         }
126
127         public ILaunch getLaunch() {
128                 return this.getDebugTarget().getLaunch();
129         }
130
131         public synchronized boolean canResume() {
132                 return isSuspended();
133         }
134
135         public synchronized boolean canSuspend() {
136                 return !isSuspended();
137         }
138
139         public synchronized boolean isSuspended() {
140                 return state.isSuspended;
141         }
142
143         protected void prepareForResume() {
144                 state.setSuspended(false);
145                 this.frames = null;
146                 DebugEvent ev = new DebugEvent(this, DebugEvent.RESUME,
147                                 DebugEvent.CLIENT_REQUEST);
148                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
149         }
150
151         public synchronized void resume() throws DebugException {
152                 if (!isSuspended())
153                         return;
154                 this.prepareForResume();
155                 ((PHPDebugTarget) this.getDebugTarget()).getPHPDBGProxy().resume();
156         }
157
158         /*
159          * public void doSuspend(SuspensionPoint suspensionPoint) { //
160          * this.getPHPDebuggerProxy().readFrames(this);
161          * this.createName(suspensionPoint) ; this.suspend() ; }
162          */
163
164         public synchronized void suspend() throws DebugException {
165                 if (isSuspended())
166                         return;
167                 state.setSuspended(true);
168                 state.setStepping(false);
169                 getDebugTarget().suspend();
170                 DebugEvent ev = new DebugEvent(this, DebugEvent.SUSPEND,
171                                 DebugEvent.BREAKPOINT);
172                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
173         }
174
175         public boolean canStepInto() {
176                 return isSuspended() && isStepping() && this.hasStackFrames();
177         }
178
179         public boolean canStepOver() {
180                 return isSuspended() && isStepping() && this.hasStackFrames();
181         }
182
183         public boolean canStepReturn() {
184                 return isSuspended() && isStepping() && this.hasStackFrames();
185         }
186
187         public boolean isStepping() {
188                 return state.isStepping();
189         }
190
191         public void stepInto() throws DebugException {
192                 try { state.setStepping(true); }
193                 catch (IllegalStateException x) {
194                         throw new DebugException(PHPeclipsePlugin.error(x));
195                 }
196                 this.frames = null;
197                 frames[0].stepInto();
198         }
199
200         public void stepOver() throws DebugException {
201                 state.setStepping(true);
202                 this.frames = null;
203                 frames[0].stepOver();
204         }
205
206         public void stepReturn() throws DebugException {
207         }
208
209         public boolean canTerminate() {
210                 return !isTerminated();
211         }
212
213         public boolean isTerminated() {
214                 return state.isTerminated();
215         }
216
217         public synchronized void terminate() throws DebugException {
218                 if (isTerminated())
219                         return;
220                 state.setTerminated(true);
221                 this.frames = null;
222                 getDebugTarget().terminate();
223         }
224
225         public Object getAdapter(Class arg0) {
226                 if (IWorkbenchAdapter.class.equals(arg0)) {
227                         return new IWorkbenchAdapter() {
228                                 public Object[] getChildren(Object o) {
229                                         Object[] children = null;
230                                         try {
231                                                 IStackFrame[] frames = getStackFrames();
232                                                 if (null != frames) {
233                                                         children = new Object[frames.length];
234                                                         for (int i = 0; i < frames.length; ++i)
235                                                                 children[i] = frames[i];
236                                                 }
237                                         } catch (DebugException x) {
238                                                 PHPeclipsePlugin.log("Unable to get stack frames.", x);
239                                         }
240                                         return children;
241                                 }
242
243                                 public ImageDescriptor getImageDescriptor(Object object) {
244                                         return null;
245                                 }
246
247                                 public String getLabel(Object o) {
248                                         throw new UnsupportedOperationException();
249                                 }
250
251                                 public Object getParent(Object o) {
252                                         return getDebugTarget();
253                                 }
254                         };
255                 }
256                 return null;
257         }
258
259         public void setStackFrames(PHPStackFrame[] frames) {
260                 this.frames = frames;
261         }
262
263         public String getName() {
264                 String name = this.name;
265                 if (isSuspended())
266                         name = name + " (suspended)";
267                 return name;
268         }
269
270         public void setName(String name) {
271                 this.name = name;
272         }
273
274         /*
275          * protected void createName(SuspensionPoint suspensionPoint) { this.name =
276          * "PHP Thread - " + this.getId() ; if (suspensionPoint != null) { this.name += " (" +
277          * suspensionPoint + ")" ; } }
278          */
279
280         public int getId() {
281                 return id;
282         }
283
284         public void setId(int id) {
285                 this.id = id;
286         }
287
288 }