Merge branch 'development' of
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / serializing / SubmodelComponentDeserializer.java
1 package net.mograsim.logic.model.serializing;
2
3 import java.io.IOException;
4 import java.util.Map;
5
6 import net.mograsim.logic.model.model.ViewModelModifiable;
7 import net.mograsim.logic.model.model.components.GUIComponent;
8 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
9 import net.mograsim.logic.model.model.wires.GUIWire;
10 import net.mograsim.logic.model.model.wires.MovablePin;
11 import net.mograsim.logic.model.serializing.SubmodelComponentParams.InterfacePinParams;
12 import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters;
13 import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters.InnerComponentParams;
14 import net.mograsim.logic.model.serializing.SubmodelComponentParams.SubmodelParameters.InnerWireParams;
15
16 /**
17  * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams}
18  */
19 public final class SubmodelComponentDeserializer
20 {
21         /**
22          * Like {@link #create(ViewModelModifiable, String, String)}, but using the default name.
23          */
24         public static SubmodelComponent create(ViewModelModifiable model, String path)
25         {
26                 return create(model, path, null);
27         }
28
29         /**
30          * Creates a {@link SubmodelComponent} from the {@link SubmodelComponentParams} located at the given path as a JSON file. The returned
31          * SubmodelComponent is a {@link DeserializedSubmodelComponent}.
32          * 
33          * @param path The path of the file describing the {@link SubmodelComponentParams}, which define the new {@link SubmodelComponent}
34          * @return A new SubmodelComponent, as described in the file located at the given path
35          */
36         public static SubmodelComponent create(ViewModelModifiable model, String path, String name)
37         {
38                 try
39                 {
40                         SubmodelComponentParams params = SubmodelComponentParams.readJson(path);
41                         SubmodelComponent ret = create(model, params, name);
42                         return ret;
43                 }
44                 catch (IOException e)
45                 {
46                         throw new RuntimeException("Failed to construct GUICustomComponent. Parameters were not found.", e);
47                 }
48         }
49
50         /**
51          * Creates a {@link SubmodelComponent} from the specified {@link SubmodelComponentParams}. The returned SubmodelComponent is a
52          * {@link DeserializedSubmodelComponent}.
53          * 
54          * @param params The parameters describing the {@link SubmodelComponent}
55          * 
56          * @return A new SubmodelComponent, as described by the {@link SubmodelComponentParams}
57          */
58         public static SubmodelComponent create(ViewModelModifiable model, SubmodelComponentParams params, String name)
59         {
60                 DeserializedSubmodelComponent comp = createSubmodelComponent(model, params, name);
61                 initSubmodel(comp, params.submodel);
62                 return comp;
63         }
64
65         private static DeserializedSubmodelComponent createSubmodelComponent(ViewModelModifiable model, SubmodelComponentParams params,
66                         String name)
67         {
68                 DeserializedSubmodelComponent comp = new DeserializedSubmodelComponent(model, name);
69                 comp.setSubmodelScale(params.submodel.innerScale);
70                 comp.setOutlineRenderer(CodeSnippetSupplier.outlineRendererSupplier.getSnippetSupplier(params.outlineRendererSnippetID).create(comp,
71                                 params.outlineRendererParams));
72                 comp.setSymbolRenderer(CodeSnippetSupplier.symbolRendererSupplier.getSnippetSupplier(params.symbolRendererSnippetID).create(comp,
73                                 params.symbolRendererParams));
74                 // TODO high level states
75                 comp.setSize(params.width, params.height);
76                 for (InterfacePinParams iPinParams : params.interfacePins)
77                         comp.addSubmodelInterface(
78                                         new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y));
79                 return comp;
80         }
81
82         @SuppressWarnings("unused") // GUIWire being created
83         private static void initSubmodel(DeserializedSubmodelComponent comp, SubmodelParameters params)
84         {
85                 ViewModelModifiable submodelModifiable = comp.getSubmodelModifiable();
86                 Map<String, GUIComponent> componentsByName = submodelModifiable.getComponentsByName();
87                 GUIComponent[] components = new GUIComponent[params.subComps.length];
88                 for (int i = 0; i < components.length; i++)
89                 {
90                         InnerComponentParams cParams = params.subComps[i];
91                         components[i] = IndirectGUIComponentCreator.createComponent(submodelModifiable, cParams.id, cParams.params, cParams.name);
92                         components[i].moveTo(cParams.pos.x, cParams.pos.y);
93                 }
94
95                 for (int i = 0; i < params.innerWires.length; i++)
96                 {
97                         InnerWireParams innerWire = params.innerWires[i];
98                         new GUIWire(submodelModifiable, componentsByName.get(innerWire.pin1.compName).getPin(innerWire.pin1.pinName),
99                                         componentsByName.get(innerWire.pin2.compName).getPin(innerWire.pin2.pinName), innerWire.path);
100                 }
101         }
102 }