a819140ca7e8dfeca7fbb86f9e8f039cf9618a96
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / model / PHPStackFrame.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 java.util.Vector;
15
16 import net.sourceforge.phpdt.internal.debug.core.PHPDBGProxy;
17
18 import org.eclipse.debug.core.DebugEvent;
19 import org.eclipse.debug.core.DebugException;
20 import org.eclipse.debug.core.DebugPlugin;
21 import org.eclipse.debug.core.ILaunch;
22 import org.eclipse.debug.core.model.IDebugTarget;
23 import org.eclipse.debug.core.model.IRegisterGroup;
24 import org.eclipse.debug.core.model.IStackFrame;
25 import org.eclipse.debug.core.model.IThread;
26 import org.eclipse.debug.core.model.IVariable;
27
28 /**
29  *
30  * TODO Remove the variables array and use only the varList vector
31  *      Have also to change hasVariables
32  */
33 public class PHPStackFrame extends PHPDebugElement implements IStackFrame, Comparable{
34
35         private PHPThread               thread;                         // The thread to which this stackframe belongs
36         private String                  file;               // The file name???
37         private int                     lineNumber;         //
38         private int                     index;              //
39         private int                     modno;              //
40         private PHPVariable[]   variables;          // The array of variables TODO: better introduce a vector?
41         private Vector          varList  = new Vector ();
42         private String                  description;        //
43
44         /**
45          *
46          * @param thread
47          * @param file
48          * @param line
49          * @param index
50          * @param desc
51          * @param modno
52          */
53         public PHPStackFrame (PHPThread thread, String file, int line, int index, String desc, int modno) {
54                 super (null);
55
56                 this.lineNumber  = line;
57                 this.index           = index;
58                 this.file            = file;
59                 this.thread      = thread;
60                 this.description = desc;
61                 this.modno               = modno;
62         }
63
64         /**
65          *
66          * @param thread
67          * @param file
68          * @param line
69          * @param index
70          */
71         public PHPStackFrame (PHPThread thread, String file, int line, int index) {
72                 super (null);
73
74                 this.lineNumber = line;
75                 this.index              = index;
76                 this.file               = file;
77                 this.thread     = thread;
78         }
79
80         /**
81          *
82          */
83         public IThread getThread () {
84                 return thread;
85         }
86
87         /**
88          * @param thread
89          */
90         public void setThread (PHPThread thread) {
91                 this.thread = thread;
92         }
93
94         /**
95          *
96          * This function returns the array of PHPVariables for this stackframe
97          * The PHPVariables should not change (newly build up) between two steps
98          * (or breaks).
99          * A PHPVariable with the same name but with different object ID is
100          * handled as a new variable.
101          *
102          * TODO Remove the intermediate storage array
103          *
104          * @return The array of PHPVariables for this stackframe.
105          */
106         public IVariable[] getVariables() throws DebugException {
107                 //PHPVariable[] variablesNew;                                 // The intermediate storage of the variable array we get from DBG proxy
108
109                 //variablesNew = this.getPHPDBGProxy ().readVariables (this); // Get the variable array from DBG proxy
110                 //variables    = variablesNew;                                // Store the array the stackframes member variable
111                 varList   = this.getPHPDBGProxy ().readVariables (this);
112
113                 variables = (PHPVariable[]) varList.toArray (new PHPVariable[varList.size ()]);
114
115                 return variables;                                           // Give the array back to user interface
116         }
117
118         /**
119          *
120          */
121         private PHPVariable findVariable (Vector varList, String varname) {
122                 PHPVariable     variable;
123                 PHPValue        value;
124                 int                     i;
125
126                 for (i = 0; i < varList.size (); i++) {                                         // For all variables
127                         variable = (PHPVariable) varList.get (i);                               // Get the variable
128                         value    = (PHPValue) variable.getValue ();                             // Get the value of the variable
129
130                         try {
131                                 if (value.hasVariables ()) {                                            // Does the variable/value have children
132                                         variable = findVariable (value.getChildVariables (), varname);
133
134                                         if (variable != null) {
135                                                 return variable;
136                                         }
137                                 }
138                                 else if ((variable.getName ()).equals (varname)) {      //
139                                         return variable;                                                                //
140                                 }
141                         }
142                         catch (DebugException e) {                                                              // That's, because of the hasVariables method
143                         }
144                 }
145
146                 return null;
147         }
148
149         /**
150          * This method is called from the UI (e.g. from PHPDebugHover
151          * to find the variable the mouse is pointing to)
152          *
153          * @param s The variable name we are looking for.
154          * @return
155          */
156         public IVariable findVariable (String s) throws DebugException {
157                 return (findVariable (varList, s));                                                     // Prefix the variable name with $
158         }
159
160         /**
161          *
162          */
163         public boolean hasVariables () throws DebugException {
164                 if (variables == null) {                                                                        // Do we have a variables array?
165                         return false;                                                                                   // No
166                 }
167
168                 return variables.length > 0;                                // Is there something within the array?
169         }
170
171         public int getLineNumber() {
172                 return lineNumber;
173         }
174
175         public void setLineNumber(int line) {
176                 lineNumber = line;
177         }
178
179         public int getCharStart() throws DebugException {
180                 // not supported
181                 return -1;
182         }
183
184         public int getCharEnd() throws DebugException {
185                 // not supported
186                 return -1;
187         }
188
189         public String getName() {
190                 if(!this.getDescription().equals(""))
191                         return this.getDescription() + " [line: " + this.getLineNumber() + "]";
192                 else
193                         return this.getFileName() + " [line: " + this.getLineNumber() + "]";
194         }
195
196         public String getFileName() {
197                 return file;
198         }
199
200         public void setDescription(String desc) {
201                 this.description= desc;
202         }
203
204         public String getDescription() {
205                 return this.description;
206         }
207
208         public IRegisterGroup[] getRegisterGroups() throws DebugException {
209                 return null;
210         }
211
212         public boolean hasRegisterGroups() throws DebugException {
213                 return false;
214         }
215
216         public String getModelIdentifier() {
217                 return this.getThread().getModelIdentifier();
218         }
219
220         public IDebugTarget getDebugTarget() {
221                 return this.getThread().getDebugTarget();
222         }
223
224         public ILaunch getLaunch() {
225                 return this.getDebugTarget().getLaunch();
226         }
227
228         public boolean canStepInto() {
229                 return canResume();
230         }
231
232         public boolean canStepOver() {
233                 return canResume();
234         }
235
236         public boolean canStepReturn() {
237                 return canResume();
238         }
239
240         public boolean isStepping() {
241                 return false;
242         }
243
244         /**
245          *
246          */
247         public void stepInto () throws DebugException {
248                 DebugEvent      ev;
249
250         thread.prepareForResume (DebugEvent.STEP_INTO);             // Don't know why, but this is necessary
251                 this.getPHPDBGProxy ().readStepIntoEnd (PHPStackFrame.this);
252
253                 ev = new DebugEvent (this.getThread (), DebugEvent.RESUME, DebugEvent.STEP_INTO);
254                 DebugPlugin.getDefault().fireDebugEventSet (new DebugEvent[] { ev });
255         }
256
257         /**
258          *
259          */
260         public void stepOver () throws DebugException {
261                 DebugEvent      ev;
262
263         thread.prepareForResume (DebugEvent.STEP_OVER);
264                 this.getPHPDBGProxy ().readStepOverEnd (PHPStackFrame.this) ;
265
266                 ev = new DebugEvent (this.getThread (), DebugEvent.RESUME, DebugEvent.STEP_OVER);
267                 DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev });
268         }
269
270         /**
271          *
272          */
273         public void stepReturn () throws DebugException {
274                 DebugEvent      ev;
275
276         thread.prepareForResume (DebugEvent.STEP_RETURN);
277                 this.getPHPDBGProxy ().readStepReturnEnd (PHPStackFrame.this) ;
278
279                 ev = new DebugEvent (this.getThread (), DebugEvent.RESUME, DebugEvent.STEP_RETURN);
280                 DebugPlugin.getDefault ().fireDebugEventSet (new DebugEvent[] { ev });
281         }
282
283
284         public boolean canResume() {
285                 return this.getThread().canResume();
286         }
287
288         public boolean canSuspend() {
289                 return this.getThread().canSuspend();
290         }
291
292         public boolean isSuspended() {
293                 return this.getThread().isSuspended();
294         }
295
296         public void resume() throws DebugException {
297                 this.getThread().resume();
298         }
299
300         public void suspend() throws DebugException {
301         }
302
303         public boolean canTerminate() {
304                 return this.getThread().canTerminate();
305         }
306
307         public boolean isTerminated() {
308                 return this.getThread().isTerminated();
309         }
310
311         public void terminate() throws DebugException {
312                 getPHPDBGProxy().stop();
313         }
314
315         public int getIndex() {
316                 return index;
317         }
318
319         public void setIndex (int index) {
320                 this.index = index;
321         }
322
323         public PHPDBGProxy getPHPDBGProxy() {
324                 PHPDebugTarget DebugTarget;
325
326                 DebugTarget = (PHPDebugTarget) thread.getDebugTarget ();
327
328                 return DebugTarget.getPHPDBGProxy ();
329         }
330
331         public void setFile(String file) {
332                 this.file = file;
333         }
334
335         public int getModNo() {
336                 return modno;
337         }
338
339         /**
340          * This function is needed when sorting the stackframes by their index numbers.
341          *
342          * @param obj The stackframe which this one is compared to.
343          * @return
344          * <ul>
345          * <li> -1 if the index of this stackframe is less.
346          * <li> 0 if the index of both stackfream is equal (should no happen).
347          * <li> 1 if the index of this stackfram is greater.
348          * </ul>
349          */
350         public int compareTo (Object obj)
351         {
352                 if (index < ((PHPStackFrame) obj).getIndex ()) {
353                         return -1;
354                 }
355                 else if (index > ((PHPStackFrame) obj).getIndex ()) {
356                         return 1;
357                 }
358
359                 return 0;
360         }
361 }