]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/participant/RouteGraphConnectTool.java
Simplified diagram connection creation
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / participant / RouteGraphConnectTool.java
index 57f5cd4900310263031a81d3227c99f355a25917..8f99207e19c77cb00f55288f8bd4d218aca06cdf 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2016 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *     Semantum Oy - Fixed bug #6364\r
- *******************************************************************************/\r
-package org.simantics.diagram.participant;\r
-\r
-import java.awt.AlphaComposite;\r
-import java.awt.Composite;\r
-import java.awt.Shape;\r
-import java.awt.geom.AffineTransform;\r
-import java.awt.geom.Point2D;\r
-import java.awt.geom.Rectangle2D;\r
-import java.util.ArrayDeque;\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Deque;\r
-import java.util.HashSet;\r
-import java.util.Iterator;\r
-import java.util.Set;\r
-\r
-import org.simantics.Simantics;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.diagram.connection.RouteGraph;\r
-import org.simantics.diagram.connection.RouteGraphConnectionClass;\r
-import org.simantics.diagram.connection.RouteLine;\r
-import org.simantics.diagram.connection.RoutePoint;\r
-import org.simantics.diagram.connection.RouteTerminal;\r
-import org.simantics.diagram.connection.delta.RouteGraphDelta;\r
-import org.simantics.diagram.connection.rendering.IRouteGraphRenderer;\r
-import org.simantics.diagram.connection.rendering.arrows.ArrowLineEndStyle;\r
-import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;\r
-import org.simantics.diagram.connection.rendering.arrows.PlainLineEndStyle;\r
-import org.simantics.diagram.synchronization.ISynchronizationContext;\r
-import org.simantics.diagram.synchronization.SynchronizationHints;\r
-import org.simantics.diagram.synchronization.graph.RouteGraphConnection;\r
-import org.simantics.g2d.canvas.ICanvasContext;\r
-import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
-import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;\r
-import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;\r
-import org.simantics.g2d.connection.ConnectionEntity;\r
-import org.simantics.g2d.connection.IConnectionAdvisor;\r
-import org.simantics.g2d.diagram.DiagramHints;\r
-import org.simantics.g2d.diagram.DiagramUtils;\r
-import org.simantics.g2d.diagram.IDiagram;\r
-import org.simantics.g2d.diagram.handler.PickRequest;\r
-import org.simantics.g2d.diagram.handler.Topology.Connection;\r
-import org.simantics.g2d.diagram.handler.Topology.Terminal;\r
-import org.simantics.g2d.diagram.participant.ElementPainter;\r
-import org.simantics.g2d.diagram.participant.TerminalPainter;\r
-import org.simantics.g2d.diagram.participant.TerminalPainter.TerminalHoverStrategy;\r
-import org.simantics.g2d.diagram.participant.pointertool.AbstractMode;\r
-import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor;\r
-import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil;\r
-import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo;\r
-import org.simantics.g2d.element.ElementClass;\r
-import org.simantics.g2d.element.ElementClasses;\r
-import org.simantics.g2d.element.ElementHints;\r
-import org.simantics.g2d.element.ElementUtils;\r
-import org.simantics.g2d.element.IElement;\r
-import org.simantics.g2d.element.IElementClassProvider;\r
-import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;\r
-import org.simantics.g2d.element.handler.SceneGraph;\r
-import org.simantics.g2d.element.handler.TerminalTopology;\r
-import org.simantics.g2d.element.handler.impl.BranchPointTerminal;\r
-import org.simantics.g2d.element.impl.Element;\r
-import org.simantics.g2d.elementclass.FlagClass;\r
-import org.simantics.g2d.elementclass.FlagHandler;\r
-import org.simantics.g2d.participant.TransformUtil;\r
-import org.simantics.g2d.utils.geom.DirectionSet;\r
-import org.simantics.scenegraph.g2d.G2DParentNode;\r
-import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
-import org.simantics.scenegraph.g2d.events.KeyEvent;\r
-import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonEvent;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;\r
-import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
-import org.simantics.scenegraph.g2d.events.command.Commands;\r
-import org.simantics.scenegraph.g2d.nodes.BranchPointNode;\r
-import org.simantics.scenegraph.g2d.nodes.ConnectionNode;\r
-import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphNode;\r
-import org.simantics.scenegraph.g2d.snap.ISnapAdvisor;\r
-import org.simantics.scenegraph.utils.GeometryUtils;\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.datastructures.Triple;\r
-import org.simantics.utils.logging.TimeLogger;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-import org.simantics.utils.ui.ExceptionUtils;\r
-\r
-import gnu.trove.map.hash.THashMap;\r
-\r
-/**\r
- * A basic tool for making connection on diagrams.\r
- * \r
- * This version defines the starting, ending and route points of a connection.\r
- * The routing itself is left up to the diagram router employed by\r
- * {@link DiagramUtils#validateAndFix(IDiagram, ICanvasContext)}.\r
- * \r
- * Manual:\r
- * \r
- * This tool is added to the diagram when a connection sequence is initiated by\r
- * another participant. PointerInteractor is one such participant which adds the\r
- * tool when a terminal or non-terminal-occupied canvas space is ALT+clicked\r
- * (see {@link PointerInteractor#checkInitiateConnectTool(MouseEvent, Point2D)}\r
- * ). The connection will be finished when another allowed terminal is clicked\r
- * upon or empty canvas space is ALT+clicked. Route points for the connection\r
- * can be created by clicking around on non-terminal-occupied canvas space while\r
- * connecting.\r
- * \r
- * <p>\r
- * Connections can be started from and ended in flags by pressing ALT while\r
- * left-clicking.\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class RouteGraphConnectTool extends AbstractMode {\r
-\r
-    private static final String      END_TERMINAL_DATA     = "END";\r
-\r
-    public static final int          PAINT_PRIORITY        = ElementPainter.ELEMENT_PAINT_PRIORITY + 5;\r
-\r
-    @Dependency\r
-    protected TransformUtil          util;\r
-\r
-    @Dependency\r
-    protected ElementPainter         diagramPainter;\r
-\r
-    @Dependency\r
-    protected PointerInteractor      pi;\r
-\r
-    /**\r
-     * Starting point designation.\r
-     * \r
-     * The value is received by the constructor.\r
-     */\r
-    protected RouteGraphTarget       startingPoint;\r
-\r
-    protected TerminalInfo           startTerminal         = new TerminalInfo();\r
-\r
-    /**\r
-     * <code>true</code> if this tool should create connection continuation\r
-     * flags, <code>false</code> otherwise.\r
-     */\r
-    protected boolean                createFlags;\r
-\r
-    /**\r
-     * \r
-     */\r
-    protected IElementClassProvider  elementClassProvider;\r
-\r
-    /**\r
-     * \r
-     */\r
-    protected Deque<ControlPoint>    controlPoints         = new ArrayDeque<ControlPoint>();\r
-\r
-    /**\r
-     * Element terminal of connection end element. <code>null</code> if\r
-     * connection cannot be ended where it is currently being attempted to end.\r
-     */\r
-    protected TerminalInfo           endTerminal;\r
-\r
-    /**\r
-     * The latest connectability judgment from the active\r
-     * {@link IConnectionAdvisor} should the connection happen between\r
-     * {@link #startTerminal} and {@link #endTerminal}.\r
-     */\r
-    protected ConnectionJudgement    connectionJudgment;\r
-\r
-    /**\r
-     * A temporary variable for use with\r
-     * {@link TerminalTopology#getTerminals(IElement, Collection)}.\r
-     */\r
-    protected Collection<Terminal>   terminals             = new ArrayList<Terminal>();\r
-\r
-    /**\r
-     * Previous mouse canvas position recorded by\r
-     * {@link #processMouseMove(MouseMovedEvent)}.\r
-     */\r
-    protected Point2D                lastMouseCanvasPos    = new Point2D.Double();\r
-\r
-    /**\r
-     * Set to true once {@link #processMouseMove(MouseMovedEvent)} has been\r
-     * invoked at least once. This is used to tell whether to allow creation of\r
-     * branch points or finising the connection in thin air. It will not be\r
-     * allowed if the mouse has not moved at all since starting the connection.\r
-     */\r
-    protected boolean                mouseHasMoved         = false;\r
-\r
-    protected TerminalHoverStrategy  originalStrategy      = null;\r
-\r
-    protected TerminalHoverStrategy  terminalHoverStrategy = new TerminalHoverStrategy() {\r
-        @Override\r
-        public boolean highlightEnabled() {\r
-            return !isEndingInFlag();\r
-        }\r
-\r
-        @Override\r
-        public boolean highlight(TerminalInfo ti) {\r
-            return canConnect(ti.e, ti.t) != null;\r
-        }\r
-    };\r
-\r
-    protected final static Composite ALPHA_COMPOSITE       = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f);\r
-\r
-    /**\r
-     * Root scene graph node for all visualization performed by this tool.\r
-     */\r
-    protected ConnectionNode         ghostNode;\r
-\r
-    protected RouteGraphNode           rgNode;\r
-\r
-    protected  RouteGraph               routeGraph;\r
-    protected  RouteTerminal            endRouteTerminal;\r
-    private ILineEndStyle            endTerminalStyle;\r
-\r
-    /**\r
-     * Indicates whether the connection is about to be ended into a new\r
-     * flag/branchpoint or not.\r
-     */\r
-    protected TerminalInfo           endFlag;\r
-\r
-    protected G2DParentNode          endFlagNode;\r
-\r
-    private RouteLine                attachedToRouteLine;\r
-\r
-    protected RouteGraph               beforeRouteGraph;\r
-\r
-    private IRouteGraphRenderer      beforeRenderer;\r
-\r
-    private boolean                  beforeEditable;\r
-\r
-    protected RouteGraphDelta          routeGraphDelta;\r
-\r
-    private Set<Pair<IElement, Terminal>> alreadyConnected;\r
-    \r
-    /**\r
-     * @param startElement\r
-     * @param routeGraphConnection\r
-     * @param mouseId\r
-     * @param startCanvasPos\r
-     */\r
-    public RouteGraphConnectTool(RouteGraphTarget startingPoint, int mouseId) {\r
-        super(mouseId);\r
-\r
-        Point2D intersection = startingPoint.getIntersectionPosition();\r
-\r
-        this.startingPoint = startingPoint;\r
-        this.lastMouseCanvasPos.setLocation(intersection);\r
-\r
-        BranchPointTerminal t = BranchPointTerminal.existingTerminal(\r
-                AffineTransform.getTranslateInstance(intersection.getX(), intersection.getY()),\r
-                DirectionSet.ANY,\r
-                BranchPointNode.SHAPE);\r
-\r
-        Point2D p = startingPoint.getIntersectionPosition();\r
-        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());\r
-\r
-        startTerminal.e = startingPoint.getElement();\r
-        startTerminal.t = t;\r
-        startTerminal.posElem = at;\r
-        startTerminal.posDia = at;\r
-        startTerminal.shape = t.getShape();\r
-\r
-        controlPoints.add( newControlPoint( startingPoint.getIntersectionPosition() ) );\r
-        controlPoints.add( newControlPoint( startingPoint.getCanvasPosition() ) );\r
-\r
-        alreadyConnected = new HashSet<Pair<IElement,Terminal>>();\r
-        IElement connection = startingPoint.getElement();\r
-        ConnectionEntity ce = connection.getHint(ElementHints.KEY_CONNECTION_ENTITY);\r
-        Collection<Connection> tcs = ce.getTerminalConnections(null);\r
-        for (Connection tc : tcs)\r
-            alreadyConnected.add(Pair.make(tc.node, tc.terminal));\r
-    }\r
-\r
-    @Override\r
-    public void addedToContext(ICanvasContext ctx) {\r
-        super.addedToContext(ctx);\r
-\r
-        // Force terminals to always be highlighted without pressing certain\r
-        // keys or key combinations.\r
-        originalStrategy = getHint(TerminalPainter.TERMINAL_HOVER_STRATEGY);\r
-        setHint(TerminalPainter.TERMINAL_HOVER_STRATEGY, terminalHoverStrategy);\r
-    }\r
-\r
-    @Override\r
-    protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) {\r
-        if (newDiagram != null) {\r
-            // Get IElementClassProvider\r
-            ISynchronizationContext ctx = newDiagram.getHint(SynchronizationHints.CONTEXT);\r
-            if (ctx != null) {\r
-                this.elementClassProvider = ctx.get(SynchronizationHints.ELEMENT_CLASS_PROVIDER);\r
-            }\r
-\r
-            // See if flags should be created or not.\r
-            this.createFlags = Boolean.TRUE.equals(newDiagram.getHint(DiagramHints.KEY_USE_CONNECTION_FLAGS));\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public void removedFromContext(ICanvasContext ctx) {\r
-        if (getHint(TerminalPainter.TERMINAL_HOVER_STRATEGY) == terminalHoverStrategy) {\r
-            if (originalStrategy != null)\r
-                setHint(TerminalPainter.TERMINAL_HOVER_STRATEGY, originalStrategy);\r
-            else\r
-                removeHint(TerminalPainter.TERMINAL_HOVER_STRATEGY);\r
-        }\r
-\r
-        super.removedFromContext(ctx);\r
-    }\r
-\r
-    int straightDirections(RouteLine line) {\r
-        return line.isHorizontal()\r
-                ? (RouteTerminal.DIR_DOWN | RouteTerminal.DIR_UP)\r
-                : (RouteTerminal.DIR_LEFT | RouteTerminal.DIR_RIGHT);\r
-    }\r
-\r
-    private static RouteTerminal addTerminal(Object data, RouteGraph rg, double x, double y, Rectangle2D bounds, int allowedDirections, ILineEndStyle style) {\r
-        RouteTerminal rt;\r
-        if (bounds != null)\r
-            rt = rg.addTerminal(x, y, bounds.getMinX(), bounds.getMinY(), bounds.getMaxX(), bounds.getMaxY(), allowedDirections, style);\r
-        else\r
-            rt = rg.addTerminal(x, y, x, y, x, y, allowedDirections, style);\r
-        rt.setData( data );\r
-        return rt;\r
-    }\r
-\r
-    private RouteTerminal setEndTerminal(double x, double y, Rectangle2D bounds, int allowedDirections) {\r
-\r
-       // First add then remove to prevent deletion of route lines in case there are 2 only terminals\r
-       \r
-       RouteTerminal toRemove = endRouteTerminal;\r
-       \r
-        endRouteTerminal = addTerminal( END_TERMINAL_DATA, routeGraph, x, y, bounds, allowedDirections, endTerminalStyle );\r
-        routeGraph.link( attachedToRouteLine, endRouteTerminal );\r
-        \r
-        if (toRemove != null)\r
-            routeGraph.remove(toRemove);\r
-        \r
-        return endRouteTerminal;\r
-        \r
-    }\r
-\r
-    protected void setEndTerminal(TerminalInfo ti) {\r
-        Rectangle2D bounds = ElementUtils.getElementBoundsOnDiagram(ti.e, new Rectangle2D.Double());\r
-        GeometryUtils.expandRectangle(bounds, 2);\r
-        int dir = RouteGraphConnectionClass.shortestDirectionOutOfBounds(\r
-                ti.posDia.getTranslateX(), ti.posDia.getTranslateY(), bounds);\r
-        setEndTerminal(ti.posDia.getTranslateX(), ti.posDia.getTranslateY(), bounds, dir);\r
-    }\r
-\r
-    @SGInit\r
-    public void initSG(G2DParentNode parent) {\r
-        ghostNode = parent.addNode("branched connection", ConnectionNode.class);\r
-        //ghostNode.setComposite(AlphaComposite.SrcOver.derive(0.5f));\r
-        ghostNode.setZIndex(PAINT_PRIORITY);\r
-\r
-        rgNode = ghostNode.addNode("branch", RouteGraphNode.class);\r
-\r
-        double ex = startingPoint.getCanvasPosition().getX();\r
-        double ey = startingPoint.getCanvasPosition().getY();\r
-\r
-        beforeRouteGraph = startingPoint.getNode().getRouteGraph();\r
-        beforeEditable = startingPoint.getNode().isEditable();\r
-\r
-        RouteGraphNode beforeRgNode = startingPoint.getNode();\r
-        beforeRenderer = beforeRgNode.getRenderer();\r
-\r
-        rgNode.setRenderer(beforeRenderer);\r
-        rgNode.setEditable(false);\r
-\r
-        beforeRgNode.setEditable(beforeEditable);\r
-        beforeRgNode.setRenderer(null);\r
-        \r
-        initRG(ex, ey);\r
-        \r
-    }\r
-    \r
-    public void initRG(double ex, double ey) {\r
-       \r
-        THashMap<Object, Object> map = new THashMap<Object, Object>();\r
-        routeGraph = beforeRouteGraph.copy(map);\r
-\r
-        endTerminalStyle = PlainLineEndStyle.INSTANCE;\r
-        for (RouteTerminal t : routeGraph.getTerminals()) {\r
-            if (t.getRenderStyle() instanceof ArrowLineEndStyle) {\r
-                endTerminalStyle = t.getRenderStyle();\r
-                break;\r
-            }\r
-        }\r
-\r
-        attachedToRouteLine = (RouteLine) map.get(startingPoint.getLine());\r
-        routeGraph.makePersistent(attachedToRouteLine);\r
-        for (RouteLine line : routeGraph.getAllLines()) {\r
-            if (!line.isTransient() && line.isHorizontal() == attachedToRouteLine.isHorizontal()\r
-                    && line.getPosition() == attachedToRouteLine.getPosition()) {\r
-                attachedToRouteLine = line;\r
-                break;\r
-            }\r
-        }\r
-        routeGraphDelta = new RouteGraphDelta(beforeRouteGraph, routeGraph);\r
-\r
-//        beforeRouteGraph.print();\r
-//        routeGraph.print();\r
-//        routeGraphDelta.print();\r
-\r
-        setEndTerminal(ex, ey, null, 0xf);\r
-\r
-        rgNode.setRouteGraph(routeGraph);\r
-       \r
-    }\r
-    \r
-    @SGCleanup\r
-    public void cleanupSG() {\r
-        RouteGraphNode beforeRgNode = startingPoint.getNode();\r
-        beforeRgNode.setRouteGraph(beforeRouteGraph);\r
-        beforeRgNode.setRenderer(beforeRenderer);\r
-        beforeRgNode.setEditable(beforeEditable);\r
-\r
-        ghostNode.remove();\r
-        ghostNode = null;\r
-\r
-        setDirty();\r
-    }\r
-\r
-    @EventHandler(priority = 200)\r
-    public boolean handleCommandEvents(CommandEvent ce) {\r
-        if (ce.command.equals(Commands.CANCEL)) {\r
-            setDirty();\r
-            remove();\r
-            return true;\r
-        } else if (ce.command.equals(Commands.ROTATE_ELEMENT_CCW) || ce.command.equals(Commands.ROTATE_ELEMENT_CW)) {\r
-            // TODO: rotate flag?\r
-        }\r
-        return false;\r
-    }\r
-\r
-    @EventHandler(priority = PointerInteractor.TOOL_PRIORITY + 20)\r
-    public boolean handleKeyEvents(KeyEvent ke) {\r
-        if (ke instanceof KeyPressedEvent) {\r
-            // Back-space, cancel prev bend\r
-            if (ke.keyCode == java.awt.event.KeyEvent.VK_BACK_SPACE)\r
-                return cancelPreviousBend();\r
-        }\r
-\r
-        if (ke.keyCode == java.awt.event.KeyEvent.VK_ALT) {\r
-            if (createFlags) {\r
-                endWithoutTerminal(lastMouseCanvasPos, shouldEndWithFlag(ke instanceof KeyPressedEvent));\r
-                return true;\r
-            }\r
-        }\r
-\r
-        return false;\r
-    }\r
-\r
-    @EventHandler(priority = PointerInteractor.TOOL_PRIORITY + 20)\r
-    public boolean handleEvent(MouseEvent me) {\r
-        // Only handle events for the connection-initiating mouse\r
-        if (me.mouseId != mouseId)\r
-            return false;\r
-\r
-        if (me instanceof MouseMovedEvent)\r
-            return processMouseMove((MouseMovedEvent) me);\r
-\r
-        if (me instanceof MouseButtonPressedEvent)\r
-            return processMouseButtonPress((MouseButtonPressedEvent) me);\r
-\r
-        return false;\r
-    }\r
-\r
-    protected boolean processMouseMove(MouseMovedEvent me) {\r
-        mouseHasMoved = true;\r
-\r
-        Point2D mouseControlPos = me.controlPosition;\r
-        Point2D mouseCanvasPos = util.controlToCanvas(mouseControlPos, new Point2D.Double());\r
-\r
-        ISnapAdvisor snapAdvisor = getHint(DiagramHints.SNAP_ADVISOR);\r
-        if (snapAdvisor != null)\r
-            snapAdvisor.snap(mouseCanvasPos);\r
-\r
-        // Record last snapped canvas position of mouse.\r
-        this.lastMouseCanvasPos.setLocation(mouseCanvasPos);\r
-\r
-        if (isEndingInFlag()) {\r
-            endFlagNode.setTransform(AffineTransform.getTranslateInstance(mouseCanvasPos.getX(), mouseCanvasPos.getY()));\r
-        }\r
-\r
-        TerminalInfo ti = pi.pickTerminal(me.controlPosition);\r
-        if (ti != null) {\r
-            Object canConnect = canConnect(ti.e, ti.t);\r
-            if (canConnect != null) {\r
-                connectionJudgment = (ConnectionJudgement) canConnect;\r
-\r
-                if (!isEndingInFlag() || !TerminalUtil.isSameTerminal(ti, endTerminal)) {\r
-                    controlPoints.getLast()\r
-                    .setPosition(ti.posDia)\r
-                    .setAttachedToTerminal(ti);\r
-\r
-                    endTerminal = ti;\r
-\r
-                    connect(ti);\r
-                    \r
-                }\r
-\r
-                // Make sure that we are ending with a flag if ALT is pressed\r
-                // and no end terminal is defined.\r
-                endWithoutTerminal(lastMouseCanvasPos, shouldEndWithFlag(me));\r
-\r
-                updateSG(new Point2D.Double(ti.posDia.getTranslateX(), ti.posDia.getTranslateY()));\r
-                return false;\r
-            }\r
-        }\r
-\r
-        connectionJudgment = 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
-            // no longer a terminal to connect to.\r
-            //\r
-            // => Disconnect the last edge segment from the previous\r
-            // terminal, mark endElement/endTerminal non-existent\r
-            // and connect the disconnected edge to a new branch point.\r
-\r
-            disconnect(mouseCanvasPos);\r
-            \r
-            controlPoints.getLast()\r
-            .setPosition(mouseCanvasPos)\r
-            .setAttachedToTerminal(null);\r
-\r
-            endTerminal = null;\r
-        } else {\r
-            // CASE: Mouse was not previously on top of a valid ending\r
-            // element terminal.\r
-            //\r
-            // => Move and re-orient last branch point.\r
-\r
-            controlPoints.getLast()\r
-            .setPosition(mouseCanvasPos);\r
-        }\r
-\r
-        // Make sure that we are ending with a flag if ALT is pressed and no end\r
-        // terminal is defined.\r
-        endWithoutTerminal(lastMouseCanvasPos, shouldEndWithFlag(me));\r
-\r
-        updateSG(lastMouseCanvasPos);\r
-\r
-        return false;\r
-    }\r
-\r
-    protected void connect(TerminalInfo ti) {\r
-        setEndTerminal(ti);\r
-    }\r
-    \r
-    protected void disconnect(Point2D mouseCanvasPos) {\r
-        setEndTerminal(mouseCanvasPos.getX(), mouseCanvasPos.getY(), null, 0xf);\r
-    }\r
-    \r
-    protected boolean processMouseButtonPress(MouseButtonPressedEvent e) {\r
-        MouseButtonEvent me = e;\r
-\r
-        // Do nothing before the mouse has moved at least a little.\r
-        // This prevents the user from ending the connection right where\r
-        // it started.\r
-        if (!mouseHasMoved)\r
-            return true;\r
-\r
-        if (me.button == MouseEvent.LEFT_BUTTON) {\r
-            Point2D mouseControlPos = me.controlPosition;\r
-            Point2D mouseCanvasPos = util.getInverseTransform().transform(mouseControlPos, new Point2D.Double());\r
-\r
-            ISnapAdvisor snapAdvisor = getHint(DiagramHints.SNAP_ADVISOR);\r
-            if (snapAdvisor != null)\r
-                snapAdvisor.snap(mouseCanvasPos);\r
-\r
-            // Clicked on an allowed end terminal. End connection & end mode.\r
-            if (isEndTerminalDefined()) {\r
-                createConnection();\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
-                if ((me.stateMask & MouseEvent.ALT_MASK) != 0 && startTerminal != null) {\r
-                    connectionJudgment = (ConnectionJudgement) canConnect(null, null);\r
-                    if (connectionJudgment == null)\r
-                        return true;\r
-                    createConnection();\r
-                    remove();\r
-                    return true;\r
-                } else if (routePointsAllowed()\r
-                        && (me.stateMask & (MouseEvent.ALT_MASK | MouseEvent.SHIFT_MASK | MouseEvent.CTRL_MASK)) == 0) {\r
-                    // Add new connection control point.\r
-                    controlPoints.add(newControlPoint(mouseCanvasPos));\r
-                    updateSG(mouseCanvasPos);\r
-                }\r
-            }\r
-\r
-            // Eat the event to prevent other participants from doing\r
-            // incompatible things while in this connection mode.\r
-            return true;\r
-        } else if (me.button == MouseEvent.RIGHT_BUTTON) {\r
-            return cancelPreviousBend();\r
-        }\r
-\r
-        return false;\r
-    }\r
-\r
-    protected boolean cancelPreviousBend() {\r
-        if (!routePointsAllowed())\r
-            return false;\r
-\r
-        // Just to make this code more comprehensible, prevent an editing\r
-        // case that requires ugly code to work.\r
-        if (isEndingInFlag())\r
-            return true;\r
-\r
-        // If there are no real route points, cancel whole connection.\r
-        if (controlPoints.size() <= 2) {\r
-            setDirty();\r
-            remove();\r
-            return true;\r
-        }\r
-\r
-        // Cancel last bend\r
-        controlPoints.removeLast();\r
-        controlPoints.getLast().setPosition(lastMouseCanvasPos);\r
-\r
-        updateSG(lastMouseCanvasPos);\r
-        return true;\r
-    }\r
-\r
-    protected void updateSG(Point2D mousePos) {\r
-        routeGraph.setLocation(endRouteTerminal, mousePos.getX(), mousePos.getY());\r
-        //routeGraph.print(System.err);\r
-        setDirty();\r
-    }\r
-\r
-    protected boolean shouldEndWithFlag(MouseEvent me) {\r
-        return shouldEndWithFlag((me.stateMask & MouseEvent.ALT_MASK) != 0);\r
-    }\r
-\r
-    protected boolean shouldEndWithFlag(boolean altPressed) {\r
-        return altPressed && !isEndTerminalDefined() && createFlags;\r
-    }\r
-\r
-    protected boolean isEndTerminalDefined() {\r
-        return endTerminal != null;\r
-    }\r
-\r
-    protected boolean isFlagTerminal(TerminalInfo ti) {\r
-        return ti.e.getElementClass().containsClass(FlagHandler.class);\r
-    }\r
-\r
-\r
-    protected boolean isEndingInFlag() {\r
-        return endFlag != null;\r
-    }\r
-\r
-    protected void endWithoutTerminal(Point2D mousePos, boolean altDown) {\r
-        // Just go with branch points if flags are not allowed.\r
-        if (!createFlags)\r
-            return;\r
-\r
-        boolean endTerminalDefined = isEndTerminalDefined();\r
-\r
-        if (altDown) {\r
-            if (!isEndingInFlag()) {\r
-                endFlag = createFlag(EdgeEnd.End);\r
-                endFlagNode = showElement(ghostNode, "endFlag", endFlag.e, mousePos);\r
-                controlPoints.getLast()\r
-                .setAttachedToTerminal(endFlag);\r
-\r
-                // TerminalPainter must refresh\r
-                setHint(TerminalPainter.TERMINAL_HOVER_STRATEGY, terminalHoverStrategy);\r
-\r
-                updateSG(mousePos);\r
-            }\r
-        } else {\r
-            if (isEndingInFlag()) {\r
-                // Currently ending with flag but ALT is no longer down\r
-                // so that flag must be removed.\r
-                endFlag = null;\r
-                endFlagNode.remove();\r
-                endFlagNode = null;\r
-\r
-                ControlPoint cp = controlPoints.getLast();\r
-                cp.setAttachedToTerminal(endTerminal);\r
-\r
-                if (endTerminalDefined) {\r
-                    cp.setPosition(endTerminal.posDia);\r
-                } else {\r
-                    cp.setPosition(mousePos);\r
-                }\r
-\r
-                // Force Terminalpainter refresh\r
-                setHint(TerminalPainter.TERMINAL_HOVER_STRATEGY, terminalHoverStrategy);\r
-\r
-                updateSG(mousePos);\r
-            }\r
-        }\r
-    }\r
-\r
-    private G2DParentNode showElement(G2DParentNode parent, String nodeId, IElement element, Point2D pos) {\r
-        return showElement(parent, nodeId, element, AffineTransform.getTranslateInstance(pos.getX(), pos.getY()));\r
-    }\r
-\r
-    private G2DParentNode showElement(G2DParentNode parent, String nodeId, IElement element, AffineTransform tr) {\r
-        G2DParentNode elementParent = parent.getOrCreateNode(nodeId, G2DParentNode.class);\r
-        elementParent.setTransform(tr);\r
-        elementParent.removeNodes();\r
-        for (SceneGraph sg : element.getElementClass().getItemsByClass(SceneGraph.class))\r
-            sg.init(element, elementParent);\r
-        return elementParent;\r
-    }\r
-\r
-    /**\r
-     * @param canvasPos\r
-     * @return\r
-     */\r
-    protected ControlPoint newControlPoint(Point2D canvasPos) {\r
-        return new ControlPoint(canvasPos);\r
-    }\r
-\r
-    protected Triple<RouteGraph,RouteGraph,RouteGraphDelta> prepareRouteGraphDelta() {\r
-        // Prevent route graph connection synchronization from crashing on the\r
-        // transient route terminal that doesn't exist yet. It is created after\r
-        // persisting the route line, which is the only purpose of this route\r
-        // graph synchronization.\r
-        routeGraph.remove(endRouteTerminal);\r
-        return Triple.make(beforeRouteGraph, routeGraph, routeGraphDelta);\r
-    }\r
-\r
-    protected void createConnection() {\r
-        TimeLogger.resetTimeAndLog(getClass(), "createConnection");\r
-        final ConnectionJudgement judgment = this.connectionJudgment;\r
-        if (judgment == null) {\r
-            ErrorLogger.defaultLogError("Cannot create connection, no judgment available on connection validity", null);\r
-            return;\r
-        }\r
-\r
-        final ConnectionBuilder builder = new ConnectionBuilder(this.diagram);\r
-        final Triple<RouteGraph,RouteGraph,RouteGraphDelta> rgs = prepareRouteGraphDelta();\r
-\r
-        Simantics.getSession().asyncRequest(new WriteRequest() {\r
-            @Override\r
-            public void perform(WriteGraph graph) throws DatabaseException {\r
-                graph.markUndoPoint();\r
-                Resource connection = ElementUtils.getObject(startTerminal.e);\r
-                if (!rgs.third.isEmpty()) {\r
-                    new RouteGraphConnection(graph, connection).synchronize(graph, rgs.first, rgs.second, rgs.third);\r
-                }\r
-                Resource attachToLine = RouteGraphConnection.deserialize(graph, attachedToRouteLine.getData());\r
-                builder.attachToRouteGraph(graph, judgment, connection, attachToLine, controlPoints, endTerminal, FlagClass.Type.Out);\r
-            }\r
-        }, new Callback<DatabaseException>() {\r
-            @Override\r
-            public void run(DatabaseException parameter) {\r
-                if (parameter != null)\r
-                    ExceptionUtils.logAndShowError(parameter);\r
-            }\r
-        });\r
-    }\r
-\r
-    protected boolean routePointsAllowed() {\r
-        return Boolean.TRUE.equals(diagram.getHint(DiagramHints.KEY_ALLOW_ROUTE_POINTS));\r
-    }\r
-\r
-    protected Object canConnect(IElement endElement, Terminal endTerminal) {\r
-        IConnectionAdvisor advisor = diagram.getHint(DiagramHints.CONNECTION_ADVISOR);\r
-        return canConnect(advisor, endElement, endTerminal);\r
-    }\r
-\r
-    protected Object canConnect(IConnectionAdvisor advisor, IElement endElement, Terminal endTerminal) {\r
-        if (advisor == null)\r
-            return ConnectionJudgement.CANBEMADELEGAL;\r
-        if (alreadyConnected.contains(Pair.make(endElement, endTerminal)))\r
-            return null;\r
-        return advisor.canBeConnected(null, startTerminal.e, startTerminal.t, endElement, endTerminal);\r
-    }\r
-\r
-    protected static FlagClass.Type endToFlagType(EdgeEnd end) {\r
-        switch (end) {\r
-            case Begin:\r
-                return FlagClass.Type.In;\r
-            case End:\r
-                return FlagClass.Type.Out;\r
-            default:\r
-                throw new IllegalArgumentException("unrecognized edge end: " + end);\r
-        }\r
-    }\r
-\r
-    protected TerminalInfo createFlag(EdgeEnd connectionEnd) {\r
-        ElementClass flagClass = elementClassProvider.get(ElementClasses.FLAG);\r
-        IElement e = Element.spawnNew(flagClass);\r
-\r
-        e.setHint(FlagClass.KEY_FLAG_TYPE, endToFlagType(connectionEnd));\r
-        e.setHint(FlagClass.KEY_FLAG_MODE, FlagClass.Mode.Internal);\r
-\r
-        TerminalInfo ti = new TerminalInfo();\r
-        ti.e = e;\r
-        ti.t = ElementUtils.getSingleTerminal(e);\r
-        ti.posElem = TerminalUtil.getTerminalPosOnElement(e, ti.t);\r
-        ti.posDia = TerminalUtil.getTerminalPosOnDiagram(e, ti.t);\r
-\r
-        return ti;\r
-    }\r
-\r
-    // ------------------------------------------------------------------------\r
-\r
-    static RouteGraphTarget pickRouteGraphConnection(IDiagram diagram, Shape pickShape, double pickDistance) {\r
-        ArrayList<IElement> elements = new ArrayList<IElement>();\r
-        PickRequest req = new PickRequest(pickShape);\r
-        DiagramUtils.pick(diagram, req, elements);\r
-        for (Iterator<IElement> it = elements.iterator(); it.hasNext();) {\r
-            IElement e = it.next();\r
-            RouteGraphNode rgn = e.getHint(RouteGraphConnectionClass.KEY_RG_NODE);\r
-            if (rgn == null || rgn.getRouteGraph() == null)\r
-                it.remove();\r
-        }\r
-        if (elements.isEmpty())\r
-            return null;\r
-\r
-        Rectangle2D pickRect = pickShape.getBounds2D();\r
-        final double x = pickRect.getCenterX();\r
-        final double y = pickRect.getCenterY();\r
-\r
-        return pickNearestRouteGraphConnection(elements, x, y, pickDistance);\r
-    }\r
-\r
-    private static RouteGraphTarget pickNearestRouteGraphConnection(ArrayList<IElement> elements, double x, double y, double pd) {\r
-        // Find the nearest distance at which we get hits.\r
-        double hi = pd + 1;\r
-        double lo = hi * .01;\r
-        double limit = 0.5;\r
-        while (true) {\r
-            double delta = (hi - lo);\r
-            if (delta <= limit)\r
-                break;\r
-\r
-            pd = (lo + hi) * .5;\r
-\r
-            boolean hit = false;\r
-            for (IElement connection : elements) {\r
-                RouteGraphNode rgn = connection.getHint(RouteGraphConnectionClass.KEY_RG_NODE);\r
-                RouteLine line = rgn.getRouteGraph().pickLine(x, y, pd);\r
-                if (line != null) {\r
-                    hit = true;\r
-                    break;\r
-                }\r
-            }\r
-\r
-            if (hit)\r
-                hi = pd;\r
-            else\r
-                lo = pd;\r
-        }\r
-\r
-        // Now that the nearest hitting distance is found, find the nearest intersection.\r
-        RouteGraphTarget nearestTarget = null;\r
-        double nearest = Double.MAX_VALUE;\r
-        for (IElement connection : elements) {\r
-            RouteGraphNode rgn = connection.getHint(RouteGraphConnectionClass.KEY_RG_NODE);\r
-            RouteLine line = rgn.getRouteGraph().pickLine(x, y, pd);\r
-            if (line == null)\r
-                continue;\r
-\r
-            Point2D intersection = intersectionPoint(x, y, line);\r
-            if (intersection == null)\r
-                continue;\r
-\r
-            double dx = intersection.getX() - x;\r
-            double dy = intersection.getY() - y;\r
-            double dist = Math.sqrt(dx*dx + dy*dy);\r
-            if (dist < nearest) {\r
-                nearest = dist;\r
-                nearestTarget = new RouteGraphTarget(connection, rgn, line, new Point2D.Double(x, y), intersection);\r
-            }\r
-        }\r
-\r
-        return nearestTarget;\r
-    }\r
-\r
-    static Point2D intersectionPoint(double x, double y, RouteLine line) {\r
-        Collection<RoutePoint> points = line.getPoints();\r
-        if (points.size() < 2)\r
-            return null;\r
-        RoutePoint s = line.getBegin();\r
-        RoutePoint e = line.getEnd();\r
-        return GeometryUtils.intersectionToLine(s.getX(), s.getY(), e.getX(), e.getY(), x, y);\r
-    }\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2016 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - Fixed bug #6364
+ *******************************************************************************/
+package org.simantics.diagram.participant;
+
+import java.awt.AlphaComposite;
+import java.awt.Composite;
+import java.awt.Shape;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Point2D;
+import java.awt.geom.Rectangle2D;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Deque;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.simantics.Simantics;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.diagram.connection.RouteGraph;
+import org.simantics.diagram.connection.RouteGraphConnectionClass;
+import org.simantics.diagram.connection.RouteLine;
+import org.simantics.diagram.connection.RoutePoint;
+import org.simantics.diagram.connection.RouteTerminal;
+import org.simantics.diagram.connection.delta.RouteGraphDelta;
+import org.simantics.diagram.connection.rendering.IRouteGraphRenderer;
+import org.simantics.diagram.connection.rendering.arrows.ArrowLineEndStyle;
+import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;
+import org.simantics.diagram.connection.rendering.arrows.PlainLineEndStyle;
+import org.simantics.diagram.synchronization.ISynchronizationContext;
+import org.simantics.diagram.synchronization.SynchronizationHints;
+import org.simantics.diagram.synchronization.graph.RouteGraphConnection;
+import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
+import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;
+import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
+import org.simantics.g2d.connection.ConnectionEntity;
+import org.simantics.g2d.connection.IConnectionAdvisor;
+import org.simantics.g2d.diagram.DiagramHints;
+import org.simantics.g2d.diagram.DiagramUtils;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.diagram.handler.PickRequest;
+import org.simantics.g2d.diagram.handler.Topology.Connection;
+import org.simantics.g2d.diagram.handler.Topology.Terminal;
+import org.simantics.g2d.diagram.participant.ElementPainter;
+import org.simantics.g2d.diagram.participant.TerminalPainter;
+import org.simantics.g2d.diagram.participant.TerminalPainter.TerminalHoverStrategy;
+import org.simantics.g2d.diagram.participant.pointertool.AbstractMode;
+import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor;
+import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil;
+import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo;
+import org.simantics.g2d.element.ElementClass;
+import org.simantics.g2d.element.ElementClasses;
+import org.simantics.g2d.element.ElementHints;
+import org.simantics.g2d.element.ElementUtils;
+import org.simantics.g2d.element.IElement;
+import org.simantics.g2d.element.IElementClassProvider;
+import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;
+import org.simantics.g2d.element.handler.SceneGraph;
+import org.simantics.g2d.element.handler.TerminalTopology;
+import org.simantics.g2d.element.handler.impl.BranchPointTerminal;
+import org.simantics.g2d.element.impl.Element;
+import org.simantics.g2d.elementclass.FlagClass;
+import org.simantics.g2d.elementclass.FlagHandler;
+import org.simantics.g2d.participant.TransformUtil;
+import org.simantics.g2d.utils.geom.DirectionSet;
+import org.simantics.scenegraph.g2d.G2DParentNode;
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
+import org.simantics.scenegraph.g2d.events.KeyEvent;
+import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
+import org.simantics.scenegraph.g2d.events.MouseEvent;
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonEvent;
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent;
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
+import org.simantics.scenegraph.g2d.events.command.CommandEvent;
+import org.simantics.scenegraph.g2d.events.command.Commands;
+import org.simantics.scenegraph.g2d.nodes.BranchPointNode;
+import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
+import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphNode;
+import org.simantics.scenegraph.g2d.snap.ISnapAdvisor;
+import org.simantics.scenegraph.utils.GeometryUtils;
+import org.simantics.structural2.modelingRules.ConnectionJudgement;
+import org.simantics.utils.datastructures.Pair;
+import org.simantics.utils.datastructures.Triple;
+import org.simantics.utils.logging.TimeLogger;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.ExceptionUtils;
+
+import gnu.trove.map.hash.THashMap;
+
+/**
+ * A basic tool for making connection on diagrams.
+ * 
+ * This version defines the starting, ending and route points of a connection.
+ * The routing itself is left up to the diagram router employed by
+ * {@link DiagramUtils#validateAndFix(IDiagram, ICanvasContext)}.
+ * 
+ * Manual:
+ * 
+ * This tool is added to the diagram when a connection sequence is initiated by
+ * another participant. PointerInteractor is one such participant which adds the
+ * tool when a terminal or non-terminal-occupied canvas space is ALT+clicked
+ * (see {@link PointerInteractor#checkInitiateConnectTool(MouseEvent, Point2D)}
+ * ). The connection will be finished when another allowed terminal is clicked
+ * upon or empty canvas space is ALT+clicked. Route points for the connection
+ * can be created by clicking around on non-terminal-occupied canvas space while
+ * connecting.
+ * 
+ * <p>
+ * Connections can be started from and ended in flags by pressing ALT while
+ * left-clicking.
+ * 
+ * @author Tuukka Lehtonen
+ */
+public class RouteGraphConnectTool extends AbstractMode {
+
+    private static final String      END_TERMINAL_DATA     = "END";
+
+    public static final int          PAINT_PRIORITY        = ElementPainter.ELEMENT_PAINT_PRIORITY + 5;
+
+    @Dependency
+    protected TransformUtil          util;
+
+    @Dependency
+    protected ElementPainter         diagramPainter;
+
+    @Dependency
+    protected PointerInteractor      pi;
+
+    /**
+     * Starting point designation.
+     * 
+     * The value is received by the constructor.
+     */
+    protected RouteGraphTarget       startingPoint;
+
+    protected TerminalInfo           startTerminal         = new TerminalInfo();
+
+    /**
+     * <code>true</code> if this tool should create connection continuation
+     * flags, <code>false</code> otherwise.
+     */
+    protected boolean                createFlags;
+
+    /**
+     * 
+     */
+    protected IElementClassProvider  elementClassProvider;
+
+    /**
+     * 
+     */
+    protected Deque<ControlPoint>    controlPoints         = new ArrayDeque<ControlPoint>();
+
+    /**
+     * Element terminal of connection end element. <code>null</code> if
+     * connection cannot be ended where it is currently being attempted to end.
+     */
+    protected TerminalInfo           endTerminal;
+
+    /**
+     * The latest connectability judgment from the active
+     * {@link IConnectionAdvisor} should the connection happen between
+     * {@link #startTerminal} and {@link #endTerminal}.
+     */
+    protected ConnectionJudgement    connectionJudgment;
+
+    /**
+     * A temporary variable for use with
+     * {@link TerminalTopology#getTerminals(IElement, Collection)}.
+     */
+    protected Collection<Terminal>   terminals             = new ArrayList<Terminal>();
+
+    /**
+     * Previous mouse canvas position recorded by
+     * {@link #processMouseMove(MouseMovedEvent)}.
+     */
+    protected Point2D                lastMouseCanvasPos    = new Point2D.Double();
+
+    /**
+     * Set to true once {@link #processMouseMove(MouseMovedEvent)} has been
+     * invoked at least once. This is used to tell whether to allow creation of
+     * branch points or finising the connection in thin air. It will not be
+     * allowed if the mouse has not moved at all since starting the connection.
+     */
+    protected boolean                mouseHasMoved         = false;
+
+    protected TerminalHoverStrategy  originalStrategy      = null;
+
+    protected TerminalHoverStrategy  terminalHoverStrategy = new TerminalHoverStrategy() {
+        @Override
+        public boolean highlightEnabled() {
+            return !isEndingInFlag();
+        }
+
+        @Override
+        public boolean highlight(TerminalInfo ti) {
+            return canConnect(ti.e, ti.t) != null;
+        }
+    };
+
+    protected final static Composite ALPHA_COMPOSITE       = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f);
+
+    /**
+     * Root scene graph node for all visualization performed by this tool.
+     */
+    protected ConnectionNode         ghostNode;
+
+    protected RouteGraphNode           rgNode;
+
+    protected  RouteGraph               routeGraph;
+    protected  RouteTerminal            endRouteTerminal;
+    private ILineEndStyle            endTerminalStyle;
+
+    /**
+     * Indicates whether the connection is about to be ended into a new
+     * flag/branchpoint or not.
+     */
+    protected TerminalInfo           endFlag;
+
+    protected G2DParentNode          endFlagNode;
+
+    private RouteLine                attachedToRouteLine;
+
+    protected RouteGraph               beforeRouteGraph;
+
+    private IRouteGraphRenderer      beforeRenderer;
+
+    private boolean                  beforeEditable;
+
+    protected RouteGraphDelta          routeGraphDelta;
+
+    private Set<Pair<IElement, Terminal>> alreadyConnected;
+    
+    /**
+     * @param startElement
+     * @param routeGraphConnection
+     * @param mouseId
+     * @param startCanvasPos
+     */
+    public RouteGraphConnectTool(RouteGraphTarget startingPoint, int mouseId) {
+        super(mouseId);
+
+        Point2D intersection = startingPoint.getIntersectionPosition();
+
+        this.startingPoint = startingPoint;
+        this.lastMouseCanvasPos.setLocation(intersection);
+
+        BranchPointTerminal t = BranchPointTerminal.existingTerminal(
+                AffineTransform.getTranslateInstance(intersection.getX(), intersection.getY()),
+                DirectionSet.ANY,
+                BranchPointNode.SHAPE);
+
+        Point2D p = startingPoint.getIntersectionPosition();
+        AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
+
+        startTerminal.e = startingPoint.getElement();
+        startTerminal.t = t;
+        startTerminal.posElem = at;
+        startTerminal.posDia = at;
+        startTerminal.shape = t.getShape();
+
+        controlPoints.add( newControlPoint( startingPoint.getIntersectionPosition() ) );
+        controlPoints.add( newControlPoint( startingPoint.getCanvasPosition() ) );
+
+        alreadyConnected = new HashSet<Pair<IElement,Terminal>>();
+        IElement connection = startingPoint.getElement();
+        ConnectionEntity ce = connection.getHint(ElementHints.KEY_CONNECTION_ENTITY);
+        Collection<Connection> tcs = ce.getTerminalConnections(null);
+        for (Connection tc : tcs)
+            alreadyConnected.add(Pair.make(tc.node, tc.terminal));
+    }
+
+    @Override
+    public void addedToContext(ICanvasContext ctx) {
+        super.addedToContext(ctx);
+
+        // Force terminals to always be highlighted without pressing certain
+        // keys or key combinations.
+        originalStrategy = getHint(TerminalPainter.TERMINAL_HOVER_STRATEGY);
+        setHint(TerminalPainter.TERMINAL_HOVER_STRATEGY, terminalHoverStrategy);
+    }
+
+    @Override
+    protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) {
+        if (newDiagram != null) {
+            // Get IElementClassProvider
+            ISynchronizationContext ctx = newDiagram.getHint(SynchronizationHints.CONTEXT);
+            if (ctx != null) {
+                this.elementClassProvider = ctx.get(SynchronizationHints.ELEMENT_CLASS_PROVIDER);
+            }
+
+            // See if flags should be created or not.
+            this.createFlags = Boolean.TRUE.equals(newDiagram.getHint(DiagramHints.KEY_USE_CONNECTION_FLAGS));
+        }
+    }
+
+    @Override
+    public void removedFromContext(ICanvasContext ctx) {
+        if (getHint(TerminalPainter.TERMINAL_HOVER_STRATEGY) == terminalHoverStrategy) {
+            if (originalStrategy != null)
+                setHint(TerminalPainter.TERMINAL_HOVER_STRATEGY, originalStrategy);
+            else
+                removeHint(TerminalPainter.TERMINAL_HOVER_STRATEGY);
+        }
+
+        super.removedFromContext(ctx);
+    }
+
+    int straightDirections(RouteLine line) {
+        return line.isHorizontal()
+                ? (RouteTerminal.DIR_DOWN | RouteTerminal.DIR_UP)
+                : (RouteTerminal.DIR_LEFT | RouteTerminal.DIR_RIGHT);
+    }
+
+    private static RouteTerminal addTerminal(Object data, RouteGraph rg, double x, double y, Rectangle2D bounds, int allowedDirections, ILineEndStyle style) {
+        RouteTerminal rt;
+        if (bounds != null)
+            rt = rg.addTerminal(x, y, bounds.getMinX(), bounds.getMinY(), bounds.getMaxX(), bounds.getMaxY(), allowedDirections, style);
+        else
+            rt = rg.addTerminal(x, y, x, y, x, y, allowedDirections, style);
+        rt.setData( data );
+        return rt;
+    }
+
+    private RouteTerminal setEndTerminal(double x, double y, Rectangle2D bounds, int allowedDirections) {
+
+       // First add then remove to prevent deletion of route lines in case there are 2 only terminals
+       
+       RouteTerminal toRemove = endRouteTerminal;
+       
+        endRouteTerminal = addTerminal( END_TERMINAL_DATA, routeGraph, x, y, bounds, allowedDirections, endTerminalStyle );
+        routeGraph.link( attachedToRouteLine, endRouteTerminal );
+        
+        if (toRemove != null)
+            routeGraph.remove(toRemove);
+        
+        return endRouteTerminal;
+        
+    }
+
+    protected void setEndTerminal(TerminalInfo ti) {
+        Rectangle2D bounds = ElementUtils.getElementBoundsOnDiagram(ti.e, new Rectangle2D.Double());
+        GeometryUtils.expandRectangle(bounds, 2);
+        int dir = RouteGraphConnectionClass.shortestDirectionOutOfBounds(
+                ti.posDia.getTranslateX(), ti.posDia.getTranslateY(), bounds);
+        setEndTerminal(ti.posDia.getTranslateX(), ti.posDia.getTranslateY(), bounds, dir);
+    }
+
+    @SGInit
+    public void initSG(G2DParentNode parent) {
+        ghostNode = parent.addNode("branched connection", ConnectionNode.class);
+        //ghostNode.setComposite(AlphaComposite.SrcOver.derive(0.5f));
+        ghostNode.setZIndex(PAINT_PRIORITY);
+
+        rgNode = ghostNode.addNode("branch", RouteGraphNode.class);
+
+        double ex = startingPoint.getCanvasPosition().getX();
+        double ey = startingPoint.getCanvasPosition().getY();
+
+        beforeRouteGraph = startingPoint.getNode().getRouteGraph();
+        beforeEditable = startingPoint.getNode().isEditable();
+
+        RouteGraphNode beforeRgNode = startingPoint.getNode();
+        beforeRenderer = beforeRgNode.getRenderer();
+
+        rgNode.setRenderer(beforeRenderer);
+        rgNode.setEditable(false);
+
+        beforeRgNode.setEditable(beforeEditable);
+        beforeRgNode.setRenderer(null);
+        
+        initRG(ex, ey);
+        
+    }
+    
+    public void initRG(double ex, double ey) {
+       
+        THashMap<Object, Object> map = new THashMap<Object, Object>();
+        routeGraph = beforeRouteGraph.copy(map);
+
+        endTerminalStyle = PlainLineEndStyle.INSTANCE;
+        for (RouteTerminal t : routeGraph.getTerminals()) {
+            if (t.getRenderStyle() instanceof ArrowLineEndStyle) {
+                endTerminalStyle = t.getRenderStyle();
+                break;
+            }
+        }
+
+        attachedToRouteLine = (RouteLine) map.get(startingPoint.getLine());
+        routeGraph.makePersistent(attachedToRouteLine);
+        for (RouteLine line : routeGraph.getAllLines()) {
+            if (!line.isTransient() && line.isHorizontal() == attachedToRouteLine.isHorizontal()
+                    && line.getPosition() == attachedToRouteLine.getPosition()) {
+                attachedToRouteLine = line;
+                break;
+            }
+        }
+        routeGraphDelta = new RouteGraphDelta(beforeRouteGraph, routeGraph);
+
+//        beforeRouteGraph.print();
+//        routeGraph.print();
+//        routeGraphDelta.print();
+
+        setEndTerminal(ex, ey, null, 0xf);
+
+        rgNode.setRouteGraph(routeGraph);
+       
+    }
+    
+    @SGCleanup
+    public void cleanupSG() {
+        RouteGraphNode beforeRgNode = startingPoint.getNode();
+        beforeRgNode.setRouteGraph(beforeRouteGraph);
+        beforeRgNode.setRenderer(beforeRenderer);
+        beforeRgNode.setEditable(beforeEditable);
+
+        ghostNode.remove();
+        ghostNode = null;
+
+        setDirty();
+    }
+
+    @EventHandler(priority = 200)
+    public boolean handleCommandEvents(CommandEvent ce) {
+        if (ce.command.equals(Commands.CANCEL)) {
+            setDirty();
+            remove();
+            return true;
+        } else if (ce.command.equals(Commands.ROTATE_ELEMENT_CCW) || ce.command.equals(Commands.ROTATE_ELEMENT_CW)) {
+            // TODO: rotate flag?
+        }
+        return false;
+    }
+
+    @EventHandler(priority = PointerInteractor.TOOL_PRIORITY + 20)
+    public boolean handleKeyEvents(KeyEvent ke) {
+        if (ke instanceof KeyPressedEvent) {
+            // Back-space, cancel prev bend
+            if (ke.keyCode == java.awt.event.KeyEvent.VK_BACK_SPACE)
+                return cancelPreviousBend();
+        }
+
+        if (ke.keyCode == java.awt.event.KeyEvent.VK_ALT) {
+            if (createFlags) {
+                endWithoutTerminal(lastMouseCanvasPos, shouldEndWithFlag(ke instanceof KeyPressedEvent));
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    @EventHandler(priority = PointerInteractor.TOOL_PRIORITY + 20)
+    public boolean handleEvent(MouseEvent me) {
+        // Only handle events for the connection-initiating mouse
+        if (me.mouseId != mouseId)
+            return false;
+
+        if (me instanceof MouseMovedEvent)
+            return processMouseMove((MouseMovedEvent) me);
+
+        if (me instanceof MouseButtonPressedEvent)
+            return processMouseButtonPress((MouseButtonPressedEvent) me);
+
+        // #7653: Support creating connections between terminals without lifting mouse button in between.
+        if (me instanceof MouseButtonReleasedEvent)
+            return processMouseButtonRelease((MouseButtonReleasedEvent) me);
+
+        return false;
+    }
+
+    protected boolean processMouseMove(MouseMovedEvent me) {
+        mouseHasMoved = true;
+
+        Point2D mouseControlPos = me.controlPosition;
+        Point2D mouseCanvasPos = util.controlToCanvas(mouseControlPos, new Point2D.Double());
+
+        ISnapAdvisor snapAdvisor = getHint(DiagramHints.SNAP_ADVISOR);
+        if (snapAdvisor != null)
+            snapAdvisor.snap(mouseCanvasPos);
+
+        // Record last snapped canvas position of mouse.
+        this.lastMouseCanvasPos.setLocation(mouseCanvasPos);
+
+        if (isEndingInFlag()) {
+            endFlagNode.setTransform(AffineTransform.getTranslateInstance(mouseCanvasPos.getX(), mouseCanvasPos.getY()));
+        }
+
+        TerminalInfo ti = pi.pickTerminal(me.controlPosition);
+        if (ti != null) {
+            Object canConnect = canConnect(ti.e, ti.t);
+            if (canConnect != null) {
+                connectionJudgment = (ConnectionJudgement) canConnect;
+
+                if (!isEndingInFlag() || !TerminalUtil.isSameTerminal(ti, endTerminal)) {
+                    controlPoints.getLast()
+                    .setPosition(ti.posDia)
+                    .setAttachedToTerminal(ti);
+
+                    endTerminal = ti;
+
+                    connect(ti);
+                    
+                }
+
+                // Make sure that we are ending with a flag if ALT is pressed
+                // and no end terminal is defined.
+                endWithoutTerminal(lastMouseCanvasPos, shouldEndWithFlag(me));
+
+                updateSG(new Point2D.Double(ti.posDia.getTranslateX(), ti.posDia.getTranslateY()));
+                return false;
+            }
+        }
+
+        connectionJudgment = null;
+        if (isEndTerminalDefined()) {
+            // CASE: Mouse was previously on top of a valid terminal to end
+            // the connection. Now the mouse has been moved where there is
+            // no longer a terminal to connect to.
+            //
+            // => Disconnect the last edge segment from the previous
+            // terminal, mark endElement/endTerminal non-existent
+            // and connect the disconnected edge to a new branch point.
+
+            disconnect(mouseCanvasPos);
+            
+            controlPoints.getLast()
+            .setPosition(mouseCanvasPos)
+            .setAttachedToTerminal(null);
+
+            endTerminal = null;
+        } else {
+            // CASE: Mouse was not previously on top of a valid ending
+            // element terminal.
+            //
+            // => Move and re-orient last branch point.
+
+            controlPoints.getLast()
+            .setPosition(mouseCanvasPos);
+        }
+
+        // Make sure that we are ending with a flag if ALT is pressed and no end
+        // terminal is defined.
+        endWithoutTerminal(lastMouseCanvasPos, shouldEndWithFlag(me));
+
+        updateSG(lastMouseCanvasPos);
+
+        return false;
+    }
+
+    protected void connect(TerminalInfo ti) {
+        setEndTerminal(ti);
+    }
+    
+    protected void disconnect(Point2D mouseCanvasPos) {
+        setEndTerminal(mouseCanvasPos.getX(), mouseCanvasPos.getY(), null, 0xf);
+    }
+
+    protected boolean processMouseButtonPress(MouseButtonEvent me) {
+        // Do nothing before the mouse has moved at least a little.
+        // This prevents the user from ending the connection right where
+        // it started.
+        if (!mouseHasMoved)
+            return true;
+
+        if (me.button == MouseEvent.LEFT_BUTTON) {
+            Point2D mouseControlPos = me.controlPosition;
+            Point2D mouseCanvasPos = util.getInverseTransform().transform(mouseControlPos, new Point2D.Double());
+
+            ISnapAdvisor snapAdvisor = getHint(DiagramHints.SNAP_ADVISOR);
+            if (snapAdvisor != null)
+                snapAdvisor.snap(mouseCanvasPos);
+
+            // Clicked on an allowed end terminal. End connection & end mode.
+            if (tryEndConnection()) {
+                return true;
+            } else {
+                // Finish connection in thin air only if the
+                // connection was started from a valid terminal.
+                if ((me.stateMask & MouseEvent.ALT_MASK) != 0 && startTerminal != null) {
+                    connectionJudgment = (ConnectionJudgement) canConnect(null, null);
+                    if (connectionJudgment == null)
+                        return true;
+                    createConnection();
+                    remove();
+                    return true;
+                } else if (routePointsAllowed()
+                        && (me.stateMask & (MouseEvent.ALT_MASK | MouseEvent.SHIFT_MASK | MouseEvent.CTRL_MASK)) == 0) {
+                    // Add new connection control point.
+                    controlPoints.add(newControlPoint(mouseCanvasPos));
+                    updateSG(mouseCanvasPos);
+                }
+            }
+
+            // Eat the event to prevent other participants from doing
+            // incompatible things while in this connection mode.
+            return true;
+        } else if (me.button == MouseEvent.RIGHT_BUTTON) {
+            return cancelPreviousBend();
+        }
+
+        return false;
+    }
+
+    private int mouseLeftReleaseCount = 0;
+
+    protected boolean processMouseButtonRelease(MouseButtonReleasedEvent me) {
+        if (me.button == MouseEvent.LEFT_BUTTON
+                && ++mouseLeftReleaseCount == 1) {
+            return tryEndConnection();
+        }
+        return false;
+    }
+
+    /**
+     * @return <code>true</code> if connection was successfully ended
+     */
+    private boolean tryEndConnection() {
+        if (isEndTerminalDefined()) {
+            createConnection();
+            remove();
+            return true;
+        }
+        return false;
+    }
+
+    protected boolean cancelPreviousBend() {
+        if (!routePointsAllowed())
+            return false;
+
+        // Just to make this code more comprehensible, prevent an editing
+        // case that requires ugly code to work.
+        if (isEndingInFlag())
+            return true;
+
+        // If there are no real route points, cancel whole connection.
+        if (controlPoints.size() <= 2) {
+            setDirty();
+            remove();
+            return true;
+        }
+
+        // Cancel last bend
+        controlPoints.removeLast();
+        controlPoints.getLast().setPosition(lastMouseCanvasPos);
+
+        updateSG(lastMouseCanvasPos);
+        return true;
+    }
+
+    protected void updateSG(Point2D mousePos) {
+        routeGraph.setLocation(endRouteTerminal, mousePos.getX(), mousePos.getY());
+        //routeGraph.print(System.err);
+        setDirty();
+    }
+
+    protected boolean shouldEndWithFlag(MouseEvent me) {
+        return shouldEndWithFlag((me.stateMask & MouseEvent.ALT_MASK) != 0);
+    }
+
+    protected boolean shouldEndWithFlag(boolean altPressed) {
+        return altPressed && !isEndTerminalDefined() && createFlags;
+    }
+
+    protected boolean isEndTerminalDefined() {
+        return endTerminal != null;
+    }
+
+    protected boolean isFlagTerminal(TerminalInfo ti) {
+        return ti.e.getElementClass().containsClass(FlagHandler.class);
+    }
+
+
+    protected boolean isEndingInFlag() {
+        return endFlag != null;
+    }
+
+    protected void endWithoutTerminal(Point2D mousePos, boolean altDown) {
+        // Just go with branch points if flags are not allowed.
+        if (!createFlags)
+            return;
+
+        boolean endTerminalDefined = isEndTerminalDefined();
+
+        if (altDown) {
+            if (!isEndingInFlag()) {
+                endFlag = createFlag(EdgeEnd.End);
+                endFlagNode = showElement(ghostNode, "endFlag", endFlag.e, mousePos);
+                controlPoints.getLast()
+                .setAttachedToTerminal(endFlag);
+
+                // TerminalPainter must refresh
+                setHint(TerminalPainter.TERMINAL_HOVER_STRATEGY, terminalHoverStrategy);
+
+                updateSG(mousePos);
+            }
+        } else {
+            if (isEndingInFlag()) {
+                // Currently ending with flag but ALT is no longer down
+                // so that flag must be removed.
+                endFlag = null;
+                endFlagNode.remove();
+                endFlagNode = null;
+
+                ControlPoint cp = controlPoints.getLast();
+                cp.setAttachedToTerminal(endTerminal);
+
+                if (endTerminalDefined) {
+                    cp.setPosition(endTerminal.posDia);
+                } else {
+                    cp.setPosition(mousePos);
+                }
+
+                // Force Terminalpainter refresh
+                setHint(TerminalPainter.TERMINAL_HOVER_STRATEGY, terminalHoverStrategy);
+
+                updateSG(mousePos);
+            }
+        }
+    }
+
+    private G2DParentNode showElement(G2DParentNode parent, String nodeId, IElement element, Point2D pos) {
+        return showElement(parent, nodeId, element, AffineTransform.getTranslateInstance(pos.getX(), pos.getY()));
+    }
+
+    private G2DParentNode showElement(G2DParentNode parent, String nodeId, IElement element, AffineTransform tr) {
+        G2DParentNode elementParent = parent.getOrCreateNode(nodeId, G2DParentNode.class);
+        elementParent.setTransform(tr);
+        elementParent.removeNodes();
+        for (SceneGraph sg : element.getElementClass().getItemsByClass(SceneGraph.class))
+            sg.init(element, elementParent);
+        return elementParent;
+    }
+
+    /**
+     * @param canvasPos
+     * @return
+     */
+    protected ControlPoint newControlPoint(Point2D canvasPos) {
+        return new ControlPoint(canvasPos);
+    }
+
+    protected Triple<RouteGraph,RouteGraph,RouteGraphDelta> prepareRouteGraphDelta() {
+        // Prevent route graph connection synchronization from crashing on the
+        // transient route terminal that doesn't exist yet. It is created after
+        // persisting the route line, which is the only purpose of this route
+        // graph synchronization.
+        routeGraph.remove(endRouteTerminal);
+        return Triple.make(beforeRouteGraph, routeGraph, routeGraphDelta);
+    }
+
+    protected void createConnection() {
+        TimeLogger.resetTimeAndLog(getClass(), "createConnection");
+        final ConnectionJudgement judgment = this.connectionJudgment;
+        if (judgment == null) {
+            ErrorLogger.defaultLogError("Cannot create connection, no judgment available on connection validity", null);
+            return;
+        }
+
+        final ConnectionBuilder builder = new ConnectionBuilder(this.diagram);
+        final Triple<RouteGraph,RouteGraph,RouteGraphDelta> rgs = prepareRouteGraphDelta();
+
+        Simantics.getSession().asyncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                graph.markUndoPoint();
+                Resource connection = ElementUtils.getObject(startTerminal.e);
+                if (!rgs.third.isEmpty()) {
+                    new RouteGraphConnection(graph, connection).synchronize(graph, rgs.first, rgs.second, rgs.third);
+                }
+                Resource attachToLine = RouteGraphConnection.deserialize(graph, attachedToRouteLine.getData());
+                builder.attachToRouteGraph(graph, judgment, connection, attachToLine, controlPoints, endTerminal, FlagClass.Type.Out);
+            }
+        }, e -> {
+            if (e != null)
+                ExceptionUtils.logAndShowError(e);
+        });
+    }
+
+    protected boolean routePointsAllowed() {
+        return Boolean.TRUE.equals(diagram.getHint(DiagramHints.KEY_ALLOW_ROUTE_POINTS));
+    }
+
+    protected Object canConnect(IElement endElement, Terminal endTerminal) {
+        IConnectionAdvisor advisor = diagram.getHint(DiagramHints.CONNECTION_ADVISOR);
+        return canConnect(advisor, endElement, endTerminal);
+    }
+
+    protected Object canConnect(IConnectionAdvisor advisor, IElement endElement, Terminal endTerminal) {
+        if (advisor == null)
+            return ConnectionJudgement.CANBEMADELEGAL;
+        if (alreadyConnected.contains(Pair.make(endElement, endTerminal)))
+            return null;
+        return advisor.canBeConnected(null, startTerminal.e, startTerminal.t, endElement, endTerminal);
+    }
+
+    protected static FlagClass.Type endToFlagType(EdgeEnd end) {
+        switch (end) {
+            case Begin:
+                return FlagClass.Type.In;
+            case End:
+                return FlagClass.Type.Out;
+            default:
+                throw new IllegalArgumentException("unrecognized edge end: " + end);
+        }
+    }
+
+    protected TerminalInfo createFlag(EdgeEnd connectionEnd) {
+        ElementClass flagClass = elementClassProvider.get(ElementClasses.FLAG);
+        IElement e = Element.spawnNew(flagClass);
+
+        e.setHint(FlagClass.KEY_FLAG_TYPE, endToFlagType(connectionEnd));
+        e.setHint(FlagClass.KEY_FLAG_MODE, FlagClass.Mode.Internal);
+
+        TerminalInfo ti = new TerminalInfo();
+        ti.e = e;
+        ti.t = ElementUtils.getSingleTerminal(e);
+        ti.posElem = TerminalUtil.getTerminalPosOnElement(e, ti.t);
+        ti.posDia = TerminalUtil.getTerminalPosOnDiagram(e, ti.t);
+
+        return ti;
+    }
+
+    // ------------------------------------------------------------------------
+
+    static RouteGraphTarget pickRouteGraphConnection(IDiagram diagram, Shape pickShape, double pickDistance) {
+        ArrayList<IElement> elements = new ArrayList<IElement>();
+        PickRequest req = new PickRequest(pickShape);
+        DiagramUtils.pick(diagram, req, elements);
+        for (Iterator<IElement> it = elements.iterator(); it.hasNext();) {
+            IElement e = it.next();
+            RouteGraphNode rgn = e.getHint(RouteGraphConnectionClass.KEY_RG_NODE);
+            if (rgn == null || rgn.getRouteGraph() == null)
+                it.remove();
+        }
+        if (elements.isEmpty())
+            return null;
+
+        Rectangle2D pickRect = pickShape.getBounds2D();
+        final double x = pickRect.getCenterX();
+        final double y = pickRect.getCenterY();
+
+        return pickNearestRouteGraphConnection(elements, x, y, pickDistance);
+    }
+
+    private static RouteGraphTarget pickNearestRouteGraphConnection(ArrayList<IElement> elements, double x, double y, double pd) {
+        // Find the nearest distance at which we get hits.
+        double hi = pd + 1;
+        double lo = hi * .01;
+        double limit = 0.5;
+        while (true) {
+            double delta = (hi - lo);
+            if (delta <= limit)
+                break;
+
+            pd = (lo + hi) * .5;
+
+            boolean hit = false;
+            for (IElement connection : elements) {
+                RouteGraphNode rgn = connection.getHint(RouteGraphConnectionClass.KEY_RG_NODE);
+                RouteLine line = rgn.getRouteGraph().pickLine(x, y, pd);
+                if (line != null) {
+                    hit = true;
+                    break;
+                }
+            }
+
+            if (hit)
+                hi = pd;
+            else
+                lo = pd;
+        }
+
+        // Now that the nearest hitting distance is found, find the nearest intersection.
+        RouteGraphTarget nearestTarget = null;
+        double nearest = Double.MAX_VALUE;
+        for (IElement connection : elements) {
+            RouteGraphNode rgn = connection.getHint(RouteGraphConnectionClass.KEY_RG_NODE);
+            RouteLine line = rgn.getRouteGraph().pickLine(x, y, pd);
+            if (line == null)
+                continue;
+
+            Point2D intersection = intersectionPoint(x, y, line);
+            if (intersection == null)
+                continue;
+
+            double dx = intersection.getX() - x;
+            double dy = intersection.getY() - y;
+            double dist = Math.sqrt(dx*dx + dy*dy);
+            if (dist < nearest) {
+                nearest = dist;
+                nearestTarget = new RouteGraphTarget(connection, rgn, line, new Point2D.Double(x, y), intersection);
+            }
+        }
+
+        return nearestTarget;
+    }
+
+    static Point2D intersectionPoint(double x, double y, RouteLine line) {
+        Collection<RoutePoint> points = line.getPoints();
+        if (points.size() < 2)
+            return null;
+        RoutePoint s = line.getBegin();
+        RoutePoint e = line.getEnd();
+        return GeometryUtils.intersectionToLine(s.getX(), s.getY(), e.getX(), e.getY(), x, y);
+    }
+
 }
\ No newline at end of file