Modified: 1764120 - Variables View doesn't show global vars in class context
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.core / src / net / sourceforge / phpdt / internal / debug / core / model / PHPVariable.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     Christian Perkonig - cperkonig@gmx.at
12 **********************************************************************/
13 package net.sourceforge.phpdt.internal.debug.core.model;
14
15 import java.util.Vector;
16
17 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
18
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.debug.core.DebugEvent;
22 import org.eclipse.debug.core.DebugException;
23 import org.eclipse.debug.core.DebugPlugin;
24 import org.eclipse.debug.core.ILaunch;
25 import org.eclipse.debug.core.model.IDebugTarget;
26 import org.eclipse.debug.core.model.IValue;
27 import org.eclipse.debug.core.model.IVariable;
28
29 /**
30  *
31  */
32 public class PHPVariable implements IVariable {
33
34         private PHPValue                fValue;                                                          // The value of this variable
35         private String          fName;                                                           // The name of the variable
36         private PHPStackFrame   fStackFrame;                                             // The stackframe this variable belongs to
37         private PHPVariable     fParent;                                                         // The parent variable (a back link)
38         private String          fLongName;                                                       // The qualified name
39         private boolean         fModifiable = true;
40
41         /**
42          *
43          */
44         PHPVariable() {
45                 this(null, "", null, "", PHPValue.PEVT_UNKNOWN, null); // create an empty variable (a simple dummy node?)
46         }
47
48         /**
49          *
50          * @param frame     The stackframe this variable belongs to
51          * @param name      The name for this variable
52          * @param parent    The parent variable if this is not the root
53          * @param value     The value of this variable which is a simple value or again a variable
54          * @param valueType The type of the value (e.g. int, double, string etc.) @see PHPValue
55          * @param subitems
56          */
57         public PHPVariable(PHPStackFrame frame, String name, PHPVariable parent,
58                         String value, int valueType, Vector subitems) {
59                 this.fStackFrame = frame;
60                 this.fValue = new PHPValue(frame, value, valueType, subitems);
61                 this.fParent = parent;
62
63                 setName(name);
64         }
65
66         /**
67          *
68          * @param name
69          */
70         public void setName(String name) {
71                 if ((fParent == null) ||                                                                 // If we have no parent for this variable
72                                 (fParent.getName() == "")) {                                     //  or we have a parent which is just a simple node ???
73                         fLongName = name;                                                                        // Set the long name
74                         fName = name;                                                                            //  and set the name
75
76                         return;
77                 }
78
79                 switch (fParent.getReferenceType()) {                                    // Get the type of the parent variable
80                 case PHPValue.PEVT_ARRAY:                                                                // It's an array
81                         fName = "['" + name + "']";                                                      // So set the variable name as [name]
82                         fLongName = fParent.getLongName() + fName;                       // Set the longname to parentVariableLongname[name]
83                         break;
84
85                 case PHPValue.PEVT_OBJECT:                                                               // It's an object
86                         fName = name;                                                                            // Set the name to name
87                         fLongName = fParent.getLongName() + "->" + fName;        // Set the longname to parentVariableLongname.name
88                         break;
89
90                 default:
91                         fName = name;                                                                            // Set the name to name
92                         fLongName = name;                                                                        // Set the Longname to name
93                         break;
94                 }
95         }
96
97         /**
98          * @see org.eclipse.debug.core.model.IVariable#getValue()
99          */
100         public IValue getValue() {
101                 return fValue;
102         }
103
104         /**
105          * @see org.eclipse.debug.core.model.IVariable#getfName()
106          */
107         public String getName() {
108                 return fName;
109         }
110
111         /**
112          *
113          */
114         public PHPVariable getParent() {
115                 return fParent;
116         }
117
118         /**
119          *
120          */
121         public void setParent(PHPVariable parent) {
122                 this.fParent = parent;
123
124                 switch (fParent.getReferenceType()) {
125                 case PHPValue.PEVT_ARRAY:
126                         fLongName = fParent.getLongName() + fName;
127                         break;
128                 case PHPValue.PEVT_OBJECT:
129                         fLongName = fParent.getLongName() + "->" + fName;
130                         break;
131                 default:
132                         fLongName = fName;
133                         break;
134                 }
135         }
136
137         /**
138          *
139          */
140         public String getLongName() {
141                 return fLongName;
142         }
143
144         /**
145          * @see org.eclipse.debug.core.model.IVariable#getReferenceTypefName()
146          */
147         public String getReferenceTypeName() {
148                 return fValue.getReferenceTypeName();
149         }
150
151         /**
152          *
153          */
154         public int getReferenceType() {
155                 return fValue.getReferenceType();
156         }
157
158         /**
159          *
160          */
161         public int setReferenceType(int type) {
162                 return ((PHPValue) getValue()).setReferenceType(type);
163         }
164
165         /**
166          * @see org.eclipse.debug.core.model.IVariable#hasValueChanged()
167          */
168         public boolean hasValueChanged() throws DebugException {
169                 return fValue.hasValueChanged();
170         }
171
172         /**
173          *
174          * @param changed This method is called after a suspend when the list of
175          *                variables is updated, to mark that this variable has a changed
176          *                value. The variable view will show this variable in
177          *                a different color.
178          */
179         public void setValueChanged(boolean changed) {
180                 fValue.setValueChanged(changed);
181         }
182
183         /**
184          * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
185          */
186         public String getModelIdentifier() {
187                 return getDebugTarget().getModelIdentifier();
188         }
189
190         /**
191          * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
192          */
193         public IDebugTarget getDebugTarget() {
194                 return fStackFrame.getDebugTarget();
195         }
196
197         /**
198          * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
199          */
200         public ILaunch getLaunch() {
201                 return getDebugTarget().getLaunch();
202         }
203
204         /**
205          * @see org.eclipse.debug.core.model.IValueModification#setValue(java.lang.String)
206          */
207         public void setValue(String expression) throws DebugException {
208                 String evalString;
209                 if (fValue.getReferenceType() == PHPValue.PEVT_STRING)
210                         evalString = fLongName + "=\"" + expression + "\"";
211                 else
212                         evalString = fLongName + "=" + expression;
213                 PHPVariable[] vars = fStackFrame.getPHPDBGProxy().eval(fStackFrame,
214                                 evalString);
215
216                 if (vars == null || vars.length == 0) {
217                         vars = fStackFrame.getPHPDBGProxy().eval(fStackFrame, fLongName);
218                         if (vars == null || vars.length == 0) {
219                                 int code = 0;
220                                 String msg = "Could not set " + expression + " to " + fLongName;
221                                 Status status = new Status(Status.ERROR,
222                                                 PHPDebugCorePlugin.PLUGIN_ID, code, msg, null);
223                                 PHPDebugCorePlugin.log(status);
224                                 throw new DebugException(status);
225                         }
226                 }
227
228                 fValue = vars[0].fValue;
229
230                 // set parent if new value has children
231                 if (fValue.hasVariables()) {
232                         Vector variables = fValue.getChildVariables();
233                         for (int i = 0; i < variables.size(); i++) {
234                                 PHPVariable var = (PHPVariable) variables.get(i);
235                                 var.setParent(this);
236                                 // adjust name if value type is array
237                                 // (still bare name. make "['name']")
238                                 if (fValue.getReferenceType() == PHPValue.PEVT_ARRAY) {
239                                         var.setName(var.getName());
240                                 }
241                         }
242                 }
243
244                 DebugPlugin.getDefault().fireDebugEventSet(
245                                 new DebugEvent[] { new DebugEvent(this, DebugEvent.CHANGE,
246                                                 DebugEvent.CONTENT) });
247         }
248
249         /**
250          * @see org.eclipse.debug.core.model.IValueModification#setValue(org.eclipse.debug.core.model.IValue)
251          */
252         public void setValue(IValue value) throws DebugException {
253                 this.fValue = (PHPValue) value;
254         }
255
256         /**
257          * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
258          */
259         public boolean supportsValueModification() {
260                 return fModifiable;
261         }
262
263         /**
264          * Set whether this variable can be modified (default is true)
265          * 
266          * for Global Variables root element only
267          */
268         public void setModifiable(boolean modifiable) {
269                 fModifiable = modifiable;
270         }
271
272         /**
273          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(java.lang.String)
274          */
275         public boolean verifyValue(String expression) throws DebugException {
276                 // TODO Auto-generated method stub
277                 return true;
278         }
279
280         /**
281          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(org.eclipse.debug.core.model.IValue)
282          */
283         public boolean verifyValue(IValue value) throws DebugException {
284                 // TODO Auto-generated method stub
285                 return false;
286         }
287
288         /**
289          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
290          */
291         public Object getAdapter(Class adapter) {
292                 return Platform.getAdapterManager().getAdapter(this, adapter);
293         }
294
295         /**
296          * This method is called from variable view and denominates the variables
297          * with a type specific explanation.
298          */
299         public String toString() {
300                 int type = -1;
301                 String str = "";
302
303                 switch (getReferenceType()) {
304                 case PHPValue.PEVT_ARRAY:                                                                                                // Variable is an array
305                         int elements = fValue.getVariables().length;                                             // Get the number of child elements
306
307                         switch (elements) {                                                                                                      // Switch for the number of child elements
308                         case 0:                                                                                                                          // We have no child element
309                                 str = this.getName() + " [no elements]";                                                 // string => 'varname [no elements]'
310                                 break;
311
312                         case 1:                                                                                                                          // We have exactly one child element
313                                 str = this.getName() + " [1 element]";                                                   // string => 'varname [1 element]'
314                                 break;
315
316                         default:                                                                                                                         // We have more than one element
317                                 str = this.getName() + " [" + elements + " elements]";                   // string => 'varname [x elements]'
318                                 break;
319                         }
320                         break;
321
322                 case PHPValue.PEVT_OBJECT:                                                                                               // Variable is an object
323                         str = this.getName() + " [class: " + fValue.getValueString() + "]";      // string => 'varname [class: varvalue]'
324                         break;
325
326                 case PHPValue.PEVT_STRING:                                                                                               // Variable is a string
327                         str = this.getName() + " = \"" + fValue.getValueString() + "\"";         // string => 'varname = "varvalue"'
328                         break;
329
330                 case PHPValue.PEVT_SOFTREF:                                                                                              // Variable is a soft reference
331                 default:                                                                                                                                 // or anything else
332                         str = this.getName() + " = " + fValue.getValueString();                          // string => 'varname = varvalue'
333                         break;
334                 }
335
336                 return str;
337         }
338 }