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;
7 import net.mograsim.logic.ui.model.ViewModelModifiable;
8 import net.mograsim.logic.ui.model.components.ComponentParams.InnerComponentParams;
9 import net.mograsim.logic.ui.model.components.ComponentParams.InnerWireParams;
10 import net.mograsim.logic.ui.model.wires.GUIWire;
12 public class GUICustomComponent extends SimpleRectangularSubmodelComponent
14 private String path = "NONE";
16 public static GUICustomComponent create(ViewModelModifiable model, String path)
20 ComponentParams params = ComponentParams.readJSON(path);
21 return create(model, params);
25 System.err.println(String.format("Failed to create custom component from invalid path: %s", path));
28 return new GUICustomComponent(model, 0, "ERROR");
31 public static GUICustomComponent create(ViewModelModifiable model, ComponentParams params)
33 GUICustomComponent comp = new GUICustomComponent(model, params.logicWidth, params.displayName);
34 comp.setSubmodelScale(params.innerScale);
35 comp.setInputCount(params.inputCount);
36 comp.setOutputCount(params.outputCount);
37 comp.initSubmodelComponents(params);
41 private GUICustomComponent(ViewModelModifiable model, int logicWidth, String displayName)
43 super(model, logicWidth, displayName);
46 @SuppressWarnings("unused")
47 private void initSubmodelComponents(ComponentParams params)
51 GUIComponent[] components = new GUIComponent[params.subComps.length];
52 for (int i = 0; i < components.length; i++)
54 InnerComponentParams cParams = params.subComps[i];
55 String path = cParams.type;
56 if (path.startsWith("class:"))
58 path = path.substring(6);
59 components[i] = createComponent(path, cParams.logicWidth);
60 components[i].moveTo(cParams.pos.x, cParams.pos.y);
61 } else if (path.startsWith("file:"))
63 path = path.substring(5);
64 components[i] = create(submodelModifiable, path);
65 components[i].moveTo(cParams.pos.x, cParams.pos.y);
67 throw new IllegalArgumentException("Invalid submodel type! Type was neither prefixed by 'class:' nor by 'file:'");
70 for (int i = 0; i < params.innerWires.length; i++)
72 InnerWireParams innerWire = params.innerWires[i];
73 new GUIWire(submodelModifiable,
74 submodelModifiable.getComponents().get(innerWire.pin1.compId).getPins().get(innerWire.pin1.pinIndex),
75 submodelModifiable.getComponents().get(innerWire.pin2.compId).getPins().get(innerWire.pin2.pinIndex),
81 System.err.println("Failed to create custom component!");
86 private GUIComponent createComponent(String classname, int logicWidth) throws InstantiationException, IllegalAccessException,
87 IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException
89 Class<?> c = Class.forName(classname);
93 Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class);
94 comp = constructor.newInstance(submodelModifiable);
96 catch (@SuppressWarnings("unused") NoSuchMethodException e)
98 Constructor<?> constructor = c.getConstructor(ViewModelModifiable.class, int.class);
99 comp = constructor.newInstance(submodelModifiable, logicWidth);
102 if (comp instanceof GUIComponent)
103 return (GUIComponent) comp;
104 throw new IllegalArgumentException("Class given as subcomponent was not a GUIComponent!");
107 public String getPath()