94d3c996cb08ce753cdc8799f655d08148e5d091
[phpeclipse.git] /
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                 String str = "";
301
302                 switch (getReferenceType()) {
303                 case PHPValue.PEVT_ARRAY:                                                                                                // Variable is an array
304                         int elements = fValue.getVariables().length;                                             // Get the number of child elements
305
306                         switch (elements) {                                                                                                      // Switch for the number of child elements
307                         case 0:                                                                                                                          // We have no child element
308                                 str = this.getName() + " [no elements]";                                                 // string => 'varname [no elements]'
309                                 break;
310
311                         case 1:                                                                                                                          // We have exactly one child element
312                                 str = this.getName() + " [1 element]";                                                   // string => 'varname [1 element]'
313                                 break;
314
315                         default:                                                                                                                         // We have more than one element
316                                 str = this.getName() + " [" + elements + " elements]";                   // string => 'varname [x elements]'
317                                 break;
318                         }
319                         break;
320
321                 case PHPValue.PEVT_OBJECT:                                                                                               // Variable is an object
322                         str = this.getName() + " [class: " + fValue.getValueString() + "]";      // string => 'varname [class: varvalue]'
323                         break;
324
325                 case PHPValue.PEVT_STRING:                                                                                               // Variable is a string
326                         str = this.getName() + " = \"" + fValue.getValueString() + "\"";         // string => 'varname = "varvalue"'
327                         break;
328
329                 case PHPValue.PEVT_SOFTREF:                                                                                              // Variable is a soft reference
330                 default:                                                                                                                                 // or anything else
331                         str = this.getName() + " = " + fValue.getValueString();                          // string => 'varname = varvalue'
332                         break;
333                 }
334
335                 return str;
336         }
337
338         /*
339          * ONLY FOR net.sourceforge.phpdt.internal.debug.core.model.PHPDBGEvalString#copyItems(PHPVariable, PHPValue)
340          */
341         protected Object clone() throws CloneNotSupportedException {
342                 PHPVariable var = new PHPVariable();
343                 var.fValue = fValue;
344                 var.fName = fName;
345                 var.fStackFrame = fStackFrame;
346                 var.fParent = fParent;
347                 var.fLongName = fLongName;
348                 var.fModifiable = fModifiable;
349                 return var;
350         }
351
352 }