Cleaned up initializing of ModelComponents (also improved HLSDebugShell)
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / ModelComponent.java
index 40ab62f..6fcfa9a 100644 (file)
@@ -53,6 +53,17 @@ public abstract class ModelComponent implements JSONSerializable
        // creation and destruction
 
        public ModelComponent(LogicModelModifiable model, String name)
+       {
+               this(model, name, true);
+       }
+
+       /**
+        * Creates a new {@link ModelComponent} and, if <code>callInit</code>, initializes the component (See {@link #init()}).<br>
+        * If <code>callInit==false</code>, make sure to call {@link #init()}!
+        * 
+        * @author Daniel Kirschten
+        */
+       protected ModelComponent(LogicModelModifiable model, String name, boolean callInit)
        {
                this.model = model;
                this.name = name == null ? model.getDefaultComponentName(this) : name;
@@ -65,8 +76,16 @@ public abstract class ModelComponent implements JSONSerializable
                this.pinAddedListeners = new ArrayList<>();
                this.pinRemovedListeners = new ArrayList<>();
 
-               // TODO this will crash the high level state debug shell because submodel is not yet set.
-               // The same problem exists in LogicModelModifiable.getDefaultComponentName; see there
+               if (callInit)
+                       init();
+       }
+
+       /**
+        * Initializes this component. This method should be called exactly once in this component's constructor.<br>
+        * Currently, this method only registers this component in the model.
+        */
+       protected void init()
+       {
                model.componentCreated(this, this::destroyed);
        }