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
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;
15 import java.util.Vector;
17 import net.sourceforge.phpdt.internal.debug.core.PHPDebugCorePlugin;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.debug.core.DebugEvent;
22 import org.eclipse.debug.core.DebugException;
23 import org.eclipse.debug.core.DebugPlugin;
24 import org.eclipse.debug.core.ILaunch;
25 import org.eclipse.debug.core.model.IDebugTarget;
26 import org.eclipse.debug.core.model.IValue;
27 import org.eclipse.debug.core.model.IVariable;
32 public class PHPVariable implements IVariable {
34 private PHPValue fValue; // The value of this variable
35 private String fName; // The name of the variable
36 private PHPStackFrame fStackFrame; // The stackframe this variable belongs to
37 private PHPVariable fParent; // The parent variable (a back link)
38 private String fLongName; // The qualified name
44 this(null, "", null, "", PHPValue.PEVT_UNKNOWN, null); // create an empty variable (a simple dummy node?)
49 * @param frame The stackframe this variable belongs to
50 * @param name The name for this variable
51 * @param parent The parent variable if this is not the root
52 * @param value The value of this variable which is a simple value or again a variable
53 * @param valueType The type of the value (e.g. int, double, string etc.) @see PHPValue
56 public PHPVariable(PHPStackFrame frame, String name, PHPVariable parent,
57 String value, int valueType, Vector subitems) {
58 this.fStackFrame = frame;
59 this.fValue = new PHPValue(frame, value, valueType, subitems);
60 this.fParent = parent;
69 public void setName(String name) {
70 if ((fParent == null) || // If we have no parent for this variable
71 (fParent.getName() == "")) { // or we have a parent which is just a simple node ???
72 fLongName = name; // Set the long name
73 fName = name; // and set the name
78 switch (fParent.getReferenceType()) { // Get the type of the parent variable
79 case PHPValue.PEVT_ARRAY: // It's an array
80 fName = "['" + name + "']"; // So set the variable name as [name]
81 fLongName = fParent.getLongName() + fName; // Set the longname to parentVariableLongname[name]
84 case PHPValue.PEVT_OBJECT: // It's an object
85 fName = name; // Set the name to name
86 fLongName = fParent.getLongName() + "->" + fName; // Set the longname to parentVariableLongname.name
90 fName = name; // Set the name to name
91 fLongName = name; // Set the Longname to name
97 * @see org.eclipse.debug.core.model.IVariable#getValue()
99 public IValue getValue() {
104 * @see org.eclipse.debug.core.model.IVariable#getfName()
106 public String getName() {
113 public PHPVariable getParent() {
120 public void setParent(PHPVariable parent) {
121 this.fParent = parent;
122 fLongName = parent.getLongName() + fName;
128 public String getLongName() {
133 * @see org.eclipse.debug.core.model.IVariable#getReferenceTypefName()
135 public String getReferenceTypeName() {
136 return fValue.getReferenceTypeName();
142 public int getReferenceType() {
143 return fValue.getReferenceType();
149 public int setReferenceType(int type) {
150 return ((PHPValue) getValue()).setReferenceType(type);
154 * @see org.eclipse.debug.core.model.IVariable#hasValueChanged()
156 public boolean hasValueChanged() throws DebugException {
157 return fValue.hasValueChanged();
162 * @param changed This method is called after a suspend when the list of
163 * variables is updated, to mark that this variable has a changed
164 * value. The variable view will show this variable in
167 public void setValueChanged(boolean changed) {
168 fValue.setValueChanged(changed);
172 * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
174 public String getModelIdentifier() {
175 return getDebugTarget().getModelIdentifier();
179 * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
181 public IDebugTarget getDebugTarget() {
182 return fStackFrame.getDebugTarget();
186 * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
188 public ILaunch getLaunch() {
189 return getDebugTarget().getLaunch();
193 * @see org.eclipse.debug.core.model.IValueModification#setValue(java.lang.String)
195 public void setValue(String expression) throws DebugException {
197 if (fValue.getReferenceType() == PHPValue.PEVT_STRING)
198 evalString = fLongName + "=\"" + expression + "\"";
200 evalString = fLongName + "=" + expression;
201 PHPVariable[] vars = fStackFrame.getPHPDBGProxy().eval(fStackFrame,
204 if (vars == null || vars.length == 0) {
205 vars = fStackFrame.getPHPDBGProxy().eval(fStackFrame, fLongName);
206 if (vars == null || vars.length == 0) {
208 String msg = "Could not set " + expression + " to " + fLongName;
209 Status status = new Status(Status.ERROR,
210 PHPDebugCorePlugin.PLUGIN_ID, code, msg, null);
211 PHPDebugCorePlugin.log(status);
212 throw new DebugException(status);
216 setValue(vars[0].fValue);
218 DebugPlugin.getDefault().fireDebugEventSet(
219 new DebugEvent[] { new DebugEvent(this, DebugEvent.CHANGE,
220 DebugEvent.CONTENT) });
224 * @see org.eclipse.debug.core.model.IValueModification#setValue(org.eclipse.debug.core.model.IValue)
226 public void setValue(IValue value) throws DebugException {
227 this.fValue = (PHPValue) value;
231 * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
233 public boolean supportsValueModification() {
238 * @see org.eclipse.debug.core.model.IValueModification#verifyValue(java.lang.String)
240 public boolean verifyValue(String expression) throws DebugException {
241 // TODO Auto-generated method stub
246 * @see org.eclipse.debug.core.model.IValueModification#verifyValue(org.eclipse.debug.core.model.IValue)
248 public boolean verifyValue(IValue value) throws DebugException {
249 // TODO Auto-generated method stub
254 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
256 public Object getAdapter(Class adapter) {
257 return Platform.getAdapterManager().getAdapter(this, adapter);
261 * This method is called from variable view and denominates the variables
262 * with a type specific explanation.
264 public String toString() {
268 switch (getReferenceType()) {
269 case PHPValue.PEVT_ARRAY: // Variable is an array
270 int elements = fValue.getVariables().length; // Get the number of child elements
272 switch (elements) { // Switch for the number of child elements
273 case 0: // We have no child element
274 str = this.getName() + " [no elements]"; // string => 'varname [no elements]'
277 case 1: // We have exactly one child element
278 str = this.getName() + " [1 element]"; // string => 'varname [1 element]'
281 default: // We have more than one element
282 str = this.getName() + " [" + elements + " elements]"; // string => 'varname [x elements]'
287 case PHPValue.PEVT_OBJECT: // Variable is an object
288 str = this.getName() + " [class: " + fValue.getValueString() + "]"; // string => 'varname [class: varvalue]'
291 case PHPValue.PEVT_STRING: // Variable is a string
292 str = this.getName() + " = \"" + fValue.getValueString() + "\""; // string => 'varname = "varvalue"'
295 case PHPValue.PEVT_SOFTREF: // Variable is a soft reference
296 default: // or anything else
297 str = this.getName() + " = " + fValue.getValueString(); // string => 'varname = varvalue'