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