public class CircuitState implements InstanceData {
private class MyCircuitListener implements CircuitListener {
public void circuitChanged(CircuitEvent event) {
int action = event.getAction();
if (action == CircuitEvent.ACTION_ADD) {
Component comp = (Component) event.getData();
if (comp instanceof Wire) {
Wire w = (Wire) comp;
markPointAsDirty(w.getEnd0());
markPointAsDirty(w.getEnd1());
} else {
markComponentAsDirty(comp);
}
} else if (action == CircuitEvent.ACTION_REMOVE) {
Component comp = (Component) event.getData();
if (comp.getFactory() instanceof SubcircuitFactory) {
// disconnect from tree
CircuitState substate = (CircuitState) getData(comp);
if (substate != null && substate.parentComp == comp) {
substates.remove(substate);
substate.parentState = null;
substate.parentComp = null;
}
}
if (comp instanceof Wire) {
Wire w = (Wire) comp;
markPointAsDirty(w.getEnd0());
markPointAsDirty(w.getEnd1());
} else {
if (base != null) base.checkComponentEnds(CircuitState.this, comp);
dirtyComponents.remove(comp);
}
} else if (action == CircuitEvent.ACTION_CLEAR) {
substates.clear();
wireData = null;
componentData.clear();
values.clear();
dirtyComponents.clear();
dirtyPoints.clear();
causes.clear();
} else if (action == CircuitEvent.ACTION_CHANGE) {
Object data = event.getData();
if (data instanceof Collection) {
@SuppressWarnings("unchecked")
Collection<Component> comps = (Collection<Component>) data;
markComponentsDirty(comps);
if (base != null) {
for (Component comp : comps) {
base.checkComponentEnds(CircuitState.this, comp);
}
}
} else {
Component comp = (Component) event.getData();
markComponentAsDirty(comp);
if (base != null) base.checkComponentEnds(CircuitState.this, comp);
}
} else if (action == CircuitEvent.ACTION_INVALIDATE) {
Component comp = (Component) event.getData();
markComponentAsDirty(comp);
// TODO detemine if this should really be missing if (base != null) base.checkComponentEnds(CircuitState.this, comp);
} else if (action == CircuitEvent.TRANSACTION_DONE) {
ReplacementMap map = event.getResult().getReplacementMap(circuit);
if (map != null) {
for (Component comp : map.getReplacedComponents()) {
Object compState = componentData.remove(comp);
if (compState != null) {
Class<?> compFactory = comp.getFactory().getClass();
boolean found = false;
for (Component repl : map.get(comp)) {
if (repl.getFactory().getClass() == compFactory) {
found = true;
setData(repl, compState);