package net.sourceforge.phpeclipse.xdebug.php.model;


import net.sourceforge.phpeclipse.xdebug.php.model.XDebugVariable;
import net.sourceforge.phpeclipse.xdebug.php.model.XDebugTarget;

import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IWatchExpressionDelegate;
import org.eclipse.debug.core.model.IWatchExpressionListener;
import org.eclipse.debug.core.model.IWatchExpressionResult;
import org.w3c.dom.Node;

public class XDebugWatchExpressionDelegate implements IWatchExpressionDelegate {
	public void evaluateExpression(String expression, IDebugElement context, IWatchExpressionListener listener) {
		IWatchExpressionResult x;

		try {
				Node evalProperty = ((XDebugTarget) context.getDebugTarget()).eval(expression);
				XDebugVariable variable = new XDebugVariable(null, evalProperty);
				XDebugVariable result[] = {variable};
				
				if (result.length == 0) {
					x = new XDebugWatchExpressionResult(expression, null, null);
				} else {
					x = new XDebugWatchExpressionResult(expression, result[0].getValue(), null);
				}
		} catch (Exception e) {
			String[] s1;

			s1 = new String[1];
			s1[0] = e.toString();
			x = new XDebugWatchExpressionResult(expression, null, s1);
		}

		listener.watchEvaluationFinished(x);
	}
}