fix bug 678.
[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 if (typeName.equals("resource") )
92                         fValue= new XDebugResourceValue(this, property, typeName);
93                 else
94                         fValue= new XDebugValue(this,property,typeName);
95         }
96                 
97         /* (non-Javadoc)
98          * @see org.eclipse.debug.core.model.IVariable#getValue()
99          */
100         public IValue getValue() throws DebugException {
101                 return fValue;
102 //              return ((XDebugTarget)getDebugTarget()).getVariableValue(this);
103         }
104         
105         /* (non-Javadoc)
106          * @see org.eclipse.debug.core.model.IVariable#getName()
107          */
108         public String getName() throws DebugException {
109                 if (fFullName.endsWith("]"))
110                         return fFullName.substring(fFullName.lastIndexOf('['));
111                 else
112                         return fName;
113         }
114         
115         /* (non-Javadoc)
116          * @see org.eclipse.debug.core.model.IVariable#getReferenceTypeName()
117          */
118         public String getReferenceTypeName() throws DebugException {
119                 return fValue.getReferenceTypeName();
120         }
121         
122         /* (non-Javadoc)
123          * @see org.eclipse.debug.core.model.IVariable#hasValueChanged()
124          */
125         public boolean hasValueChanged() throws DebugException {
126                 return fValue.hasChanged();
127         }
128         
129         /* (non-Javadoc)
130          * @see org.eclipse.debug.core.model.IValueModification#setValue(java.lang.String)
131          */
132         public void setValue(String expression) throws DebugException {
133                 if(fValue.setValue(expression))
134                         fireEvent(new DebugEvent(this, DebugEvent.CHANGE, DebugEvent.CONTENT));
135                 //fValue.setValueA(expression);
136         }
137         
138         /* (non-Javadoc)
139          * @see org.eclipse.debug.core.model.IValueModification#setValue(org.eclipse.debug.core.model.IValue)
140          */
141         public void setValue(IValue value) throws DebugException {
142                 fValue.setValueB(value);
143         }
144
145         /* (non-Javadoc)
146          * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
147          */
148         public boolean supportsValueModification() {
149                 return fValue.supportsValueModification();
150         }
151
152         /* (non-Javadoc)
153          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(java.lang.String)
154          */
155         public boolean verifyValue(String expression) throws DebugException {
156                 return fValue.verifyValue(expression);
157         }
158
159         /* (non-Javadoc)
160          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(org.eclipse.debug.core.model.IValue)
161          */
162         public boolean verifyValue(IValue value) throws DebugException {
163                 return false;
164         }
165         
166         /**
167          * Returns the stack frame owning this variable.
168          * 
169          * @return the stack frame owning this variable
170          */
171         protected XDebugStackFrame getStackFrame() {
172                 return fFrame;
173         }
174         
175         public void setStackFrame(XDebugStackFrame frame) {
176                 fFrame = frame;
177                 super.setDebugTarget((XDebugTarget) frame.getDebugTarget());
178         }
179
180 //      public int getType() {
181 //              return fType;
182 //      }
183
184         public String getValueString() throws DebugException {
185                 return fValue.getValueString();
186         }
187         
188         /*public setValueString(String newValueStrig) throws DebugException {
189                 fValue.setValueString getValueString();
190         }*/
191         
192         public boolean hasChildren() {
193                 return (fNumChildren > 0);
194         }
195
196         public boolean isArray() {
197                 return (fValue.isArray());
198         }
199
200         public String getEncoding() {
201                 return fEncoding;
202         }
203
204         public String getVisibility() {
205                 return fFacet;
206         }
207
208         public void setEncoding(String encoding) {
209                 fEncoding = encoding;
210         }
211         
212         public String toString() {
213                 return null;
214         }
215
216         public String getFullName() {
217                 return fFullName;
218         }
219
220         public int getNumChildren() {
221                 return fNumChildren;
222         }
223
224         public void setNumChildren(int numChildren) {
225                 fNumChildren = numChildren;
226         }
227         
228         public void setChange(IVariable oldVariable)  throws DebugException {
229                 XDebugAbstractValue oldValue = null;
230                 IVariable[] newVariable = null;
231                 IVariable[] oldVariable1 = null;
232
233                 try {
234                         oldValue = (XDebugAbstractValue) oldVariable.getValue();
235                 } catch (DebugException e) {
236                 }
237
238                 try {
239                         oldVariable1 = ((XDebugVariable)oldVariable).getValue().getVariables();
240                         newVariable = fValue.getVariables();
241                 } catch (DebugException e) {
242                 }
243                 
244                 
245                 if(((XDebugVariable) oldVariable).getNumChildren() > 0) {
246                         if ( this.getNumChildren() == 0) {
247                                 try {
248                                         if (!fValue.getValueString().equals(((XDebugAbstractValue)oldValue).getValueString())) {
249                                                 fValue.sethasChanged(true);
250                                         } else {
251                                                 fValue.sethasChanged(false);                                            
252                                         }
253                                 } catch (DebugException e) {
254                                 }
255                         } else {
256                                 for(int i = 0; i < newVariable.length; i++) {
257                                         ((XDebugVariable)newVariable[i]).setChange(((XDebugVariable)oldVariable1[i]));
258                                 }
259                         }                       
260                 } else {
261                         if ( this.getNumChildren() == 0) {
262                                 // controllare anche il tipo (unknow)
263                                 //if (!fValue.getValueString().equals("uninitialized")) {
264                                 if (!fValue.getValueString().equals(oldValue.getValueString())) {
265                                         fValue.sethasChanged(true);
266                                 } else {
267                                         fValue.sethasChanged(false);                                    
268                                 }
269                         } else {
270                                 //IVariable dummy = new XDebugVariable("UNKNOWN", "");
271                                 /*dummy.setValue("uninitialized");
272                                 for(int i = 0; i < newVariable.length; i++) {
273                                         ((XDebugVariable)newVariable[i]).setChange(dummy);
274                                 }
275                                 fValue.sethasChanged(true);*/                           
276                         }
277                 }
278         }
279         /**
280          * Creates and returns an expression for the specified variable
281          * which is used to created an {@link org.eclipse.debug.core.model.IWatchExpression}.
282          * 
283          * @param variable variable a watch expression is required for
284          * @return text used to create a watch expression
285          * @exception org.eclipse.core.runtime.CoreException if unable to create a watch
286          *  expression
287          */
288         /*public String createWatchExpression(IVariable variable) throws CoreException {
289                 
290         }*/     
291 }