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;
129 case PHPValue.PEVT_OBJECT:
130 fLongName = fParent.getLongName() + "->" + fName;
142 public String getLongName() {
147 * @see org.eclipse.debug.core.model.IVariable#getReferenceTypefName()
149 public String getReferenceTypeName() {
150 return fValue.getReferenceTypeName();
156 public int getReferenceType() {
157 return fValue.getReferenceType();
163 public int setReferenceType(int type) {
164 return ((PHPValue) getValue()).setReferenceType(type);
168 * @see org.eclipse.debug.core.model.IVariable#hasValueChanged()
170 public boolean hasValueChanged() throws DebugException {
171 return fValue.hasValueChanged();
176 * @param changed This method is called after a suspend when the list of
177 * variables is updated, to mark that this variable has a changed
178 * value. The variable view will show this variable in
181 public void setValueChanged(boolean changed) {
182 fValue.setValueChanged(changed);
186 * @see org.eclipse.debug.core.model.IDebugElement#getModelIdentifier()
188 public String getModelIdentifier() {
189 return getDebugTarget().getModelIdentifier();
193 * @see org.eclipse.debug.core.model.IDebugElement#getDebugTarget()
195 public IDebugTarget getDebugTarget() {
196 return fStackFrame.getDebugTarget();
200 * @see org.eclipse.debug.core.model.IDebugElement#getLaunch()
202 public ILaunch getLaunch() {
203 return getDebugTarget().getLaunch();
207 * @see org.eclipse.debug.core.model.IValueModification#setValue(java.lang.String)
209 public void setValue (String expression) throws DebugException {
212 if (fValue.getReferenceType () == PHPValue.PEVT_STRING) {
213 evalString = fLongName + "=\"" + expression + "\"";
216 evalString = fLongName + "=" + expression;
219 PHPVariable[] vars = fStackFrame.getPHPDBGProxy ().eval (fStackFrame, evalString);
221 if (vars == null || vars.length == 0) {
222 vars = fStackFrame.getPHPDBGProxy ().eval (fStackFrame, fLongName);
224 if (vars == null || vars.length == 0) {
226 String msg = "Could not set " + expression + " to " + fLongName;
227 Status status = new Status (Status.ERROR, PHPDebugCorePlugin.PLUGIN_ID, code, msg, null);
228 PHPDebugCorePlugin.log (status);
230 throw new DebugException (status);
234 fValue = vars[0].fValue;
236 if (fValue.hasVariables ()) { // set parent if new value has children
237 Vector variables = fValue.getChildVariables ();
239 for (int i = 0; i < variables.size (); i++) {
240 PHPVariable var = (PHPVariable) variables.get (i);
242 var.setParent (this);
244 if (fValue.getReferenceType() == PHPValue.PEVT_ARRAY) { // adjust name if value type is array
245 var.setName (var.getName ()); // (still bare name. make "['name']")
250 DebugPlugin.getDefault().fireDebugEventSet (new DebugEvent[] {
251 new DebugEvent (this, DebugEvent.CHANGE, DebugEvent.CONTENT) });
256 * @see org.eclipse.debug.core.model.IValueModification#setValue(org.eclipse.debug.core.model.IValue)
258 public void setValue(IValue value) throws DebugException {
259 this.fValue = (PHPValue) value;
263 * @see org.eclipse.debug.core.model.IValueModification#supportsValueModification()
265 public boolean supportsValueModification() {
270 * Set whether this variable can be modified (default is true)
272 * for Global Variables root element only
274 public void setModifiable(boolean modifiable) {
275 fModifiable = modifiable;
279 * @see org.eclipse.debug.core.model.IValueModification#verifyValue(java.lang.String)
281 public boolean verifyValue(String expression) throws DebugException {
282 // TODO Auto-generated method stub
287 * @see org.eclipse.debug.core.model.IValueModification#verifyValue(org.eclipse.debug.core.model.IValue)
289 public boolean verifyValue(IValue value) throws DebugException {
290 // TODO Auto-generated method stub
295 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
297 public Object getAdapter(Class adapter) {
298 return Platform.getAdapterManager().getAdapter(this, adapter);
302 * This method is called from variable view and denominates the variables
303 * with a type specific explanation.
305 public String toString() {
308 switch (getReferenceType()) {
309 case PHPValue.PEVT_ARRAY: // Variable is an array
310 int elements = fValue.getVariables().length; // Get the number of child elements
312 switch (elements) { // Switch for the number of child elements
313 case 0: // We have no child element
314 str = this.getName() + " [no elements]"; // string => 'varname [no elements]'
317 case 1: // We have exactly one child element
318 str = this.getName() + " [1 element]"; // string => 'varname [1 element]'
321 default: // We have more than one element
322 str = this.getName() + " [" + elements + " elements]"; // string => 'varname [x elements]'
327 case PHPValue.PEVT_OBJECT: // Variable is an object
328 str = this.getName() + " [class: " + fValue.getValueString() + "]"; // string => 'varname [class: varvalue]'
331 case PHPValue.PEVT_STRING: // Variable is a string
332 str = this.getName() + " = \"" + fValue.getValueString() + "\""; // string => 'varname = "varvalue"'
335 case PHPValue.PEVT_SOFTREF: // Variable is a soft reference
336 default: // or anything else
337 str = this.getName() + " = " + fValue.getValueString(); // string => 'varname = varvalue'
345 * ONLY FOR net.sourceforge.phpdt.internal.debug.core.model.PHPDBGEvalString#copyItems(PHPVariable, PHPValue)
347 protected Object clone() throws CloneNotSupportedException {
348 PHPVariable var = new PHPVariable();
352 var.fStackFrame = fStackFrame;
353 var.fParent = fParent;
354 var.fLongName = fLongName;
355 var.fModifiable = fModifiable;