Updated version; added string-based methods in JsonHandler
[Mograsim.git] / net.mograsim.logic.ui.am2900 / src / net / mograsim / logic / ui / examples / JsonExample.java
index a35ab2f..c0ebad6 100644 (file)
@@ -1,31 +1,34 @@
 package net.mograsim.logic.ui.examples;
 
 import java.io.IOException;
-import java.util.HashMap;
+
+import com.google.gson.JsonNull;
 
 import net.mograsim.logic.ui.SimpleLogicUIStandalone;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.GUIBitDisplay;
-import net.mograsim.logic.ui.model.components.GUIComponentCreator;
-import net.mograsim.logic.ui.model.components.GUICustomComponentCreator;
-import net.mograsim.logic.ui.model.components.GUIManualSwitch;
-import net.mograsim.logic.ui.model.components.SimpleRectangularSubmodelComponent;
-import net.mograsim.logic.ui.model.components.SubmodelComponent;
-import net.mograsim.logic.ui.model.components.SubmodelComponentParams;
+import net.mograsim.logic.ui.model.components.atomic.GUIBitDisplay;
+import net.mograsim.logic.ui.model.components.atomic.GUIManualSwitch;
+import net.mograsim.logic.ui.model.components.mi.nandbased.GUI_rsLatch;
 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIfulladder;
 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIhalfadder;
+import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
+import net.mograsim.logic.ui.model.components.submodels.SubmodelComponent;
 import net.mograsim.logic.ui.model.wires.GUIWire;
+import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
+import net.mograsim.logic.ui.serializing.SubmodelComponentDeserializer;
+import net.mograsim.logic.ui.serializing.SubmodelComponentParams;
+import net.mograsim.logic.ui.util.JsonHandler;
 
 public class JsonExample
 {
        public static void main(String[] args)
        {
-               SimpleLogicUIStandalone.executeVisualisation(JsonExample::mappingTest);
+               SimpleLogicUIStandalone.executeVisualisation(JsonExample::basicTest);
        }
 
        public static void mappingTest(ViewModelModifiable model)
        {
-               GUIComponentCreator.create(model, "GUIAm2901", new HashMap<String, Object>());
+               IndirectGUIComponentCreator.createComponent(model, "Am2901", JsonNull.INSTANCE);
        }
 
        private static class TestComponent extends SimpleRectangularSubmodelComponent
@@ -35,7 +38,42 @@ public class JsonExample
                        super(model, 1, "Test");
                        setSubmodelScale(.4);
                        setInputPins("Input pin #0");
-                       GUICustomComponentCreator.create(submodelModifiable, "HalfAdder.json");
+                       SubmodelComponentDeserializer.create(submodelModifiable, "HalfAdder.json");
+               }
+       }
+
+       @SuppressWarnings("unused") // GUIWires being created
+       private static void basicTest(ViewModelModifiable viewModel)
+       {
+               GUI_rsLatch comp = new GUI_rsLatch(viewModel);
+               comp.moveTo(30, 0);
+               SubmodelComponentParams params = comp.calculateParams();
+               String jsonString = JsonHandler.toJson(params);
+               System.out.println(jsonString);
+               SubmodelComponentParams paramsD = JsonHandler.fromJson(jsonString, SubmodelComponentParams.class);
+               SubmodelComponent componentD = SubmodelComponentDeserializer.create(viewModel, paramsD);
+               componentD.moveTo(30, 50);
+               double h = 0;
+               for (String s : comp.getInputPinNames())
+               {
+                       GUIManualSwitch sw = new GUIManualSwitch(viewModel);
+                       sw.moveTo(0, h);
+                       new GUIWire(viewModel, sw.getOutputPin(), comp.getPin(s));
+                       sw = new GUIManualSwitch(viewModel);
+                       sw.moveTo(0, h + 50);
+                       new GUIWire(viewModel, sw.getOutputPin(), componentD.getPin(s));
+                       h += 20;
+               }
+               h = 0;
+               for (String s : comp.getOutputPinNames())
+               {
+                       GUIBitDisplay bd = new GUIBitDisplay(viewModel);
+                       bd.moveTo(80, h);
+                       new GUIWire(viewModel, bd.getInputPin(), comp.getPin(s));
+                       bd = new GUIBitDisplay(viewModel);
+                       bd.moveTo(80, h + 50);
+                       new GUIWire(viewModel, bd.getInputPin(), componentD.getPin(s));
+                       h += 20;
                }
        }
 
@@ -44,7 +82,7 @@ public class JsonExample
        {
                TestComponent t = new TestComponent(model);
                t.calculateParams().writeJson("Test.json");
-               SubmodelComponent c = GUICustomComponentCreator.create(model, "Test.json");
+               SubmodelComponent c = SubmodelComponentDeserializer.create(model, "Test.json");
                c.moveTo(0, 50);
        }
 
@@ -63,7 +101,7 @@ public class JsonExample
                        e.printStackTrace();
                }
 
-               GUICustomComponentCreator.create(model, p);
+               SubmodelComponentDeserializer.create(model, p);
        }
 
        @SuppressWarnings("unused") // for GUIWires being created
@@ -82,7 +120,7 @@ public class JsonExample
                        e.printStackTrace();
                }
 
-               SimpleRectangularSubmodelComponent adder = (SimpleRectangularSubmodelComponent) GUICustomComponentCreator.create(model,
+               SimpleRectangularSubmodelComponent adder = (SimpleRectangularSubmodelComponent) SubmodelComponentDeserializer.create(model,
                                "FullAdder.json");
 
                GUIManualSwitch swA = new GUIManualSwitch(model);
@@ -105,7 +143,7 @@ public class JsonExample
                new GUIWire(model, adder.getPin("Y"), bdY.getInputPin());
                new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin());
 
-               SubmodelComponent adder2 = GUICustomComponentCreator.create(model, pC);
+               SubmodelComponent adder2 = SubmodelComponentDeserializer.create(model, pC);
 
                swA = new GUIManualSwitch(model);
                swA.moveTo(0, 70);