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=691f552ecc2136fc3a987c95fff25a451ef63caf;hpb=b2f3d0b16783289fab229c667c18d61f84119bbd;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 691f552e..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,154 +1,134 @@ 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.params.GeneralComponentParams; -import net.mograsim.logic.ui.model.components.params.RectComponentParams; -import net.mograsim.logic.ui.model.components.params.SubComponentParams; -import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerComponentParams; -import net.mograsim.logic.ui.model.components.params.RectComponentParams.InnerWireParams; -import net.mograsim.logic.ui.model.components.params.SubComponentParams.InterfacePinParams; +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.MovablePin; -public class GUICustomComponentCreator +/** + * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams} + */ +public final class GUICustomComponentCreator { - private static class CustomRectComponent extends SimpleRectangularSubmodelComponent - { - private String path; - - protected CustomRectComponent(ViewModelModifiable model, int logicWidth, String label, String path) - { - super(model, logicWidth, label); - this.path = path; - } - - @Override - public String getIdentifier() - { - return "file:".concat(path); - } - } + private static final String rectC = SimpleRectangularSubmodelComponent.class.getSimpleName(); + /** + * Creates a {@link SubmodelComponent} from the {@link SubmodelComponentParams}, specified at the given path. The returned + * SubmodelComponent can also be e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the + * {@link SubmodelComponentParams} describe. + * + * @param path The path of the file describing the {@link SubmodelComponentParams}, which define the new {@link SubmodelComponent} + * @return A new SubmodelComponent, as described in the file located at the given path + */ public static SubmodelComponent create(ViewModelModifiable model, String path) { try { - if (path.endsWith(RectComponentParams.fileExtension)) - { - RectComponentParams params = RectComponentParams.readJson(path); - SubmodelComponent ret = create(model, params, path); - return ret; - } else if (path.endsWith(SubComponentParams.fileExtension)) - { - SubComponentParams params = SubComponentParams.readJson(path); - SubmodelComponent ret = create(model, params, path); - return ret; - } else - throw new IOException(String.format("\"%s\" does not have a valid file extension. Must be either %s or %s", path, - RectComponentParams.fileExtension, SubComponentParams.fileExtension)); + SubmodelComponentParams params = SubmodelComponentParams.readJson(path); + SubmodelComponent ret = create(model, params); + return ret; } catch (IOException e) { System.err.println("Failed to construct GUICustomComponent. Parameters were not found."); e.printStackTrace(); } - return new CustomRectComponent(model, 0, "ERROR", "NONE"); + return new SimpleRectangularSubmodelComponent(model, 0, "ERROR"); } /** - * @param path This value is used when the new SubmodelComponent is an inner component to a different SubmodelComponent, which is being - * saved to a file; Then, the new SubmodelComponent is referenced by its given path within the file. + * Creates a {@link SubmodelComponent} from the specified {@link SubmodelComponentParams}. The returned SubmodelComponent can also be + * e.g. a {@link SimpleRectangularSubmodelComponent}, depending on what the {@link SubmodelComponentParams} describe. + * + * @param params The parameters describing the {@link SubmodelComponent} + * + * @return A new SubmodelComponent, as described by the {@link SubmodelComponentParams} */ - public static SimpleRectangularSubmodelComponent create(ViewModelModifiable model, RectComponentParams params, String path) + public static SubmodelComponent create(ViewModelModifiable model, SubmodelComponentParams params) { - CustomRectComponent comp = new CustomRectComponent(model, params.logicWidth, params.displayName, path); - comp.setSubmodelScale(params.composition.innerScale); - comp.setInputCount(params.inputCount); - comp.setOutputCount(params.outputCount); - initSubmodelComponents(comp, params.composition); - return comp; - } + SubmodelComponent comp = null; + if (rectC.equals(params.type)) + { + comp = createRectComponent(model, params); + } - /** - * @param path This value is used when the new SubmodelComponent is an inner component to a different SubmodelComponent, which is being - * saved to a file; Then, the new SubmodelComponent is referenced by its given path within the file. - */ - public static SubmodelComponent create(ViewModelModifiable model, SubComponentParams params, String path) - { - // As SubmodelComponent is abstract, for now SubmodelComponents are instantiated as SimpleRectangularSubmodelComponents - CustomRectComponent comp = new CustomRectComponent(model, 0, "", path); - comp.setSubmodelScale(params.composition.innerScale); - comp.setSize(params.width, params.height); - for (InterfacePinParams iPinParams : params.interfacePins) + if (comp == null) { - comp.addSubmodelInterface(iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y); + comp = createSubmodelComponent(model, params); } - initSubmodelComponents(comp, params.composition); + comp.identifierDelegate = () -> params.name; + initInnerComponents(comp, params.composition); return comp; } - @SuppressWarnings("unused") - private static void initSubmodelComponents(SubmodelComponent comp, GeneralComponentParams params) + // May return null + @SuppressWarnings("unchecked") + private static SimpleRectangularSubmodelComponent createRectComponent(ViewModelModifiable model, SubmodelComponentParams params) { try { - 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.logicWidth); - 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:'"); - } + Map m = params.specialized; + SimpleRectangularSubmodelComponent rect = new SimpleRectangularSubmodelComponent(model, + ((Number) m.get(SimpleRectangularSubmodelComponent.kLogicWidth)).intValue(), + (String) m.get(SimpleRectangularSubmodelComponent.kLabel)); + rect.setSubmodelScale(params.composition.innerScale); + + Object[] names = ((ArrayList) m.get(SimpleRectangularSubmodelComponent.kInCount)).toArray(); + rect.setInputPins(Arrays.copyOf(names, names.length, String[].class)); - 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).getPins().get(innerWire.pin1.pinIndex), - comp.submodelModifiable.getComponents().get(innerWire.pin2.compId).getPins().get(innerWire.pin2.pinIndex), - innerWire.path); - } + names = ((ArrayList) m.get(SimpleRectangularSubmodelComponent.kOutCount)).toArray(); + rect.setOutputPins(Arrays.copyOf(names, names.length, String[].class)); + + return rect; } - catch (Exception e) + catch (ClassCastException | NullPointerException e) { - System.err.println("Failed to create custom component!"); + System.err.println("Failed to specialize component!"); e.printStackTrace(); + return null; } } - private static GUIComponent createInnerComponentFromClass(SubmodelComponent parent, String classname, int logicWidth) - throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, - NoSuchMethodException, SecurityException, ClassNotFoundException + private static SubmodelComponent createSubmodelComponent(ViewModelModifiable model, SubmodelComponentParams params) { - Class c = Class.forName(classname); - Object comp; - try + // As SubmodelComponent is abstract, for now SubmodelComponents are instantiated as SimpleRectangularSubmodelComponents + SubmodelComponent comp = new SimpleRectangularSubmodelComponent(model, 0, ""); + comp.setSubmodelScale(params.composition.innerScale); + comp.setSize(params.width, params.height); + for (InterfacePinParams iPinParams : params.interfacePins) { - Constructor constructor = c.getConstructor(ViewModelModifiable.class); - comp = constructor.newInstance(parent.submodelModifiable); + comp.addSubmodelInterface( + new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y)); } - catch (@SuppressWarnings("unused") NoSuchMethodException e) + return comp; + } + + @SuppressWarnings("unused") + private static void initInnerComponents(SubmodelComponent comp, ComponentCompositionParams params) + { + GUIComponent[] components = new GUIComponent[params.subComps.length]; + for (int i = 0; i < components.length; i++) { - Constructor constructor = c.getConstructor(ViewModelModifiable.class, int.class); - comp = constructor.newInstance(parent.submodelModifiable, logicWidth); + 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); } - if (comp instanceof GUIComponent) - return (GUIComponent) comp; - throw new IllegalArgumentException("Class given as subcomponent was not a GUIComponent!"); + 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); + } } }