X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2Fcomponents%2FGUICustomComponentCreator.java;h=1e2034e4cd82d6ac6bc3aabd5f06664f3b1bb94a;hb=67c7e16eac6ef555f7ebe0cbe6048598d7f1187e;hp=ee034c8ce4c06341e66cac19dde7cfdfa93b2601;hpb=a19dde054ec5cab3c835840d569880ee8ee156dc;p=Mograsim.git diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUICustomComponentCreator.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUICustomComponentCreator.java index ee034c8c..1e2034e4 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUICustomComponentCreator.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUICustomComponentCreator.java @@ -1,17 +1,17 @@ package net.mograsim.logic.ui.model.components; import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Map; import net.mograsim.logic.ui.model.ViewModelModifiable; -import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams; import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams; +import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams; import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerWireParams; import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InterfacePinParams; import net.mograsim.logic.ui.model.wires.GUIWire; -import net.mograsim.logic.ui.model.wires.WireCrossPoint; +import net.mograsim.logic.ui.model.wires.MovablePin; /** * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams} @@ -33,7 +33,7 @@ public final class GUICustomComponentCreator try { SubmodelComponentParams params = SubmodelComponentParams.readJson(path); - SubmodelComponent ret = create(model, params, path); + SubmodelComponent ret = create(model, params); return ret; } catch (IOException e) @@ -49,11 +49,10 @@ public final class GUICustomComponentCreator * e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the {@link SubmodelComponentParams} describe. * * @param params The parameters describing the {@link SubmodelComponent} - * @param path This value is used when the new {@link SubmodelComponent} is an inner component to a different SubmodelComponent, which - * is being saved to a file; Then, the new {@link SubmodelComponent} is referenced by its given path within the file. + * * @return A new SubmodelComponent, as described by the {@link SubmodelComponentParams} */ - public static SubmodelComponent create(ViewModelModifiable model, SubmodelComponentParams params, String path) + public static SubmodelComponent create(ViewModelModifiable model, SubmodelComponentParams params) { SubmodelComponent comp = null; if (rectC.equals(params.type)) @@ -65,12 +64,13 @@ public final class GUICustomComponentCreator { comp = createSubmodelComponent(model, params); } - comp.identifierDelegate = () -> "file:".concat(path); + comp.identifierDelegate = () -> params.name; initInnerComponents(comp, params.composition); return comp; } // May return null + @SuppressWarnings("unchecked") private static SimpleRectangularSubmodelComponent createRectComponent(ViewModelModifiable model, SubmodelComponentParams params) { try @@ -80,20 +80,12 @@ public final class GUICustomComponentCreator ((Number) m.get(SimpleRectangularSubmodelComponent.kLogicWidth)).intValue(), (String) m.get(SimpleRectangularSubmodelComponent.kLabel)); rect.setSubmodelScale(params.composition.innerScale); - // rect.setSize(params.width, params.height); - // TODO save & restore names - int inputCount = ((Number) m.get(SimpleRectangularSubmodelComponent.kInCount)).intValue(); - String[] inputNames = new String[inputCount]; - for (int i = 0; i < inputCount; i++) - inputNames[i] = params.interfacePins[i].name; - rect.setInputPins(inputNames); + Object[] names = ((ArrayList) m.get(SimpleRectangularSubmodelComponent.kInCount)).toArray(); + rect.setInputPins(Arrays.copyOf(names, names.length, String[].class)); - int outputCount = ((Number) m.get(SimpleRectangularSubmodelComponent.kOutCount)).intValue(); - String[] outputPins = new String[outputCount]; - for (int i = 0; i < outputCount; i++) - outputPins[i] = params.interfacePins[inputCount + i].name; - rect.setOutputPins(outputPins); + names = ((ArrayList) m.get(SimpleRectangularSubmodelComponent.kOutCount)).toArray(); + rect.setOutputPins(Arrays.copyOf(names, names.length, String[].class)); return rect; } @@ -113,7 +105,8 @@ public final class GUICustomComponentCreator comp.setSize(params.width, params.height); for (InterfacePinParams iPinParams : params.interfacePins) { - comp.addSubmodelInterface(iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y); + comp.addSubmodelInterface( + new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y)); } return comp; } @@ -121,59 +114,21 @@ public final class GUICustomComponentCreator @SuppressWarnings("unused") private static void initInnerComponents(SubmodelComponent comp, ComponentCompositionParams params) { - try + GUIComponent[] components = new GUIComponent[params.subComps.length]; + for (int i = 0; i < components.length; i++) { - GUIComponent[] components = new GUIComponent[params.subComps.length]; - for (int i = 0; i < components.length; i++) - { - InnerComponentParams cParams = params.subComps[i]; - String path = cParams.type; - if (path.startsWith("class:")) - { - path = path.substring(6); - components[i] = createInnerComponentFromClass(comp, path, cParams.params); - components[i].moveTo(cParams.pos.x, cParams.pos.y); - } else if (path.startsWith("file:")) - { - path = path.substring(5); - components[i] = create(comp.submodelModifiable, path); - components[i].moveTo(cParams.pos.x, cParams.pos.y); - } else - throw new IllegalArgumentException("Invalid submodel type! Type was neither prefixed by 'class:' nor by 'file:'"); - } - - for (int i = 0; i < params.innerWires.length; i++) - { - InnerWireParams innerWire = params.innerWires[i]; - new GUIWire(comp.submodelModifiable, - comp.submodelModifiable.getComponents().get(innerWire.pin1.compId).getPin(innerWire.pin1.pinName), - comp.submodelModifiable.getComponents().get(innerWire.pin2.compId).getPin(innerWire.pin2.pinName), innerWire.path); - } + InnerComponentParams cParams = params.subComps[i]; + String path = cParams.name; + components[i] = GUIComponentCreator.create(comp.submodelModifiable, cParams.name, cParams.params); + components[i].moveTo(cParams.pos.x, cParams.pos.y); } - catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException - | ClassNotFoundException | IllegalArgumentException e) - { - System.err.println("Failed to initialize custom component!"); - e.printStackTrace(); - } - } - private static GUIComponent createInnerComponentFromClass(SubmodelComponent parent, String classname, Map params) - throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException, - ClassNotFoundException - { - Class c = Class.forName(classname); - Object comp; - if (SimpleRectangularGUIGate.class.isAssignableFrom(c) || WireCrossPoint.class.equals(c)) - { - Constructor constructor = c.getConstructor(ViewModelModifiable.class, int.class); - comp = constructor.newInstance(parent.submodelModifiable, - ((Number) params.get(SimpleRectangularGUIGate.kLogicWidth)).intValue()); - } else + for (int i = 0; i < params.innerWires.length; i++) { - Constructor constructor = c.getConstructor(ViewModelModifiable.class); - comp = constructor.newInstance(parent.submodelModifiable); + InnerWireParams innerWire = params.innerWires[i]; + new GUIWire(comp.submodelModifiable, + comp.submodelModifiable.getComponents().get(innerWire.pin1.compId).getPin(innerWire.pin1.pinName), + comp.submodelModifiable.getComponents().get(innerWire.pin2.compId).getPin(innerWire.pin2.pinName), innerWire.path); } - return (GUIComponent) comp; } }