Improved Json style
authorChristian Femers <femers@in.tum.de>
Fri, 23 Aug 2019 15:06:18 +0000 (17:06 +0200)
committerChristian Femers <femers@in.tum.de>
Fri, 23 Aug 2019 15:06:18 +0000 (17:06 +0200)
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUITriStateBuffer.java

index bd4b76c..af862b0 100644 (file)
@@ -2,10 +2,8 @@ package net.mograsim.logic.model.model.components.atomic;
 \r
 import org.eclipse.swt.graphics.Color;\r
 \r
+import com.google.gson.Gson;\r
 import com.google.gson.JsonElement;\r
-import com.google.gson.JsonObject;\r
-import com.google.gson.JsonParseException;\r
-import com.google.gson.JsonPrimitive;\r
 import com.google.gson.JsonSyntaxException;\r
 \r
 import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
@@ -27,32 +25,35 @@ public class GUITriStateBuffer extends GUIComponent
        private Pin input;\r
        private Pin output;\r
        private Pin enable;\r
-       private Orientation orientation;\r
        private double[] path;\r
 \r
-       public GUITriStateBuffer(ViewModelModifiable model, int logicWidth, Orientation orientation)\r
+       private GUITriStateBufferParams params;\r
+\r
+       public GUITriStateBuffer(ViewModelModifiable model, GUITriStateBufferParams params)\r
        {\r
-               this(model, logicWidth, orientation, null);\r
+               this(model, params, null);\r
        }\r
 \r
-       public GUITriStateBuffer(ViewModelModifiable model, int logicWidth, Orientation orientation, String name)\r
+       public GUITriStateBuffer(ViewModelModifiable model, GUITriStateBufferParams params, String name)\r
        {\r
                super(model, name);\r
-               this.orientation = orientation;\r
+               this.params = params;\r
 \r
                double wHalf = width / 2;\r
                double hHalf = height / 2;\r
                double wQuar = width / 4;\r
                double hQuar = height / 4;\r
-               int ordi = orientation.ordinal();\r
+               int ordi = params.orientation.ordinal();\r
                int isVerti = (ordi % 4) / 2;\r
                int isHori = 1 ^ isVerti;\r
                int isAlt = ordi / 4;\r
                int isInv = ordi % 2;\r
                int isStd = 1 ^ isInv;\r
 \r
-               this.input = new Pin(this, "IN", logicWidth, width * isInv * isHori + wHalf * isVerti, height * isVerti * isStd + hHalf * isHori);\r
-               this.output = new Pin(this, "OUT", logicWidth, width * isStd * isHori + wHalf * isVerti, height * isVerti * isInv + hHalf * isHori);\r
+               this.input = new Pin(this, "IN", params.logicWidth, width * isInv * isHori + wHalf * isVerti,\r
+                               height * isVerti * isStd + hHalf * isHori);\r
+               this.output = new Pin(this, "OUT", params.logicWidth, width * isStd * isHori + wHalf * isVerti,\r
+                               height * isVerti * isInv + hHalf * isHori);\r
                this.enable = new Pin(this, "EN", 1, wQuar * isVerti + wHalf * (isAlt | isHori), hQuar * isHori + hHalf * (isAlt | isVerti));\r
                this.path = new double[] { width * (isStd ^ isHori), height * (isStd ^ isHori), width * isInv, height * isStd,\r
                                width * isStd * isHori + wHalf * isVerti, height * isVerti * isInv + hHalf * isHori };\r
@@ -86,10 +87,7 @@ public class GUITriStateBuffer extends GUIComponent
        @Override\r
        public JsonElement getParamsForSerializing(IdentifierGetter idGetter)\r
        {\r
-               JsonObject jo = new JsonObject();\r
-               jo.addProperty("logicWidth", input.logicWidth);\r
-               jo.addProperty("orientation", orientation.name());\r
-               return jo;\r
+               return new Gson().toJsonTree(params);\r
        }\r
 \r
        static\r
@@ -97,14 +95,19 @@ public class GUITriStateBuffer extends GUIComponent
                ViewLogicModelAdapter.addComponentAdapter(new TriStateBufferAdapter());\r
                IndirectGUIComponentCreator.setComponentSupplier(GUITriStateBuffer.class.getName(), (m, p, n) ->\r
                {\r
-                       if (!p.isJsonObject())\r
-                               throw new JsonSyntaxException("TriStateBuffer Params are not a JsonObject");\r
-                       JsonObject jo = p.getAsJsonObject();\r
-                       return new GUITriStateBuffer(m, jo.getAsJsonPrimitive("logicWidth").getAsInt(),\r
-                                       Orientation.valueOf(jo.getAsJsonPrimitive("orientation").getAsString()), n);\r
+                       GUITriStateBufferParams params = new Gson().fromJson(p, GUITriStateBufferParams.class);\r
+                       if (params == null)\r
+                               throw new JsonSyntaxException("Invalid!!!");\r
+                       return new GUITriStateBuffer(m, params, n);\r
                });\r
        }\r
 \r
+       private static class GUITriStateBufferParams\r
+       {\r
+               int logicWidth;\r
+               Orientation orientation;\r
+       }\r
+\r
        public enum Orientation\r
        {\r
                RIGHT, LEFT, UP, DOWN, RIGHT_ALT, LEFT_ALT, UP_ALT, DOWN_ALT;\r