GUIComponents now have names
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / submodels / SubmodelComponent.java
index 38241b3..6f2bab5 100644 (file)
@@ -3,11 +3,11 @@ package net.mograsim.logic.ui.model.components.submodels;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
+import java.util.function.Function;
 
 import net.haspamelodica.swt.helper.gcs.GCConfig;
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
@@ -22,11 +22,11 @@ import net.mograsim.logic.ui.model.wires.GUIWire;
 import net.mograsim.logic.ui.model.wires.MovablePin;
 import net.mograsim.logic.ui.model.wires.Pin;
 import net.mograsim.logic.ui.serializing.SubmodelComponentParams;
-import net.mograsim.logic.ui.serializing.SubmodelComponentParams.ComponentCompositionParams;
-import net.mograsim.logic.ui.serializing.SubmodelComponentParams.InnerPinParams;
-import net.mograsim.logic.ui.serializing.SubmodelComponentParams.InnerWireParams;
 import net.mograsim.logic.ui.serializing.SubmodelComponentParams.InterfacePinParams;
-import net.mograsim.logic.ui.serializing.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams;
+import net.mograsim.logic.ui.serializing.SubmodelComponentParams.SubmodelParameters;
+import net.mograsim.logic.ui.serializing.SubmodelComponentParams.SubmodelParameters.InnerComponentParams;
+import net.mograsim.logic.ui.serializing.SubmodelComponentParams.SubmodelParameters.InnerWireParams;
+import net.mograsim.logic.ui.serializing.SubmodelComponentParams.SubmodelParameters.InnerWireParams.InnerPinParams;
 
 /**
  * A {@link GUIComponent} consisting of another model. A <code>SubmodelComponent</code> can have so-called "interface pins" connecting the
@@ -34,6 +34,7 @@ import net.mograsim.logic.ui.serializing.SubmodelComponentParams.ComponentCompos
  */
 public abstract class SubmodelComponent extends GUIComponent
 {
+       private static final String SUBMODEL_INTERFACE_NAME = "_submodelinterface";
        /**
         * A modifiable view of {@link #submodel}.
         */
@@ -101,9 +102,9 @@ public abstract class SubmodelComponent extends GUIComponent
 
        // creation and destruction
 
-       public SubmodelComponent(ViewModelModifiable model)
+       public SubmodelComponent(ViewModelModifiable model, String name)
        {
-               super(model);
+               super(model, name);
                this.submodelModifiable = new ViewModelModifiable();
                this.submodel = submodelModifiable;
                this.submodelPins = new HashMap<>();
@@ -112,7 +113,7 @@ public abstract class SubmodelComponent extends GUIComponent
                this.supermodelPins = new HashMap<>();
                this.supermodelMovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
                this.supermodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
-               this.submodelInterface = new SubmodelInterface(submodelModifiable);
+               this.submodelInterface = new SubmodelInterface(submodelModifiable, SUBMODEL_INTERFACE_NAME);
 
                this.highLevelAtomicStates = new HashSet<>();
                this.subcomponentsByHighLevelStateSubcomponentID = new HashMap<>();
@@ -491,7 +492,7 @@ public abstract class SubmodelComponent extends GUIComponent
        {
                double scaledX = (x - getPosX()) / submodelScale;
                double scaledY = (y - getPosY()) / submodelScale;
-               for (GUIComponent component : submodel.getComponents())
+               for (GUIComponent component : submodel.getComponentsByName().values())
                        if (component.getBounds().contains(scaledX, scaledY) && component.clicked(scaledX, scaledY))
                                return true;
                return false;
@@ -499,15 +500,20 @@ public abstract class SubmodelComponent extends GUIComponent
 
        // serializing
 
+       // TODO move the methods below to serializing classes
+
+       public SubmodelComponentParams calculateParams()
+       {
+               return calculateParams(c -> "class:" + c.getClass().getCanonicalName());
+       }
+
        /**
         * @return {@link SubmodelComponentParams}, which describe this {@link SubmodelComponent}.
         */
-       public SubmodelComponentParams calculateParams()
+       public SubmodelComponentParams calculateParams(Function<GUIComponent, String> getIdentifier)
        {
                SubmodelComponentParams params = new SubmodelComponentParams();
-               params.name = getIdentifier();
-               params.type = SubmodelComponent.class.getSimpleName();
-               params.composition = calculateCompositionParams();
+               params.submodel = calculateSubmodelParams(getIdentifier);
 
                params.width = getWidth();
                params.height = getHeight();
@@ -527,29 +533,28 @@ public abstract class SubmodelComponent extends GUIComponent
                return params;
        }
 
-       protected ComponentCompositionParams calculateCompositionParams()
+       private SubmodelParameters calculateSubmodelParams(Function<GUIComponent, String> getIdentifier)
        {
-               ComponentCompositionParams params = new ComponentCompositionParams();
+               SubmodelParameters params = new SubmodelParameters();
                params.innerScale = getSubmodelScale();
 
-               List<GUIComponent> compList = submodelModifiable.getComponents();
-               Iterator<GUIComponent> componentIt = compList.iterator();
-               componentIt.next(); // Skip inner SubmodelInterface
-               InnerComponentParams[] comps = new InnerComponentParams[compList.size() - 1];
+               Map<String, GUIComponent> components = new HashMap<>(submodel.getComponentsByName());
+               components.remove(SUBMODEL_INTERFACE_NAME);
+               InnerComponentParams[] comps = new InnerComponentParams[components.size()];
                int i = 0;
-               while (componentIt.hasNext())
+               for (GUIComponent component : components.values())
                {
-                       GUIComponent component = componentIt.next();
                        InnerComponentParams inner = new InnerComponentParams();
                        comps[i] = inner;
-                       inner.params = component.getInstantiationParameters();
                        inner.pos = new Point(component.getPosX(), component.getPosY());
-                       inner.name = component.getIdentifier();
+                       inner.id = getIdentifier.apply(component);
+                       inner.params = component.getParams();
+                       inner.name = component.name;
                        i++;
                }
                params.subComps = comps;
 
-               List<GUIWire> wireList = submodelModifiable.getWires();
+               List<GUIWire> wireList = submodel.getWires();
                InnerWireParams wires[] = new InnerWireParams[wireList.size()];
                i = 0;
                for (GUIWire wire : wireList)
@@ -559,9 +564,9 @@ public abstract class SubmodelComponent extends GUIComponent
                        InnerPinParams pin1Params = new InnerPinParams(), pin2Params = new InnerPinParams();
 
                        pin1Params.pinName = wire.getPin1().name;
-                       pin1Params.compId = compList.indexOf(wire.getPin1().component);
+                       pin1Params.compName = wire.getPin1().component.name;
                        pin2Params.pinName = wire.getPin2().name;
-                       pin2Params.compId = compList.indexOf(wire.getPin2().component);
+                       pin2Params.compName = wire.getPin2().component.name;
                        inner.pin1 = pin1Params;
                        inner.pin2 = pin2Params;
                        inner.path = wire.getPath();