Fixed the debugger client to work in Eclipse 3.1.
[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         private IDebugTarget target;
31         private boolean isSuspended = false;
32         private boolean isTerminated = false;
33         private boolean isStepping = false;
34         private String name ;
35         private int id ;
36         
37         public PHPThread(IDebugTarget target, int id) {
38                 this.target = target;
39                 this.setId(id) ;
40                 this.createName() ;
41         }
42
43         public IStackFrame[] getStackFrames() throws DebugException {
44                 return frames;
45         }
46
47         public int getStackFramesSize() {
48                 return frames.length;
49         }
50
51         public boolean hasStackFrames() {
52                 if (frames == null) {
53                         return false;
54                 }
55                 return frames.length > 0;
56         }
57
58         public int getPriority() throws DebugException {
59                 return 0;
60         }
61
62         public IStackFrame getTopStackFrame() throws DebugException {
63                 if (frames == null || frames.length == 0) {
64                         return null;
65                 }
66                 return (IStackFrame) frames[0];
67         }
68
69
70         public IBreakpoint[] getBreakpoints() {
71                 return null;
72         }
73
74         public String getModelIdentifier() {
75                 return this.getDebugTarget().getModelIdentifier();
76         }
77
78         public IDebugTarget getDebugTarget() {
79                 return target;
80         }
81
82         public void setDebugTarget(IDebugTarget target) {
83                 this.target= target;
84         }
85         
86         public ILaunch getLaunch() {
87                 return this.getDebugTarget().getLaunch();
88         }
89
90         public boolean canResume() {
91                 return isSuspended;
92         }
93
94         public boolean canSuspend() {
95                 return !isSuspended;
96         }
97
98         public boolean isSuspended() {
99                 return isSuspended;
100         }
101
102         protected void setSuspended(boolean isSuspended) {
103                 this.isSuspended = isSuspended;
104         }
105
106         protected void prepareForResume() {
107                 isSuspended = false;
108                 this.createName() ;
109                 this.frames = null ;
110                 DebugEvent ev = new DebugEvent(this, DebugEvent.RESUME, DebugEvent.CLIENT_REQUEST);
111                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });            
112         }
113
114         public void resume() throws DebugException {
115                 this.prepareForResume() ;
116                 ((PHPDebugTarget) this.getDebugTarget()).getPHPDBGProxy().resume();
117         }
118
119         /*
120         public void doSuspend(SuspensionPoint suspensionPoint) {
121 //              this.getPHPDebuggerProxy().readFrames(this);
122                 this.createName(suspensionPoint) ;
123                 this.suspend() ;
124         }
125         */
126
127         public void suspend()  {
128                 isStepping = false;
129                 isSuspended = true;
130                 DebugEvent ev = new DebugEvent(this, DebugEvent.SUSPEND, DebugEvent.BREAKPOINT);
131                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { ev });
132         }
133
134         public boolean canStepInto() {
135                 return isSuspended && this.hasStackFrames();
136         }
137
138         public boolean canStepOver() {
139                 return isSuspended && this.hasStackFrames();
140         }
141
142         public boolean canStepReturn() {
143                 return false;
144         }
145
146         public boolean isStepping() {
147                 return isStepping;
148         }
149
150         public void stepInto() throws DebugException {
151                 isStepping = true ;
152                 this.createName() ;
153                 this.frames = null ;
154                 frames[0].stepInto();
155         }
156
157         public void stepOver() throws DebugException {
158                 isStepping = true ;             
159                 this.createName() ;
160                 this.frames = null ;            
161                 frames[0].stepOver() ;
162         }
163
164         public void stepReturn() throws DebugException {
165         }
166
167         public boolean canTerminate() {
168                 return !isTerminated;
169         }
170
171         public boolean isTerminated() {
172                 return isTerminated;
173         }
174
175         public void terminate() throws DebugException {
176                 isTerminated = true;
177                 this.frames = null;
178                 getDebugTarget().terminate();
179         }
180
181         public Object getAdapter(Class arg0) {
182                 if (IWorkbenchAdapter.class.equals(arg0)) {
183                         return new IWorkbenchAdapter() {
184                                 public Object[] getChildren(Object o) {
185                                         Object[] children = null;
186                                         try {
187                                                 IStackFrame[] frames = getStackFrames();
188                                                 if (null != frames) {
189                                                         children = new Object[frames.length];
190                                                         for (int i = 0; i < frames.length; ++i) 
191                                                                 children[i] = frames[i];
192                                                 }
193                                         } catch (DebugException x) {
194                                                 PHPeclipsePlugin.log("Unable to get stack frames.", x);
195                                         }
196                                         return children;
197                                 }
198                                 public ImageDescriptor getImageDescriptor(Object object) {
199                                         return null;
200                                 }
201                                 public String getLabel(Object o) {
202                                         throw new UnsupportedOperationException();
203                                 }
204                                 public Object getParent(Object o) {
205                                         return getDebugTarget();
206                                 }
207                         };
208                 }
209                 return null;
210         }
211
212         public void setStackFrames(PHPStackFrame[] frames) {
213                 this.frames = frames;
214         }
215
216         public String getName() {
217                 return name;
218         }
219
220         public void setName(String name) {
221                 this.name = name;
222         }
223
224         protected void createName() {
225                 //this.createName(null) ;       
226         }
227
228         /*      
229         protected void createName(SuspensionPoint suspensionPoint) {
230                 this.name = "PHP Thread - " + this.getId()  ;
231                 if (suspensionPoint != null) { 
232                         this.name += " (" + suspensionPoint + ")" ;
233                 }
234         }
235         */
236
237         public int getId() {
238                 return id;
239         }
240
241         public void setId(int id) {
242                 this.id = id;
243         }
244
245 }