fdb3b81948fc5de0731b0bee75d48156ebcdfca7
[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     Christian Perkonig - cperkonig@gmx.at
12 **********************************************************************/
13 package net.sourceforge.phpdt.internal.debug.core.model;
14
15 import java.util.Vector;
16
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.debug.core.DebugException;
19 import org.eclipse.debug.core.ILaunch;
20 import org.eclipse.debug.core.model.IDebugTarget;
21 import org.eclipse.debug.core.model.IValue;
22 import org.eclipse.debug.core.model.IVariable;
23
24 public class PHPVariable implements IVariable {
25
26         
27         private PHPValue fValue;
28         private String fName;
29         private PHPStackFrame fStackFrame;
30         private PHPVariable fParent;
31         private String fLongName;
32         
33         PHPVariable(){
34                 this(null,"",null,"",PHPValue.PEVT_UNKNOWN,null);
35         }
36         
37         PHPVariable(PHPStackFrame frame,String name, PHPVariable parent,String value,int valueType,Vector subitems)
38         {
39                 this.fStackFrame=frame;
40                 this.fValue=new PHPValue(frame,value,valueType,subitems);
41                 this.fParent=parent;
42                 setName(name);
43         }
44         
45         private void setName(String name) {
46                 if ((fParent==null) || (fParent.getName()=="")) {
47                         fLongName=name;
48                         fName=name;
49                         return;
50                 }
51                 switch (fParent.getReferenceType()) {
52                 case PHPValue.PEVT_ARRAY :
53                         fName="['"+name+"']";
54                         fLongName=fParent.getLongName()+fName;
55                         break;
56                 case PHPValue.PEVT_OBJECT :
57                         fName=name;
58                         fLongName=fParent.getLongName()+"."+fName;
59                         break;
60                 default :
61                         fName=name;
62                         fLongName=name;
63                         break;
64                 }
65         }
66         /**
67          * @see org.eclipse.debug.core.model.IVariable#getValue()
68          */
69         public IValue getValue() {
70                 return fValue;
71         }
72
73         /**
74          * @see org.eclipse.debug.core.model.IVariable#getfName()
75          */
76         public String getName()  {
77                 return fName;
78         }
79         
80         public PHPVariable getParent()
81         {
82                 return fParent;
83         }
84         
85         public void setParent(PHPVariable parent)
86         {
87                 this.fParent=parent;
88                 fLongName=parent.getLongName()+fName;
89         }
90         
91         public String getLongName() {
92                 return fLongName;
93         }
94
95         /**
96          * @see org.eclipse.debug.core.model.IVariable#getReferenceTypefName()
97          */
98         public String getReferenceTypeName() {
99                 return fValue.getReferenceTypeName();
100         }
101         
102         public int getReferenceType() {
103                 return fValue.getReferenceType();
104         }
105         
106         public int setReferenceType(int type) {
107                 return ((PHPValue) getValue()).setReferenceType(type);
108         }
109
110         /**
111          * @see org.eclipse.debug.core.model.IVariable#hasValueChanged()
112          */
113         public boolean hasValueChanged() throws DebugException {
114                 // TODO Auto-generated method stub
115                 return false;
116         }
117         
118            /**
119      * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
120      */
121     public String getModelIdentifier() {
122         return getDebugTarget().getModelIdentifier();
123     }
124
125     /**
126      * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
127      */
128     public IDebugTarget getDebugTarget() {
129         return fStackFrame.getDebugTarget();
130     }
131
132     /**
133      * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
134      */
135     public ILaunch getLaunch() {
136         return getDebugTarget().getLaunch();
137     }
138
139
140         /**
141          * @see org.eclipse.debug.core.model.IValueModification#setValue(java.lang.String)
142          */
143         public void setValue(String expression) throws DebugException {
144                 String evalString;
145                 if(fValue.getReferenceType()==PHPValue.PEVT_STRING)
146                         evalString=fLongName+"=\""+expression+"\"";
147                 else
148                         evalString=fLongName+"="+expression;
149                 PHPVariable[] vars=fStackFrame.getPHPDBGProxy().eval(fStackFrame,evalString);
150                 setValue(vars[0].fValue);
151         }
152
153         /**
154          * @see org.eclipse.debug.core.model.IValueModification#setValue(org.eclipse.debug.core.model.IValue)
155          */
156         public void setValue(IValue value) throws DebugException {
157                 // TODO Auto-generated method stub
158                 this.fValue=(PHPValue)value;
159         }
160
161         /**
162          * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
163          */
164         public boolean supportsValueModification() {
165                 return true;
166         }
167
168         /**
169          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(java.lang.String)
170          */
171         public boolean verifyValue(String expression) throws DebugException {
172                 // TODO Auto-generated method stub
173                 return true;
174         }
175
176         /**
177          * @see org.eclipse.debug.core.model.IValueModification#verifyValue(org.eclipse.debug.core.model.IValue)
178          */
179         public boolean verifyValue(IValue value) throws DebugException {
180                 // TODO Auto-generated method stub
181                 return false;
182         }
183
184     /**
185      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
186      */
187     public Object getAdapter(Class adapter) {
188         return Platform.getAdapterManager().getAdapter(this, adapter);
189     }
190     
191     public String toString() {
192         int type=-1;
193         String str="";
194         
195         switch (getReferenceType())
196                 {
197                 case PHPValue.PEVT_ARRAY :
198                         int elements=fValue.getVariables().length;
199                         switch (elements) {
200                         case 0:
201                                         str= this.getName() + " [no elements]";
202                                         break;
203                                 case 1:
204                                         str= this.getName() + " [1 element]";
205                                         break;
206                                 default:
207                                         str= this.getName() + " [" + elements + " elements]";
208                                         break;
209                         }
210                         break;
211                 case PHPValue.PEVT_OBJECT :
212                         str =this.getName() + " [class: " + fValue.getValueString() + "]";
213                         break;
214                 case PHPValue.PEVT_STRING :
215                         str =this.getName() + " = \"" + fValue.getValueString() +"\"" ;
216                                 break;
217                 case PHPValue.PEVT_SOFTREF :
218                 default :
219                         str =this.getName() + " = " + fValue.getValueString();
220                         break;
221                 }
222         
223                 return str;
224         
225     }
226 }