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