X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fsnippets%2Fhighlevelstatehandlers%2Fstandard%2Fsubcomponent%2FDelegatingSubcomponentHighLevelStateHandler.java;h=a7f2c817a8f57446445bda046d86ebae191d0720;hb=43a3e6fbdeef2b0926ee5302abaa04be65709799;hp=104cb1424588c0f8fe02fcd91f079dc3606edd61;hpb=3274bf1091e8c29cc3bff7f909bdd8b29220848d;p=Mograsim.git diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java index 104cb142..a7f2c817 100644 --- a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java @@ -1,5 +1,7 @@ package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent; +import java.util.function.Consumer; + import net.mograsim.logic.model.model.components.ModelComponent; import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; import net.mograsim.logic.model.serializing.IdentifyParams; @@ -52,9 +54,10 @@ public class DelegatingSubcomponentHighLevelStateHandler implements Subcomponent { if (delegateTarget == null) this.delegateTarget = parentComponent; - else if (parentComponent.submodel.getComponentsByName().get(delegateTarget.getName()) != delegateTarget) + else if (delegateTarget != parentComponent + && 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"); + "Can only set components belonging to the submodel of the parent component of this handler or the parent component itself as the delegate target"); this.delegateTarget = delegateTarget; } @@ -76,17 +79,35 @@ public class DelegatingSubcomponentHighLevelStateHandler implements Subcomponent @Override public Object getHighLevelState(String subStateID) { - if (delegateTarget == null) - throw new IllegalStateException("Delegating to a component that was destroyed"); + checkTarget(); return delegateTarget.getHighLevelState(getDelegateTargetHighLevelStateID(subStateID)); } @Override public void setHighLevelState(String subStateID, Object newState) + { + checkTarget(); + delegateTarget.setHighLevelState(getDelegateTargetHighLevelStateID(subStateID), newState); + } + + @Override + public void addListener(String subStateID, Consumer stateChanged) + { + checkTarget(); + delegateTarget.addHighLevelStateListener(getDelegateTargetHighLevelStateID(subStateID), stateChanged); + } + + @Override + public void removeListener(String subStateID, Consumer stateChanged) + { + checkTarget(); + delegateTarget.removeHighLevelStateListener(getDelegateTargetHighLevelStateID(subStateID), stateChanged); + } + + private void checkTarget() { if (delegateTarget == null) throw new IllegalStateException("Delegating to a component that was destroyed"); - delegateTarget.setHighLevelState(getDelegateTargetHighLevelStateID(subStateID), newState); } private String getDelegateTargetHighLevelStateID(String subStateID) @@ -103,8 +124,7 @@ public class DelegatingSubcomponentHighLevelStateHandler implements Subcomponent @Override public DelegatingSubcomponentHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams) { - if (delegateTarget == null) - throw new IllegalStateException("Delegating to a component that was destroyed"); + checkTarget(); DelegatingSubcomponentHighLevelStateHandlerParams params = new DelegatingSubcomponentHighLevelStateHandlerParams(); params.delegateTarget = delegateTarget == parentComponent ? null : delegateTarget.getName(); params.prefix = prefix;