Merge xdebug from 1.3.x.
[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.CoreException;
12 import org.eclipse.debug.core.DebugException;
13 import org.eclipse.debug.core.DebugEvent;
14 import org.eclipse.debug.core.model.IValue;
15 import org.eclipse.debug.core.model.IVariable;
16 import org.w3c.dom.Node;
17 import org.eclipse.debug.core.model.IWatchExpressionDelegate;
18 /**
19  * @author Axel
20  *
21  * TODO To change the template for this generated type comment go to
22  * Window - Preferences - Java - Code Style - Code Templates
23  */
24 public class XDebugVariable  extends XDebugElement implements IVariable/*, IWatchExpressionFactoryAdapter*/ {
25         public static final int VARTYPE_UNKNOWN = -1;
26         public static final int VARTYPE_UNINITIALIZED = 0;
27         public static final int VARTYPE_STRING = 1;
28         public static final int VARTYPE_INT = 2;
29         public static final int VARTYPE_FLOAT = 3;
30         public static final int VARTYPE_ARRAY = 8;
31         public static final int VARTYPE_HASH = 9;
32         public static final int VARTYPE_OBJECT = 10;
33         public static final int VARTYPE_RESOURCE = 11;
34         
35         private int fNumChildren;
36         private String fName;
37         private String fFullName;
38         private String fEncoding;
39         private XDebugStackFrame fFrame;
40         private XDebugAbstractValue fValue;
41         private String fFacet;
42         
43         /**
44          * Constructs a variable contained in the given stack frame
45          * with the given name.
46          * 
47          * @param frame owning stack frame
48          * @param name variable name
49          */
50         public XDebugVariable(XDebugStackFrame frame, Node property) {
51                 if (frame != null ) {
52                         //super((XDebugTarget) frame.getDebugTarget());
53                         fFrame = frame;
54                 }
55
56                 String address = PHPDebugUtils.getAttributeValue(property,"address");
57
58                 fName = PHPDebugUtils.getAttributeValue(property,"name");
59                 if ("".equals(fName)) {
60                         fName = address;
61                 } /*else {
62                         varName = Name;
63                 }*/
64                 
65                 fFullName = PHPDebugUtils.getAttributeValue(property,"fullname");
66                 fEncoding = PHPDebugUtils.getAttributeValue(property,"encoding");
67                 if (PHPDebugUtils.getAttributeValue(property,"numchildren").equals("")) {
68                         fNumChildren = 0;
69                 } else {
70                         fNumChildren = Integer.parseInt(PHPDebugUtils.getAttributeValue(property, "numchildren"));
71                 }
72
73                 String typeName = PHPDebugUtils.getAttributeValue(property, "type");
74
75                 fFacet = PHPDebugUtils.getAttributeValue(property, "facet");
76
77                 if (typeName.equals("int") ) 
78                         fValue= new XDebugIntValue(this,property,typeName);
79                 else if (typeName.equals("float") ) 
80                         fValue= new XDebugFloatValue(this,property,typeName);
81                 else if (typeName.equals("bool") ) 
82                         fValue= new XDebugBooleanValue(this,property,typeName);
83                 else if (typeName.equals("string") )
84                         fValue= new XDebugStringValue(this,property,typeName);
85                 else if (typeName.equals("array") )
86                         fValue= new XDebugArrayValue(this,property,typeName);
87                 else if (typeName.equals("hash") )
88                         fValue= new XDebugArrayValue(this,property,typeName);
89                 else if (typeName.equals("object") )
90                         fValue= new XDebugArrayValue(this,property,typeName);
91                 else
92                         fValue= new XDebugValue(this,property,typeName);
93         }
94                 
95         /* (non-Javadoc)
96          * @see org.eclipse.debug.core.model.IVariable#getValue()
97          */
98         public IValue getValue() throws DebugException {
99                 return fValue;
100 //              return ((XDebugTarget)getDebugTarget()).getVariableValue(this);
101         }
102         
103         /* (non-Javadoc)
104          * @see org.eclipse.debug.core.model.IVariable#getName()
105          */
106         public String getName() throws DebugException {
107                 if (fFullName.endsWith("]"))
108                         return fFullName.substring(fFullName.lastIndexOf('['));
109                 else
110                         return fName;
111         }
112         
113         /* (non-Javadoc)
114          * @see org.eclipse.debug.core.model.IVariable#getReferenceTypeName()
115          */
116         public String getReferenceTypeName() throws DebugException {
117                 return fValue.getReferenceTypeName();
118         }
119         
120         /* (non-Javadoc)
121          * @see org.eclipse.debug.core.model.IVariable#hasValueChanged()
122          */
123         public boolean hasValueChanged() throws DebugException {
124                 return fValue.hasChanged();
125         }
126         
127         /* (non-Javadoc)
128          * @see org.eclipse.debug.core.model.IValueModification#setValue(java.lang.String)
129          */
130         public void setValue(String expression) throws DebugException {
131                 if(fValue.setValue(expression))
132                         fireEvent(new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.CONTENT));
133                 //fValue.setValueA(expression);
134         }
135         
136         /* (non-Javadoc)
137          * @see org.eclipse.debug.core.model.IValueModification#setValue(org.eclipse.debug.core.model.IValue)
138          */
139         public void setValue(IValue value) throws DebugException {
140                 fValue.setValueB(value);
141         }
142
143         /* (non-Javadoc)
144          * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
145          */
146         public boolean supportsValueModification() {
147                 return fValue.supportsValueModification();
148         }
149
150         /* (non-Javadoc)
151          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(java.lang.String)
152          */
153         public boolean verifyValue(String expression) throws DebugException {
154                 return fValue.verifyValue(expression);
155         }
156
157         /* (non-Javadoc)
158          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(org.eclipse.debug.core.model.IValue)
159          */
160         public boolean verifyValue(IValue value) throws DebugException {
161                 return false;
162         }
163         
164         /**
165          * Returns the stack frame owning this variable.
166          * 
167          * @return the stack frame owning this variable
168          */
169         protected XDebugStackFrame getStackFrame() {
170                 return fFrame;
171         }
172         
173         public void setStackFrame(XDebugStackFrame frame) {
174                 fFrame = frame;
175                 super.setDebugTarget((XDebugTarget) frame.getDebugTarget());
176         }
177
178 //      public int getType() {
179 //              return fType;
180 //      }
181
182         public String getValueString() throws DebugException {
183                 return fValue.getValueString();
184         }
185         
186         /*public setValueString(String newValueStrig) throws DebugException {
187                 fValue.setValueString getValueString();
188         }*/
189         
190         public boolean hasChildren() {
191                 return (fNumChildren > 0);
192         }
193
194         public boolean isArray() {
195                 return (fValue.isArray());
196         }
197
198         public String getEncoding() {
199                 return fEncoding;
200         }
201
202         public String getVisibility() {
203                 return fFacet;
204         }
205
206         public void setEncoding(String encoding) {
207                 fEncoding = encoding;
208         }
209         
210         public String toString() {
211                 return fValue.toString();
212         }
213
214         public String getFullName() {
215                 return fFullName;
216         }
217
218         public int getNumChildren() {
219                 return fNumChildren;
220         }
221
222         public void setNumChildren(int numChildren) {
223                 fNumChildren = numChildren;
224         }
225         
226         public void setChange(IVariable oldVariable)  throws DebugException {
227                 XDebugAbstractValue oldValue = null;
228                 IVariable[] newVariable = null;
229                 IVariable[] oldVariable1 = null;
230
231                 try {
232                         oldValue = (XDebugAbstractValue) oldVariable.getValue();
233                 } catch (DebugException e) {
234                 }
235
236                 try {
237                         oldVariable1 = ((XDebugVariable)oldVariable).getValue().getVariables();
238                         newVariable = fValue.getVariables();
239                 } catch (DebugException e) {
240                 }
241                 
242                 
243                 if(((XDebugVariable) oldVariable).getNumChildren() > 0) {
244                         if ( this.getNumChildren() == 0) {
245                                 try {
246                                         if (!fValue.getValueString().equals(((XDebugAbstractValue)oldValue).getValueString())) {
247                                                 fValue.sethasChanged(true);
248                                         } else {
249                                                 fValue.sethasChanged(false);                                            
250                                         }
251                                 } catch (DebugException e) {
252                                 }
253                         } else {
254                                 for(int i = 0; i < newVariable.length; i++) {
255                                         ((XDebugVariable)newVariable[i]).setChange(((XDebugVariable)oldVariable1[i]));
256                                 }
257                         }                       
258                 } else {
259                         if ( this.getNumChildren() == 0) {
260                                 // controllare anche il tipo (unknow)
261                                 //if (!fValue.getValueString().equals("uninitialized")) {
262                                 if (!fValue.getValueString().equals(oldValue.getValueString())) {
263                                         fValue.sethasChanged(true);
264                                 } else {
265                                         fValue.sethasChanged(false);                                    
266                                 }
267                         } else {
268                                 //IVariable dummy = new XDebugVariable("UNKNOWN", "");
269                                 /*dummy.setValue("uninitialized");
270                                 for(int i = 0; i < newVariable.length; i++) {
271                                         ((XDebugVariable)newVariable[i]).setChange(dummy);
272                                 }
273                                 fValue.sethasChanged(true);*/                           
274                         }
275                 }
276         }
277         /**
278          * Creates and returns an expression for the specified variable
279          * which is used to created an {@link org.eclipse.debug.core.model.IWatchExpression}.
280          * 
281          * @param variable variable a watch expression is required for
282          * @return text used to create a watch expression
283          * @exception org.eclipse.core.runtime.CoreException if unable to create a watch
284          *  expression
285          */
286         /*public String createWatchExpression(IVariable variable) throws CoreException {
287                 
288         }*/     
289 }