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