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