Cleaned up implementations of GUIComponent#getParamsForSerializing
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 2 Sep 2019 11:47:01 +0000 (13:47 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 2 Sep 2019 11:48:25 +0000 (13:48 +0200)
16 files changed:
net.mograsim.logic.model.am2900/test/net/mograsim/logic/model/am2900/am2904/Am2904Testbench.java
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/GUIComponent.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIBitDisplay.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIClock.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIManualSwitch.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIMerger.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUISplitter.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUITriStateBuffer.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/SimpleRectangularGUIGate.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/TextComponent.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/wires/WireCrossPoint.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LegacySubmodelComponentSerializer.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/ViewModelSerializer.java
net.mograsim.logic.model/src/net/mograsim/logic/model/util/JsonHandler.java
net.mograsim.machine/src/net/mograsim/machine/standard/memory/GUIMemoryWA.java

index a09de95..832d786 100644 (file)
@@ -31,7 +31,7 @@ public class Am2904Testbench
                testbench.setOutlineRenderer(new Renderer()
                {
                        @Override
-                       public Object getParamsForSerializing(IdentifierGetter idGetter)
+                       public Void getParamsForSerializing(IdentifierGetter idGetter)
                        {
                                return null;
                        }
index fedca49..8712ca6 100644 (file)
@@ -90,7 +90,7 @@ public class ComponentHandle extends Handle
        public Optional<ComponentInfo> reqCopy(Point refPoint)
        {
                return Optional.of(new ComponentInfo(parent.getPosX() - refPoint.x, parent.getPosY() - refPoint.y, Editor.getIdentifier(parent),
-                               parent.getParamsForSerializing(new IdentifierGetter())));
+                               parent.getParamsForSerializingJSON(new IdentifierGetter())));
        }
 
        @Override
index 11c4334..2e4d768 100644 (file)
@@ -7,9 +7,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.function.Consumer;
 
-import com.google.gson.JsonElement;
-import com.google.gson.JsonNull;
-
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.model.model.ViewModelModifiable;
@@ -289,9 +286,9 @@ public abstract class GUIComponent implements JSONSerializable
        // serializing
 
        @Override
-       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       public Object getParamsForSerializing(IdentifierGetter idGetter)
        {
-               return JsonNull.INSTANCE;
+               return null;
        }
 
        // listeners
index 12c0ee8..0115309 100644 (file)
@@ -2,9 +2,6 @@ package net.mograsim.logic.model.model.components.atomic;
 
 import org.eclipse.swt.graphics.Color;
 
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
@@ -94,9 +91,9 @@ public class GUIBitDisplay extends GUIComponent
        }
 
        @Override
-       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       public Integer getParamsForSerializing(IdentifierGetter idGetter)
        {
-               return new JsonPrimitive(logicWidth);
+               return logicWidth;
        }
 
        static
index 91a4f32..a3ff96c 100644 (file)
@@ -2,8 +2,6 @@ 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.JsonSyntaxException;
 
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
@@ -22,6 +20,7 @@ import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.model.modeladapter.componentadapters.ClockAdapter;
 import net.mograsim.logic.model.serializing.IdentifierGetter;
 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
+import net.mograsim.logic.model.util.JsonHandler;
 import net.mograsim.preferences.Preferences;
 
 public class GUIClock extends GUIComponent
@@ -130,9 +129,9 @@ public class GUIClock extends GUIComponent
        }
 
        @Override
-       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       public GUIClockParams getParamsForSerializing(IdentifierGetter idGetter)
        {
-               return new Gson().toJsonTree(params);
+               return params;
        }
 
        static
@@ -140,7 +139,7 @@ public class GUIClock extends GUIComponent
                ViewLogicModelAdapter.addComponentAdapter(new ClockAdapter());
                IndirectGUIComponentCreator.setComponentSupplier(GUIClock.class.getName(), (m, p, n) ->
                {
-                       GUIClockParams params = new Gson().fromJson(p, GUIClockParams.class);
+                       GUIClockParams params = JsonHandler.fromJsonTree(p, GUIClockParams.class);
                        if (params == null)
                                throw new JsonSyntaxException("Invalid!!!");
                        return new GUIClock(m, params, n);
index c721f46..e9853a1 100644 (file)
@@ -2,9 +2,6 @@ package net.mograsim.logic.model.model.components.atomic;
 
 import org.eclipse.swt.graphics.Color;
 
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
@@ -166,9 +163,9 @@ public class GUIManualSwitch extends GUIComponent
        }
 
        @Override
-       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       public Integer getParamsForSerializing(IdentifierGetter idGetter)
        {
-               return new JsonPrimitive(logicWidth);
+               return logicWidth;
        }
 
        static
index 4d29042..313a51a 100644 (file)
@@ -2,9 +2,6 @@ package net.mograsim.logic.model.model.components.atomic;
 
 import org.eclipse.swt.SWT;
 
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.core.types.BitVectorFormatter;
@@ -78,9 +75,9 @@ public class GUIMerger extends GUIComponent
        }
 
        @Override
