X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2Fcomponents%2FGUICustomComponentCreator.java;fp=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2Fcomponents%2FGUICustomComponentCreator.java;h=c27b015442780e1b32b13c52cf115656e38e8f1a;hb=b555e4c29e14f455d8ffbc810284c7bb44b459f9;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..c27b0154 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 @@ -3,18 +3,20 @@ package net.mograsim.logic.ui.model.components; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +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.GeneralComponentParams.InnerComponentParams; 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.InnerWireParams; import net.mograsim.logic.ui.model.components.params.SubComponentParams.InterfacePinParams; import net.mograsim.logic.ui.model.wires.GUIWire; public class GUICustomComponentCreator { + private static final String rectC = SimpleRectangularSubmodelComponent.class.getSimpleName(); + private static class CustomRectComponent extends SimpleRectangularSubmodelComponent { private String path; @@ -36,19 +38,9 @@ public class GUICustomComponentCreator { 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)); + SubComponentParams params = SubComponentParams.readJson(path); + SubmodelComponent ret = create(model, params, path); + return ret; } catch (IOException e) { @@ -58,33 +50,45 @@ public class GUICustomComponentCreator return new CustomRectComponent(model, 0, "ERROR", "NONE"); } - /** - * @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 SimpleRectangularSubmodelComponent create(ViewModelModifiable model, RectComponentParams params, String path) - { - 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; - } - /** * @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) + // TODO: Clean up this mess + SubmodelComponent comp = null; + if (rectC.equals(params.type)) + { + try + { + Map m = params.specialized; + SimpleRectangularSubmodelComponent rect = new CustomRectComponent(model, + ((Number) m.get(SimpleRectangularSubmodelComponent.kLogicWidth)).intValue(), + (String) m.get(SimpleRectangularSubmodelComponent.kLabel), path); + rect.setSubmodelScale(params.composition.innerScale); + rect.setSize(params.width, params.height); + rect.setInputCount(((Number) m.get(SimpleRectangularSubmodelComponent.kInCount)).intValue()); + rect.setOutputCount(((Number) m.get(SimpleRectangularSubmodelComponent.kOutCount)).intValue()); + comp = rect; + } + catch (ClassCastException | NullPointerException e) + { + System.err.println("Failed to specialize component!"); + e.printStackTrace(); + } + } + if (comp == null) { - comp.addSubmodelInterface(iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y); + // As SubmodelComponent is abstract, for now SubmodelComponents are instantiated as SimpleRectangularSubmodelComponents + comp = new CustomRectComponent(model, 0, "", path); + + comp.setSubmodelScale(params.composition.innerScale); + comp.setSize(params.width, params.height); + for (InterfacePinParams iPinParams : params.interfacePins) + { + comp.addSubmodelInterface(iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y); + } } initSubmodelComponents(comp, params.composition); return comp;