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