-       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       public Integer getParamsForSerializing(IdentifierGetter idGetter)
        {
-               return new JsonPrimitive(logicWidth);
+               return logicWidth;
        }
 
        public void setLogicModelBinding(ReadEnd[] inputEnds, ReadEnd outputEnd)
index 0962daa..9527279 100644 (file)
@@ -2,9 +2,6 @@ package net.mograsim.logic.model.model.components.atomic;
 
 import org.eclipse.swt.SWT;
 
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.core.types.BitVectorFormatter;
@@ -78,9 +75,9 @@ public class GUISplitter extends GUIComponent
        }
 
        @Override
-       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       public Integer getParamsForSerializing(IdentifierGetter idGetter)
        {
-               return new JsonPrimitive(logicWidth);
+               return logicWidth;
        }
 
        public void setLogicModelBinding(ReadEnd inputEnd, ReadEnd[] outputEnds)
index 83b89f5..8900b6e 100644 (file)
@@ -2,8 +2,6 @@ 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.JsonSyntaxException;
 
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
@@ -18,6 +16,7 @@ import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.model.modeladapter.componentadapters.TriStateBufferAdapter;
 import net.mograsim.logic.model.serializing.IdentifierGetter;
 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
+import net.mograsim.logic.model.util.JsonHandler;
 import net.mograsim.preferences.Preferences;
 
 public class GUITriStateBuffer extends GUIComponent
@@ -88,9 +87,9 @@ public class GUITriStateBuffer extends GUIComponent
        }
 
        @Override
-       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       public GUITriStateBufferParams getParamsForSerializing(IdentifierGetter idGetter)
        {
-               return new Gson().toJsonTree(params);
+               return params;
        }
 
        static
@@ -98,7 +97,7 @@ public class GUITriStateBuffer extends GUIComponent
                ViewLogicModelAdapter.addComponentAdapter(new TriStateBufferAdapter());
                IndirectGUIComponentCreator.setComponentSupplier(GUITriStateBuffer.class.getName(), (m, p, n) ->
                {
-                       GUITriStateBufferParams params = new Gson().fromJson(p, GUITriStateBufferParams.class);
+                       GUITriStateBufferParams params = JsonHandler.fromJsonTree(p, GUITriStateBufferParams.class);
                        if (params == null)
                                throw new JsonSyntaxException("Invalid!!!");
                        return new GUITriStateBuffer(m, params, n);
index e343c58..86582c7 100644 (file)
@@ -5,7 +5,6 @@ import java.util.List;
 
 import org.eclipse.swt.graphics.Color;
 
-import com.google.gson.JsonElement;
 import com.google.gson.JsonPrimitive;
 
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
@@ -96,8 +95,8 @@ public class SimpleRectangularGUIGate extends GUIComponent
         * @see GUIComponent#getParamsForSerializing()
         */
        @Override
-       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       public Integer getParamsForSerializing(IdentifierGetter idGetter)
        {
-               return new JsonPrimitive(logicWidth);
+               return logicWidth;
        }
 }
\ No newline at end of file
index e00dbb9..d46acb8 100644 (file)
@@ -2,9 +2,6 @@ package net.mograsim.logic.model.model.components.atomic;
 
 import org.eclipse.swt.graphics.Color;
 
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
@@ -55,9 +52,9 @@ public class TextComponent extends GUIComponent
        // serializing
 
        @Override
-       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       public String getParamsForSerializing(IdentifierGetter idGetter)
        {
-               return new JsonPrimitive(text);
+               return text;
        }
 
        static
index 5751596..d558daa 100644 (file)
@@ -1,8 +1,5 @@
 package net.mograsim.logic.model.model.wires;
 
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.core.LogicObserver;
@@ -121,9 +118,9 @@ public class WireCrossPoint extends GUIComponent
        // serializing
 
        @Override
-       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       public Integer getParamsForSerializing(IdentifierGetter idGetter)
        {
-               return new JsonPrimitive(logicWidth);
+               return logicWidth;
        }
 
        static
index f60fe5f..c50019f 100644 (file)
@@ -241,7 +241,7 @@ public final class LegacySubmodelComponentSerializer
                        } else
                        {
                                innerComponentParams.id = idGetter.componentIDs.apply(innerComponent);
-                               innerComponentParams.params = innerComponent.getParamsForSerializing(idGetter);
+                               innerComponentParams.params = innerComponent.getParamsForSerializingJSON(idGetter);
                        }
                        innerComponentParams.name = innerComponent.name;
                        i1++;
index 38bdf9c..b6c2879 100644 (file)
@@ -158,7 +158,7 @@ public class ViewModelSerializer
                        } else
                        {
                                compParams.id = idGetter.componentIDs.apply(component);
-                               compParams.params = component.getParamsForSerializing(idGetter);
+                               compParams.params = component.getParamsForSerializingJSON(idGetter);
                        }
                        compParams.name = component.name;
                }
