GUIComponents now serialize and deserialize parameters (where needed)
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Wed, 3 Jul 2019 20:43:03 +0000 (22:43 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Wed, 3 Jul 2019 20:43:03 +0000 (22:43 +0200)
12 files changed:
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/atomic/GUIAndGate.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/atomic/GUIBitDisplay.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/atomic/GUIManualSwitch.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/atomic/GUINandGate.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/atomic/GUINotGate.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/atomic/GUIOrGate.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/atomic/SimpleRectangularGUIGate.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/atomic/TextComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/submodels/SimpleRectangularSubmodelComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/submodels/SubmodelComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/WireCrossPoint.java

index 1699da5..58c725d 100644 (file)
@@ -7,6 +7,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.function.Consumer;
 
+import com.google.gson.JsonElement;
+import com.google.gson.JsonNull;
+
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
@@ -263,6 +266,14 @@ public abstract class GUIComponent
         */
        public abstract void render(GeneralGC gc, Rectangle visibleRegion);
 
+       // serializing
+
+       @SuppressWarnings("static-method") // this method is intended to be overridden
+       public JsonElement getParams()
+       {
+               return JsonNull.INSTANCE;
+       }
+
        // listeners
 
        /**
index 8443e56..0b1a497 100644 (file)
@@ -4,6 +4,7 @@ import net.mograsim.logic.core.components.gates.AndGate;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.ui.modeladapter.componentadapters.SimpleGateAdapter;
+import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
 
 public class GUIAndGate extends SimpleRectangularGUIGate
 {
@@ -16,5 +17,6 @@ public class GUIAndGate extends SimpleRectangularGUIGate
        static
        {
                ViewLogicModelAdapter.addComponentAdapter(new SimpleGateAdapter<>(GUIAndGate.class, AndGate::new));
+               IndirectGUIComponentCreator.setComponentProvider(GUIAndGate.class.getCanonicalName(), (m, p) -> new GUIAndGate(m, p.getAsInt()));
        }
 }
\ No newline at end of file
index 9abb9e8..de755cd 100644 (file)
@@ -14,6 +14,7 @@ import net.mograsim.logic.ui.model.components.GUIComponent;
 import net.mograsim.logic.ui.model.wires.Pin;
 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.ui.modeladapter.componentadapters.BitDisplayAdapter;
+import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
 import net.mograsim.preferences.Preferences;
 
 public class GUIBitDisplay extends GUIComponent
@@ -83,5 +84,6 @@ public class GUIBitDisplay extends GUIComponent
        static
        {
                ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());
+               IndirectGUIComponentCreator.setComponentProvider(GUIBitDisplay.class.getCanonicalName(), (m, p) -> new GUIBitDisplay(m));
        }
 }
\ No newline at end of file
index 5ab078a..285cd16 100644 (file)
@@ -17,6 +17,7 @@ import net.mograsim.logic.ui.model.components.GUIComponent;
 import net.mograsim.logic.ui.model.wires.Pin;
 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.ui.modeladapter.componentadapters.ManualSwitchAdapter;
+import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
 import net.mograsim.preferences.Preferences;
 
 public class GUIManualSwitch extends GUIComponent
@@ -137,5 +138,6 @@ public class GUIManualSwitch extends GUIComponent
        static
        {
                ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
+               IndirectGUIComponentCreator.setComponentProvider(GUIManualSwitch.class.getName(), (m, p) -> new GUIManualSwitch(m));
        }
 }
\ No newline at end of file
index 359a6f6..f07b432 100644 (file)
@@ -17,7 +17,6 @@ public class GUINandGate extends SimpleRectangularGUIGate
        static
        {
                ViewLogicModelAdapter.addComponentAdapter(new SimpleGateAdapter<>(GUINandGate.class, NandGate::new));
-               // TODO read params
-               IndirectGUIComponentCreator.setComponentProvider(GUINandGate.class.getCanonicalName(), (m, p) -> new GUINandGate(m, 1));
+               IndirectGUIComponentCreator.setComponentProvider(GUINandGate.class.getCanonicalName(), (m, p) -> new GUINandGate(m, p.getAsInt()));
        }
 }
\ No newline at end of file
index 2a7c12b..f6ef024 100644 (file)
@@ -4,6 +4,7 @@ import net.mograsim.logic.core.components.gates.NotGate;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.ui.modeladapter.componentadapters.SimpleGateAdapter;
+import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
 
 public class GUINotGate extends SimpleRectangularGUIGate
 {
@@ -16,5 +17,6 @@ public class GUINotGate extends SimpleRectangularGUIGate
        static
        {
                ViewLogicModelAdapter.addComponentAdapter(new SimpleGateAdapter<>(GUINotGate.class, (t, p, o, i) -> new NotGate(t, p, i[0], o)));
+               IndirectGUIComponentCreator.setComponentProvider(GUINotGate.class.getCanonicalName(), (m, p) -> new GUINotGate(m, p.getAsInt()));
        }
 }
\ No newline at end of file
index fc88509..50050e0 100644 (file)
@@ -4,6 +4,7 @@ import net.mograsim.logic.core.components.gates.OrGate;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.ui.modeladapter.componentadapters.SimpleGateAdapter;
+import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
 
 public class GUIOrGate extends SimpleRectangularGUIGate
 {
@@ -16,5 +17,6 @@ public class GUIOrGate extends SimpleRectangularGUIGate
        static
        {
                ViewLogicModelAdapter.addComponentAdapter(new SimpleGateAdapter<>(GUIOrGate.class, OrGate::new));
+               IndirectGUIComponentCreator.setComponentProvider(GUIOrGate.class.getCanonicalName(), (m, p) -> new GUIOrGate(m, p.getAsInt()));
        }
 }
\ No newline at end of file
index c5b6c8e..7debf23 100644 (file)
@@ -5,6 +5,9 @@ import java.util.List;
 
 import org.eclipse.swt.graphics.Color;
 
+import com.google.gson.JsonElement;
+import com.google.gson.JsonPrimitive;
+
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
@@ -30,8 +33,6 @@ public class SimpleRectangularGUIGate extends GUIComponent
        private MovablePin outputPin;
        private final List<Pin> inputPins;
 
-       public static final String kLogicWidth = "logicWidth";
-
        protected SimpleRectangularGUIGate(ViewModelModifiable model, int logicWidth, String label, boolean isInverted)
        {
                super(model);
@@ -83,4 +84,18 @@ public class SimpleRectangularGUIGate extends GUIComponent
                gc.drawText(label, getPosX() + (rectWidth - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
                gc.setFont(oldFont);
        }
+
+       // serializing
+
+       /**
+        * {@link SimpleRectangularGUIGate}s implementation returns a {@link JsonPrimitive} of type int containing the {@link #logicWidth} of
+        * this component.
+        * 
+        * @see GUIComponent#getParams()
+        */
+       @Override
+       public JsonElement getParams()
+       {
+               return new JsonPrimitive(logicWidth);
+       }
 }
