Updated SubmodelComponentParams json format
authorFabian Stemmler <stemmler@in.tum.de>
Fri, 21 Jun 2019 10:22:25 +0000 (12:22 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Fri, 21 Jun 2019 10:22:25 +0000 (12:22 +0200)
Instead of saving an inner GUIComponents logicWidth, a Map<String,
Object> containing all relevant information for instantiation is saved.
Insured that FileWriter/Readers are closed in JsonHandler.

12 files changed:
net.mograsim.logic.ui/FullAdder.json
net.mograsim.logic.ui/HalfAdder.json
net.mograsim.logic.ui/Test.json
net.mograsim.logic.ui/src/net/mograsim/logic/ui/examples/JsonExample.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUICustomComponentCreator.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SimpleRectangularGUIGate.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponentParams.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/WireCrossPoint.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/JsonHandler.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/util/Version.java

index ff9724e..40c51c7 100644 (file)
@@ -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": [
index e9b9f74..48c1980 100644 (file)
@@ -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": [
index 651205a..56085df 100644 (file)
@@ -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": []
index 9596a69..4ebd32a 100644 (file)
@@ -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
index 69b02bf..c19b800 100644 (file)
@@ -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<String, Object> getInstantiationParameters()
+       {
+               return new TreeMap<>();
+       }
 }
\ No newline at end of file
index 02b022a..ee034c8 100644 (file)
@@ -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<String, Object> 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;
        }
 }
index 9058d2e..96bf11e 100644 (file)
@@ -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<Pin> 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<String, Object> getInstantiationParameters()
+       {
+               Map<String, Object> m = super.getInstantiationParameters();
+               m.put(kLogicWidth, logicWidth);
+               return m;
+       }
 }
\ No newline at end of file
index 27699bd..c2a900c 100644 (file)
@@ -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();
index b166d57..3eb0629 100644 (file)
@@ -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<String, Object> params;
                }
        }
 
index e48d775..1124233 100644 (file)
@@ -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<String, Object> getInstantiationParameters()
+       {
+               Map<String, Object> m = super.getInstantiationParameters();
+               m.put(SimpleRectangularGUIGate.kLogicWidth, logicWidth);
+               return m;
+       }
+
 }
\ No newline at end of file
index 94231a3..dc8ace3 100644 (file)
@@ -12,24 +12,22 @@ public class JsonHandler
 {
        private static Gson parser = new GsonBuilder().setPrettyPrinting().create();
 
-       @SuppressWarnings("resource")
        public static <T> T readJson(String path, Class<T> 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));
+               }
        }
 }
index cfd168e..366ab02 100644 (file)
@@ -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)