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