1 package net.mograsim.logic.ui.model.components;
3 import java.io.IOException;
4 import java.lang.reflect.Constructor;
5 import java.lang.reflect.InvocationTargetException;
6 import java.util.ArrayList;
7 import java.util.Arrays;
10 import net.mograsim.logic.ui.model.ViewModelModifiable;
11 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams;
12 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams;
13 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerWireParams;
14 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InterfacePinParams;
15 import net.mograsim.logic.ui.model.wires.GUIWire;
16 import net.mograsim.logic.ui.model.wires.MovablePin;
17 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
20 * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams}
22 public final class GUICustomComponentCreator
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 SubmodelComponent comp = null;
61 if (rectC.equals(params.type))
63 comp = createRectComponent(model, params);
68 comp = createSubmodelComponent(model, params);
70 comp.identifierDelegate = () -> params.name;
71 initInnerComponents(comp, params.composition);
76 @SuppressWarnings("unchecked")
77 private static SimpleRectangularSubmodelComponent createRectComponent(ViewModelModifiable model, SubmodelComponentParams params)
81 Map<String, Object> m = params.specialized;
82 SimpleRectangularSubmodelComponent rect = new SimpleRectangularSubmodelComponent(model,
83 ((Number) m.get(SimpleRectangularSubmodelComponent.kLogicWidth)).intValue(),
84 (String) m.get(SimpleRectangularSubmodelComponent.kLabel));
85 rect.setSubmodelScale(params.composition.innerScale);
87 Object[] names = ((ArrayList<Object>) m.get(SimpleRectangularSubmodelComponent.kInCount)).toArray();
88 rect.setInputPins(Arrays.copyOf(names, names.length, String[].class));
90 names = ((ArrayList<Object>) m.get(SimpleRectangularSubmodelComponent.kOutCount)).toArray();
91 rect.setOutputPins(Arrays.copyOf(names, names.length, String[].class));
95 catch (ClassCastException | NullPointerException e)
97 System.err.println("Failed to specialize component!");
103 private static SubmodelComponent createSubmodelComponent(ViewModelModifiable model, SubmodelComponentParams params)
105 // As SubmodelComponent is abstract, for now SubmodelComponents are instantiated as SimpleRectangularSubmodelComponents
106 SubmodelComponent comp = new SimpleRectangularSubmodelComponent(model, 0, "");
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(SubmodelComponent 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] = GUIComponentCreator.create(comp.submodelModifiable, 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.submodelModifiable,
133 comp.submodelModifiable.getComponents().get(innerWire.pin1.compId).getPin(innerWire.pin1.pinName),
134 comp.submodelModifiable.getComponents().get(innerWire.pin2.compId).getPin(innerWire.pin2.pinName), innerWire.path);