Made CodeSnippetSupplier generic
[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(CodeSnippetSupplier.outlineRendererProviderSupplier.getSnippetProvider(params.outlineRendererSnippetID)
65                                 .create(comp, params.outlineRendererParams));
66                 comp.setSymbolRenderer(CodeSnippetSupplier.symbolRendererProviderSupplier.getSnippetProvider(params.symbolRendererSnippetID)
67                                 .create(comp, params.symbolRendererParams));
68                 // TODO high level states
69                 comp.setSize(params.width, params.height);
70                 for (InterfacePinParams iPinParams : params.interfacePins)
71                         comp.addSubmodelInterface(
72                                         new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y));
73                 return comp;
74         }
75
76         @SuppressWarnings("unused")
77         private static void initSubmodel(DeserializedSubmodelComponent comp, SubmodelParameters params)
78         {
79                 GUIComponent[] components = new GUIComponent[params.subComps.length];
80                 for (int i = 0; i < components.length; i++)
81                 {
82                         InnerComponentParams cParams = params.subComps[i];
83                         String path = cParams.id;
84                         components[i] = IndirectGUIComponentCreator.createComponent(comp.getSubmodelModifiable(), cParams.id, cParams.params);
85                         components[i].moveTo(cParams.pos.x, cParams.pos.y);
86                 }
87
88                 for (int i = 0; i < params.innerWires.length; i++)
89                 {
90                         InnerWireParams innerWire = params.innerWires[i];
91                         new GUIWire(comp.getSubmodelModifiable(),
92                                         comp.getSubmodelModifiable().getComponents().get(innerWire.pin1.compId).getPin(innerWire.pin1.pinName),
93                                         comp.getSubmodelModifiable().getComponents().get(innerWire.pin2.compId).getPin(innerWire.pin2.pinName), innerWire.path);
94                 }
95         }
96 }