From a19dde054ec5cab3c835840d569880ee8ee156dc Mon Sep 17 00:00:00 2001 From: Fabian Stemmler Date: Fri, 21 Jun 2019 12:22:25 +0200 Subject: [PATCH] Updated SubmodelComponentParams json format Instead of saving an inner GUIComponents logicWidth, a Map containing all relevant information for instantiation is saved. Insured that FileWriter/Readers are closed in JsonHandler. --- net.mograsim.logic.ui/FullAdder.json | 10 ++++--- net.mograsim.logic.ui/HalfAdder.json | 30 ++++++++++++++----- net.mograsim.logic.ui/Test.json | 4 +-- .../logic/ui/examples/JsonExample.java | 10 +++---- .../ui/model/components/GUIComponent.java | 7 +++++ .../components/GUICustomComponentCreator.java | 26 ++++++++-------- .../components/SimpleRectangularGUIGate.java | 11 +++++++ .../model/components/SubmodelComponent.java | 2 +- .../components/SubmodelComponentParams.java | 20 ++++++------- .../logic/ui/model/wires/WireCrossPoint.java | 12 ++++++++ .../mograsim/logic/ui/util/JsonHandler.java | 24 +++++++-------- .../net/mograsim/logic/ui/util/Version.java | 2 +- 12 files changed, 100 insertions(+), 58 deletions(-) diff --git a/net.mograsim.logic.ui/FullAdder.json b/net.mograsim.logic.ui/FullAdder.json index ff9724e5..40c51c76 100644 --- a/net.mograsim.logic.ui/FullAdder.json +++ b/net.mograsim.logic.ui/FullAdder.json @@ -1,4 +1,4 @@ -mograsim version: 0.1.0 +mograsim version: 0.1.1 { "type": "SimpleRectangularSubmodelComponent", "width": 35.0, @@ -54,7 +54,7 @@ mograsim version: 0.1.0 "y": 40.0 }, "type": "class:net.mograsim.logic.ui.model.components.mi.nandbased.GUIhalfadder", - "logicWidth": 1 + "params": {} }, { "pos": { @@ -62,7 +62,7 @@ mograsim version: 0.1.0 "y": 7.5 }, "type": "class:net.mograsim.logic.ui.model.components.mi.nandbased.GUIhalfadder", - "logicWidth": 1 + "params": {} }, { "pos": { @@ -70,7 +70,9 @@ mograsim version: 0.1.0 "y": 40.0 }, "type": "class:net.mograsim.logic.ui.model.components.GUINandGate", - "logicWidth": 1 + "params": { + "logicWidth": 1 + } } ], "innerWires": [ diff --git a/net.mograsim.logic.ui/HalfAdder.json b/net.mograsim.logic.ui/HalfAdder.json index e9b9f744..48c19802 100644 --- a/net.mograsim.logic.ui/HalfAdder.json +++ b/net.mograsim.logic.ui/HalfAdder.json @@ -1,4 +1,4 @@ -mograsim version: 0.1.0 +mograsim version: 0.1.1 { "type": "SimpleRectangularSubmodelComponent", "width": 35.0, @@ -46,7 +46,9 @@ mograsim version: 0.1.0 "y": 15.0 }, "type": "class:net.mograsim.logic.ui.model.components.GUINandGate", - "logicWidth": 1 + "params": { + "logicWidth": 1 + } }, { "pos": { @@ -54,7 +56,9 @@ mograsim version: 0.1.0 "y": 2.5 }, "type": "class:net.mograsim.logic.ui.model.components.GUINandGate", - "logicWidth": 1 + "params": { + "logicWidth": 1 + } }, { "pos": { @@ -62,7 +66,9 @@ mograsim version: 0.1.0 "y": 27.5 }, "type": "class:net.mograsim.logic.ui.model.components.GUINandGate", - "logicWidth": 1 + "params": { + "logicWidth": 1 + } }, { "pos": { @@ -70,7 +76,9 @@ mograsim version: 0.1.0 "y": 2.5 }, "type": "class:net.mograsim.logic.ui.model.components.GUINandGate", - "logicWidth": 1 + "params": { + "logicWidth": 1 + } }, { "pos": { @@ -78,7 +86,9 @@ mograsim version: 0.1.0 "y": 11.5 }, "type": "class:net.mograsim.logic.ui.model.wires.WireCrossPoint", - "logicWidth": 1 + "params": { + "logicWidth": 1 + } }, { "pos": { @@ -86,7 +96,9 @@ mograsim version: 0.1.0 "y": 36.5 }, "type": "class:net.mograsim.logic.ui.model.wires.WireCrossPoint", - "logicWidth": 1 + "params": { + "logicWidth": 1 + } }, { "pos": { @@ -94,7 +106,9 @@ mograsim version: 0.1.0 "y": 24.0 }, "type": "class:net.mograsim.logic.ui.model.wires.WireCrossPoint", - "logicWidth": 1 + "params": { + "logicWidth": 1 + } } ], "innerWires": [ diff --git a/net.mograsim.logic.ui/Test.json b/net.mograsim.logic.ui/Test.json index 651205aa..56085df7 100644 --- a/net.mograsim.logic.ui/Test.json +++ b/net.mograsim.logic.ui/Test.json @@ -1,4 +1,4 @@ -mograsim version: 0.1.0 +mograsim version: 0.1.1 { "type": "SimpleRectangularSubmodelComponent", "width": 35.0, @@ -22,7 +22,7 @@ mograsim version: 0.1.0 "y": 0.0 }, "type": "file:HalfAdder.json", - "logicWidth": 1 + "params": {} } ], "innerWires": [] diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/examples/JsonExample.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/examples/JsonExample.java index 9596a693..4ebd32ae 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/examples/JsonExample.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/examples/JsonExample.java @@ -114,11 +114,11 @@ public class JsonExample bdZ = new GUIBitDisplay(model); bdZ.moveTo(90, 85); - new GUIWire(model, swA.getOutputPin(), adder.getPin("A")); - new GUIWire(model, swB.getOutputPin(), adder.getPin("B")); - new GUIWire(model, swC.getOutputPin(), adder.getPin("C")); + new GUIWire(model, swA.getOutputPin(), adder2.getPin("A")); + new GUIWire(model, swB.getOutputPin(), adder2.getPin("B")); + new GUIWire(model, swC.getOutputPin(), adder2.getPin("C")); - new GUIWire(model, adder.getPin("Y"), bdY.getInputPin()); - new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin()); + new GUIWire(model, adder2.getPin("Y"), bdY.getInputPin()); + new GUIWire(model, adder2.getPin("Z"), bdZ.getInputPin()); } } \ No newline at end of file diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java index 69b02bf9..c19b8003 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java @@ -6,6 +6,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.TreeMap; import java.util.function.Consumer; import java.util.function.Supplier; @@ -150,4 +151,10 @@ public abstract class GUIComponent { return identifierDelegate.get(); } + + @SuppressWarnings("static-method") + public Map getInstantiationParameters() + { + return new TreeMap<>(); + } } \ No newline at end of file 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 02b022a9..ee034c8c 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 @@ -11,6 +11,7 @@ import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentC 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; /** * Creates {@link SubmodelComponent}s from {@link SubmodelComponentParams} @@ -130,7 +131,7 @@ public final class GUICustomComponentCreator if (path.startsWith("class:")) { path = path.substring(6); - components[i] = createInnerComponentFromClass(comp, path, cParams.logicWidth); + components[i] = createInnerComponentFromClass(comp, path, cParams.params); components[i].moveTo(cParams.pos.x, cParams.pos.y); } else if (path.startsWith("file:")) { @@ -157,25 +158,22 @@ public final class GUICustomComponentCreator } } - private static GUIComponent createInnerComponentFromClass(SubmodelComponent parent, String classname, int logicWidth) - throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, - NoSuchMethodException, SecurityException, ClassNotFoundException + private static GUIComponent createInnerComponentFromClass(SubmodelComponent parent, String classname, Map params) + throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException, + ClassNotFoundException { Class c = Class.forName(classname); Object comp; - try + 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 { Constructor constructor = c.getConstructor(ViewModelModifiable.class); comp = constructor.newInstance(parent.submodelModifiable); } - catch (@SuppressWarnings("unused") NoSuchMethodException e) - { - Constructor constructor = c.getConstructor(ViewModelModifiable.class, int.class); - comp = constructor.newInstance(parent.submodelModifiable, logicWidth); - } - - if (comp instanceof GUIComponent) - return (GUIComponent) comp; - throw new IllegalArgumentException("Class given as subcomponent was not a GUIComponent!"); + return (GUIComponent) comp; } } diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SimpleRectangularGUIGate.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SimpleRectangularGUIGate.java index 9058d2e0..96bf11e6 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SimpleRectangularGUIGate.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SimpleRectangularGUIGate.java @@ -2,6 +2,7 @@ package net.mograsim.logic.ui.model.components; import java.util.ArrayList; import java.util.List; +import java.util.Map; import net.haspamelodica.swt.helper.gcs.GeneralGC; import net.haspamelodica.swt.helper.swtobjectwrappers.Font; @@ -26,6 +27,8 @@ public class SimpleRectangularGUIGate extends GUIComponent private MovablePin outputPin; private final List inputPins; + public static final String kLogicWidth = "logicWidth"; + protected SimpleRectangularGUIGate(ViewModelModifiable model, int logicWidth, String label, boolean isInverted) { super(model); @@ -74,4 +77,12 @@ public class SimpleRectangularGUIGate extends GUIComponent if (isInverted) gc.drawOval(posX + rectWidth, posY + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam); } + + @Override + public Map getInstantiationParameters() + { + Map m = super.getInstantiationParameters(); + m.put(kLogicWidth, logicWidth); + return m; + } } \ No newline at end of file diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java index 27699bd9..c2a900c2 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java @@ -257,7 +257,7 @@ public abstract class SubmodelComponent extends GUIComponent GUIComponent component = componentIt.next(); InnerComponentParams inner = new InnerComponentParams(); comps[i] = inner; - inner.logicWidth = component.getPins().iterator().next().logicWidth; // TODO This could be done a little more elegantly + inner.params = component.getInstantiationParameters(); Rectangle bounds = component.getBounds(); inner.pos = new Point(bounds.x, bounds.y); inner.type = component.getIdentifier(); diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponentParams.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponentParams.java index b166d577..3eb06297 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponentParams.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponentParams.java @@ -19,21 +19,21 @@ public class SubmodelComponentParams public static class InterfacePinParams { - public Point location; - public String name; - public int logicWidth; + Point location; + String name; + int logicWidth; } public static class InnerWireParams { - public InnerPinParams pin1, pin2; - public Point[] path; + InnerPinParams pin1, pin2; + Point[] path; } public static class InnerPinParams { - public int compId; - public String pinName; + int compId; + String pinName; } public static class ComponentCompositionParams @@ -44,9 +44,9 @@ public class SubmodelComponentParams public static class InnerComponentParams { - public Point pos; - public String type; - public int logicWidth; + Point pos; + String type; + Map params; } } diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/WireCrossPoint.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/WireCrossPoint.java index e48d7756..1124233b 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/WireCrossPoint.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/WireCrossPoint.java @@ -1,5 +1,7 @@ package net.mograsim.logic.ui.model.wires; +import java.util.Map; + import net.haspamelodica.swt.helper.gcs.GeneralGC; import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; import net.mograsim.logic.core.LogicObservable; @@ -9,6 +11,7 @@ import net.mograsim.logic.core.wires.Wire.ReadEnd; import net.mograsim.logic.ui.ColorHelper; import net.mograsim.logic.ui.model.ViewModelModifiable; import net.mograsim.logic.ui.model.components.GUIComponent; +import net.mograsim.logic.ui.model.components.SimpleRectangularGUIGate; public class WireCrossPoint extends GUIComponent { @@ -72,4 +75,13 @@ public class WireCrossPoint extends GUIComponent { return pin; } + + @Override + public Map getInstantiationParameters() + { + Map m = super.getInstantiationParameters(); + m.put(SimpleRectangularGUIGate.kLogicWidth, logicWidth); + return m; + } + } \ No newline at end of file diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java index 94231a3d..dc8ace3d 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java @@ -12,24 +12,22 @@ public class JsonHandler { private static Gson parser = new GsonBuilder().setPrettyPrinting().create(); - @SuppressWarnings("resource") public static T readJson(String path, Class type) throws IOException { - FileReader reader = new FileReader(path); - BufferedReader bf = new BufferedReader(reader); - bf.readLine(); // Skip version - String json = bf.lines().dropWhile(s -> s.length() == 0 || s.charAt(0) != '{').reduce("", (x, y) -> x.concat(y)); - T params = parser.fromJson(json, type); - reader.close(); - return params; + try (FileReader reader = new FileReader(path); BufferedReader bf = new BufferedReader(reader)) + { + String json = bf.lines().dropWhile(s -> s.length() == 0 || s.charAt(0) != '{').reduce("", (x, y) -> x.concat(y)); + T params = parser.fromJson(json, type); + return params; + } } public static void writeJson(Object o, String path) throws IOException { - @SuppressWarnings("resource") - FileWriter writer = new FileWriter(path); - writer.write(String.format("mograsim version: %s\n", Version.current.toString())); - writer.write(parser.toJson(o)); - writer.close(); // TODO: Insure that writer is closed + try (FileWriter writer = new FileWriter(path)) + { + writer.write(String.format("mograsim version: %s\n", Version.jsonCompVersion.toString())); + writer.write(parser.toJson(o)); + } } } diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/Version.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/Version.java index cfd168e4..366ab02b 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/Version.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/Version.java @@ -2,7 +2,7 @@ package net.mograsim.logic.ui.util; public final class Version { - public final static Version current = new Version(0, 1, 0); + public final static Version jsonCompVersion = new Version(0, 1, 1); public final int major, minor, patch; public Version(int major, int minor, int patch) -- 2.17.1