1 package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent;
3 import net.mograsim.logic.model.model.components.ModelComponent;
4 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
5 import net.mograsim.logic.model.serializing.IdentifyParams;
6 import net.mograsim.logic.model.snippets.SnippetDefinintion;
7 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers;
9 public class DelegatingSubcomponentHighLevelStateHandler implements SubcomponentHighLevelStateHandler
11 private final SubmodelComponent parentComponent;
12 private ModelComponent delegateTarget;
13 private String prefix;
15 public DelegatingSubcomponentHighLevelStateHandler(SubmodelComponent component)
17 this(component, null);
20 public DelegatingSubcomponentHighLevelStateHandler(SubmodelComponent component,
21 DelegatingSubcomponentHighLevelStateHandlerParams params)
23 this.parentComponent = component;
27 if (params.delegateTarget == null)
28 setDelegateTarget(parentComponent);
31 ModelComponent delegateTarget = parentComponent.submodel.getComponentsByName().get(params.delegateTarget);
32 if (delegateTarget == null)
33 throw new NullPointerException("No subcomponent with name " + params.delegateTarget);
34 setDelegateTarget(delegateTarget);
36 setPrefix(params.prefix);
38 parentComponent.submodel.addComponentRemovedListener(c ->
40 if (delegateTarget == c)
41 delegateTarget = null;
45 public void set(ModelComponent delegateTarget, String prefix)
47 setDelegateTarget(delegateTarget);
51 public void setDelegateTarget(ModelComponent delegateTarget)
53 if (delegateTarget == null)
54 this.delegateTarget = parentComponent;
55 else if (parentComponent.submodel.getComponentsByName().get(delegateTarget.getName()) != delegateTarget)
56 throw new IllegalArgumentException(
57 "Can only set components belonging to the submodel of the parent component of this handler as the delegate target");
58 this.delegateTarget = delegateTarget;
61 public ModelComponent getDelegateTarget()
63 return delegateTarget;
66 public void setPrefix(String prefix)
71 public String getPrefix()
77 public Object getHighLevelState(String subStateID)
79 if (delegateTarget == null)
80 throw new IllegalStateException("Delegating to a component that was destroyed");
81 return delegateTarget.getHighLevelState(getDelegateTargetHighLevelStateID(subStateID));
85 public void setHighLevelState(String subStateID, Object newState)
87 if (delegateTarget == null)
88 throw new IllegalStateException("Delegating to a component that was destroyed");
89 delegateTarget.setHighLevelState(getDelegateTargetHighLevelStateID(subStateID), newState);
92 private String getDelegateTargetHighLevelStateID(String subStateID)
94 return prefix == null ? subStateID : prefix + '.' + subStateID;
98 public String getIDForSerializing(IdentifyParams idParams)
104 public DelegatingSubcomponentHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams)
106 DelegatingSubcomponentHighLevelStateHandlerParams params = new DelegatingSubcomponentHighLevelStateHandlerParams();
107 params.delegateTarget = delegateTarget.getName();
108 params.prefix = prefix;
112 public static class DelegatingSubcomponentHighLevelStateHandlerParams
114 public String delegateTarget;
115 public String prefix;
120 StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier
121 .setSnippetSupplier(DelegatingSubcomponentHighLevelStateHandler.class.getCanonicalName(), SnippetDefinintion
122 .create(DelegatingSubcomponentHighLevelStateHandlerParams.class, DelegatingSubcomponentHighLevelStateHandler::new));