X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fsnippets%2Fhighlevelstatehandlers%2Fstandard%2Fatomic%2FDelegatingAtomicHighLevelStateHandler.java;h=9d2645d7232a1a911a773dcb2c874c39c1bcb4eb;hb=8bed58cd47f4e53a0a83e066d38864aa6875502f;hp=7a568827be7e1aa9f45b61f85b1a4234efa10c9f;hpb=b32414f8406634aca730d724a011023c0da8bf22;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java index 7a568827..9d2645d7 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java @@ -4,7 +4,6 @@ import net.mograsim.logic.model.model.components.ModelComponent; import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; import net.mograsim.logic.model.serializing.IdentifyParams; import net.mograsim.logic.model.snippets.SnippetDefinintion; -import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.HighLevelStateHandlerContext; import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers; public class DelegatingAtomicHighLevelStateHandler implements AtomicHighLevelStateHandler @@ -13,23 +12,33 @@ public class DelegatingAtomicHighLevelStateHandler implements AtomicHighLevelSta private ModelComponent delegateTarget; private String subStateID; - public DelegatingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context) + public DelegatingAtomicHighLevelStateHandler(SubmodelComponent component) { - this(context, null); + this(component, null); } - public DelegatingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context, DelegatingAtomicHighLevelStateHandlerParams params) + public DelegatingAtomicHighLevelStateHandler(SubmodelComponent component, DelegatingAtomicHighLevelStateHandlerParams params) { - this.parentComponent = context.component; + this.parentComponent = component; if (params != null) { // TODO document this if (params.delegateTarget == null) setDelegateTarget(parentComponent); else - setDelegateTarget(parentComponent.submodel.getComponentsByName().get(params.delegateTarget)); + { + ModelComponent delegateTarget = parentComponent.submodel.getComponentsByName().get(params.delegateTarget); + if (delegateTarget == null) + throw new NullPointerException("No subcomponent with name " + params.delegateTarget); + setDelegateTarget(delegateTarget); + } setSubStateID(params.subStateID); } + parentComponent.submodel.addComponentRemovedListener(c -> + { + if (delegateTarget == c) + delegateTarget = null; + }); } public void set(ModelComponent delegateTarget, String subStateID) @@ -42,26 +51,40 @@ public class DelegatingAtomicHighLevelStateHandler implements AtomicHighLevelSta { if (delegateTarget == null) this.delegateTarget = parentComponent; - else if (parentComponent.submodel.getComponentsByName().get(delegateTarget.name) != delegateTarget) + else if (parentComponent.submodel.getComponentsByName().get(delegateTarget.getName()) != delegateTarget) throw new IllegalArgumentException( "Can only set components belonging to the submodel of the parent component of this handler as the delegate target"); this.delegateTarget = delegateTarget; } + public ModelComponent getDelegateTarget() + { + return delegateTarget; + } + public void setSubStateID(String subStateID) { this.subStateID = subStateID; } + public String getSubStateID() + { + return subStateID; + } + @Override public Object getHighLevelState() { + if (delegateTarget == null) + throw new IllegalStateException("Delegating to a component that was destroyed"); return delegateTarget.getHighLevelState(subStateID); } @Override public void setHighLevelState(Object newState) { + if (delegateTarget == null) + throw new IllegalStateException("Delegating to a component that was destroyed"); delegateTarget.setHighLevelState(subStateID, newState); } @@ -74,8 +97,10 @@ public class DelegatingAtomicHighLevelStateHandler implements AtomicHighLevelSta @Override public DelegatingAtomicHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams) { + if (delegateTarget == null) + throw new IllegalStateException("Delegating to a component that was destroyed"); DelegatingAtomicHighLevelStateHandlerParams params = new DelegatingAtomicHighLevelStateHandlerParams(); - params.delegateTarget = delegateTarget.name; + params.delegateTarget = delegateTarget.getName(); params.subStateID = subStateID; return params; }