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 if (delegateTarget == null)
107 throw new IllegalStateException("Delegating to a component that was destroyed");
108 DelegatingSubcomponentHighLevelStateHandlerParams params = new DelegatingSubcomponentHighLevelStateHandlerParams();
109 params.delegateTarget = delegateTarget == parentComponent ? null : delegateTarget.getName();
110 params.prefix = prefix;
114 public static class DelegatingSubcomponentHighLevelStateHandlerParams
116 public String delegateTarget;
117 public String prefix;
122 StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier
123 .setSnippetSupplier(DelegatingSubcomponentHighLevelStateHandler.class.getCanonicalName(), SnippetDefinintion
124 .create(DelegatingSubcomponentHighLevelStateHandlerParams.class, DelegatingSubcomponentHighLevelStateHandler::new));