Fixed a problem with ModelComponent's names
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Sun, 8 Sep 2019 11:38:16 +0000 (13:38 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Sun, 8 Sep 2019 11:38:16 +0000 (13:38 +0200)
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/ReserializeJSONsSettingUsages.java
net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/LogicModel.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/ModelComponent.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LegacySubmodelComponentSerializer.java
net.mograsim.logic.model/src/net/mograsim/logic/model/serializing/LogicModelSerializer.java
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/atomic/DelegatingAtomicHighLevelStateHandler.java
net.mograsim.logic.model/src/net/mograsim/logic/model/snippets/highlevelstatehandlers/standard/subcomponent/DelegatingSubcomponentHighLevelStateHandler.java

index b15ecdd..b91f27e 100644 (file)
@@ -129,12 +129,12 @@ public class ReserializeJSONsSettingUsages
 
                                Optional<ModelComponent> 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<String, String> 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)
index b14a69e..812f401 100644 (file)
@@ -224,11 +224,11 @@ public class LogicUICanvas extends ZoomableCanvas
        private void addComponentSelectorItems(List<ModelComponent> 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)
index 227d9b1..839d5f9 100644 (file)
@@ -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();
        }
index 6fcfa9a..9171ee8 100644 (file)
@@ -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.<br>
+        * 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.<br>
-        * Currently, this method only registers this component in the model.
+        * <ul>
+        * <li>If <code>{@link #name}==null</code>, sets {@link #name} to {@link LogicModelModifiable#getDefaultComponentName(ModelComponent)}.
+        * <li>Registers this component in the model.
+        * </ul>
         */
        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.<br>
index b41fc52..93fc945 100644 (file)
@@ -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;
index f871c87..97b84ce 100644 (file)
@@ -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;
index 7a56882..621dde3 100644 (file)
@@ -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;
        }
index 0d1fd65..fc3c6bd 100644 (file)
@@ -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;
        }