1 package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent;
3 import java.util.function.Consumer;
5 import net.mograsim.logic.model.model.components.ModelComponent;
6 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
7 import net.mograsim.logic.model.serializing.IdentifyParams;
8 import net.mograsim.logic.model.snippets.SnippetDefinintion;
9 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers;
11 public class DelegatingSubcomponentHighLevelStateHandler implements SubcomponentHighLevelStateHandler
13 private final SubmodelComponent parentComponent;
14 private ModelComponent delegateTarget;
15 private String prefix;
17 public DelegatingSubcomponentHighLevelStateHandler(SubmodelComponent component)
19 this(component, null);
22 public DelegatingSubcomponentHighLevelStateHandler(SubmodelComponent component,
23 DelegatingSubcomponentHighLevelStateHandlerParams params)
25 this.parentComponent = component;
29 if (params.delegateTarget == null)
30 setDelegateTarget(parentComponent);
33 ModelComponent delegateTarget = parentComponent.submodel.getComponentsByName().get(params.delegateTarget);
34 if (delegateTarget == null)
35 throw new NullPointerException("No subcomponent with name " + params.delegateTarget);
36 setDelegateTarget(delegateTarget);
38 setPrefix(params.prefix);
40 parentComponent.submodel.addComponentRemovedListener(c ->
42 if (delegateTarget == c)
43 delegateTarget = null;
47 public void set(ModelComponent delegateTarget, String prefix)
49 setDelegateTarget(delegateTarget);
53 public void setDelegateTarget(ModelComponent delegateTarget)
55 if (delegateTarget == null)
56 this.delegateTarget = parentComponent;
57 else if (parentComponent.submodel.getComponentsByName().get(delegateTarget.getName()) != delegateTarget)
58 throw new IllegalArgumentException(
59 "Can only set components belonging to the submodel of the parent component of this handler as the delegate target");
60 this.delegateTarget = delegateTarget;
63 public ModelComponent getDelegateTarget()
65 return delegateTarget;
68 public void setPrefix(String prefix)
73 public String getPrefix()
79 public Object getHighLevelState(String subStateID)
82 return delegateTarget.getHighLevelState(getDelegateTargetHighLevelStateID(subStateID));
86 public void setHighLevelState(String subStateID, Object newState)
89 delegateTarget.setHighLevelState(getDelegateTargetHighLevelStateID(subStateID), newState);
93 public void addListener(String subStateID, Consumer<Object> stateChanged)
96 delegateTarget.addHighLevelStateListener(getDelegateTargetHighLevelStateID(subStateID), stateChanged);
100 public void removeListener(String subStateID, Consumer<Object> stateChanged)
103 delegateTarget.removeHighLevelStateListener(getDelegateTargetHighLevelStateID(subStateID), stateChanged);
106 private void checkTarget()
108 if (delegateTarget == null)
109 throw new IllegalStateException("Delegating to a component that was destroyed");
112 private String getDelegateTargetHighLevelStateID(String subStateID)
114 return prefix == null ? subStateID : prefix + '.' + subStateID;
118 public String getIDForSerializing(IdentifyParams idParams)
124 public DelegatingSubcomponentHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams)
127 DelegatingSubcomponentHighLevelStateHandlerParams params = new DelegatingSubcomponentHighLevelStateHandlerParams();
128 params.delegateTarget = delegateTarget == parentComponent ? null : delegateTarget.getName();
129 params.prefix = prefix;
133 public static class DelegatingSubcomponentHighLevelStateHandlerParams
135 public String delegateTarget;
136 public String prefix;
141 StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier
142 .setSnippetSupplier(DelegatingSubcomponentHighLevelStateHandler.class.getCanonicalName(), SnippetDefinintion
143 .create(DelegatingSubcomponentHighLevelStateHandlerParams.class, DelegatingSubcomponentHighLevelStateHandler::new));