1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.participant;
\r
14 import java.awt.AlphaComposite;
\r
15 import java.awt.BasicStroke;
\r
16 import java.awt.Color;
\r
17 import java.awt.Composite;
\r
18 import java.awt.geom.AffineTransform;
\r
19 import java.awt.geom.Path2D;
\r
20 import java.awt.geom.Point2D;
\r
21 import java.awt.geom.Rectangle2D;
\r
22 import java.util.ArrayDeque;
\r
23 import java.util.ArrayList;
\r
24 import java.util.Arrays;
\r
25 import java.util.Collection;
\r
26 import java.util.Collections;
\r
27 import java.util.Deque;
\r
28 import java.util.Iterator;
\r
29 import java.util.List;
\r
31 import org.simantics.Simantics;
\r
32 import org.simantics.db.ReadGraph;
\r
33 import org.simantics.db.Resource;
\r
34 import org.simantics.db.WriteGraph;
\r
35 import org.simantics.db.common.request.UniqueRead;
\r
36 import org.simantics.db.common.request.WriteRequest;
\r
37 import org.simantics.db.common.utils.NameUtils;
\r
38 import org.simantics.db.exception.DatabaseException;
\r
39 import org.simantics.diagram.connection.RouteGraph;
\r
40 import org.simantics.diagram.connection.RouteGraphConnectionClass;
\r
41 import org.simantics.diagram.connection.RouteLine;
\r
42 import org.simantics.diagram.connection.RouteTerminal;
\r
43 import org.simantics.diagram.connection.delta.RouteGraphDelta;
\r
44 import org.simantics.diagram.connection.rendering.arrows.PlainLineEndStyle;
\r
45 import org.simantics.diagram.content.ResourceTerminal;
\r
46 import org.simantics.diagram.stubs.DiagramResource;
\r
47 import org.simantics.diagram.synchronization.ISynchronizationContext;
\r
48 import org.simantics.diagram.synchronization.SynchronizationHints;
\r
49 import org.simantics.diagram.synchronization.graph.RouteGraphConnection;
\r
50 import org.simantics.g2d.canvas.ICanvasContext;
\r
51 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
\r
52 import org.simantics.g2d.canvas.impl.DependencyReflection.Reference;
\r
53 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;
\r
54 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
\r
55 import org.simantics.g2d.connection.IConnectionAdvisor;
\r
56 import org.simantics.g2d.diagram.DiagramHints;
\r
57 import org.simantics.g2d.diagram.DiagramUtils;
\r
58 import org.simantics.g2d.diagram.IDiagram;
\r
59 import org.simantics.g2d.diagram.handler.PickContext;
\r
60 import org.simantics.g2d.diagram.handler.Topology.Terminal;
\r
61 import org.simantics.g2d.diagram.participant.ElementPainter;
\r
62 import org.simantics.g2d.diagram.participant.TerminalPainter;
\r
63 import org.simantics.g2d.diagram.participant.TerminalPainter.TerminalHoverStrategy;
\r
64 import org.simantics.g2d.diagram.participant.pointertool.AbstractMode;
\r
65 import org.simantics.g2d.diagram.participant.pointertool.PointerInteractor;
\r
66 import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil;
\r
67 import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo;
\r
68 import org.simantics.g2d.element.ElementClass;
\r
69 import org.simantics.g2d.element.ElementClasses;
\r
70 import org.simantics.g2d.element.ElementUtils;
\r
71 import org.simantics.g2d.element.IElement;
\r
72 import org.simantics.g2d.element.IElementClassProvider;
\r
73 import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;
\r
74 import org.simantics.g2d.element.handler.SceneGraph;
\r
75 import org.simantics.g2d.element.handler.TerminalTopology;
\r
76 import org.simantics.g2d.element.handler.impl.BranchPointTerminal;
\r
77 import org.simantics.g2d.element.impl.Element;
\r
78 import org.simantics.g2d.elementclass.BranchPoint;
\r
79 import org.simantics.g2d.elementclass.BranchPoint.Direction;
\r
80 import org.simantics.g2d.elementclass.FlagClass;
\r
81 import org.simantics.g2d.elementclass.FlagHandler;
\r
82 import org.simantics.g2d.participant.RenderingQualityInteractor;
\r
83 import org.simantics.g2d.participant.TransformUtil;
\r
84 import org.simantics.g2d.utils.geom.DirectionSet;
\r
85 import org.simantics.modeling.ModelingResources;
\r
86 import org.simantics.scenegraph.g2d.G2DParentNode;
\r
87 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
\r
88 import org.simantics.scenegraph.g2d.events.KeyEvent;
\r
89 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
\r
90 import org.simantics.scenegraph.g2d.events.MouseEvent;
\r
91 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonEvent;
\r
92 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;
\r
93 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
\r
94 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
\r
95 import org.simantics.scenegraph.g2d.events.command.Commands;
\r
96 import org.simantics.scenegraph.g2d.nodes.BranchPointNode;
\r
97 import org.simantics.scenegraph.g2d.nodes.ShapeNode;
\r
98 import org.simantics.scenegraph.g2d.snap.ISnapAdvisor;
\r
99 import org.simantics.scenegraph.utils.GeometryUtils;
\r
100 import org.simantics.scenegraph.utils.Quality;
\r
101 import org.simantics.structural2.modelingRules.ConnectionJudgement;
\r
102 import org.simantics.utils.datastructures.Pair;
\r
103 import org.simantics.utils.logging.TimeLogger;
\r
104 import org.simantics.utils.ui.ErrorLogger;
\r
105 import org.simantics.utils.ui.ExceptionUtils;
\r
107 import gnu.trove.map.hash.THashMap;
\r
110 * A basic tool for making connection on diagrams.
\r
112 * This version defines the starting, ending and route points of a connection.
\r
113 * The routing itself is left up to the diagram router employed by
\r
114 * {@link DiagramUtils#validateAndFix(IDiagram, ICanvasContext)}.
\r
118 * This tool is added to the diagram when a connection sequence is initiated by
\r
119 * another participant. PointerInteractor is one such participant which adds the
\r
120 * tool when a terminal or non-terminal-occupied canvas space is ALT+clicked
\r
121 * (see {@link PointerInteractor#checkInitiateConnectTool(MouseEvent, Point2D)}
\r
122 * ). The connection will be finished when another allowed terminal is clicked
\r
123 * upon or empty canvas space is ALT+clicked. Route points for the connection
\r
124 * can be created by clicking around on non-terminal-occupied canvas space while
\r
128 * Connections can be started from and ended in flags by pressing ALT while
\r
131 * @author Tuukka Lehtonen
\r
133 public class ConnectTool2 extends AbstractMode {
\r
135 public static final int PAINT_PRIORITY = ElementPainter.ELEMENT_PAINT_PRIORITY + 5;
\r
138 protected RenderingQualityInteractor quality;
\r
141 protected TransformUtil util;
\r
144 protected ElementPainter diagramPainter;
\r
147 protected PointerInteractor pi;
\r
150 protected PickContext pickContext;
\r
153 * Start element terminal of the connection. <code>null</code> if connection
\r
154 * was started from a flag or a branch point.
\r
156 * The value is received by the constructor.
\r
158 protected List<TerminalInfo> startTerminals;
\r
161 * Refers to any of the possible overlapping start terminals. The value is
\r
162 * taken from the first index of {@link #startTerminals} assuming that the
\r
163 * first one is the nearest. It is <code>null</code> if
\r
164 * {@link #startTerminals} is empty.
\r
166 protected TerminalInfo startTerminal;
\r
168 protected TerminalInfo startFlag;
\r
171 * Starting position of the connection, received as an external argument.
\r
173 protected final Point2D startPos;
\r
176 * <code>true</code> if this tool should create connection continuation
\r
177 * flags, <code>false</code> otherwise.
\r
179 protected boolean createFlags;
\r
184 protected IElementClassProvider elementClassProvider;
\r
189 protected Deque<ControlPoint> controlPoints = new ArrayDeque<ControlPoint>();
\r
192 * Contains <code>null</code> when a connection is started from a new flag
\r
193 * or one of the terminals in {@link #startTerminals} when a connection is
\r
194 * being created starting from a terminal or possibly a set of terminals.
\r
197 * Note that this is different from {@link #startTerminal} which simply
\r
198 * represents the first element of {@link #startTerminals}.
\r
201 * Only when this value and {@link #endTerminal} is properly set will a
\r
202 * connection be created between two element terminals.
\r
204 protected TerminalInfo selectedStartTerminal;
\r
207 * Element terminal of connection end element. <code>null</code> if
\r
208 * connection cannot be ended where it is currently being attempted to end.
\r
210 protected TerminalInfo endTerminal;
\r
213 * The latest connectability judgment from the active
\r
214 * {@link IConnectionAdvisor} should the connection happen between
\r
215 * {@link #selectedStartTerminal} and {@link #endTerminal}.
\r
217 protected ConnectionJudgement connectionJudgment;
\r
220 * The latest connectability judgment from the active
\r
221 * {@link IConnectionAdvisor} should the connection happen between
\r
222 * {@link #selectedStartTerminal} and {@link #lastRouteGraphTarget}.
\r
224 protected ConnectionJudgement attachToConnectionJudgement;
\r
227 * If non-null during connection drawing this field tells the direction
\r
228 * forced for the current branch point by the user through the UI commands
\r
229 * {@link Commands#ROTATE_ELEMENT_CCW} and
\r
230 * {@link Commands#ROTATE_ELEMENT_CW}.
\r
232 private Direction forcedBranchPointDirection;
\r
235 * A temporary variable for use with
\r
236 * {@link TerminalTopology#getTerminals(IElement, Collection)}.
\r
238 protected Collection<Terminal> terminals = new ArrayList<Terminal>();
\r
241 * Previous mouse canvas position recorded by
\r
242 * {@link #processMouseMove(MouseMovedEvent)}.
\r
244 protected Point2D lastMouseCanvasPos = new Point2D.Double();
\r
247 * Set to true once {@link #processMouseMove(MouseMovedEvent)} has been
\r
248 * invoked at least once. This is used to tell whether to allow creation of
\r
249 * branch points or finising the connection in thin air. It will not be
\r
250 * allowed if the mouse has not moved at all since starting the connection.
\r
252 protected boolean mouseHasMoved = false;
\r
254 protected TerminalHoverStrategy originalStrategy = null;
\r
256 protected TerminalHoverStrategy terminalHoverStrategy = new TerminalHoverStrategy() {
\r
258 public boolean highlightEnabled() {
\r
259 return !isEndingInFlag();
\r
263 public boolean highlight(TerminalInfo ti) {
\r
264 boolean reflexive = isStartTerminal(ti.e, ti.t);
\r
265 if (reflexive && !allowReflexiveConnections())
\r
268 return canConnect(ti.e, ti.t) != null;
\r
272 protected final static Composite ALPHA_COMPOSITE = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f);
\r
275 * Root scene graph node for all visualization performed by this tool.
\r
277 protected G2DParentNode ghostNode;
\r
280 * Indicates whether the connection is about to be ended into a new
\r
281 * flag/branchpoint or not.
\r
283 protected TerminalInfo endFlag;
\r
285 protected G2DParentNode endFlagNode;
\r
287 private RouteGraphTarget lastRouteGraphTarget;
\r
290 * @param startTerminal
\r
292 * @param startCanvasPos
\r
294 public ConnectTool2(TerminalInfo startTerminal, int mouseId, Point2D startCanvasPos) {
\r
295 this(startTerminal == null ? Collections.<TerminalInfo> emptyList()
\r
296 : Collections.singletonList(startTerminal),
\r
302 * @param startTerminals
\r
304 * @param startCanvasPos
\r
306 public ConnectTool2(List<TerminalInfo> startTerminals, int mouseId, Point2D startCanvasPos) {
\r
309 if (startCanvasPos == null)
\r
310 throw new NullPointerException("null start position");
\r
311 if (startTerminals == null)
\r
312 throw new NullPointerException("null start terminals");
\r
314 this.startPos = startCanvasPos;
\r
315 this.lastMouseCanvasPos.setLocation(startPos);
\r
317 this.startTerminals = startTerminals;
\r
318 this.startTerminal = startTerminals.isEmpty() ? null : startTerminals.get(0);
\r
322 public void addedToContext(ICanvasContext ctx) {
\r
323 super.addedToContext(ctx);
\r
325 if (quality != null)
\r
326 quality.setStaticQuality(Quality.LOW);
\r
328 // Force terminals to always be highlighted without pressing certain
\r
329 // keys or key combinations.
\r
330 originalStrategy = getHint(TerminalPainter.TERMINAL_HOVER_STRATEGY);
\r
331 setHint(TerminalPainter.TERMINAL_HOVER_STRATEGY, terminalHoverStrategy);
\r
335 protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) {
\r
336 if (newDiagram != null) {
\r
337 // Get IElementClassProvider
\r
338 ISynchronizationContext ctx = newDiagram.getHint(SynchronizationHints.CONTEXT);
\r
340 this.elementClassProvider = ctx.get(SynchronizationHints.ELEMENT_CLASS_PROVIDER);
\r
343 // See if flags should be created or not.
\r
344 this.createFlags = Boolean.TRUE.equals(newDiagram.getHint(DiagramHints.KEY_USE_CONNECTION_FLAGS));
\r
350 public void removedFromContext(ICanvasContext ctx) {
\r
351 if (getHint(TerminalPainter.TERMINAL_HOVER_STRATEGY) == terminalHoverStrategy) {
\r
352 if (originalStrategy != null)
\r
353 setHint(TerminalPainter.TERMINAL_HOVER_STRATEGY, originalStrategy);
\r
355 removeHint(TerminalPainter.TERMINAL_HOVER_STRATEGY);
\r
358 if (quality != null)
\r
359 quality.setStaticQuality(null);
\r
361 super.removedFromContext(ctx);
\r
364 protected void startConnection() {
\r
365 Point2D startPos = (Point2D) this.startPos.clone();
\r
366 ISnapAdvisor snapAdvisor = getHint(DiagramHints.SNAP_ADVISOR);
\r
367 if (snapAdvisor != null)
\r
368 snapAdvisor.snap(startPos);
\r
370 // Resolve the first element and terminal of the connection.
\r
371 ControlPoint start = new ControlPoint(startPos);
\r
373 if (startTerminal != null) {
\r
374 assert ElementUtils.peekDiagram(startTerminal.e) == diagram;
\r
375 Point2D terminalPos = new Point2D.Double(startTerminal.posDia.getTranslateX(),
\r
376 startTerminal.posDia.getTranslateY());
\r
377 start.setPosition(terminalPos).setAttachedToTerminal(startTerminal);
\r
379 // Create TerminalInfo describing the flag to be created.
\r
381 // This prevents connection creation from creating a branch
\r
382 // point in place of this flag.
\r
383 startFlag = createFlag(EdgeEnd.Begin);
\r
384 start.setAttachedToTerminal(startFlag);
\r
385 showElement(ghostNode, "startFlag", startFlag.e, startPos);
\r
388 controlPoints.add(start);
\r
389 controlPoints.add(new ControlPoint(startPos));
\r
391 // Make sure that we are ending with a flag if ALT is pressed.
\r
392 // This makes the tool always start with a flag which can be quite
\r
393 // cumbersome and is therefore disabled. The current version will not
\r
394 // end the connection if the mouse has not moved at all.
\r
395 //if (keyUtil.isKeyPressed(java.awt.event.KeyEvent.VK_ALT)) {
\r
396 // endWithoutTerminal(lastMouseCanvasPos, true);
\r
401 public void initSG(G2DParentNode parent) {
\r
402 ghostNode = parent.addNode(G2DParentNode.class);
\r
403 ghostNode.setZIndex(PAINT_PRIORITY);
\r
405 ShapeNode pathNode = ghostNode.getOrCreateNode("path", ShapeNode.class);
\r
406 pathNode.setColor(new Color(160, 0, 0));
\r
407 pathNode.setStroke(new BasicStroke(0.1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10,
\r
408 new float[] { 0.5f, 0.2f }, 0));
\r
409 pathNode.setScaleStroke(false);
\r
410 pathNode.setZIndex(0);
\r
412 G2DParentNode points = ghostNode.getOrCreateNode("points", G2DParentNode.class);
\r
413 points.setZIndex(1);
\r
418 private RouteTerminal addControlPoint(RouteGraph routeGraph, ControlPoint cp) {
\r
419 TerminalInfo ti = cp.getAttachedTerminal();
\r
420 if(ti != null && ti != startFlag && ti != endFlag) {
\r
421 Rectangle2D bounds = ElementUtils.getElementBoundsOnDiagram(ti.e, new Rectangle2D.Double());
\r
422 GeometryUtils.expandRectangle(bounds, 2);
\r
423 int allowedDirections = RouteGraphConnectionClass.shortestDirectionOutOfBounds(
\r
424 ti.posDia.getTranslateX(), ti.posDia.getTranslateY(), bounds);
\r
425 return routeGraph.addTerminal(ti.posDia.getTranslateX(), ti.posDia.getTranslateY(),
\r
426 bounds, allowedDirections, PlainLineEndStyle.INSTANCE);
\r
429 double x = cp.getPosition().getX();
\r
430 double y = cp.getPosition().getY();
\r
431 int allowedDirections = 0xf;
\r
432 switch(cp.getDirection()) {
\r
433 case Horizontal: allowedDirections = 5; break;
\r
434 case Vertical: allowedDirections = 10; break;
\r
435 case Any: allowedDirections = 15; break;
\r
437 return routeGraph.addTerminal(x, y, x, y, x, y, allowedDirections);
\r
441 protected void updateSG() {
\r
442 if (controlPoints.size() != 2)
\r
445 ControlPoint begin = controlPoints.getFirst();
\r
446 ControlPoint end = controlPoints.getLast();
\r
448 RouteGraph routeGraph = new RouteGraph();
\r
449 RouteTerminal a = addControlPoint(routeGraph, begin);
\r
450 RouteTerminal b = addControlPoint(routeGraph, end);
\r
451 routeGraph.link(a, b);
\r
453 Path2D path = routeGraph.getPath2D();
\r
455 // Create scene graph to visualize the connection.
\r
456 ShapeNode pathNode = ghostNode.getOrCreateNode("path", ShapeNode.class);
\r
457 pathNode.setShape(path);
\r
462 private G2DParentNode showElement(G2DParentNode parent, String nodeId, IElement element, Point2D pos) {
\r
463 return showElement(parent, nodeId, element, AffineTransform.getTranslateInstance(pos.getX(), pos.getY()));
\r
466 private G2DParentNode showElement(G2DParentNode parent, String nodeId, IElement element, AffineTransform tr) {
\r
467 G2DParentNode elementParent = parent.getOrCreateNode(nodeId, G2DParentNode.class);
\r
468 elementParent.setTransform(tr);
\r
469 elementParent.removeNodes();
\r
470 for (SceneGraph sg : element.getElementClass().getItemsByClass(SceneGraph.class))
\r
471 sg.init(element, elementParent);
\r
472 return elementParent;
\r
476 public void cleanupSG() {
\r
477 ghostNode.remove();
\r
481 @EventHandler(priority = 200)
\r
482 public boolean handleCommandEvents(CommandEvent ce) {
\r
483 if (ce.command.equals(Commands.CANCEL)) {
\r
487 } else if (ce.command.equals(Commands.ROTATE_ELEMENT_CCW) || ce.command.equals(Commands.ROTATE_ELEMENT_CW)) {
\r
488 return rotateLastBranchPoint(ce.command.equals(Commands.ROTATE_ELEMENT_CW));
\r
493 @EventHandler(priority = PointerInteractor.TOOL_PRIORITY + 20)
\r
494 public boolean handleKeyEvents(KeyEvent ke) {
\r
495 if (ke instanceof KeyPressedEvent) {
\r
496 // Back-space, cancel prev bend
\r
497 if (ke.keyCode == java.awt.event.KeyEvent.VK_BACK_SPACE)
\r
498 return cancelPreviousBend();
\r
501 if (ke.keyCode == java.awt.event.KeyEvent.VK_ALT) {
\r
503 endWithoutTerminal(lastMouseCanvasPos, shouldEndWithFlag(ke instanceof KeyPressedEvent));
\r
511 @EventHandler(priority = PointerInteractor.TOOL_PRIORITY + 20)
\r
512 public boolean handleEvent(MouseEvent me) {
\r
513 // Only handle events for the connection-initiating mouse
\r
514 if (me.mouseId != mouseId)
\r
517 if (me instanceof MouseMovedEvent)
\r
518 return processMouseMove((MouseMovedEvent) me);
\r
520 if (me instanceof MouseButtonPressedEvent)
\r
521 return processMouseButtonPress((MouseButtonPressedEvent) me);
\r
526 protected boolean processMouseMove(MouseMovedEvent me) {
\r
527 mouseHasMoved = true;
\r
529 Point2D mouseControlPos = me.controlPosition;
\r
530 Point2D mouseCanvasPos = util.controlToCanvas(mouseControlPos, new Point2D.Double());
\r
532 ISnapAdvisor snapAdvisor = getHint(DiagramHints.SNAP_ADVISOR);
\r
533 if (snapAdvisor != null)
\r
534 snapAdvisor.snap(mouseCanvasPos);
\r
536 // Record last snapped canvas position of mouse.
\r
537 this.lastMouseCanvasPos.setLocation(mouseCanvasPos);
\r
539 if (isEndingInFlag()) {
\r
540 endFlagNode.setTransform(AffineTransform.getTranslateInstance(mouseCanvasPos.getX(), mouseCanvasPos.getY()));
\r
543 List<TerminalInfo> tis = pi.pickTerminals(me.controlPosition);
\r
544 tis = TerminalUtil.findNearestOverlappingTerminals(tis);
\r
545 if (!tis.isEmpty() && !containsStartTerminal(tis)) {
\r
546 //System.out.println("end terminals (" + tis.size() + "):\n" + EString.implode(tis));
\r
547 for (TerminalInfo ti : tis) {
\r
548 Pair<ConnectionJudgement, TerminalInfo> canConnect = canConnect(ti.e, ti.t);
\r
549 if (canConnect != null) {
\r
550 connectionJudgment = canConnect.first;
\r
552 if (!isEndingInFlag() || !TerminalUtil.isSameTerminal(ti, endTerminal)) {
\r
553 if (canConnect.second != null) {
\r
554 controlPoints.getFirst()
\r
555 .setPosition(canConnect.second.posDia)
\r
556 .setAttachedToTerminal(canConnect.second);
\r
558 controlPoints.getLast()
\r
559 .setPosition(ti.posDia)
\r
560 .setAttachedToTerminal(ti);
\r
562 selectedStartTerminal = canConnect.second;
\r
566 // Make sure that we are ending with a flag if ALT is pressed
\r
567 // and no end terminal is defined.
\r
568 if (!endWithoutTerminal(lastMouseCanvasPos, shouldEndWithFlag(me)))
\r
574 RouteGraphTarget cp = RouteGraphConnectTool.pickRouteGraphConnection(
\r
576 pi.getCanvasPickShape(me.controlPosition),
\r
577 pi.getPickDistance());
\r
579 // Remove branch point highlight from previously picked route graph.
\r
580 if (lastRouteGraphTarget != null && cp.getNode() != lastRouteGraphTarget.getNode())
\r
581 cp.getNode().showBranchPoint(null);
\r
582 lastRouteGraphTarget = cp;
\r
584 // Validate connection before visualizing connectability
\r
585 Point2D isectPos = cp.getIntersectionPosition();
\r
586 TerminalInfo ti = TerminalInfo.create(
\r
589 BranchPointTerminal.existingTerminal(
\r
592 BranchPointNode.SHAPE),
\r
593 BranchPointNode.SHAPE);
\r
594 Pair<ConnectionJudgement, TerminalInfo> canConnect = canConnect(ti.e, ti.t);
\r
595 if (canConnect != null) {
\r
596 attachToConnectionJudgement = canConnect.first;
\r
597 controlPoints.getLast().setPosition(ti.posDia).setAttachedToTerminal(ti);
\r
599 cp.getNode().showBranchPoint(isectPos);
\r
600 if (!endWithoutTerminal(lastMouseCanvasPos, shouldEndWithFlag(me)))
\r
605 if (lastRouteGraphTarget != null) {
\r
606 lastRouteGraphTarget.getNode().showBranchPoint(null);
\r
607 lastRouteGraphTarget = null;
\r
612 connectionJudgment = null;
\r
613 attachToConnectionJudgement = null;
\r
614 if (isEndTerminalDefined()) {
\r
615 // CASE: Mouse was previously on top of a valid terminal to end
\r
616 // the connection. Now the mouse has been moved where there is
\r
617 // no longer a terminal to connect to.
\r
619 // => Disconnect the last edge segment from the previous
\r
620 // terminal, mark endElement/endTerminal non-existent
\r
621 // and connect the disconnected edge to a new branch point.
\r
623 controlPoints.getLast()
\r
624 .setPosition(mouseCanvasPos)
\r
625 .setDirection(calculateCurrentBranchPointDirection())
\r
626 .setAttachedToTerminal(null);
\r
628 endTerminal = null;
\r
630 // CASE: Mouse was not previously on top of a valid ending
\r
631 // element terminal.
\r
633 // => Move and re-orient last branch point.
\r
635 controlPoints.getLast()
\r
636 .setPosition(mouseCanvasPos)
\r
637 .setDirection(calculateCurrentBranchPointDirection());
\r
640 // Make sure that we are ending with a flag if ALT is pressed and no end
\r
641 // terminal is defined.
\r
642 if (!endWithoutTerminal(lastMouseCanvasPos, shouldEndWithFlag(me)))
\r
648 protected boolean processMouseButtonPress(MouseButtonPressedEvent e) {
\r
649 MouseButtonEvent me = e;
\r
651 // Do nothing before the mouse has moved at least a little.
\r
652 // This prevents the user from ending the connection right where
\r
654 if (!mouseHasMoved)
\r
657 if (me.button == MouseEvent.LEFT_BUTTON) {
\r
658 Point2D mouseControlPos = me.controlPosition;
\r
659 Point2D mouseCanvasPos = util.getInverseTransform().transform(mouseControlPos, new Point2D.Double());
\r
661 ISnapAdvisor snapAdvisor = getHint(DiagramHints.SNAP_ADVISOR);
\r
662 if (snapAdvisor != null)
\r
663 snapAdvisor.snap(mouseCanvasPos);
\r
665 if (isEndTerminalDefined() && connectionJudgment != null) {
\r
666 // Clicked on an allowed end terminal. End connection & end mode.
\r
667 createConnection();
\r
670 } else if (lastRouteGraphTarget != null && attachToConnectionJudgement != null) {
\r
671 lastRouteGraphTarget.getNode().showBranchPoint(null);
\r
672 attachToConnection();
\r
676 // Finish connection in thin air only if the
\r
677 // connection was started from a valid terminal.
\r
678 if (me.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK) && !startTerminals.isEmpty()) {
\r
679 Pair<ConnectionJudgement, TerminalInfo> pair = canConnect(null, null);
\r
680 if (pair != null) {
\r
681 connectionJudgment = (ConnectionJudgement) pair.first;
\r
682 selectedStartTerminal = pair.second;
\r
683 // endFlag = createFlag(EdgeEnd.End);
\r
684 // controlPoints.getLast().setAttachedToTerminal(endFlag);
\r
685 createConnection();
\r
689 // Inform the user why connection couldn't be created.
\r
690 String tmsg = terminalsToString(startTerminals);
\r
691 ErrorLogger.defaultLogWarning("Can't resolve connection type for new connection when starting from one of the following terminals:\n" + tmsg, null);
\r
694 } else if (routePointsAllowed()
\r
695 && (me.stateMask & (MouseEvent.ALT_MASK | MouseEvent.SHIFT_MASK | MouseEvent.CTRL_MASK)) == 0) {
\r
696 // Add new connection control point.
\r
697 controlPoints.add(newControlPointWithCalculatedDirection(mouseCanvasPos));
\r
698 resetForcedBranchPointDirection();
\r
703 // Eat the event to prevent other participants from doing
\r
704 // incompatible things while in this connection mode.
\r
706 } else if (me.button == MouseEvent.RIGHT_BUTTON) {
\r
707 return cancelPreviousBend();
\r
713 private void attachToConnection() {
\r
714 ConnectionJudgement judgment = this.attachToConnectionJudgement;
\r
715 if (judgment == null) {
\r
716 ErrorLogger.defaultLogError("Cannot attach to connection, no judgment available on connection validity", null);
\r
720 ConnectionBuilder builder = new ConnectionBuilder(this.diagram);
\r
721 RouteGraph before = lastRouteGraphTarget.getNode().getRouteGraph();
\r
722 THashMap<Object, Object> copyMap = new THashMap<>();
\r
723 RouteGraph after = before.copy(copyMap);
\r
725 RouteLine attachTo = (RouteLine) copyMap.get(lastRouteGraphTarget.getLine());
\r
726 after.makePersistent(attachTo);
\r
727 for (RouteLine line : after.getAllLines()) {
\r
728 if (!line.isTransient() && line.isHorizontal() == attachTo.isHorizontal()
\r
729 && line.getPosition() == attachTo.getPosition()) {
\r
734 RouteLine attachToLine = attachTo;
\r
735 RouteGraphDelta delta = new RouteGraphDelta(before, after);
\r
737 Simantics.getSession().asyncRequest(new WriteRequest() {
\r
739 public void perform(WriteGraph graph) throws DatabaseException {
\r
740 graph.markUndoPoint();
\r
741 Resource connection = ElementUtils.getObject(endTerminal.e);
\r
742 if (!delta.isEmpty()) {
\r
743 new RouteGraphConnection(graph, connection).synchronize(graph, before, after, delta);
\r
745 Resource line = RouteGraphConnection.deserialize(graph, attachToLine.getData());
\r
746 Deque<ControlPoint> cps = new ArrayDeque<>();
\r
747 for (Iterator<ControlPoint> iterator = controlPoints.descendingIterator(); iterator.hasNext();)
\r
748 cps.add(iterator.next());
\r
749 builder.attachToRouteGraph(graph, judgment, connection, line, cps, startTerminal, FlagClass.Type.In);
\r
752 if (parameter != null)
\r
753 ExceptionUtils.logAndShowError(parameter);
\r
757 protected boolean cancelPreviousBend() {
\r
758 if (!routePointsAllowed())
\r
761 // Just to make this code more comprehensible, prevent an editing
\r
762 // case that requires ugly code to work.
\r
763 if (isEndingInFlag())
\r
766 // If there are no real route points, cancel whole connection.
\r
767 if (controlPoints.size() <= 2) {
\r
773 // Cancel last bend
\r
774 controlPoints.removeLast();
\r
775 controlPoints.getLast().setPosition(lastMouseCanvasPos);
\r
776 resetForcedBranchPointDirection();
\r
783 * Rotates the last branch point in the created connection in either
\r
784 * clockwise or counter-clockwise direction as a response to a user
\r
788 * At the same time it use {@link #forcedBranchPointDirection} to mark the
\r
789 * current last branch point to be forcefully oriented according to the
\r
790 * users wishes instead of calculating a default value for the orientation
\r
791 * from the routed connection path. See
\r
792 * {@link #calculateCurrentBranchPointDirection()} for more information on
\r
796 * The logic of this method goes as follows:
\r
798 * <li>Calculate the current branch point direction</li>
\r
799 * <li>If the branch point direction is currently user selected (
\r
800 * {@link #forcedBranchPointDirection}</li>
\r
806 * @return <code>true</code> if the rotation was successful
\r
808 protected boolean rotateLastBranchPoint(boolean clockwise) {
\r
809 Direction oldDir = calculateCurrentBranchPointDirection();
\r
811 if (forcedBranchPointDirection == null) {
\r
812 forcedBranchPointDirection = oldDir.toggleDetermined();
\r
814 forcedBranchPointDirection = clockwise ? oldDir.cycleNext() : oldDir.cyclePrevious();
\r
817 controlPoints.getLast().setDirection(forcedBranchPointDirection);
\r
825 * Set preferred direction for a branch/route point element.
\r
827 * @param branchPoint the element to set the direction for
\r
828 * @param direction the direction to set
\r
831 protected void setDirection(IElement branchPoint, Direction direction) {
\r
832 branchPoint.getElementClass().getSingleItem(BranchPoint.class).setDirectionPreference(branchPoint, direction);
\r
835 protected Direction forcedBranchPointDirection() {
\r
836 return forcedBranchPointDirection;
\r
839 protected void resetForcedBranchPointDirection() {
\r
840 forcedBranchPointDirection = null;
\r
843 protected void forceBranchPointDirection(Direction direction) {
\r
844 forcedBranchPointDirection = direction;
\r
850 protected Direction calculateCurrentBranchPointDirection() {
\r
851 // If this is not the first branch point, toggle direction compared to
\r
853 if (forcedBranchPointDirection != null)
\r
854 return forcedBranchPointDirection;
\r
856 if (controlPoints.size() > 2) {
\r
857 // This is not the first edge segment, toggle route point
\r
859 Iterator<ControlPoint> it = controlPoints.descendingIterator();
\r
861 ControlPoint secondLastCp = it.next();
\r
863 Direction dir = secondLastCp.getDirection();
\r
866 return Direction.Vertical;
\r
868 return Direction.Horizontal;
\r
873 // If this is the first branch point, calculate based on edge segment
\r
875 if (controlPoints.size() > 1) {
\r
876 Iterator<ControlPoint> it = controlPoints.descendingIterator();
\r
877 ControlPoint last = it.next();
\r
878 ControlPoint secondLast = it.next();
\r
880 double angle = Math.atan2(Math.abs(last.getPosition().getY() - secondLast.getPosition().getY()),
\r
881 Math.abs(last.getPosition().getX() - secondLast.getPosition().getX()));
\r
883 if (angle >= 0 && angle < Math.PI / 4) {
\r
884 return Direction.Horizontal;
\r
885 } else if (angle > Math.PI / 4 && angle <= Math.PI / 2) {
\r
886 return Direction.Vertical;
\r
890 return Direction.Any;
\r
893 protected boolean isEndingInFlag() {
\r
894 return endFlag != null;
\r
900 * @return <code>true</code> if updateSG was executed, <code>false</code>
\r
903 protected boolean endWithoutTerminal(Point2D mousePos, boolean altDown) {
\r
904 // Just go with branch points if flags are not allowed.
\r
908 boolean endTerminalDefined = isEndTerminalDefined();
\r
911 if (!isEndingInFlag()) {
\r
912 endFlag = createFlag(EdgeEnd.End);
\r
913 endFlagNode = showElement(ghostNode, "endFlag", endFlag.e, mousePos);
\r
914 controlPoints.getLast()
\r
915 .setDirection(calculateCurrentBranchPointDirection())
\r
916 .setAttachedToTerminal(endFlag);
\r
918 // TerminalPainter must refresh
\r
919 setHint(TerminalPainter.TERMINAL_HOVER_STRATEGY, terminalHoverStrategy);
\r
925 if (isEndingInFlag()) {
\r
926 // Currently ending with flag but ALT is no longer down
\r
927 // so that flag must be removed.
\r
929 endFlagNode.remove();
\r
930 endFlagNode = null;
\r
932 ControlPoint cp = controlPoints.getLast();
\r
933 cp.setDirection(calculateCurrentBranchPointDirection())
\r
934 .setAttachedToTerminal(endTerminal);
\r
936 if (endTerminalDefined) {
\r
937 cp.setPosition(endTerminal.posDia);
\r
939 cp.setPosition(mousePos);
\r
942 // Force TerminalPainter refresh
\r
943 setHint(TerminalPainter.TERMINAL_HOVER_STRATEGY, terminalHoverStrategy);
\r
952 protected void createConnection() {
\r
954 this.selectedStartTerminal,
\r
956 this.connectionJudgment,
\r
957 this.controlPoints);
\r
960 protected void createConnection(
\r
961 final TerminalInfo startTerminal,
\r
962 final TerminalInfo endTerminal,
\r
963 final ConnectionJudgement judgement,
\r
964 final Deque<ControlPoint> controlPoints)
\r
966 TimeLogger.resetTimeAndLog(getClass(), "createConnection");
\r
967 if (judgement == null) {
\r
968 // Inform the user why connection couldn't be created.
\r
969 String tmsg = terminalsToString(Arrays.asList(startTerminal, endTerminal));
\r
970 ErrorLogger.defaultLogError("Cannot create connection, no judgment available on connection validity when connecting the terminals:\n" + tmsg, null);
\r
974 final ConnectionBuilder builder = new ConnectionBuilder(this.diagram);
\r
976 Simantics.getSession().asyncRequest(new WriteRequest() {
\r
978 public void perform(WriteGraph graph) throws DatabaseException {
\r
979 builder.create(graph, judgement, controlPoints, startTerminal, endTerminal);
\r
982 if (parameter != null)
\r
983 ExceptionUtils.logAndShowError(parameter);
\r
991 protected ControlPoint newControlPointWithCalculatedDirection(Point2D canvasPos) {
\r
992 return new ControlPoint(canvasPos, calculateCurrentBranchPointDirection());
\r
998 * @return <code>true</code> if the specified element terminal matches any
\r
999 * TerminalInfo in {@link #startTerminals}
\r
1001 protected boolean isStartTerminal(IElement e, Terminal t) {
\r
1002 if (startTerminal == null)
\r
1004 for (TerminalInfo st : startTerminals) {
\r
1005 if (st.e == e && st.t == t) {
\r
1015 * @return <code>true</code> if the specified element terminal matches any
\r
1016 * TerminalInfo in {@link #startTerminals}
\r
1018 protected boolean containsStartTerminal(List<TerminalInfo> tis) {
\r
1019 if (startTerminal == null)
\r
1021 for (TerminalInfo st : startTerminals) {
\r
1022 for (TerminalInfo et : tis) {
\r
1023 if (st.e == et.e && st.t == et.t) {
\r
1031 protected static FlagClass.Type endToFlagType(EdgeEnd end) {
\r
1034 return FlagClass.Type.In;
\r
1036 return FlagClass.Type.Out;
\r
1038 throw new IllegalArgumentException("unrecognized edge end: " + end);
\r
1042 protected TerminalInfo createFlag(EdgeEnd connectionEnd) {
\r
1043 ElementClass flagClass = elementClassProvider.get(ElementClasses.FLAG);
\r
1044 IElement e = Element.spawnNew(flagClass);
\r
1046 e.setHint(FlagClass.KEY_FLAG_TYPE, endToFlagType(connectionEnd));
\r
1047 e.setHint(FlagClass.KEY_FLAG_MODE, FlagClass.Mode.Internal);
\r
1049 TerminalInfo ti = new TerminalInfo();
\r
1051 ti.t = ElementUtils.getSingleTerminal(e);
\r
1052 ti.posElem = TerminalUtil.getTerminalPosOnElement(e, ti.t);
\r
1053 ti.posDia = TerminalUtil.getTerminalPosOnDiagram(e, ti.t);
\r
1058 protected boolean shouldEndWithFlag(MouseEvent me) {
\r
1059 return shouldEndWithFlag( me.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK) );
\r
1062 protected boolean shouldEndWithFlag(boolean altPressed) {
\r
1063 return altPressed && !isEndTerminalDefined() && createFlags && startFlag == null;
\r
1066 protected boolean isEndTerminalDefined() {
\r
1067 return endTerminal != null;
\r
1070 protected boolean isFlagTerminal(TerminalInfo ti) {
\r
1071 return ti.e.getElementClass().containsClass(FlagHandler.class);
\r
1074 protected boolean allowReflexiveConnections() {
\r
1078 protected boolean routePointsAllowed() {
\r
1079 return Boolean.TRUE.equals(diagram.getHint(DiagramHints.KEY_ALLOW_ROUTE_POINTS));
\r
1083 * @param endElement
\r
1084 * @param endTerminal
\r
1087 @SuppressWarnings("unchecked")
\r
1088 protected final Pair<ConnectionJudgement, TerminalInfo> canConnect(IElement endElement, Terminal endTerminal) {
\r
1089 IConnectionAdvisor advisor = diagram.getHint(DiagramHints.CONNECTION_ADVISOR);
\r
1090 Object judgement = canConnect(advisor, endElement, endTerminal);
\r
1091 if (judgement == null)
\r
1093 if (judgement instanceof Pair<?, ?>)
\r
1094 return (Pair<ConnectionJudgement, TerminalInfo>) judgement;
\r
1095 return Pair.<ConnectionJudgement, TerminalInfo>make((ConnectionJudgement) judgement, startTerminal);
\r
1098 protected Object canConnect(IConnectionAdvisor advisor, IElement endElement, Terminal endTerminal) {
\r
1099 if (advisor == null)
\r
1100 return Pair.make(ConnectionJudgement.CANBEMADELEGAL, startTerminal);
\r
1101 if (startTerminals.isEmpty()) {
\r
1102 ConnectionJudgement obj = (ConnectionJudgement) advisor.canBeConnected(null, null, null, endElement, endTerminal);
\r
1103 return obj != null ? Pair.<ConnectionJudgement, TerminalInfo>make(obj, null) : null;
\r
1105 for (TerminalInfo st : startTerminals) {
\r
1106 ConnectionJudgement obj = (ConnectionJudgement) advisor.canBeConnected(null, st.e, st.t, endElement, endTerminal);
\r
1107 if (obj != null) {
\r
1108 return Pair.make(obj, st);
\r
1115 * For generating debugging information of what was attempted by the user
\r
1116 * when a connection couldn't be created.
\r
1121 private String terminalsToString(final Iterable<TerminalInfo> ts) {
\r
1123 return Simantics.sync(new UniqueRead<String>() {
\r
1125 public String perform(ReadGraph graph) throws DatabaseException {
\r
1126 DiagramResource DIA = DiagramResource.getInstance(graph);
\r
1127 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
1128 StringBuilder sb = new StringBuilder();
\r
1129 boolean first = true;
\r
1130 for (TerminalInfo ti : ts) {
\r
1134 sb.append("element ");
\r
1135 Object o = ElementUtils.getObject(ti.e);
\r
1136 if (o instanceof Resource) {
\r
1137 Resource er = (Resource) o;
\r
1138 Resource cer = graph.getPossibleObject(er, MOD.ElementToComponent);
\r
1139 Resource r = cer != null ? cer : er;
\r
1140 sb.append(NameUtils.getSafeName(graph, r)).append(" : ");
\r
1141 for (Resource type : graph.getPrincipalTypes(r)) {
\r
1142 sb.append(NameUtils.getSafeName(graph, type, true));
\r
1145 sb.append(ti.e.toString());
\r
1147 sb.append(", terminal ");
\r
1148 if (ti.t instanceof ResourceTerminal) {
\r
1149 Resource tr = ((ResourceTerminal) ti.t).getResource();
\r
1150 Resource cp = graph.getPossibleObject(tr, DIA.HasConnectionPoint);
\r
1151 Resource r = cp != null ? cp : tr;
\r
1152 sb.append(NameUtils.getSafeName(graph, r, true));
\r
1154 sb.append(ti.t.toString());
\r
1157 return sb.toString();
\r
1160 } catch (DatabaseException e) {
\r
1161 return e.getMessage();
\r