12a98887c54153e0196dd5580bb34135f0896407
[Mograsim.git] /
1 package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent;
2
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;
8
9 public class DelegatingSubcomponentHighLevelStateHandler implements SubcomponentHighLevelStateHandler
10 {
11         private final SubmodelComponent parentComponent;
12         private ModelComponent delegateTarget;
13         private String prefix;
14
15         public DelegatingSubcomponentHighLevelStateHandler(SubmodelComponent component)
16         {
17                 this(component, null);
18         }
19
20         public DelegatingSubcomponentHighLevelStateHandler(SubmodelComponent component,
21                         DelegatingSubcomponentHighLevelStateHandlerParams params)
22         {
23                 this.parentComponent = component;
24                 if (params != null)
25                 {
26                         // TODO document this
27                         if (params.delegateTarget == null)
28                                 setDelegateTarget(parentComponent);
29                         else
30                         {
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);
35                         }
36                         setPrefix(params.prefix);
37                 }
38                 parentComponent.submodel.addComponentRemovedListener(c ->
39                 {
40                         if (delegateTarget == c)
41                                 delegateTarget = null;
42                 });
43         }
44
45         public void set(ModelComponent delegateTarget, String prefix)
46         {
47                 setDelegateTarget(delegateTarget);
48                 setPrefix(prefix);
49         }
50
51         public void setDelegateTarget(ModelComponent delegateTarget)
52         {
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;
59         }
60
61         public ModelComponent getDelegateTarget()
62         {
63                 return delegateTarget;
64         }
65
66         public void setPrefix(String prefix)
67         {
68                 this.prefix = prefix;
69         }
70
71         public String getPrefix()
72         {
73                 return prefix;
74         }
75
76         @Override
77         public Object getHighLevelState(String subStateID)
78         {
79                 if (delegateTarget == null)
80                         throw new IllegalStateException("Delegating to a component that was destroyed");
81                 return delegateTarget.getHighLevelState(getDelegateTargetHighLevelStateID(subStateID));
82         }
83
84         @Override
85         public void setHighLevelState(String subStateID, Object newState)
86         {
87                 if (delegateTarget == null)
88                         throw new IllegalStateException("Delegating to a component that was destroyed");
89                 delegateTarget.setHighLevelState(getDelegateTargetHighLevelStateID(subStateID), newState);
90         }
91
92         private String getDelegateTargetHighLevelStateID(String subStateID)
93         {
94                 return prefix == null ? subStateID : prefix + '.' + subStateID;
95         }
96
97         @Override
98         public String getIDForSerializing(IdentifyParams idParams)
99         {
100                 return "delegating";
101         }
102
103         @Override
104         public DelegatingSubcomponentHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams)
105         {
106                 DelegatingSubcomponentHighLevelStateHandlerParams params = new DelegatingSubcomponentHighLevelStateHandlerParams();
107                 params.delegateTarget = delegateTarget.getName();
108                 params.prefix = prefix;
109                 return params;
110         }
111
112         public static class DelegatingSubcomponentHighLevelStateHandlerParams
113         {
114                 public String delegateTarget;
115                 public String prefix;
116         }
117
118         static
119         {
120                 StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier
121                                 .setSnippetSupplier(DelegatingSubcomponentHighLevelStateHandler.class.getCanonicalName(), SnippetDefinintion
122                                                 .create(DelegatingSubcomponentHighLevelStateHandlerParams.class, DelegatingSubcomponentHighLevelStateHandler::new));
123         }
124 }