Fixed issues with WirePointHandle
[Mograsim.git] / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / handles / HandleManager.java
index 91b0e3b..ee755f9 100644 (file)
@@ -8,6 +8,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
 
@@ -89,8 +90,8 @@ public class HandleManager
                        comps.remove(interfaceComp);
                        registerInterfaceComponent(interfaceComp);
                        comps.forEach(c -> registerComponent(c));
-                       
-                       model.getWires().forEach(w -> registerWire(w));
+
+                       model.getWiresByName().values().forEach(w -> registerWire(w));
                        addHandle(cornerHandle = new CornerHandle(editor.toBeEdited));
                }
        }
@@ -114,19 +115,24 @@ public class HandleManager
 
        private void registerWire(GUIWire wire)
        {
-               wire.addPathChangedListener((w, diff) ->
+               Point[] path = wire.getPath();
+               AtomicInteger oldLength = new AtomicInteger(path == null ? 0 : path.length);
+               wire.addPathChangedListener(w ->
                {
-                       if(diff != 0)
+                       Point[] newPath = w.getPath();
+                       int newLength = newPath == null ? 0 : newPath.length;
+                       int diff = newLength - oldLength.getAndSet(newLength);
+                       if (diff != 0)
                        {
-                               if(diff > 0)
+                               if (diff > 0)
                                {
-                                       for(int i = 0; i < diff; i++)
+                                       for (int i = 0; i < diff; i++)
                                                addWirePointHandle(w);
                                }
-                               
+
                                List<WirePointHandle> wpHandles = pointHandlesPerWire.get(w);
                                int size = wpHandles.size();
-                               for(int i = 0; i < size; i++)
+                               for (int i = 0; i < size; i++)
                                {
                                        wpHandles.get(i).setIndex(i);
                                }
@@ -134,9 +140,9 @@ public class HandleManager
                        pointHandlesPerWire.get(w).forEach(h -> h.updatePos());
                });
                addWireHandle(wire);
-               if (wire.getPath() == null)
+               if (path == null)
                        return;
-               for (int i = 0; i < wire.getPath().length; i++)
+               for (int i = 0; i < path.length; i++)
                {
                        addWirePointHandle(wire);
                }
@@ -176,8 +182,8 @@ public class HandleManager
 
        private void addInterfacePinHandle(Pin p)
        {
-               //The following is not an alternative to the cast, because the new pin is not yet in the map, when the listener is called
-               //editor.toBeEdited.getSubmodelMovablePins().get(p.name);
+               // The following is not an alternative to the cast, because the new pin is not yet in the map, when the listener is called
+               // editor.toBeEdited.getSubmodelMovablePins().get(p.name);
                MovablePin pM = (MovablePin) p;
                InterfacePinHandle h = new InterfacePinHandle(pM, editor.toBeEdited);
                handlePerInterfacePin.put(pM, h);
@@ -207,19 +213,22 @@ public class HandleManager
                this.wirePointHandles.add(h);
                addHandle(h);
        }
-       
+
        void destroyWirePointHandle(GUIWire owner, WirePointHandle h)
        {
-               List<WirePointHandle> handles = pointHandlesPerWire.get(owner);
-               int pointIndex = handles.indexOf(h);
-               handles.remove(pointIndex);
-               removeHandle(h);
-               owner.removePathPoint(pointIndex);
+               if (pointHandlesPerWire.containsKey(owner))
+               {
+                       List<WirePointHandle> handles = pointHandlesPerWire.get(owner);
+                       int pointIndex = handles.indexOf(h);
+                       handles.remove(pointIndex);
+                       removeHandle(h);
+                       owner.removePathPoint(pointIndex);
+               }
        }
 
        private void removeWirePointHandles(GUIWire owner)
        {
-               if(!pointHandlesPerWire.containsKey(owner))
+               if (!pointHandlesPerWire.containsKey(owner))
                        return;
                pointHandlesPerWire.get(owner).forEach(h ->
                {
@@ -270,7 +279,7 @@ public class HandleManager
        {
                return handlePerWire.get(parent);
        }
-       
+
        public Handle getInterfacePinHandle(Pin p)
        {
                return handlePerInterfacePin.get(p);
@@ -299,7 +308,7 @@ public class HandleManager
        {
                return Collections.unmodifiableCollection(handlePerPin.values());
        }
-       
+
        /**
         * @return An unmodifiable view of all registered {@link InterfacePinHandle}s
         */
@@ -342,11 +351,11 @@ public class HandleManager
                                        if (!click(getWirePointHandles(), clicked, entryState, stateMask))
                                                if (!click(getWireHandles(), clicked, entryState, stateMask))
                                                        if (!click(handlePerComp.values(), clicked, entryState, stateMask))
-                                                                       entryState.clickedEmpty(clicked, stateMask);
+                                                               entryState.clickedEmpty(clicked, stateMask);
                entryState.clicked(clicked, stateMask);
        }
 
-       private boolean click(Collection<? extends Handle> handles, Point clicked, EditorState state, int stateMask)
+       private static boolean click(Collection<? extends Handle> handles, Point clicked, EditorState state, int stateMask)
        {
                for (Handle h : handles)
                        if (h.click(clicked.x, clicked.y, stateMask, state))