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
39 private boolean fModifiable = true;
45 this(null, "", null, "", PHPValue.PEVT_UNKNOWN, null); // create an empty variable (a simple dummy node?)
50 * @param frame The stackframe this variable belongs to
51 * @param name The name for this variable
52 * @param parent The parent variable if this is not the root
53 * @param value The value of this variable which is a simple value or again a variable
54 * @param valueType The type of the value (e.g. int, double, string etc.) @see PHPValue
57 public PHPVariable(PHPStackFrame frame, String name, PHPVariable parent,
58 String value, int valueType, Vector subitems) {
59 this.fStackFrame = frame;
60 this.fValue = new PHPValue(frame, value, valueType, subitems);
61 this.fParent = parent;
70 public void setName(String name) {
71 if ((fParent == null) || // If we have no parent for this variable
72 (fParent.getName() == "")) { // or we have a parent which is just a simple node ???
73 fLongName = name; // Set the long name
74 fName = name; // and set the name
79 switch (fParent.getReferenceType()) { // Get the type of the parent variable
80 case PHPValue.PEVT_ARRAY: // It's an array
81 fName = "['" + name + "']"; // So set the variable name as [name]
82 fLongName = fParent.getLongName() + fName; // Set the longname to parentVariableLongname[name]
85 case PHPValue.PEVT_OBJECT: // It's an object
86 fName = name; // Set the name to name
87 fLongName = fParent.getLongName() + "->" + fName; // Set the longname to parentVariableLongname.name
91 fName = name; // Set the name to name
92 fLongName = name; // Set the Longname to name
98 * @see org.eclipse.debug.core.model.IVariable#getValue()
100 public IValue getValue() {
105 * @see org.eclipse.debug.core.model.IVariable#getfName()
107 public String getName() {
114 public PHPVariable getParent() {
121 public void setParent(PHPVariable parent) {
122 this.fParent = parent;
124 switch (fParent.getReferenceType()) {
125 case PHPValue.PEVT_ARRAY:
126 fLongName = fParent.getLongName() + fName;
128 case PHPValue.PEVT_OBJECT:
129 fLongName = fParent.getLongName() + "->" + fName;
140 public String getLongName() {
145 * @see org.eclipse.debug.core.model.IVariable#getReferenceTypefName()
147 public String getReferenceTypeName() {
148 return fValue.getReferenceTypeName();
154 public int getReferenceType() {
155 return fValue.getReferenceType();
161 public int setReferenceType(int type) {
162 return ((PHPValue) getValue()).setReferenceType(type);
166 * @see org.eclipse.debug.core.model.IVariable#hasValueChanged()
168 public boolean hasValueChanged() throws DebugException {
169 return fValue.hasValueChanged();
174 * @param changed This method is called after a suspend when the list of
175 * variables is updated, to mark that this variable has a changed
176 * value. The variable view will show this variable in
179 public void setValueChanged(boolean changed) {
180 fValue.setValueChanged(changed);
184 * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
186 public String getModelIdentifier() {
187 return getDebugTarget().getModelIdentifier();
191 * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
193 public IDebugTarget getDebugTarget() {
194 return fStackFrame.getDebugTarget();
198 * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
200 public ILaunch getLaunch() {
201 return getDebugTarget().getLaunch();
205 * @see org.eclipse.debug.core.model.IValueModification#setValue(java.lang.String)
207 public void setValue(String expression) throws DebugException {
209 if (fValue.getReferenceType() == PHPValue.PEVT_STRING)
210 evalString = fLongName + "=\"" + expression + "\"";
212 evalString = fLongName + "=" + expression;
213 PHPVariable[] vars = fStackFrame.getPHPDBGProxy().eval(fStackFrame,
216 if (vars == null || vars.length == 0) {
217 vars = fStackFrame.getPHPDBGProxy().eval(fStackFrame, fLongName);
218 if (vars == null || vars.length == 0) {
220 String msg = "Could not set " + expression + " to " + fLongName;
221 Status status = new Status(Status.ERROR,
222 PHPDebugCorePlugin.PLUGIN_ID, code, msg, null);
223 PHPDebugCorePlugin.log(status);
224 throw new DebugException(status);
228 fValue = vars[0].fValue;
230 // set parent if new value has children
231 if (fValue.hasVariables()) {
232 Vector variables = fValue.getChildVariables();
233 for (int i = 0; i < variables.size(); i++) {
234 PHPVariable var = (PHPVariable) variables.get(i);
236 // adjust name if value type is array
237 // (still bare name. make "['name']")
238 if (fValue.getReferenceType() == PHPValue.PEVT_ARRAY) {
239 var.setName(var.getName());
244 DebugPlugin.getDefault().fireDebugEventSet(
245 new DebugEvent[] { new DebugEvent(this, DebugEvent.CHANGE,
246 DebugEvent.CONTENT) });
250 * @see org.eclipse.debug.core.model.IValueModification#setValue(org.eclipse.debug.core.model.IValue)
252 public void setValue(IValue value) throws DebugException {
253 this.fValue = (PHPValue) value;
257 * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
259 public boolean supportsValueModification() {
264 * Set whether this variable can be modified (default is true)
266 * for Global Variables root element only
268 public void setModifiable(boolean modifiable) {
269 fModifiable = modifiable;
273 * @see org.eclipse.debug.core.model.IValueModification#verifyValue(java.lang.String)
275 public boolean verifyValue(String expression) throws DebugException {
276 // TODO Auto-generated method stub
281 * @see org.eclipse.debug.core.model.IValueModification#verifyValue(org.eclipse.debug.core.model.IValue)
283 public boolean verifyValue(IValue value) throws DebugException {
284 // TODO Auto-generated method stub
289 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
291 public Object getAdapter(Class adapter) {
292 return Platform.getAdapterManager().getAdapter(this, adapter);
296 * This method is called from variable view and denominates the variables
297 * with a type specific explanation.
299 public String toString() {
302 switch (getReferenceType()) {
303 case PHPValue.PEVT_ARRAY: // Variable is an array
304 int elements = fValue.getVariables().length; // Get the number of child elements
306 switch (elements) { // Switch for the number of child elements
307 case 0: // We have no child element
308 str = this.getName() + " [no elements]"; // string => 'varname [no elements]'
311 case 1: // We have exactly one child element
312 str = this.getName() + " [1 element]"; // string => 'varname [1 element]'
315 default: // We have more than one element
316 str = this.getName() + " [" + elements + " elements]"; // string => 'varname [x elements]'
321 case PHPValue.PEVT_OBJECT: // Variable is an object
322 str = this.getName() + " [class: " + fValue.getValueString() + "]"; // string => 'varname [class: varvalue]'
325 case PHPValue.PEVT_STRING: // Variable is a string
326 str = this.getName() + " = \"" + fValue.getValueString() + "\""; // string => 'varname = "varvalue"'
329 case PHPValue.PEVT_SOFTREF: // Variable is a soft reference
330 default: // or anything else
331 str = this.getName() + " = " + fValue.getValueString(); // string => 'varname = varvalue'
339 * ONLY FOR net.sourceforge.phpdt.internal.debug.core.model.PHPDBGEvalString#copyItems(PHPVariable, PHPValue)
341 protected Object clone() throws CloneNotSupportedException {
342 PHPVariable var = new PHPVariable();
345 var.fStackFrame = fStackFrame;
346 var.fParent = fParent;
347 var.fLongName = fLongName;
348 var.fModifiable = fModifiable;