X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Fmodel%2Fcomponents%2FModelComponent.java;h=d543c543ebc2050f044e8cd309e6b07a36f98e1a;hb=4d3cc22893c8c7f43699f02af87c6cc40b133b46;hp=40ab62fcea78a325441d10644be8be17abe2b8e5;hpb=1f645882d229fc3d4081e4c5060559d75dc2cc24;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/ModelComponent.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/ModelComponent.java index 40ab62fc..d543c543 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/ModelComponent.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/ModelComponent.java @@ -30,9 +30,10 @@ public abstract class ModelComponent implements JSONSerializable */ protected final LogicModelModifiable model; /** - * The name of this component. Is unique for all components in its model. + * The name of this component. Is unique for all components in its model.
+ * Does never change, but can't be final since it is set in {@link #init()}. */ - public final String name; + private String name; private final Rectangle bounds; /** * The list of all pins of this component by name. @@ -53,9 +54,20 @@ 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 callInit, initializes the component (See {@link #init()}).
+ * If callInit==false, 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; + this.name = name; this.bounds = new Rectangle(0, 0, 0, 0); this.pinsByName = new HashMap<>(); this.pinsUnmodifiable = Collections.unmodifiableMap(pinsByName); @@ -65,11 +77,31 @@ 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.
+ * + */ + protected void init() + { + if (name == null) + name = model.getDefaultComponentName(this); model.componentCreated(this, this::destroyed); } + // basic getters + + public String getName() + { + return name; + } + /** * Destroys this component. This method is called from {@link LogicModelModifiable#componentDestroyed(ModelComponent) * destroyComponent()} of the model this component is a part of.
@@ -136,17 +168,30 @@ public abstract class ModelComponent implements JSONSerializable * Returns the pin with the given name of this component. * * @throws IllegalArgumentException if there is no pin with the given name + * @see #getPinOrNull(String) * * @author Daniel Kirschten */ public Pin getPin(String name) { - Pin pin = pinsByName.get(name); + Pin pin = getPinOrNull(name); if (pin == null) throw new IllegalArgumentException("No pin with the name " + name); return pin; } + /** + * Returns the pin with the given name of this component, or null if there is no such pin. + * + * @see #getPin(String) + * + * @author Daniel Kirschten + */ + public Pin getPinOrNull(String name) + { + return pinsByName.get(name); + } + // high-level access /**