GUIComponents now have names
[Mograsim.git] / net.mograsim.logic.ui.am2900 / src / net / mograsim / logic / ui / examples / JsonExample.java
index 09e8069..009dedd 100644 (file)
@@ -1,75 +1,94 @@
 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.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)
        {
-               IndirectGUIComponentCreator.create(model, "GUIAm2901", new HashMap<String, Object>());
+               IndirectGUIComponentCreator.createComponent(model, "Am2901", JsonNull.INSTANCE, "Am2901 instance");
        }
 
        private static class TestComponent extends SimpleRectangularSubmodelComponent
        {
-               protected TestComponent(ViewModelModifiable model)
+               protected TestComponent(ViewModelModifiable model, String name)
                {
-                       super(model, 1, "Test");
+                       super(model, 1, "Test", name);
                        setSubmodelScale(.4);
                        setInputPins("Input pin #0");
-                       SubmodelComponentDeserializer.create(submodelModifiable, "HalfAdder.json");
+                       SubmodelComponentDeserializer.create(submodelModifiable, "HalfAdder.json", "halfadder");
                }
        }
 
-       // Execute only after HalfAdder.json has been created
-       public static void refJsonFromJsonTest(ViewModelModifiable model)
+       @SuppressWarnings("unused") // GUIWires being created
+       private static void basicTest(ViewModelModifiable viewModel)
        {
-               TestComponent t = new TestComponent(model);
-               t.calculateParams().writeJson("Test.json");
-               SubmodelComponent c = SubmodelComponentDeserializer.create(model, "Test.json");
-               c.moveTo(0, 50);
-       }
-
-       public static void createHalfAdderExample(ViewModelModifiable model)
-       {
-               GUIhalfadder tmp = new GUIhalfadder(model);
-               tmp.moveTo(1000, 50);
-               SubmodelComponentParams p = tmp.calculateParams();
-               try
+               GUI_rsLatch comp = new GUI_rsLatch(viewModel, "Original RS latch");
+               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, "Deserialized RS latch");
+               componentD.moveTo(30, 50);
+               double h = 0;
+               for (String s : comp.getInputPinNames())
                {
-                       p.writeJson("HalfAdder.json");
-                       p = SubmodelComponentParams.readJson("HalfAdder.json");
+                       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;
                }
-               catch (IOException e)
+               h = 0;
+               for (String s : comp.getOutputPinNames())
                {
-                       e.printStackTrace();
+                       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;
                }
+       }
 
-               SubmodelComponentDeserializer.create(model, p);
+       // Execute only after HalfAdder.json has been created
+       public static void refJsonFromJsonTest(ViewModelModifiable model)
+       {
+               TestComponent t = new TestComponent(model, "Original component");
+               t.calculateParams().writeJson("Test.json");
+               SubmodelComponent c = SubmodelComponentDeserializer.create(model, "Test.json", "Deserialized component");
+               c.moveTo(0, 50);
        }
 
        @SuppressWarnings("unused") // for GUIWires being created
        public static void createFromJsonExample(ViewModelModifiable model)
        {
-               SimpleRectangularSubmodelComponent tmp = new GUIfulladder(model);
+               SimpleRectangularSubmodelComponent tmp = new GUIfulladder(model, "Original full adder");
                SubmodelComponentParams pC = tmp.calculateParams();
                tmp.moveTo(1000, 100);
                try
@@ -83,7 +102,7 @@ public class JsonExample
                }
 
                SimpleRectangularSubmodelComponent adder = (SimpleRectangularSubmodelComponent) SubmodelComponentDeserializer.create(model,
-                               "FullAdder.json");
+                               "FullAdder.json", "Deserialized full adder");
 
                GUIManualSwitch swA = new GUIManualSwitch(model);
                swA.moveTo(0, 0);
@@ -105,7 +124,7 @@ public class JsonExample
                new GUIWire(model, adder.getPin("Y"), bdY.getInputPin());
                new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin());
 
-               SubmodelComponent adder2 = SubmodelComponentDeserializer.create(model, pC);
+               SubmodelComponent adder2 = SubmodelComponentDeserializer.create(model, pC, "Full adder created from params instance");
 
                swA = new GUIManualSwitch(model);
                swA.moveTo(0, 70);