From 1ce42890e85c1a114bf647002d1aa2743ff9871a Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Sun, 8 Sep 2019 13:38:16 +0200 Subject: [PATCH] Fixed a problem with ModelComponent's names --- .../ReserializeJSONsSettingUsages.java | 8 +++---- .../mograsim/logic/model/LogicUICanvas.java | 4 ++-- .../logic/model/model/LogicModel.java | 12 +++++------ .../model/components/ModelComponent.java | 21 +++++++++++++++---- .../LegacySubmodelComponentSerializer.java | 6 +++--- .../serializing/LogicModelSerializer.java | 6 +++--- ...DelegatingAtomicHighLevelStateHandler.java | 4 ++-- ...tingSubcomponentHighLevelStateHandler.java | 4 ++-- 8 files changed, 39 insertions(+), 26 deletions(-) diff --git a/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/ReserializeJSONsSettingUsages.java b/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/ReserializeJSONsSettingUsages.java index b15ecdd8..b91f27ed 100644 --- a/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/ReserializeJSONsSettingUsages.java +++ b/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/ReserializeJSONsSettingUsages.java @@ -129,12 +129,12 @@ public class ReserializeJSONsSettingUsages Optional o; while ((o = submodelModifiable.getComponentsByName().values().stream() - .filter(c -> !c.name.equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)).findAny()).isPresent()) + .filter(c -> !c.getName().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)).findAny()).isPresent()) submodelModifiable.destroyComponent(o.get()); - tempModel.getComponentsByName().values().stream().filter(c -> !c.name.equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)) + tempModel.getComponentsByName().values().stream().filter(c -> !c.getName().equals(SubmodelComponent.SUBMODEL_INTERFACE_NAME)) .forEach(c -> IndirectModelComponentCreator - .createComponent(submodelModifiable, c.getIDForSerializing(iP), c.getParamsForSerializingJSON(iP), c.name) + .createComponent(submodelModifiable, c.getIDForSerializing(iP), c.getParamsForSerializingJSON(iP), c.getName()) .moveTo(c.getPosX(), c.getPosY())); for (ModelWire w : tempModel.getWiresByName().values()) createWire(Function.identity(), submodelModifiable, w); @@ -167,7 +167,7 @@ public class ReserializeJSONsSettingUsages private static Pin getRemappedPin(Function componentNameRemapping, LogicModelModifiable tempModelForDefaultNames, Pin pin) { - return tempModelForDefaultNames.getComponentsByName().get(componentNameRemapping.apply(pin.component.name)).getPin(pin.name); + return tempModelForDefaultNames.getComponentsByName().get(componentNameRemapping.apply(pin.component.getName())).getPin(pin.name); } private static int compareStringsWithIntegers(String a, String b) diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java index b14a69e1..812f401a 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java @@ -224,11 +224,11 @@ public class LogicUICanvas extends ZoomableCanvas private void addComponentSelectorItems(List componentsByItemIndex, String base, Combo componentSelector, LogicModel model) { - model.getComponentsByName().values().stream().sorted((c1, c2) -> c1.name.compareTo(c2.name)).forEach(c -> + model.getComponentsByName().values().stream().sorted((c1, c2) -> c1.getName().compareTo(c2.getName())).forEach(c -> { if (!(c instanceof ModelWireCrossPoint || c instanceof SubmodelInterface)) { - String item = base + c.name; + String item = base + c.getName(); componentsByItemIndex.add(c); componentSelector.add(item); if (c instanceof SubmodelComponent) diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/LogicModel.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/LogicModel.java index 227d9b1a..839d5f9a 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/LogicModel.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/LogicModel.java @@ -51,10 +51,10 @@ public class LogicModel */ protected void componentCreated(ModelComponent component, Runnable destroyed) { - if (components.containsKey(component.name)) + if (components.containsKey(component.getName())) throw new IllegalStateException("Don't add the same component twice!"); - components.put(component.name, component); - componentDestroyFunctions.put(component.name, destroyed); + components.put(component.getName(), component); + componentDestroyFunctions.put(component.getName(), destroyed); callComponentAddedListeners(component); requestRedraw(); } @@ -66,10 +66,10 @@ public class LogicModel */ protected void destroyComponent(ModelComponent component) { - componentDestroyFunctions.get(component.name).run(); - if (!components.containsKey(component.name)) + componentDestroyFunctions.get(component.getName()).run(); + if (!components.containsKey(component.getName())) throw new IllegalStateException("Don't remove the same component twice!"); - components.remove(component.name); + components.remove(component.getName()); callComponentRemovedListeners(component); requestRedraw(); } 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 6fcfa9a1..9171ee83 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. @@ -66,7 +67,7 @@ public abstract class ModelComponent implements JSONSerializable 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); @@ -82,13 +83,25 @@ public abstract class ModelComponent implements JSONSerializable /** * Initializes this component. This method should be called exactly once in this component's constructor.
- * Currently, this method only registers this component in the model. + *
    + *
  • If {@link #name}==null, sets {@link #name} to {@link LogicModelModifiable#getDefaultComponentName(ModelComponent)}. + *
  • Registers this component in the model. + *
*/ 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.
diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LegacySubmodelComponentSerializer.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LegacySubmodelComponentSerializer.java index b41fc523..93fc945e 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LegacySubmodelComponentSerializer.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LegacySubmodelComponentSerializer.java @@ -234,7 +234,7 @@ public final class LegacySubmodelComponentSerializer innerComponentParams.pos = new Point(innerComponent.getPosX(), innerComponent.getPosY()); innerComponentParams.id = innerComponent.getIDForSerializing(idParams); innerComponentParams.params = innerComponent.getParamsForSerializingJSON(idParams); - innerComponentParams.name = innerComponent.name; + innerComponentParams.name = innerComponent.getName(); i1++; } submodelParams.subComps = componentParams; @@ -249,9 +249,9 @@ public final class LegacySubmodelComponentSerializer LegacyInnerPinParams pin1Params = new LegacyInnerPinParams(), pin2Params = new LegacyInnerPinParams(); pin1Params.pinName = innerWire.getPin1().name; - pin1Params.compName = innerWire.getPin1().component.name; + pin1Params.compName = innerWire.getPin1().component.getName(); pin2Params.pinName = innerWire.getPin2().name; - pin2Params.compName = innerWire.getPin2().component.name; + pin2Params.compName = innerWire.getPin2().component.getName(); innerWireParams.name = innerWire.name; innerWireParams.pin1 = pin1Params; innerWireParams.pin2 = pin2Params; diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LogicModelSerializer.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LogicModelSerializer.java index f871c873..97b84ce3 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LogicModelSerializer.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LogicModelSerializer.java @@ -142,7 +142,7 @@ public class LogicModelSerializer compParams.pos = new Point(component.getPosX(), component.getPosY()); compParams.id = component.getIDForSerializing(idParams); compParams.params = component.getParamsForSerializingJSON(idParams); - compParams.name = component.name; + compParams.name = component.getName(); } modelParams.components = componentsParams.toArray(ComponentParams[]::new); Arrays.sort(modelParams.components, Comparator.comparing(c -> c.name)); @@ -156,9 +156,9 @@ public class LogicModelSerializer PinParams pin1Params = new PinParams(), pin2Params = new PinParams(); pin1Params.pinName = innerWire.getPin1().name; - pin1Params.compName = innerWire.getPin1().component.name; + pin1Params.compName = innerWire.getPin1().component.getName(); pin2Params.pinName = innerWire.getPin2().name; - pin2Params.compName = innerWire.getPin2().component.name; + pin2Params.compName = innerWire.getPin2().component.getName(); innerWireParams.name = innerWire.name; innerWireParams.pin1 = pin1Params; innerWireParams.pin2 = pin2Params; diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java index 7a568827..621dde31 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java @@ -42,7 +42,7 @@ public class DelegatingAtomicHighLevelStateHandler implements AtomicHighLevelSta { if (delegateTarget == null) this.delegateTarget = parentComponent; - else if (parentComponent.submodel.getComponentsByName().get(delegateTarget.name) != delegateTarget) + else if (parentComponent.submodel.getComponentsByName().get(delegateTarget.getName()) != delegateTarget) throw new IllegalArgumentException( "Can only set components belonging to the submodel of the parent component of this handler as the delegate target"); this.delegateTarget = delegateTarget; @@ -75,7 +75,7 @@ public class DelegatingAtomicHighLevelStateHandler implements AtomicHighLevelSta public DelegatingAtomicHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams) { DelegatingAtomicHighLevelStateHandlerParams params = new DelegatingAtomicHighLevelStateHandlerParams(); - params.delegateTarget = delegateTarget.name; + params.delegateTarget = delegateTarget.getName(); params.subStateID = subStateID; return params; } diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java index 0d1fd655..fc3c6bd1 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java @@ -43,7 +43,7 @@ public class DelegatingSubcomponentHighLevelStateHandler implements Subcomponent { if (delegateTarget == null) this.delegateTarget = parentComponent; - else if (parentComponent.submodel.getComponentsByName().get(delegateTarget.name) != delegateTarget) + else if (parentComponent.submodel.getComponentsByName().get(delegateTarget.getName()) != delegateTarget) throw new IllegalArgumentException( "Can only set components belonging to the submodel of the parent component of this handler as the delegate target"); this.delegateTarget = delegateTarget; @@ -81,7 +81,7 @@ public class DelegatingSubcomponentHighLevelStateHandler implements Subcomponent public DelegatingSubcomponentHighLevelStateHandlerParams getParamsForSerializing(IdentifyParams idParams) { DelegatingSubcomponentHighLevelStateHandlerParams params = new DelegatingSubcomponentHighLevelStateHandlerParams(); - params.delegateTarget = delegateTarget.name; + params.delegateTarget = delegateTarget.getName(); params.prefix = prefix; return params; } -- 2.17.1