Sync git svn branch with SVN repository r33392.
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / participant / ConnectTool2.java
index 71a8d28290f9b7e020ef3bbb368ccc994287f8bc..2165794e89c489c34e34daddf5613fa22d13e9ae 100644 (file)
@@ -99,7 +99,6 @@ import org.simantics.scenegraph.g2d.snap.ISnapAdvisor;
 import org.simantics.scenegraph.utils.GeometryUtils;\r
 import org.simantics.scenegraph.utils.Quality;\r
 import org.simantics.structural2.modelingRules.ConnectionJudgement;\r
-import org.simantics.utils.datastructures.Callback;\r
 import org.simantics.utils.datastructures.Pair;\r
 import org.simantics.utils.logging.TimeLogger;\r
 import org.simantics.utils.ui.ErrorLogger;\r
@@ -217,6 +216,13 @@ public class ConnectTool2 extends AbstractMode {
      */\r
     protected ConnectionJudgement    connectionJudgment;\r
 \r
+    /**\r
+     * The latest connectability judgment from the active\r
+     * {@link IConnectionAdvisor} should the connection happen between\r
+     * {@link #selectedStartTerminal} and {@link #lastRouteGraphTarget}.\r
+     */\r
+    protected ConnectionJudgement    attachToConnectionJudgement;\r
+\r
     /**\r
      * If non-null during connection drawing this field tells the direction\r
      * forced for the current branch point by the user through the UI commands\r
@@ -409,22 +415,6 @@ public class ConnectTool2 extends AbstractMode {
         updateSG();\r
     }\r
 \r
-    static class Segment {\r
-        public final ControlPoint begin;\r
-        public final ControlPoint end;\r
-        public Path2D             path;\r
-\r
-        public Segment(ControlPoint begin, ControlPoint end) {\r
-            this.begin = begin;\r
-            this.end = end;\r
-        }\r
-\r
-        @Override\r
-        public String toString() {\r
-            return "Segment[begin=" + begin + ", end=" + end + ", path=" + path + "]";\r
-        }\r
-    }\r
-\r
     private RouteTerminal addControlPoint(RouteGraph routeGraph, ControlPoint cp) {\r
         TerminalInfo ti = cp.getAttachedTerminal();\r
         if(ti != null && ti != startFlag && ti != endFlag) {\r
@@ -482,23 +472,6 @@ public class ConnectTool2 extends AbstractMode {
         return elementParent;\r
     }\r
 \r
-    private List<Segment> toSegments(Deque<ControlPoint> points) {\r
-        if (points.isEmpty())\r
-            return Collections.emptyList();\r
-\r
-        List<Segment> segments = new ArrayList<Segment>();\r
-\r
-        Iterator<ControlPoint> it = points.iterator();\r
-        ControlPoint prev = it.next();\r
-        while (it.hasNext()) {\r
-            ControlPoint next = it.next();\r
-            segments.add(new Segment(prev, next));\r
-            prev = next;\r
-        }\r
-\r
-        return segments;\r
-    }\r
-\r
     @SGCleanup\r
     public void cleanupSG() {\r
         ghostNode.remove();\r
@@ -620,7 +593,7 @@ public class ConnectTool2 extends AbstractMode {
                         BranchPointNode.SHAPE);\r
                 Pair<ConnectionJudgement, TerminalInfo> canConnect = canConnect(ti.e, ti.t);\r
                 if (canConnect != null) {\r
-                    connectionJudgment = canConnect.first;\r
+                    attachToConnectionJudgement = canConnect.first;\r
                     controlPoints.getLast().setPosition(ti.posDia).setAttachedToTerminal(ti);\r
                     endTerminal = ti;\r
                     cp.getNode().showBranchPoint(isectPos);\r
@@ -637,6 +610,7 @@ public class ConnectTool2 extends AbstractMode {
         }\r
 \r
         connectionJudgment = null;\r
+        attachToConnectionJudgement = null;\r
         if (isEndTerminalDefined()) {\r
             // CASE: Mouse was previously on top of a valid terminal to end\r
             // the connection. Now the mouse has been moved where there is\r
@@ -688,16 +662,16 @@ public class ConnectTool2 extends AbstractMode {
             if (snapAdvisor != null)\r
                 snapAdvisor.snap(mouseCanvasPos);\r
 \r
-            if (lastRouteGraphTarget != null) {\r
-                lastRouteGraphTarget.getNode().showBranchPoint(null);\r
-                attachToConnection();\r
-                remove();\r
-                return true;\r
-            } else if (isEndTerminalDefined()) {\r
+            if (isEndTerminalDefined() && connectionJudgment != null) {\r
                 // Clicked on an allowed end terminal. End connection & end mode.\r
                 createConnection();\r
                 remove();\r
                 return true;\r
+            } else if (lastRouteGraphTarget != null && attachToConnectionJudgement != null) {\r
+                lastRouteGraphTarget.getNode().showBranchPoint(null);\r
+                attachToConnection();\r
+                remove();\r
+                return true;\r
             } else {\r
                 // Finish connection in thin air only if the\r
                 // connection was started from a valid terminal.\r
@@ -737,7 +711,7 @@ public class ConnectTool2 extends AbstractMode {
     }\r
 \r
     private void attachToConnection() {\r
-        ConnectionJudgement judgment = this.connectionJudgment;\r
+        ConnectionJudgement judgment = this.attachToConnectionJudgement;\r
         if (judgment == null) {\r
             ErrorLogger.defaultLogError("Cannot attach to connection, no judgment available on connection validity", null);\r
             return;\r
@@ -774,12 +748,9 @@ public class ConnectTool2 extends AbstractMode {
                     cps.add(iterator.next());\r
                 builder.attachToRouteGraph(graph, judgment, connection, line, cps, startTerminal, FlagClass.Type.In);\r
             }\r
-        }, new Callback<DatabaseException>() {\r
-            @Override\r
-            public void run(DatabaseException parameter) {\r
-                if (parameter != null)\r
-                    ExceptionUtils.logAndShowError(parameter);\r
-            }\r
+        }, parameter -> {\r
+            if (parameter != null)\r
+                ExceptionUtils.logAndShowError(parameter);\r
         });\r
     }\r
 \r
@@ -1007,12 +978,9 @@ public class ConnectTool2 extends AbstractMode {
             public void perform(WriteGraph graph) throws DatabaseException {\r
                 builder.create(graph, judgement, controlPoints, startTerminal, endTerminal);\r
             }\r
-        }, new Callback<DatabaseException>() {\r
-            @Override\r
-            public void run(DatabaseException parameter) {\r
-                if (parameter != null)\r
-                    ExceptionUtils.logAndShowError(parameter);\r
-            }\r
+        }, parameter -> {\r
+            if (parameter != null)\r
+                ExceptionUtils.logAndShowError(parameter);\r
         });\r
     }\r
 \r