index 4f60bdf..0f151b9 100644 (file)
@@ -42,6 +42,11 @@ public class JsonHandler
                return parser.fromJson(rawJson, type);
        }
 
+       public static <T> T fromJsonTree(JsonElement src, Class<T> type)
+       {
+               return parser.fromJson(src, type);
+       }
+
        public static void writeJson(Object o, String path) throws IOException
        {
                try (FileWriter writer = new FileWriter(path))
index 6dbff4c..5222926 100644 (file)
@@ -2,9 +2,6 @@ package net.mograsim.machine.standard.memory;
 
 import org.eclipse.swt.graphics.Color;
 
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
@@ -17,18 +14,16 @@ import net.mograsim.logic.model.model.wires.PinUsage;
 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.model.serializing.IdentifierGetter;
 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
+import net.mograsim.logic.model.util.JsonHandler;
 import net.mograsim.machine.MainMemoryDefinition;
-import net.mograsim.machine.standard.memory.WordAddressableMemoryComponent;
 import net.mograsim.preferences.Preferences;
 
 public class GUIMemoryWA extends GUIComponent
 {
-       private final MainMemoryDefinition definition;
-       private final Pin addrPin, dataPin, rWPin;
-       private WordAddressableMemoryComponent memory;
-       private final static int width = 100, height = 300;
-       
-       private final static String addrKey = "addrBits", cellWidthKey = "cellWidth", minAddrKey = "minAddr", maxAddrKey = "maxAddr";
+       private final MainMemoryDefinition              definition;
+       private final Pin                                               addrPin, dataPin, rWPin;
+       private WordAddressableMemoryComponent  memory;
+       private final static int                                width   = 100, height = 300;
 
        public GUIMemoryWA(ViewModelModifiable model, MainMemoryDefinition definition, String name)
        {
@@ -55,17 +50,17 @@ public class GUIMemoryWA extends GUIComponent
        {
                return rWPin;
        }
-       
+
        public void setLogicModelBinding(WordAddressableMemoryComponent memory)
        {
                this.memory = memory;
        }
-       
+
        public MainMemoryDefinition getDefinition()
        {
                return definition;
        }
-       
+
        public WordAddressableMemoryComponent getMemory()
        {
                return memory;
@@ -76,7 +71,7 @@ public class GUIMemoryWA extends GUIComponent
        {
                // TODO This is copied from SimpleRectangularGUIGate; do this via delegation instead
                Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
-               if (foreground != null)
+               if(foreground != null)
                        gc.setForeground(foreground);
                gc.drawRectangle(getPosX(), getPosY(), width, height);
                Font oldFont = gc.getFont();
@@ -85,21 +80,21 @@ public class GUIMemoryWA extends GUIComponent
                String label = "RAM";
                Point textExtent = gc.textExtent(label);
                Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
-               if (textColor != null)
+               if(textColor != null)
                        gc.setForeground(textColor);
                gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
                gc.setFont(oldFont);
        }
 
        @Override
-       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       public GUIMemoryWAParams getParamsForSerializing(IdentifierGetter idGetter)
        {
-               JsonObject o = new JsonObject();
-               o.addProperty(addrKey, definition.getMemoryAddressBits());
-               o.addProperty(cellWidthKey, definition.getCellWidth());
-               o.addProperty(maxAddrKey, definition.getMaximalAddress());
-               o.addProperty(minAddrKey, definition.getMinimalAddress());
-               return o;
+               GUIMemoryWAParams params = new GUIMemoryWAParams();
+               params.addrBits = definition.getMemoryAddressBits();
+               params.cellWidth = definition.getCellWidth();
+               params.minAddr = definition.getMinimalAddress();
+               params.maxAddr = definition.getMaximalAddress();
+               return params;
        }
 
        static
@@ -107,12 +102,16 @@ public class GUIMemoryWA extends GUIComponent
                ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
                IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
                {
-                       JsonObject o = (JsonObject) p;
-                       int addressBits = o.get(addrKey).getAsInt();
-                       int cellWidth = o.get(cellWidthKey).getAsInt();
-                       long maxAddr = o.get(maxAddrKey).getAsLong();
-                       long minAddr = o.get(minAddrKey).getAsLong();
-                       return new GUIMemoryWA(m, MainMemoryDefinition.create(addressBits, cellWidth, minAddr, maxAddr), n);
+                       GUIMemoryWAParams params = JsonHandler.fromJsonTree(p, GUIMemoryWAParams.class);
+                       return new GUIMemoryWA(m, MainMemoryDefinition.create(params.addrBits, params.cellWidth, params.minAddr, params.maxAddr), n);
                });
        }
+
+       public static class GUIMemoryWAParams
+       {
+               public int      addrBits;
+               public int      cellWidth;
+               public long     minAddr;
+               public long     maxAddr;
+       }
 }