1) Although dbg will be dropped from being supported or bundled with PHPeclipse,...
[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
129                         case PHPValue.PEVT_OBJECT:
130                                 fLongName = fParent.getLongName() + "->" + fName;
131                                 break;
132
133                         default:
134                                 fLongName = fName;
135                                 break;
136                 }
137         }
138
139         /**
140          *
141          */
142         public String getLongName() {
143                 return fLongName;
144         }
145
146         /**
147          * @see org.eclipse.debug.core.model.IVariable#getReferenceTypefName()
148          */
149         public String getReferenceTypeName() {
150                 return fValue.getReferenceTypeName();
151         }
152
153         /**
154          *
155          */
156         public int getReferenceType() {
157                 return fValue.getReferenceType();
158         }
159
160         /**
161          *
162          */
163         public int setReferenceType(int type) {
164                 return ((PHPValue) getValue()).setReferenceType(type);
165         }
166
167         /**
168          * @see org.eclipse.debug.core.model.IVariable#hasValueChanged()
169          */
170         public boolean hasValueChanged() throws DebugException {
171                 return fValue.hasValueChanged();
172         }
173
174         /**
175          *
176          * @param changed This method is called after a suspend when the list of
177          *                variables is updated, to mark that this variable has a changed
178          *                value. The variable view will show this variable in
179          *                a different color.
180          */
181         public void setValueChanged(boolean changed) {
182                 fValue.setValueChanged(changed);
183         }
184
185         /**
186          * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
187          */
188         public String getModelIdentifier() {
189                 return getDebugTarget().getModelIdentifier();
190         }
191
192         /**
193          * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
194          */
195         public IDebugTarget getDebugTarget() {
196                 return fStackFrame.getDebugTarget();
197         }
198
199         /**
200          * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
201          */
202         public ILaunch getLaunch() {
203                 return getDebugTarget().getLaunch();
204         }
205
206         /**
207          * @see org.eclipse.debug.core.model.IValueModification#setValue(java.lang.String)
208          */
209         public void setValue (String expression) throws DebugException {
210                 String evalString;
211
212                 if (fValue.getReferenceType () == PHPValue.PEVT_STRING) {
213                         evalString = fLongName + "=\"" + expression + "\"";
214                 }
215                 else {
216                         evalString = fLongName + "=" + expression;
217                 }
218
219                 PHPVariable[] vars = fStackFrame.getPHPDBGProxy ().eval (fStackFrame, evalString);
220 ///*
221                 if (vars == null || vars.length == 0) {
222                         vars = fStackFrame.getPHPDBGProxy ().eval (fStackFrame, fLongName);
223
224                         if (vars == null || vars.length == 0) {
225                                 int     code   = 0;
226                                 String  msg    = "Could not set " + expression + " to " + fLongName;
227                                 Status  status = new Status (Status.ERROR, PHPDebugCorePlugin.PLUGIN_ID, code, msg, null);
228                                 PHPDebugCorePlugin.log (status);
229
230                                 throw new DebugException (status);
231                         }
232                 }
233 //*/
234                 fValue = vars[0].fValue;
235 ///*
236                 if (fValue.hasVariables ()) {                                     // set parent if new value has children
237                         Vector variables = fValue.getChildVariables ();
238
239                         for (int i = 0; i < variables.size (); i++) {
240                                 PHPVariable var = (PHPVariable) variables.get (i);
241
242                                 var.setParent (this);
243
244                                 if (fValue.getReferenceType() == PHPValue.PEVT_ARRAY) {   // adjust name if value type is array
245                                         var.setName (var.getName ());                         // (still bare name. make "['name']")
246                                 }
247                         }
248                 }
249
250                 DebugPlugin.getDefault().fireDebugEventSet (new DebugEvent[] {
251                                  new DebugEvent (this, DebugEvent.CHANGE, DebugEvent.CONTENT) });
252 //*/
253         }
254
255         /**
256          * @see org.eclipse.debug.core.model.IValueModification#setValue(org.eclipse.debug.core.model.IValue)
257          */
258         public void setValue(IValue value) throws DebugException {
259                 this.fValue = (PHPValue) value;
260         }
261
262         /**
263          * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
264          */
265         public boolean supportsValueModification() {
266                 return fModifiable;
267         }
268
269         /**
270          * Set whether this variable can be modified (default is true)
271          *
272          * for Global Variables root element only
273          */
274         public void setModifiable(boolean modifiable) {
275                 fModifiable = modifiable;
276         }
277
278         /**
279          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(java.lang.String)
280          */
281         public boolean verifyValue(String expression) throws DebugException {
282                 // TODO Auto-generated method stub
283                 return true;
284         }
285
286         /**
287          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(org.eclipse.debug.core.model.IValue)
288          */
289         public boolean verifyValue(IValue value) throws DebugException {
290                 // TODO Auto-generated method stub
291                 return false;
292         }
293
294         /**
295          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
296          */
297         public Object getAdapter(Class adapter) {
298                 return Platform.getAdapterManager().getAdapter(this, adapter);
299         }
300
301         /**
302          * This method is called from variable view and denominates the variables
303          * with a type specific explanation.
304          */
305         public String toString() {
306                 String str = "";
307
308                 switch (getReferenceType()) {
309                 case PHPValue.PEVT_ARRAY:                                                                                                // Variable is an array
310                         int elements = fValue.getVariables().length;                                             // Get the number of child elements
311
312                         switch (elements) {                                                                                                      // Switch for the number of child elements
313                         case 0:                                                                                                                          // We have no child element
314                                 str = this.getName() + " [no elements]";                                                 // string => 'varname [no elements]'
315                                 break;
316
317                         case 1:                                                                                                                          // We have exactly one child element
318                                 str = this.getName() + " [1 element]";                                                   // string => 'varname [1 element]'
319                                 break;
320
321                         default:                                                                                                                         // We have more than one element
322                                 str = this.getName() + " [" + elements + " elements]";                   // string => 'varname [x elements]'
323                                 break;
324                         }
325                         break;
326
327                 case PHPValue.PEVT_OBJECT:                                                                                               // Variable is an object
328                         str = this.getName() + " [class: " + fValue.getValueString() + "]";      // string => 'varname [class: varvalue]'
329                         break;
330
331                 case PHPValue.PEVT_STRING:                                                                                               // Variable is a string
332                         str = this.getName() + " = \"" + fValue.getValueString() + "\"";         // string => 'varname = "varvalue"'
333                         break;
334
335                 case PHPValue.PEVT_SOFTREF:                                                                                              // Variable is a soft reference
336                 default:                                                                                                                                 // or anything else
337                         str = this.getName() + " = " + fValue.getValueString();                          // string => 'varname = varvalue'
338                         break;
339                 }
340
341                 return str;
342         }
343
344         /*
345          * ONLY FOR net.sourceforge.phpdt.internal.debug.core.model.PHPDBGEvalString#copyItems(PHPVariable, PHPValue)
346          */
347         protected Object clone() throws CloneNotSupportedException {
348                 PHPVariable var = new PHPVariable();
349
350                 var.fValue              = fValue;
351                 var.fName               = fName;
352                 var.fStackFrame = fStackFrame;
353                 var.fParent     = fParent;
354                 var.fLongName   = fLongName;
355                 var.fModifiable = fModifiable;
356
357                 return var;
358         }
359
360 }