e20be3d5230762f59c7f23a08b4b5e410a977f5c
[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.Collections;
16 import java.util.Iterator;
17 import java.util.Vector;
18
19 import org.eclipse.debug.core.DebugException;
20 import org.eclipse.debug.core.ILaunch;
21 import org.eclipse.debug.core.model.IDebugTarget;
22 import org.eclipse.debug.core.model.IValue;
23 import org.eclipse.debug.core.model.IVariable;
24
25 /**
26  * PHPValue object belongs to a PHPVariable (is a member of PHPVariable).
27  * A PHPValue itself can have PHPVariables as children.
28  *
29  */
30 public class PHPValue implements IValue {
31
32         final static String[] PEV_NAMES = {
33                         "undefined",                                                                                    // 0
34                         "long",                                                                                                 // 1
35                         "double",                                                                                               // 2
36                         "string",                                                                                               // 3
37                         "array",                                                                                                // 4
38                         "object",                                                                                               // 5
39                         "boolean",                                                                                              // 6
40                         "resource",                                                                                     // 7
41                         "reference",                                                                                    // 8
42                         "soft reference" };                                                                     // 9
43
44         public final static int PEVT_UNKNOWN            = 0;
45         public final static int PEVT_LONG               = 1;
46         public final static int PEVT_DOUBLE             = 2;
47         public final static int PEVT_STRING             = 3;
48         public final static int PEVT_ARRAY              = 4;
49         public final static int PEVT_OBJECT             = 5;
50         public final static int PEVT_BOOLEAN            = 6;
51         public final static int PEVT_RESOURCE   = 7;
52         public final static int PEVT_REF                        = 8;
53         public final static int PEVT_SOFTREF            = 9;
54
55         private int                     fValueType;                                                     // The type of this value (see the PEVT_... values)
56         //private boolean               hasChildren;                                                    // This value (variable) has children (more variables)
57         private String          fValueString;                                                   // The value of this variable as text
58         private Vector          fVariables;                                                     // The children of this variable (other variables) if any
59         private PHPStackFrame   fStackFrame;                                                    // The stackframe this value (variable) belongs to
60         private boolean                 fHasChanged;                                                    // The value has changed between two suspends
61                                                                                                                                         // This variable was moved from PHPVariable due to the fact,
62                                                                                                                                         // that two PHPVariables can reference the same PHPValue,
63                                                                                                                                         // so only the first PHPVariable would win, when we check the variable tree
64                                                                                                                                         // for changed values.
65         private boolean                 fSorted;
66
67         /**
68          *
69          */
70         PHPValue() {
71                 this(null, "", PEVT_UNKNOWN, null);                                             // Creates an empty value
72         }
73
74         /**
75          *
76          * @param frame       The stackframe this value (and variable) belongs to.
77          * @param value       The value of this value.
78          * @param fValueType  The type of this value (see the PEVT_... values).
79          * @param subitems    This value has subitems.
80          */
81         public PHPValue(PHPStackFrame frame, String value, int fValueType, Vector subitems) {
82                 this.fValueType = fValueType;
83                 this.fValueString = value;
84                 this.fStackFrame = frame;
85                 this.fHasChanged = false;
86                 this.fSorted = false;
87
88                 if (subitems != null) {                                                                         // If there are children for this value (variable)
89                         this.fVariables = new Vector(subitems);                                 // Then add the children to this value (variable)
90                 } else {
91                         this.fVariables = new Vector();                                                 // Create an empty vector
92                 }
93         }
94
95         /**
96          *
97          * @param item
98          */
99         public Vector addVariable(Vector item) {
100                 if (item != null) {                                                                             // If there is something we want to add
101                         this.fVariables.addAll(item);
102                         this.fSorted = false;
103                 }
104
105                 return this.fVariables;
106         }
107
108         /**
109          *
110          * @param parent
111          */
112         public void setParent(PHPVariable parent) {
113                 if (!fVariables.isEmpty()) {                                                            // If we have child variables
114                         Iterator iter = fVariables.iterator();                                  // Create an iterator for the children
115
116                         while (iter.hasNext()) {                                                                // As long as we have children
117                                 ((PHPVariable) iter.next()).setParent(parent);          // Set all child's parent
118                         }
119                 }
120         }
121
122         /**
123          * @see org.eclipse.debug.core.model.IValue#getReferenceTypeName()
124          */
125         public String getReferenceTypeName() {
126                 return PEV_NAMES[fValueType];
127         }
128
129         /**
130          *
131          */
132         public int getReferenceType() {
133                 return fValueType;
134         }
135
136         /**
137          * @param type Set the reference type (see the PEVT_... values).
138          */
139         public int setReferenceType(int type) {
140                 return fValueType = type;
141         }
142
143         /**
144          * This method is called whenever this value (variable) is changed.
145          *
146          * @param value The changed value for this variable.
147          */
148         public void setValueString(String value) {
149                 fValueString = value;
150         }
151
152         /**
153          * @see org.eclipse.debug.core.model.IValue#getfValueString()
154          */
155         public String getValueString() {
156                 return fValueString;
157         }
158
159         /**
160          * @see org.eclipse.debug.core.model.IValue#isAllocated()
161          */
162         public boolean isAllocated() throws DebugException {
163                 return false;
164         }
165
166         /**
167          * @see org.eclipse.debug.core.model.IValue#getVariables()
168          *
169          * @return The array of child variable for this value (variable).
170          */
171         public IVariable[] getVariables() {
172                 return (PHPVariable[]) getChildVariables().toArray(
173                                 new PHPVariable[fVariables.size()]);
174         }
175
176         /**
177          *
178          */
179         public Vector getChildVariables() {
180                 if (!fSorted) {
181                         Collections.sort(fVariables, new PHPVariableComparator());
182                         fSorted = true;
183                 }
184
185                 return fVariables;
186         }
187
188         /**
189          * @see org.eclipse.debug.core.model.IValue#hasVariables()
190          *
191          * @return
192          * <ul>
193          * <li> <code>true</code> if this value (variable) has child variables
194          * <li> <code>false</code> if no child variable available
195          * </ul>
196          */
197         public boolean hasVariables() throws DebugException {
198                 // return (!fVariables.isEmpty());
199                 return (fVariables.size() != 0);
200         }
201
202         /**
203          * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
204          */
205         public String getModelIdentifier() {
206                 // TODO Auto-generated method stub
207                 return null;
208         }
209
210         /**
211          * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
212          */
213         public IDebugTarget getDebugTarget() {
214                 return fStackFrame.getDebugTarget();
215         }
216
217         /**
218          * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
219          */
220         public ILaunch getLaunch() {
221                 return getDebugTarget().getLaunch();
222         }
223
224         /**
225          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
226          */
227         public Object getAdapter(Class adapter) {
228                 return null;
229         }
230
231         public boolean hasValueChanged() throws DebugException {
232                 return fHasChanged;
233         }
234
235         public void setValueChanged(boolean changed) {
236                 fHasChanged = changed;
237         }
238
239         /*
240          * ONLY FOR net.sourceforge.phpdt.internal.debug.core.model.PHPDBGEvalString#copyItems(PHPVariable, PHPValue)
241          */
242         protected void setVariables(Vector variables) {
243                 fVariables = variables;
244         }
245
246         /*
247          * ONLY FOR net.sourceforge.phpdt.internal.debug.core.model.PHPDBGEvalString#copyItems(PHPVariable, PHPValue)
248          */
249         protected Object clone() throws CloneNotSupportedException {
250                 PHPValue val = new PHPValue();
251                 val.fValueType = fValueType;
252                 val.fValueString = fValueString;
253                 val.fVariables = fVariables;
254                 val.fStackFrame = fStackFrame;
255                 val.fHasChanged = fHasChanged;
256                 val.fSorted = fSorted;
257                 return val;
258         }
259
260 }