GUIComponents now have names
[Mograsim.git] / net.mograsim.logic.ui.am2900 / src / net / mograsim / logic / ui / model / components / mi / nandbased / GUIram2.java
index 3e4d013..a7d9e9c 100644 (file)
@@ -3,10 +3,11 @@ package net.mograsim.logic.ui.model.components.mi.nandbased;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 import net.mograsim.logic.core.types.BitVector;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.SimpleRectangularSubmodelComponent;
+import net.mograsim.logic.ui.model.components.submodels.SimpleRectangularSubmodelComponent;
 import net.mograsim.logic.ui.model.wires.GUIWire;
 import net.mograsim.logic.ui.model.wires.Pin;
 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
+import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
 
 public class GUIram2 extends SimpleRectangularSubmodelComponent
 {
@@ -17,7 +18,12 @@ public class GUIram2 extends SimpleRectangularSubmodelComponent
 
        public GUIram2(ViewModelModifiable model)
        {
-               super(model, 1, "GUIram2");
+               this(model, null);
+       }
+
+       public GUIram2(ViewModelModifiable model, String name)
+       {
+               super(model, 1, "GUIram2", name);
                setSubmodelScale(.1);
                setInputPins("A0", "A1", "B0", "B1", "WE", "D1", "D2", "D3", "D4");
                setOutputPins("QA1", "QA2", "QA3", "QA4", "QB1", "QB2", "QB3", "QB4");
@@ -49,10 +55,10 @@ public class GUIram2 extends SimpleRectangularSubmodelComponent
                GUIdemux2   demuxA   = new GUIdemux2  (submodelModifiable);
                GUIdemux2   demuxB   = new GUIdemux2  (submodelModifiable);
                GUIand41    weAndB   = new GUIand41   (submodelModifiable);
-                 cell00   = new GUIdlatch4 (submodelModifiable);
-                 cell01   = new GUIdlatch4 (submodelModifiable);
-                 cell10   = new GUIdlatch4 (submodelModifiable);
-                 cell11   = new GUIdlatch4 (submodelModifiable);
+               cell00   = new GUIdlatch4 (submodelModifiable);
+               cell01   = new GUIdlatch4 (submodelModifiable);
+               cell10   = new GUIdlatch4 (submodelModifiable);
+               cell11   = new GUIdlatch4 (submodelModifiable);
                GUIand41    andA00   = new GUIand41   (submodelModifiable);
                GUIandor414 andorA01 = new GUIandor414(submodelModifiable);
                GUIandor414 andorA10 = new GUIandor414(submodelModifiable);
@@ -277,10 +283,16 @@ public class GUIram2 extends SimpleRectangularSubmodelComponent
                new GUIWire(submodelModifiable, andorB11.getPin("Y3"), QB3                           , new Point(180, 760), new Point(180, 890), new Point(335, 890), new Point(335, 650));
                new GUIWire(submodelModifiable, andorB11.getPin("Y4"), QB4                           , new Point(175, 770), new Point(175, 895), new Point(340, 895), new Point(340, 750));
                //@formatter:on
+
+               addHighLevelStateSubcomponentID("c00", cell00);
+               addHighLevelStateSubcomponentID("c01", cell01);
+               addHighLevelStateSubcomponentID("c10", cell10);
+               addHighLevelStateSubcomponentID("c11", cell11);
+               addAtomicHighLevelStateID("q");
        }
 
        @Override
-       public void setHighLevelState(String stateID, Object newState)
+       public void setAtomicHighLevelState(String stateID, Object newState)
        {
                switch (stateID)
                {
@@ -292,33 +304,13 @@ public class GUIram2 extends SimpleRectangularSubmodelComponent
                        setHighLevelState("c11.q", newStateCasted.subVector(12, 16));
                        break;
                default:
-                       int indexOfDot = stateID.indexOf('.');
-                       if (indexOfDot != -1)
-                               switch (stateID.substring(0, indexOfDot))
-                               {
-                               case "c00":
-                                       cell00.setHighLevelState(stateID.substring(indexOfDot + 1), newState);
-                                       break;
-                               case "c01":
-                                       cell01.setHighLevelState(stateID.substring(indexOfDot + 1), newState);
-                                       break;
-                               case "c10":
-                                       cell10.setHighLevelState(stateID.substring(indexOfDot + 1), newState);
-                                       break;
-                               case "c11":
-                                       cell11.setHighLevelState(stateID.substring(indexOfDot + 1), newState);
-                                       break;
-                               default:
-                                       super.setHighLevelState(stateID, newState);
-                                       break;
-                               }
-                       else
-                               super.setHighLevelState(stateID, newState);
+                       // should not happen because we tell SubmodelComponent to only allow these state IDs.
+                       throw new IllegalStateException("Illegal atomic state ID: " + stateID);
                }
        }
 
        @Override
-       public Object getHighLevelState(String stateID)
+       public Object getAtomicHighLevelState(String stateID)
        {
                switch (stateID)
                {
@@ -329,22 +321,13 @@ public class GUIram2 extends SimpleRectangularSubmodelComponent
                        BitVector q11 = (BitVector) getHighLevelState("c11.q");
                        return q00.concat(q01).concat(q10).concat(q11);
                default:
-                       int indexOfDot = stateID.indexOf('.');
-                       if (indexOfDot != -1)
-                               switch (stateID.substring(0, indexOfDot))
-                               {
-                               case "c00":
-                                       return cell00.getHighLevelState(stateID.substring(indexOfDot + 1));
-                               case "c01":
-                                       return cell01.getHighLevelState(stateID.substring(indexOfDot + 1));
-                               case "c10":
-                                       return cell10.getHighLevelState(stateID.substring(indexOfDot + 1));
-                               case "c11":
-                                       return cell11.getHighLevelState(stateID.substring(indexOfDot + 1));
-                               default:
-                                       return super.getHighLevelState(stateID);
-                               }
-                       return super.getHighLevelState(stateID);
+                       // should not happen because we tell SubmodelComponent to only allow these state IDs.
+                       throw new IllegalStateException("Illegal atomic state ID: " + stateID);
                }
        }
+
+       static
+       {
+               IndirectGUIComponentCreator.setComponentSupplier(GUIram2.class.getCanonicalName(), (m, p, n) -> new GUIram2(m, n));
+       }
 }
\ No newline at end of file