Cleaned up implementations of GUIComponent#getParamsForSerializing
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / standard / memory / GUIMemoryWA.java
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;
+       }
 }