1) Indeces of arrays will be surrounded by [''], so it's easier to distinguish betwee...
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.core / src / net / sourceforge / phpeclipse / xdebug / php / model / XDebugVariable.java
1 /*
2  * Created on 23.11.2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */
7 package net.sourceforge.phpeclipse.xdebug.php.model;
8
9 import net.sourceforge.phpeclipse.xdebug.core.PHPDebugUtils;
10
11 import org.eclipse.core.runtime.Platform;
12 import org.eclipse.debug.core.DebugException;
13 import org.eclipse.debug.core.model.IValue;
14 import org.eclipse.debug.core.model.IVariable;
15 import org.w3c.dom.Node;
16
17 /**
18  * @author Axel
19  *
20  * TODO To change the template for this generated type comment go to
21  * Window - Preferences - Java - Code Style - Code Templates
22  */
23 public class XDebugVariable extends XDebugElement implements IVariable {
24         private String                          fName;
25         private String              fNameFull;
26         private XDebugStackFrame        fFrame;
27         private XDebugAbstractValue fValue;
28         private String                          fFacet;
29         private XDebugVariable          fParent;                // The parent variable (a back link)
30         private String              fTypeName;
31
32
33         /**
34          * Constructs a variable contained in the given stack frame
35          * with the given name.
36          *
37          * @param frame owning stack frame
38          * @param name variable name
39          */
40         public XDebugVariable(XDebugStackFrame frame, Node property, XDebugVariable parent) throws DebugException {
41                 super((XDebugTarget) frame.getDebugTarget());
42
43                 this.fParent = parent;
44
45                 if (frame != null ) {
46                         fFrame = frame;
47                 }
48
49                 String name = PHPDebugUtils.getAttributeValue (property, "name"); // Get the name of the variable (XDebug submits the name without the '$' prefix)
50
51                 if (fParent == null) {
52                     fName = "$" + name;                                           // Prepend the variable 'short' name with the php variable prefix '$'
53                 }
54                 else {
55                     if (fParent.isArray ()) {
56                         fName = "['" + name + "']";
57                     }
58                     else if (fParent.isObject ()) {
59                         fName = name;
60                     }
61                     else {
62                         fName = name;                                             // If this is the root variable don't prepend prefix '$'
63                     }
64                 }
65
66                 fNameFull = PHPDebugUtils.getAttributeValue(property,"fullname");   // The fullname has the '$' prepended, but it is the fully qualified name
67                                                                                     // e.g. $myvar->child->a_variable. The fullname would be suitable to take for
68                                                                                     // the setting a watch expression
69
70                 if ("".equals(fName)) {
71                         fName = PHPDebugUtils.getAttributeValue (property, "address");
72                 }
73
74                 fFacet    = PHPDebugUtils.getAttributeValue (property, "facet");
75                 fTypeName = PHPDebugUtils.getAttributeValue (property, "type");
76
77                 if (fTypeName.equals ("int")) {
78                         fValue = new XDebugIntValue (frame, property);
79                 }
80                 else if (fTypeName.equals ("float")) {
81                         fValue = new XDebugFloatValue (frame, property);
82                 }
83                 else if (fTypeName.equals ("bool")) {
84                         fValue = new XDebugBooleanValue (frame, property);
85                 }
86                 else if (fTypeName.equals ("string")) {
87                         fValue = new XDebugStringValue (frame, property);
88                 }
89                 else if (fTypeName.equals ("array")) {
90                         fValue = new XDebugArrayValue (frame, property, this);
91                 }
92                 else if (fTypeName.equals ("object")) {
93                         fValue = new XDebugObjectValue (frame, property, this);
94                 }
95                 else if (fTypeName.equals ("resource")) {
96                         fValue = new XDebugResourceValue (frame, property);
97                 }
98                 else {
99                         fValue = new XDebugValue (frame, property);
100                 }
101         }
102
103         public boolean isArray () {
104             return fTypeName.equals ("array");
105         }
106
107     public boolean isObject () {
108         return fTypeName.equals ("object");
109     }
110
111     /**
112      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
113      */
114     public Object getAdapter(Class adapter) {
115         if (adapter.getName ().equals ("org.eclipse.debug.ui.actions.IWatchExpressionFactoryAdapter")) {
116             return new XDebugWatchExpressionFactoryAdapter ();
117         }
118         
119         return Platform.getAdapterManager().getAdapter (this, adapter);     
120     }
121
122         /* (non-Javadoc)
123          * @see org.eclipse.debug.core.model.IVariable#getValue()
124          */
125         public IValue getValue() {
126                 return fValue;
127         }
128
129         /* (non-Javadoc)
130          * @see org.eclipse.debug.core.model.IVariable#getName()
131          */
132         public String getName () {
133                 return fName;
134         }
135
136         /*
137          * @return The fully qualified name of the variable
138          */
139     public String getNameFull () {
140         return fNameFull;
141     }
142
143         /* (non-Javadoc)
144          * @see org.eclipse.debug.core.model.IVariable#getReferenceTypeName()
145          */
146         public String getReferenceTypeName() throws DebugException {
147                 return fValue.getReferenceTypeName();
148         }
149
150         /* (non-Javadoc)
151          * @see org.eclipse.debug.core.model.IVariable#hasValueChanged()
152          */
153         public boolean hasValueChanged() throws DebugException {
154                 return fValue.hasChanged();
155         }
156
157         /* (non-Javadoc)
158          * @see org.eclipse.debug.core.model.IValueModification#setValue(java.lang.String)
159          */
160         public void setValue(String expression) throws DebugException {
161                 if (fFrame.setVariableValue(this, expression)) {
162                         fValue.setValue(expression);
163                 }
164         }
165
166         /* (non-Javadoc)
167          * @see org.eclipse.debug.core.model.IValueModification#setValue(org.eclipse.debug.core.model.IValue)
168          */
169         public void setValue(IValue value) throws DebugException {
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.setChanged(changed);
181         }
182
183         /* (non-Javadoc)
184          * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
185          */
186         public boolean supportsValueModification() {
187                 return fValue.supportsValueModification();
188         }
189
190         /* (non-Javadoc)
191          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(java.lang.String)
192          */
193         public boolean verifyValue(String expression) throws DebugException {
194                 /*return true; */return fValue.verifyValue(expression);
195         }
196
197         /* (non-Javadoc)
198          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(org.eclipse.debug.core.model.IValue)
199          */
200         public boolean verifyValue(IValue value) throws DebugException {
201                 return false;
202         }
203
204         public String getValueString() throws DebugException {
205                 return fValue.getValueString();
206         }
207
208         public String getVisibility() {
209                 return fFacet;
210         }
211
212         /**
213          *
214          */
215         public XDebugVariable getParent() {
216                 return fParent;
217         }
218
219         /**
220          *
221          */
222         public void setParent(XDebugVariable parent) {
223                 this.fParent = parent;
224         }
225 }