First submit for debug plugin
[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 **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.core.model;
13
14 import org.eclipse.core.runtime.Platform;
15 import org.eclipse.debug.core.DebugException;
16 import org.eclipse.debug.core.ILaunch;
17 import org.eclipse.debug.core.model.IDebugTarget;
18 import org.eclipse.debug.core.model.IValue;
19 import org.eclipse.debug.core.model.IVariable;
20
21 public class PHPVariable implements IVariable {
22
23         private boolean isBoolean;
24         private boolean isString;
25         private boolean isResource;
26     private boolean isObject;
27     private boolean isLocal;
28     private PHPStackFrame stackFrame;
29     private String name;
30     private String objectId;
31         private String className;
32     private PHPValue value;
33     private PHPVariable parent;
34
35     public PHPVariable(PHPStackFrame stackFrame, String name, String scope) {
36         this.initialize(stackFrame, name, scope, null, new PHPValue(this), "", "string");
37     }
38
39     public PHPVariable(PHPStackFrame stackFrame, String name, String scope, String value, String type, boolean hasChildren, String objectId, String className) {
40         this.initialize(stackFrame, name, scope, objectId, new PHPValue(this, value, type, hasChildren), className, type);
41     }
42
43         public PHPVariable(PHPStackFrame stackFrame, String name, String scope, boolean isRef, PHPValue varPHP) {
44                 if(isRef)
45                         this.initialize(stackFrame, name, scope, "-1", varPHP, "", "string");
46                 else
47                         this.initialize(stackFrame, name, scope, "-1", new PHPValue(this), "", "string");
48         }
49
50     protected final void initialize(PHPStackFrame stackFrame, String name, String scope, String objectId, PHPValue value, String className, String typeName) {
51         this.stackFrame = stackFrame;
52         this.value = value;
53         this.name = name;
54         this.objectId = objectId;
55         // scope
56                 this.isLocal = scope.equals("local");
57                 // type
58                 this.isObject = typeName.equals("object");
59                 this.isResource = typeName.equals("resource");
60                 this.isBoolean = typeName.equals("boolean");
61                 this.isString = typeName.equals("string");
62                 
63         this.className = className;
64     }
65
66     /**
67      * @see org.eclipse.debug.core.model.IVariable#getValue()
68      */
69     public IValue getValue() {
70         return value;
71     }
72
73     /**
74      * @see org.eclipse.debug.core.model.IVariable#getName()
75      */
76     public String getName() {
77         return name;
78     }
79
80     /**
81      * @see org.eclipse.debug.core.model.IVariable#getReferenceTypeName()
82      */
83     public String getReferenceTypeName() {
84         return "RefTypeName";
85     }
86
87     /**
88      * @see org.eclipse.debug.core.model.IVariable#hasValueChanged()
89      */
90     public boolean hasValueChanged() throws DebugException {
91         return false;
92     }
93
94     /**
95      * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
96      */
97     public String getModelIdentifier() {
98         return this.getDebugTarget().getModelIdentifier();
99     }
100
101     /**
102      * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
103      */
104     public IDebugTarget getDebugTarget() {
105         return stackFrame.getDebugTarget();
106     }
107
108     /**
109      * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
110      */
111     public ILaunch getLaunch() {
112         return this.getDebugTarget().getLaunch();
113     }
114
115     /**
116      * @see org.eclipse.debug.core.model.IValueModification#setValue(String)
117      */
118     public void setValue(String expression) throws DebugException {
119     }
120
121     /**
122      * @see org.eclipse.debug.core.model.IValueModification#setValue(IValue)
123      */
124     public void setValue(IValue value) throws DebugException {
125     }
126
127     /**
128      * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
129      */
130     public boolean supportsValueModification() {
131         return false;
132     }
133
134     /**
135      * @see org.eclipse.debug.core.model.IValueModification#verifyValue(String)
136      */
137     public boolean verifyValue(String expression) throws DebugException {
138         return false;
139     }
140
141     /**
142      * @see org.eclipse.debug.core.model.IValueModification#verifyValue(IValue)
143      */
144     public boolean verifyValue(IValue value) throws DebugException {
145         return false;
146     }
147
148     /**
149      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
150      */
151     public Object getAdapter(Class adapter) {
152         return Platform.getAdapterManager().getAdapter(this, adapter);
153     }
154
155     public String toString() {
156         
157                 if(this.isObject()) {
158                         return this.getName() + " [class: " + this.getClassName() + "]";
159                 } else if(this.isResource()) {
160                         return this.getName() + " [resource: " + this.getClassName() + "] = " + ((PHPValue) this.getValue());
161                 } else if (this.isHashValue()) {
162                 int elements= Integer.parseInt(((PHPValue) this.getValue()).getValueString());
163                 switch (elements) {
164                         case 0:
165                                         return this.getName() + " [no elements]";
166                                 case 1:
167                                         return this.getName() + " [1 element]";
168                                 default:
169                                         return this.getName() + " [" + elements + " elements]";
170                 }
171         } else {
172                         return this.getName() + " = " + ((PHPValue) this.getValue());
173         }
174
175     }
176
177     public PHPStackFrame getStackFrame() {
178         return stackFrame;
179     }
180
181     public PHPVariable getParent() {
182         return parent;
183     }
184
185     public void setParent(PHPVariable parent) {
186         this.parent = parent;
187     }
188
189     public String getQualifiedName() {
190         return this.getName();
191     }
192
193         public boolean isBoolean() {
194                 return isBoolean;
195         }
196
197         public boolean isResource() {
198                 return isResource;
199         }
200         
201         public boolean isString() {
202                 return isString;
203         }
204
205     public boolean isLocal() {
206         return isLocal;
207     }
208
209     public boolean isObject() {
210         return isObject;
211     }
212
213     public String getObjectId() {
214         return objectId;
215     }
216
217     public boolean isHashValue() {
218         try {
219                         return ((PHPValue) this.getValue()).hasVariables();
220         } catch (DebugException e) {
221                         return false; 
222         }
223     }
224
225         public String getClassName() {
226                 return className;
227         }
228 }