Updated to new SWTHelper version
[Mograsim.git] / net.mograsim.logic.ui.am2900 / src / net / mograsim / logic / ui / examples / JsonExample.java
1 package net.mograsim.logic.ui.examples;
2
3 import java.io.IOException;
4
5 import com.google.gson.JsonNull;
6
7 import net.mograsim.logic.ui.SimpleLogicUIStandalone;
8 import net.mograsim.logic.ui.model.ViewModelModifiable;
9 import net.mograsim.logic.ui.model.components.atomic.GUIBitDisplay;
10 import net.mograsim.logic.ui.model.components.atomic.GUIManualSwitch;
11 import net.mograsim.logic.ui.model.components.mi.nandbased.GUI_rsLatch;
12 import net.mograsim.logic.ui.model.components.mi.nandbased.GUIfulladder;
13 import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
14 import net.mograsim.logic.ui.model.components.submodels.SubmodelComponent;
15 import net.mograsim.logic.ui.model.wires.GUIWire;
16 import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
17 import net.mograsim.logic.ui.serializing.SubmodelComponentDeserializer;
18 import net.mograsim.logic.ui.serializing.SubmodelComponentParams;
19 import net.mograsim.logic.ui.util.JsonHandler;
20
21 public class JsonExample
22 {
23         public static void main(String[] args)
24         {
25                 SimpleLogicUIStandalone.executeVisualisation(JsonExample::basicTest);
26         }
27
28         public static void mappingTest(ViewModelModifiable model)
29         {
30                 IndirectGUIComponentCreator.createComponent(model, "Am2901", JsonNull.INSTANCE, "Am2901 instance");
31         }
32
33         private static class TestComponent extends SimpleRectangularSubmodelComponent
34         {
35                 protected TestComponent(ViewModelModifiable model, String name)
36                 {
37                         super(model, 1, "Test", name);
38                         setSubmodelScale(.4);
39                         setInputPins("Input pin #0");
40                         SubmodelComponentDeserializer.create(submodelModifiable, "HalfAdder.json", "halfadder");
41                 }
42         }
43
44         @SuppressWarnings("unused") // GUIWires being created
45         private static void basicTest(ViewModelModifiable viewModel)
46         {
47                 GUI_rsLatch comp = new GUI_rsLatch(viewModel, "Original RS latch");
48                 comp.moveTo(30, 0);
49                 SubmodelComponentParams params = comp.calculateParams();
50                 String jsonString = JsonHandler.toJson(params);
51                 System.out.println(jsonString);
52                 SubmodelComponentParams paramsD = JsonHandler.fromJson(jsonString, SubmodelComponentParams.class);
53                 SubmodelComponent componentD = SubmodelComponentDeserializer.create(viewModel, paramsD, "Deserialized RS latch");
54                 componentD.moveTo(30, 50);
55                 double h = 0;
56                 for (String s : comp.getInputPinNames())
57                 {
58                         GUIManualSwitch sw = new GUIManualSwitch(viewModel);
59                         sw.moveTo(0, h);
60                         new GUIWire(viewModel, sw.getOutputPin(), comp.getPin(s));
61                         sw = new GUIManualSwitch(viewModel);
62                         sw.moveTo(0, h + 50);
63                         new GUIWire(viewModel, sw.getOutputPin(), componentD.getPin(s));
64                         h += 20;
65                 }
66                 h = 0;
67                 for (String s : comp.getOutputPinNames())
68                 {
69                         GUIBitDisplay bd = new GUIBitDisplay(viewModel);
70                         bd.moveTo(80, h);
71                         new GUIWire(viewModel, bd.getInputPin(), comp.getPin(s));
72                         bd = new GUIBitDisplay(viewModel);
73                         bd.moveTo(80, h + 50);
74                         new GUIWire(viewModel, bd.getInputPin(), componentD.getPin(s));
75                         h += 20;
76                 }
77         }
78
79         // Execute only after HalfAdder.json has been created
80         public static void refJsonFromJsonTest(ViewModelModifiable model)
81         {
82                 TestComponent t = new TestComponent(model, "Original component");
83                 t.calculateParams().writeJson("Test.json");
84                 SubmodelComponent c = SubmodelComponentDeserializer.create(model, "Test.json", "Deserialized component");
85                 c.moveTo(0, 50);
86         }
87
88         @SuppressWarnings("unused") // for GUIWires being created
89         public static void createFromJsonExample(ViewModelModifiable model)
90         {
91                 SimpleRectangularSubmodelComponent tmp = new GUIfulladder(model, "Original full adder");
92                 SubmodelComponentParams pC = tmp.calculateParams();
93                 tmp.moveTo(1000, 100);
94                 try
95                 {
96                         pC.writeJson("FullAdder.json");
97                         pC = SubmodelComponentParams.readJson("FullAdder.json");
98                 }
99                 catch (IOException e)
100                 {
101                         e.printStackTrace();
102                 }
103
104                 SimpleRectangularSubmodelComponent adder = (SimpleRectangularSubmodelComponent) SubmodelComponentDeserializer.create(model,
105                                 "FullAdder.json", "Deserialized full adder");
106
107                 GUIManualSwitch swA = new GUIManualSwitch(model);
108                 swA.moveTo(0, 0);
109                 GUIManualSwitch swB = new GUIManualSwitch(model);
110                 swB.moveTo(0, 25);
111                 GUIManualSwitch swC = new GUIManualSwitch(model);
112                 swC.moveTo(0, 50);
113
114                 adder.moveTo(30, 10);
115                 GUIBitDisplay bdY = new GUIBitDisplay(model);
116                 bdY.moveTo(90, 12.5);
117                 GUIBitDisplay bdZ = new GUIBitDisplay(model);
118                 bdZ.moveTo(90, 30);
119
120                 new GUIWire(model, swA.getOutputPin(), adder.getPin("A"));
121                 new GUIWire(model, swB.getOutputPin(), adder.getPin("B"));
122                 new GUIWire(model, swC.getOutputPin(), adder.getPin("C"));
123
124                 new GUIWire(model, adder.getPin("Y"), bdY.getInputPin());
125                 new GUIWire(model, adder.getPin("Z"), bdZ.getInputPin());
126
127                 SubmodelComponent adder2 = SubmodelComponentDeserializer.create(model, pC, "Full adder created from params instance");
128
129                 swA = new GUIManualSwitch(model);
130                 swA.moveTo(0, 70);
131                 swB = new GUIManualSwitch(model);
132                 swB.moveTo(0, 85);
133                 swC = new GUIManualSwitch(model);
134                 swC.moveTo(0, 100);
135
136                 adder2.moveTo(30, 80);
137                 bdY = new GUIBitDisplay(model);
138                 bdY.moveTo(90, 70);
139                 bdZ = new GUIBitDisplay(model);
140                 bdZ.moveTo(90, 85);
141
142                 new GUIWire(model, swA.getOutputPin(), adder2.getPin("A"));
143                 new GUIWire(model, swB.getOutputPin(), adder2.getPin("B"));
144                 new GUIWire(model, swC.getOutputPin(), adder2.getPin("C"));
145
146                 new GUIWire(model, adder2.getPin("Y"), bdY.getInputPin());
147                 new GUIWire(model, adder2.getPin("Z"), bdZ.getInputPin());
148         }
149 }