1 package net.mograsim.logic.ui.serializing;
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
8 import net.mograsim.logic.ui.model.ViewModelModifiable;
9 import net.mograsim.logic.ui.model.components.GUIComponent;
10 import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
11 import net.mograsim.logic.ui.model.components.submodels.SubmodelComponent;
12 import net.mograsim.logic.ui.model.wires.GUIWire;
13 import net.mograsim.logic.ui.model.wires.MovablePin;
14 import net.mograsim.logic.ui.serializing.SubmodelComponentParams.ComponentCompositionParams;
15 import net.mograsim.logic.ui.serializing.SubmodelComponentParams.InnerWireParams;
16 import net.mograsim.logic.ui.serializing.SubmodelComponentParams.InterfacePinParams;
17 import net.mograsim.logic.ui.serializing.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams;
20 * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams}
22 public final class SubmodelComponentDeserializer
24 private static final String rectC = SimpleRectangularSubmodelComponent.class.getSimpleName();
27 * Creates a {@link SubmodelComponent} from the {@link SubmodelComponentParams}, specified at the given path. The returned
28 * SubmodelComponent can also be e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the
29 * {@link SubmodelComponentParams} describe.
31 * @param path The path of the file describing the {@link SubmodelComponentParams}, which define the new {@link SubmodelComponent}
32 * @return A new SubmodelComponent, as described in the file located at the given path
34 public static SubmodelComponent create(ViewModelModifiable model, String path)
38 SubmodelComponentParams params = SubmodelComponentParams.readJson(path);
39 SubmodelComponent ret = create(model, params);
44 System.err.println("Failed to construct GUICustomComponent. Parameters were not found.");
47 return new SimpleRectangularSubmodelComponent(model, 0, "ERROR");
51 * Creates a {@link SubmodelComponent} from the specified {@link SubmodelComponentParams}. The returned SubmodelComponent can also be
52 * e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the {@link SubmodelComponentParams} describe.
54 * @param params The parameters describing the {@link SubmodelComponent}
56 * @return A new SubmodelComponent, as described by the {@link SubmodelComponentParams}
58 public static SubmodelComponent create(ViewModelModifiable model, SubmodelComponentParams params)
60 DeserializedSubmodelComponentI comp = null;
61 if (rectC.equals(params.type))
63 comp = createRectComponent(model, params);
68 comp = createSubmodelComponent(model, params);
70 comp.setIdentifierDelegate(() -> params.name);
71 initInnerComponents(comp, params.composition);
72 return (SubmodelComponent) comp;
76 @SuppressWarnings("unchecked")
77 private static DeserializedSimpleRectangularSubmodelComponent createRectComponent(ViewModelModifiable model,
78 SubmodelComponentParams params)
82 Map<String, Object> m = params.specialized;
83 DeserializedSimpleRectangularSubmodelComponent rect = new DeserializedSimpleRectangularSubmodelComponent(model,
84 ((Number) m.get(SimpleRectangularSubmodelComponent.kLogicWidth)).intValue(),
85 (String) m.get(SimpleRectangularSubmodelComponent.kLabel));
86 rect.setSubmodelScale(params.composition.innerScale);
88 Object[] names = ((ArrayList<Object>) m.get(SimpleRectangularSubmodelComponent.kInCount)).toArray();
89 rect.setInputPins(Arrays.copyOf(names, names.length, String[].class));
91 names = ((ArrayList<Object>) m.get(SimpleRectangularSubmodelComponent.kOutCount)).toArray();
92 rect.setOutputPins(Arrays.copyOf(names, names.length, String[].class));
96 catch (ClassCastException | NullPointerException e)
98 System.err.println("Failed to specialize component!");
104 private static DeserializedSubmodelComponent createSubmodelComponent(ViewModelModifiable model, SubmodelComponentParams params)
106 DeserializedSubmodelComponent comp = new DeserializedSubmodelComponent(model);
107 comp.setSubmodelScale(params.composition.innerScale);
108 comp.setSize(params.width, params.height);
109 for (InterfacePinParams iPinParams : params.interfacePins)
111 comp.addSubmodelInterface(
112 new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y));
117 @SuppressWarnings("unused")
118 private static void initInnerComponents(DeserializedSubmodelComponentI comp, ComponentCompositionParams params)
120 GUIComponent[] components = new GUIComponent[params.subComps.length];
121 for (int i = 0; i < components.length; i++)
123 InnerComponentParams cParams = params.subComps[i];
124 String path = cParams.name;
125 components[i] = IndirectGUIComponentCreator.create(comp.getSubmodelModifiable(), cParams.name, cParams.params);
126 components[i].moveTo(cParams.pos.x, cParams.pos.y);
129 for (int i = 0; i < params.innerWires.length; i++)
131 InnerWireParams innerWire = params.innerWires[i];
132 new GUIWire(comp.getSubmodelModifiable(),
133 comp.getSubmodelModifiable().getComponents().get(innerWire.pin1.compId).getPin(innerWire.pin1.pinName),
134 comp.getSubmodelModifiable().getComponents().get(innerWire.pin2.compId).getPin(innerWire.pin2.pinName), innerWire.path);