Better exception in DelegatingAtomicHighLevelStateHandler
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / snippets / highlevelstatehandlers / standard / atomic / DelegatingAtomicHighLevelStateHandler.java
1 package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic;
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.HighLevelStateHandlerContext;
8 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers;
9
10 public class DelegatingAtomicHighLevelStateHandler implements AtomicHighLevelStateHandler
11 {
12         private final SubmodelComponent parentComponent;
13         private ModelComponent delegateTarget;
14         private String subStateID;
15
16         public DelegatingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context)
17         {
18                 this(context, null);
19         }
20
21         public DelegatingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context, DelegatingAtomicHighLevelStateHandlerParams params)
22         {
23                 this.parentComponent = context.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                         setSubStateID(params.subStateID);
37                 }
38                 parentComponent.submodel.addComponentRemovedListener(c ->
39                 {
40                         if (delegateTarget == c)
41                                 delegateTarget = null;
42                 });
43         }
44
45         public void set(ModelComponent delegateTarget, String subStateID)
46         {
47                 setDelegateTarget(delegateTarget);
48                 setSubStateID(subStateID);
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 setSubStateID(String subStateID)
67         {
68                 this.subStateID = subStateID;
69         }
70
71         public String getSubStateID()
72         {
73                 return subStateID;
74         }
75
76         @Override
77         public Object getHighLevelState()
78         {
79                 if (delegateTarget == null)
80                         throw new IllegalStateException("Delegating to a component that was destroyed");
81                 return delegateTarget.getHighLevelState(subStateID);
82         }
83
84         @Override
85         public void setHighLevelState(Object newState)
86         {
87                 if (delegateTarget == null)
88                         throw new IllegalStateException("Delegating to a component that was destroyed");
89                 delegateTarget.setHighLevelState(subStateID, newState);
90         }
91
92         @Override
93         public String getIDForSerializing(IdentifyParams idParams)
94         {
95                 return "delegating";
96         }
97
98         @Override
99         public DelegatingAtomicHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams)
100         {
101                 if (delegateTarget == null)
102                         throw new IllegalStateException("Delegating to a component that was destroyed");
103                 DelegatingAtomicHighLevelStateHandlerParams params = new DelegatingAtomicHighLevelStateHandlerParams();
104                 params.delegateTarget = delegateTarget.getName();
105                 params.subStateID = subStateID;
106                 return params;
107         }
108
109         public static class DelegatingAtomicHighLevelStateHandlerParams
110         {
111                 public String delegateTarget;
112                 public String subStateID;
113         }
114
115         static
116         {
117                 StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier.setSnippetSupplier(
118                                 DelegatingAtomicHighLevelStateHandler.class.getCanonicalName(),
119                                 SnippetDefinintion.create(DelegatingAtomicHighLevelStateHandlerParams.class, DelegatingAtomicHighLevelStateHandler::new));
120         }
121 }