\ No newline at end of file
index e1fdc6c..d7255d3 100644 (file)
@@ -2,6 +2,9 @@ package net.mograsim.logic.ui.model.components.atomic;
 
 import org.eclipse.swt.graphics.Color;
 
+import com.google.gson.JsonElement;
+import com.google.gson.JsonPrimitive;
+
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
@@ -9,6 +12,7 @@ import net.mograsim.logic.ui.model.ViewModelModifiable;
 import net.mograsim.logic.ui.model.components.GUIComponent;
 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.ui.modeladapter.componentadapters.NoLogicAdapter;
+import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
 import net.mograsim.preferences.Preferences;
 
 //TODO clean size calculation mess
@@ -37,8 +41,17 @@ public class TextComponent extends GUIComponent
                gc.drawText(text, getPosX(), getPosY(), true);
        }
 
+       // serializing
+
+       @Override
+       public JsonElement getParams()
+       {
+               return new JsonPrimitive(text);
+       }
+
        static
        {
                ViewLogicModelAdapter.addComponentAdapter(new NoLogicAdapter<>(TextComponent.class));
+               IndirectGUIComponentCreator.setComponentProvider(TextComponent.class.getName(), (m, p) -> new TextComponent(m, p.getAsString()));
        }
 }
index 430da4e..65114b6 100644 (file)
@@ -19,8 +19,6 @@ import net.mograsim.preferences.Preferences;
 
 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
 {
-       public static String kLabel = "label", kInCount = "input_count", kOutCount = "output_count", kLogicWidth = "logic_width";
-
        private static final double width = 35;
        private static final double pinDistance = 10;
        private static final double pinNameMargin = .5;
index fd22f6f..49c2b5c 100644 (file)
@@ -498,7 +498,9 @@ public abstract class SubmodelComponent extends GUIComponent
                return false;
        }
 
-       // serializing; TODO move to serializing classes
+       // serializing
+
+       // TODO move the methods below to serializing classes
 
        public SubmodelComponentParams calculateParams()
        {
@@ -548,6 +550,7 @@ public abstract class SubmodelComponent extends GUIComponent
                        comps[i] = inner;
                        inner.pos = new Point(component.getPosX(), component.getPosY());
                        inner.id = getIdentifier.apply(component);
+                       inner.params = component.getParams();
                        i++;
                }
                params.subComps = comps;
index ea79cc7..1a3f893 100644 (file)
@@ -1,5 +1,8 @@
 package net.mograsim.logic.ui.model.wires;
 
+import com.google.gson.JsonElement;
+import com.google.gson.JsonPrimitive;
+
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.core.LogicObserver;
@@ -105,9 +108,17 @@ public class WireCrossPoint extends GUIComponent implements ConnectionPoint
                return end != null;
        }
 
+       // serializing
+
+       @Override
+       public JsonElement getParams()
+       {
+               return new JsonPrimitive(pin.logicWidth);
+       }
+
        static
        {
-               // TODO read params
-               IndirectGUIComponentCreator.setComponentProvider(WireCrossPoint.class.getCanonicalName(), (m, p) -> new WireCrossPoint(m, 1));
+               IndirectGUIComponentCreator.setComponentProvider(WireCrossPoint.class.getCanonicalName(),
+                               (m, p) -> new WireCrossPoint(m, p.getAsInt()));
        }
 }
\ No newline at end of file