Updated to new SWTHelper version
[Mograsim.git] / net.mograsim.logic.ui.am2900 / src / net / mograsim / logic / ui / examples / JsonExample.java
index 128ec05..009dedd 100644 (file)
@@ -2,8 +2,6 @@ package net.mograsim.logic.ui.examples;
 
 import java.io.IOException;
 
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 import com.google.gson.JsonNull;
 
 import net.mograsim.logic.ui.SimpleLogicUIStandalone;
@@ -12,13 +10,13 @@ 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
 {
@@ -29,31 +27,31 @@ public class JsonExample
 
        public static void mappingTest(ViewModelModifiable model)
        {
-               IndirectGUIComponentCreator.createComponent(model, "Am2901", JsonNull.INSTANCE);
+               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");
                }
        }
 
        @SuppressWarnings("unused") // GUIWires being created
        private static void basicTest(ViewModelModifiable viewModel)
        {
-               GUI_rsLatch comp = new GUI_rsLatch(viewModel);
+               GUI_rsLatch comp = new GUI_rsLatch(viewModel, "Original RS latch");
                comp.moveTo(30, 0);
                SubmodelComponentParams params = comp.calculateParams();
-               String jsonString = new GsonBuilder().setPrettyPrinting().create().toJson(params);
+               String jsonString = JsonHandler.toJson(params);
                System.out.println(jsonString);
-               SubmodelComponent deserialized = SubmodelComponentDeserializer.create(viewModel,
-                               new Gson().fromJson(jsonString, SubmodelComponentParams.class));
-               deserialized.moveTo(30, 50);
+               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())
                {
@@ -62,7 +60,7 @@ public class JsonExample
                        new GUIWire(viewModel, sw.getOutputPin(), comp.getPin(s));
                        sw = new GUIManualSwitch(viewModel);
                        sw.moveTo(0, h + 50);
-                       new GUIWire(viewModel, sw.getOutputPin(), deserialized.getPin(s));
+                       new GUIWire(viewModel, sw.getOutputPin(), componentD.getPin(s));
                        h += 20;
                }
                h = 0;
@@ -73,7 +71,7 @@ public class JsonExample
                        new GUIWire(viewModel, bd.getInputPin(), comp.getPin(s));
                        bd = new GUIBitDisplay(viewModel);
                        bd.moveTo(80, h + 50);
-                       new GUIWire(viewModel, bd.getInputPin(), deserialized.getPin(s));
+                       new GUIWire(viewModel, bd.getInputPin(), componentD.getPin(s));
                        h += 20;
                }
        }
@@ -81,34 +79,16 @@ public class JsonExample
        // Execute only after HalfAdder.json has been created
        public static void refJsonFromJsonTest(ViewModelModifiable model)
        {
-               TestComponent t = new TestComponent(model);
+               TestComponent t = new TestComponent(model, "Original component");
                t.calculateParams().writeJson("Test.json");
-               SubmodelComponent c = SubmodelComponentDeserializer.create(model, "Test.json");
+               SubmodelComponent c = SubmodelComponentDeserializer.create(model, "Test.json", "Deserialized component");
                c.moveTo(0, 50);
        }
 
-       public static void createHalfAdderExample(ViewModelModifiable model)
-       {
-               GUIhalfadder tmp = new GUIhalfadder(model);
-               tmp.moveTo(1000, 50);
-               SubmodelComponentParams p = tmp.calculateParams();
-               try
-               {
-                       p.writeJson("HalfAdder.json");
-                       p = SubmodelComponentParams.readJson("HalfAdder.json");
-               }
-               catch (IOException e)
-               {
-                       e.printStackTrace();
-               }
-
-               SubmodelComponentDeserializer.create(model, p);
-       }
-
        @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
@@ -122,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);
@@ -144,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);