Copyright (C) 2013 Madis Pink
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Softwa...
If you think the Android project bad listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.madisp.bad.expr;
/*fromwww.java2s.com*/import com.madisp.bad.eval.BadVar;
import com.madisp.bad.eval.Scope;
import com.madisp.bad.eval.Watcher;
/**
* Created with IntelliJ IDEA.
* User: madis
* Date: 3/23/13
* Time: 1:57 PM
*/publicclass VarExpression extends BasableExpression {
String var;
public VarExpression(BasableExpression base, String var) {
super(base);
this.var = var;
}
@Override
public Object value(Scope scope) {
if (var == null) {
return null;
}
if (!hasBase()) {
return scope.getVar(null, var);
}
Object base = getBase(scope);
if (base != null) {
return scope.getVar(base, var);
}
return null;
}
public String getIdentifier() {
return var;
}
@SuppressWarnings("unchecked")
@Override
publicvoid addWatcher(final Scope scope, final Watcher w) {
super.addWatcher(scope, w);
Object o = value(scope);
final BadVar.BadWatcher watcher = new BadVar.BadWatcher() {
@Override
publicvoid fire(BadVar var) {
w.fire(scope);
}
};
if (o instanceof BadVar) {
((BadVar)o).addWatcher(watcher);
}
scope.addOnRebasedListener(new Scope.OnScopeRebasedListener() {
@Override
publicvoid onScopeDetached(Scope scope) {
Object o = value(scope);
if (o instanceof BadVar) {
((BadVar)o).removeWatcher(watcher);
}
}
@Override
publicvoid onScopeAttached(Scope scope) {
Object o = value(scope);
if (o instanceof BadVar) {
((BadVar)o).addWatcher(watcher);
}
w.fire(scope);
}
});
}
}