1 /*******************************************************************************
\r
2 * Copyright (c) 2011 Association for Decentralized Information Management in
\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.scenegraph.g2d.nodes.connection;
\r
14 import java.awt.BasicStroke;
\r
15 import java.awt.Color;
\r
16 import java.awt.Graphics2D;
\r
17 import java.awt.RenderingHints;
\r
18 import java.awt.Shape;
\r
19 import java.awt.Stroke;
\r
20 import java.awt.event.KeyEvent;
\r
21 import java.awt.geom.AffineTransform;
\r
22 import java.awt.geom.Path2D;
\r
23 import java.awt.geom.Point2D;
\r
24 import java.awt.geom.Rectangle2D;
\r
25 import java.lang.reflect.Constructor;
\r
26 import java.util.Collection;
\r
27 import java.util.Map;
\r
29 import org.simantics.diagram.connection.RouteGraph;
\r
30 import org.simantics.diagram.connection.RouteLine;
\r
31 import org.simantics.diagram.connection.RouteLink;
\r
32 import org.simantics.diagram.connection.RouteTerminal;
\r
33 import org.simantics.diagram.connection.actions.IAction;
\r
34 import org.simantics.diagram.connection.actions.IReconnectAction;
\r
35 import org.simantics.diagram.connection.actions.MoveAction;
\r
36 import org.simantics.diagram.connection.actions.ReconnectLineAction;
\r
37 import org.simantics.diagram.connection.delta.RouteGraphDelta;
\r
38 import org.simantics.diagram.connection.rendering.BasicConnectionStyle;
\r
39 import org.simantics.diagram.connection.rendering.ConnectionStyle;
\r
40 import org.simantics.diagram.connection.rendering.IRouteGraphRenderer;
\r
41 import org.simantics.diagram.connection.rendering.StyledRouteGraphRenderer;
\r
42 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;
\r
43 import org.simantics.diagram.connection.splitting.SplittedRouteGraph;
\r
44 import org.simantics.scenegraph.INode;
\r
45 import org.simantics.scenegraph.ISelectionPainterNode;
\r
46 import org.simantics.scenegraph.g2d.G2DNode;
\r
47 import org.simantics.scenegraph.g2d.G2DParentNode;
\r
48 import org.simantics.scenegraph.g2d.IG2DNode;
\r
49 import org.simantics.scenegraph.g2d.events.EventTypes;
\r
50 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
\r
51 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyReleasedEvent;
\r
52 import org.simantics.scenegraph.g2d.events.MouseEvent;
\r
53 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;
\r
54 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent;
\r
55 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent;
\r
56 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDragBegin;
\r
57 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
\r
58 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
\r
59 import org.simantics.scenegraph.g2d.events.command.Commands;
\r
60 import org.simantics.scenegraph.g2d.nodes.GridNode;
\r
61 import org.simantics.scenegraph.g2d.nodes.LinkNode;
\r
62 import org.simantics.scenegraph.g2d.nodes.connection.HighlightActionPointsAction.Action;
\r
63 import org.simantics.scenegraph.g2d.nodes.connection.HighlightActionPointsAction.Pick;
\r
64 import org.simantics.scenegraph.g2d.snap.ISnapAdvisor;
\r
65 import org.simantics.scenegraph.utils.GeometryUtils;
\r
66 import org.simantics.scenegraph.utils.InitValueSupport;
\r
67 import org.simantics.scenegraph.utils.NodeUtil;
\r
69 import gnu.trove.map.hash.THashMap;
\r
72 * @author Tuukka Lehtonen
\r
74 public class RouteGraphNode extends G2DNode implements ISelectionPainterNode, InitValueSupport {
\r
76 private static final long serialVersionUID = -917194130412280965L;
\r
78 private static final double TOLERANCE = IAction.TOLERANCE;
\r
79 private static final Stroke SELECTION_STROKE = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
\r
80 private static final Color SELECTION_COLOR = new Color(255, 0, 255, 96);
\r
82 private static final HighlightActionPointsAction highlightActions = new HighlightActionPointsAction(null);
\r
84 protected RouteGraph rg;
\r
85 protected IRouteGraphRenderer baseRenderer;
\r
86 protected IRouteGraphRenderer renderer;
\r
87 protected double pickTolerance = TOLERANCE;
\r
88 protected boolean editable = true;
\r
89 private boolean branchable = true;
\r
91 protected IRouteGraphListener rgListener;
\r
92 protected RouteGraphDelta rgDelta;
\r
94 protected transient double mouseX;
\r
95 protected transient double mouseY;
\r
96 protected transient Point2D pt = new Point2D.Double();
\r
98 protected transient MoveAction dragAction;
\r
99 protected transient IAction currentAction;
\r
100 protected transient Rectangle2D bounds;
\r
103 * Dynamic color for connection rendering.
\r
105 protected transient Color dynamicColor;
\r
108 * Dynamic stroke for connection rendering.
\r
110 protected transient Stroke dynamicStroke;
\r
112 protected transient Path2D selectionPath = new Path2D.Double();
\r
113 protected transient Stroke selectionStroke = null;
\r
115 protected transient boolean highlightActionsEnabled = false;
\r
116 protected transient AffineTransform lastViewTransform = null;
\r
119 * x = NaN is used to indicate that possible branch point should not be
\r
120 * rendered but interaction has not ended yet.
\r
122 protected transient Point2D newBranchPointPosition = null;
\r
125 protected transient Map<Object,ILineEndStyle> dynamicStyles = null;
\r
128 public void initValues() {
\r
129 dynamicColor = null;
\r
133 @PropertySetter("color")
\r
134 @SyncField(value = {"dynamicColor"})
\r
135 public void setDynamicColor(Color color) {
\r
136 this.dynamicColor = color;
\r
140 @PropertySetter("width")
\r
141 @SyncField("dynamicStroke")
\r
142 public void setDynamicStroke(Stroke stroke) {
\r
143 this.dynamicStroke = stroke;
\r
145 createSelectionStroke();
\r
148 @SyncField(value = {"dynamicStyles"})
\r
149 public void setDynamicLineEnd(RouteTerminal terminal, ILineEndStyle style) {
\r
150 if (dynamicStyles == null)
\r
151 dynamicStyles = new THashMap<Object, ILineEndStyle>();
\r
152 terminal.setDynamicStyle(style);
\r
153 if (terminal.getData() != null) {
\r
155 dynamicStyles.put(terminal.getData(),style);
\r
157 dynamicStyles.remove(terminal.getData());
\r
161 private void updateLineEnds() {
\r
162 if (dynamicStyles == null)
\r
164 for (RouteTerminal t : rg.getTerminals()) {
\r
165 if (t.getData() == null)
\r
167 ILineEndStyle dynamicStyle = dynamicStyles.get(t.getData());
\r
168 if (dynamicStyle != null)
\r
169 t.setDynamicStyle(dynamicStyle);
\r
173 @SyncField(value = {"rg"})
\r
174 public void setRouteGraph(RouteGraph graph) {
\r
180 @SyncField(value = {"rgDelta"})
\r
181 public void setRouteGraphDelta(RouteGraphDelta delta) {
\r
182 this.rgDelta = delta;
\r
185 @SyncField(value = {"renderer"})
\r
186 public void setRenderer(IRouteGraphRenderer renderer) {
\r
188 this.baseRenderer = renderer;
\r
191 createSelectionStroke();
\r
194 private void createSelectionStroke() {
\r
195 BasicConnectionStyle style = tryGetStyle();
\r
196 selectionStroke = null;
\r
197 if (style != null) {
\r
198 BasicStroke stroke = (BasicStroke) style.getLineStroke();
\r
199 if (stroke != null) {
\r
200 float width = Math.max(stroke.getLineWidth() + 0.75f, stroke.getLineWidth()*1.3f);
\r
201 selectionStroke = new BasicStroke(width, BasicStroke.CAP_BUTT, stroke.getLineJoin());
\r
204 selectionStroke = SELECTION_STROKE;
\r
208 private void wrapRenderer() {
\r
210 if(baseRenderer == null) {
\r
215 if(dynamicColor != null || dynamicStroke != null) {
\r
216 BasicConnectionStyle baseStyle = (BasicConnectionStyle)tryGetStyle(baseRenderer);
\r
218 Constructor<? extends BasicConnectionStyle> c = baseStyle.getClass().getConstructor(Color.class, Color.class, double.class, Stroke.class, Stroke.class, double.class);
\r
219 renderer = new StyledRouteGraphRenderer(c.newInstance(
\r
220 dynamicColor != null ? dynamicColor : baseStyle.getLineColor(),
\r
221 baseStyle.getBranchPointColor(), baseStyle.getBranchPointRadius(),
\r
222 dynamicStroke != null ? dynamicStroke : baseStyle.getLineStroke(),
\r
223 dynamicStroke != null ? dynamicStroke : baseStyle.getRouteLineStroke(),
\r
224 baseStyle.getDegeneratedLineLength()));
\r
225 } catch (Exception e) {
\r
226 renderer = new StyledRouteGraphRenderer(new BasicConnectionStyle(
\r
227 dynamicColor != null ? dynamicColor : baseStyle.getLineColor(),
\r
228 baseStyle.getBranchPointColor(), baseStyle.getBranchPointRadius(),
\r
229 dynamicStroke != null ? dynamicStroke : baseStyle.getLineStroke(),
\r
230 dynamicStroke != null ? dynamicStroke : baseStyle.getRouteLineStroke(),
\r
231 baseStyle.getDegeneratedLineLength()));
\r
236 renderer = baseRenderer;
\r
241 @SyncField(value = {"pickTolerance"})
\r
242 public void setPickTolerance(double tolerance) {
\r
243 this.pickTolerance = tolerance;
\r
246 @SyncField(value = {"editable"})
\r
247 public void setEditable(boolean editable) {
\r
248 this.editable = editable;
\r
251 @SyncField(value = {"branchable"})
\r
252 public void setBranchable(boolean branchable) {
\r
253 this.branchable = branchable;
\r
256 public RouteGraph getRouteGraph() {
\r
260 public RouteGraphDelta getRouteGraphDelta() {
\r
264 public IRouteGraphRenderer getRenderer() {
\r
268 public boolean isEditable() {
\r
272 public boolean isBranchable() {
\r
276 public double getPickTolerance() {
\r
277 return pickTolerance;
\r
281 * When in client-server mode, listener is only set on the server side and
\r
282 * fireRouteGraphChanged will tell it when rg has changed.
\r
286 public void setRouteGraphListener(IRouteGraphListener listener) {
\r
287 this.rgListener = listener;
\r
293 * @return <code>true</code> if changes were fired
\r
295 private boolean setRouteGraphAndFireChanges(RouteGraph before, RouteGraph after) {
\r
296 RouteGraphDelta delta = new RouteGraphDelta(before, after);
\r
297 if (!delta.isEmpty()) {
\r
298 setRouteGraph(after);
\r
299 setRouteGraphDelta(delta);
\r
300 fireRouteGraphChanged(before, after, delta);
\r
307 protected void fireRouteGraphChanged(RouteGraph before, RouteGraph after, RouteGraphDelta delta) {
\r
308 if (rgListener != null) {
\r
309 RouteGraphChangeEvent event = new RouteGraphChangeEvent(this, before, after, delta);
\r
310 rgListener.routeGraphChanged(event);
\r
314 public void showBranchPoint(Point2D p) {
\r
315 newBranchPointPosition = p;
\r
319 public void init() {
\r
321 addEventHandler(this);
\r
325 public void cleanup() {
\r
327 removeEventHandler(this);
\r
331 protected boolean isSelected() {
\r
332 return NodeUtil.isSelected(this, 1);
\r
336 public void render(Graphics2D g) {
\r
337 if (renderer == null)
\r
340 AffineTransform ot = null;
\r
341 AffineTransform t = getTransform();
\r
342 if (t != null && !t.isIdentity()) {
\r
343 ot = g.getTransform();
\r
344 g.transform(getTransform());
\r
347 Object aaHint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
\r
348 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
\r
350 boolean selected = NodeUtil.isSelected(this, 1);
\r
352 if (currentAction != null) {
\r
353 currentAction.render(g, renderer, mouseX, mouseY);
\r
355 if (selected && selectionStroke != null) {
\r
356 selectionPath.reset();
\r
357 rg.getPath2D(selectionPath);
\r
358 Shape selectionShape = selectionStroke.createStrokedShape(selectionPath);
\r
359 g.setColor(SELECTION_COLOR);
\r
360 g.fill(selectionShape);
\r
363 renderer.render(g, rg);
\r
365 renderer.renderGuides(g, rg);
\r
367 if (selected && highlightActionsEnabled) {
\r
368 // Needed for performing actions in #mouseClicked
\r
369 this.lastViewTransform = g.getTransform();
\r
370 highlightActions.setRouteGraph(rg);
\r
371 highlightActions.render(g, renderer, mouseX, mouseY);
\r
372 highlightActions.setRouteGraph(null);
\r
376 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
\r
378 if (editable && branchable && newBranchPointPosition != null && !Double.isNaN(newBranchPointPosition.getX())) {
\r
379 ConnectionStyle style = tryGetStyle();
\r
381 style.drawBranchPoint(g, newBranchPointPosition.getX(), newBranchPointPosition.getY());
\r
385 g.setTransform(ot);
\r
388 private BasicConnectionStyle tryGetStyle() {
\r
389 return tryGetStyle(renderer);
\r
392 private BasicConnectionStyle tryGetStyle(IRouteGraphRenderer renderer) {
\r
393 if (renderer instanceof StyledRouteGraphRenderer) {
\r
394 ConnectionStyle cs = ((StyledRouteGraphRenderer) renderer).getStyle();
\r
395 if (cs instanceof BasicConnectionStyle)
\r
396 return (BasicConnectionStyle) cs;
\r
401 private double getSelectionStrokeWidth() {
\r
402 if (selectionStroke instanceof BasicStroke) {
\r
403 BasicStroke bs = (BasicStroke) selectionStroke;
\r
404 return bs.getLineWidth();
\r
410 public Rectangle2D getBoundsInLocal() {
\r
414 protected void updateBounds() {
\r
415 Rectangle2D r = this.bounds;
\r
417 r = new Rectangle2D.Double();
\r
418 this.bounds = calculateBounds(r);
\r
420 // Need to expand to take stroke width into account.
\r
421 double sw = getSelectionStrokeWidth() / 2;
\r
422 GeometryUtils.expandRectangle(this.bounds, sw, sw);
\r
425 protected Rectangle2D calculateBounds(Rectangle2D rect) {
\r
426 RouteGraph rg = this.rg;
\r
427 if (currentAction instanceof MoveAction)
\r
428 rg = ((MoveAction) currentAction).getRouteGraph();
\r
429 rg.getBounds(rect);
\r
433 protected void getMouseLocalPos(MouseEvent e) {
\r
434 //System.out.println("m: " + e.controlPosition);
\r
435 pt.setLocation(e.controlPosition);
\r
436 //System.out.println("parent: " + pt);
\r
437 pt = NodeUtil.worldToLocal(this, pt, pt);
\r
438 //System.out.println("local: " + pt);
\r
439 mouseX = pt.getX();
\r
440 mouseY = pt.getY();
\r
444 protected boolean mouseDragged(MouseDragBegin e) {
\r
445 if (dragAction != null && !e.hasAnyModifier(MouseEvent.ALL_MODIFIERS_MASK) && e.button == MouseEvent.LEFT_BUTTON) {
\r
446 currentAction = dragAction;
\r
449 return updateCurrentAction(e, true);
\r
453 protected boolean mouseMoved(MouseMovedEvent e) {
\r
454 //System.out.println("mouse moved: " + e);
\r
456 // Handle connection branching visualization.
\r
457 getMouseLocalPos(e);
\r
458 if (newBranchPointPosition == null && e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
\r
459 if (bounds.contains(mouseX, mouseY)) {
\r
460 newBranchPointPosition = new Point2D.Double(Double.NaN, Double.NaN);
\r
463 if (newBranchPointPosition != null) {
\r
464 RouteLine line = rg.pickLine(mouseX, mouseY, pickTolerance);
\r
465 if (line != null) {
\r
466 newBranchPointPosition.setLocation(mouseX, mouseY);
\r
467 SplittedRouteGraph.snapToLine(newBranchPointPosition, line);
\r
469 } else if (!Double.isNaN(newBranchPointPosition.getX())) {
\r
470 newBranchPointPosition.setLocation(Double.NaN, Double.NaN);
\r
475 // Make sure that highlight action rendering is according to mouse hover.
\r
476 if (highlightActionsEnabled) {
\r
477 if (NodeUtil.isSelected(this, 1)) {
\r
482 return updateCurrentAction(e, false);
\r
485 protected boolean updateCurrentAction(MouseEvent e, boolean updateMousePos) {
\r
486 boolean oldHighlight = highlightActionsEnabled;
\r
487 highlightActionsEnabled = e.hasAllModifiers(MouseEvent.CTRL_MASK);
\r
488 if (oldHighlight != highlightActionsEnabled)
\r
491 if (currentAction != null) {
\r
492 if (updateMousePos)
\r
493 getMouseLocalPos(e);
\r
496 // Repaint, but only if absolutely necessary.
\r
497 if (currentAction instanceof MoveAction || bounds.contains(mouseX, mouseY))
\r
506 protected boolean mouseClicked(MouseClickEvent e) {
\r
510 if (e.button == MouseEvent.LEFT_BUTTON) {
\r
511 if (isSelected() && highlightActionsEnabled) {
\r
512 // Reconnection / segment deletion only available for branched connections.
\r
513 if (rg.getTerminals().size() > 2) {
\r
514 Pick pick = highlightActions.pickAction(rg, lastViewTransform, mouseX, mouseY);
\r
515 if (pick.hasAction(Action.REMOVE)) {
\r
516 RemoveLineAction remove = RemoveLineAction.perform(rg, pick.line.getLine(), mouseX, mouseY);
\r
517 if (remove != null) {
\r
518 setRouteGraphAndFireChanges(remove.getOriginalRouteGraph(), remove.getRouteGraph());
\r
523 if (pick.hasAction(Action.RECONNECT)) {
\r
524 currentAction = ReconnectLineAction.create(rg, mouseX, mouseY);
\r
525 if (currentAction != null) {
\r
537 protected boolean mouseButtonPressed(MouseButtonPressedEvent e) {
\r
541 if (e.button == MouseEvent.LEFT_BUTTON) {
\r
542 // Visualize new branch point no longer.
\r
543 newBranchPointPosition = null;
\r
545 getMouseLocalPos(e);
\r
547 // if(currentAction instanceof HighlightActionPointsAction) {
\r
548 // RemoveLineAction remove = RemoveLineAction.perform(rg, mouseX, mouseY);
\r
549 // if (remove != null) {
\r
550 // setRouteGraphAndFireChanges(remove.getOriginalRouteGraph(), remove.getRouteGraph());
\r
553 // currentAction = ReconnectLineAction.create(rg, mouseX, mouseY);
\r
554 // if (currentAction != null)
\r
559 if(currentAction instanceof IReconnectAction) {
\r
560 RouteGraph originalRg = rg.copy();
\r
561 ((IReconnectAction)currentAction).finish(mouseX, mouseY);
\r
562 currentAction = null;
\r
564 setRouteGraphAndFireChanges(originalRg, rg);
\r
566 currentAction = null;
\r
571 if (!allowConnectionRerouting()) {
\r
574 //System.out.println("move action");
\r
575 dragAction = SnappingMoveAction.create(rg, mouseX, mouseY, pickTolerance, moveFilter, getSnapAdvisor());
\r
576 //System.out.println("DRAG ACTION: " + dragAction);
\r
579 //System.out.println(this + " NEW action: " + currentAction);
\r
580 if (currentAction != null)
\r
587 * Checks the selections data node in the scene graph for any links
\r
590 private boolean allowConnectionRerouting() {
\r
591 final int maxOtherNodesSelected = 1;
\r
593 INode selections = NodeUtil.tryLookup(this, "selections");
\r
594 if (!(selections instanceof G2DParentNode))
\r
596 G2DParentNode p = (G2DParentNode) selections;
\r
597 for (IG2DNode selection : p.getNodes()) {
\r
598 if (!(selection instanceof G2DParentNode))
\r
601 G2DParentNode sp = (G2DParentNode) selection;
\r
602 Collection<IG2DNode> links = sp.getNodes();
\r
603 if (links.isEmpty())
\r
605 int othersSelected = 0;
\r
606 for (IG2DNode link : links) {
\r
607 if (link instanceof LinkNode) {
\r
608 INode node = ((LinkNode) link).getDelegate();
\r
609 if (!NodeUtil.isParentOf(node, this)) {
\r
611 if (othersSelected > maxOtherNodesSelected)
\r
616 if (othersSelected > maxOtherNodesSelected)
\r
622 protected ISnapAdvisor getSnapAdvisor() {
\r
623 GridNode grid = lookupNode(GridNode.GRID_NODE_ID, GridNode.class);
\r
624 return grid != null ? grid.getSnapAdvisor() : null;
\r
627 MoveAction.TargetFilter moveFilter = new MoveAction.TargetFilter() {
\r
629 public boolean accept(Object target) {
\r
630 return (target instanceof RouteLine) || (target instanceof RouteLink);
\r
635 protected boolean handleCommand(CommandEvent e) {
\r
636 /*if (Commands.DELETE.equals(e.command)) {
\r
637 Object target = rg.pick(mouseX, mouseY, pickTolerance);
\r
638 return deleteTarget(target);
\r
639 } else if (Commands.SPLIT_CONNECTION.equals(e.command)) {
\r
640 Object target = rg.pick(mouseX, mouseY, pickTolerance);
\r
641 return splitTarget(target);
\r
643 if (Commands.CANCEL.equals(e.command)) {
\r
644 return cancelCurrentAction();
\r
649 protected boolean mouseButtonReleased(MouseButtonReleasedEvent e) {
\r
650 if (currentAction instanceof MoveAction) {
\r
651 MoveAction move = (MoveAction) currentAction;
\r
652 RouteGraph originalRg = rg.copy();
\r
653 move.finish(mouseX, mouseY);
\r
655 setRouteGraphAndFireChanges(originalRg, rg);
\r
657 currentAction = null;
\r
665 protected boolean keyPressed(KeyPressedEvent e) {
\r
669 if (!e.hasAnyModifier(MouseEvent.ALL_MODIFIERS_MASK) && e.keyCode == KeyEvent.VK_S) {
\r
670 Object target = rg.pick(mouseX, mouseY, pickTolerance, RouteGraph.PICK_PERSISTENT_LINES | RouteGraph.PICK_TRANSIENT_LINES);
\r
671 return splitTarget(target);
\r
673 else if (!e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK | MouseEvent.CTRL_MASK) && (e.keyCode == KeyEvent.VK_R || e.keyCode == KeyEvent.VK_D)) {
\r
674 Object target = rg.pick(mouseX, mouseY, pickTolerance, RouteGraph.PICK_PERSISTENT_LINES);
\r
675 return deleteTarget(target);
\r
677 else if (e.keyCode == KeyEvent.VK_ESCAPE) {
\r
678 return cancelCurrentAction();
\r
680 // else if (e.keyCode == KeyEvent.VK_D) {
\r
681 // if (target instanceof RouteTerminal) {
\r
682 // RouteTerminal terminal = (RouteTerminal) target;
\r
683 // RouteGraph before = rg.copy();
\r
684 // rg.toggleDirectLines(terminal);
\r
685 // setRouteGraphAndFireChanges(before, rg);
\r
689 // else if (target != null && e.getKeyCode() == KeyEvent.VK_P) {
\r
692 else if (e.keyCode == KeyEvent.VK_CONTROL) {
\r
693 highlightActionsEnabled = true;
\r
696 else if (e.keyCode == KeyEvent.VK_ALT) {
\r
697 // Begin connection branching visualization.
\r
698 RouteLine line = rg.pickLine(mouseX, mouseY, pickTolerance);
\r
699 if (branchable && line != null) {
\r
700 newBranchPointPosition = new Point2D.Double(mouseX, mouseY);
\r
701 SplittedRouteGraph.snapToLine(newBranchPointPosition, line);
\r
710 protected boolean keyReleased(KeyReleasedEvent e) {
\r
711 if (e.keyCode == KeyEvent.VK_ALT) {
\r
712 // End connection branching visualization.
\r
713 if (newBranchPointPosition != null) {
\r
714 newBranchPointPosition = null;
\r
718 if (e.keyCode == KeyEvent.VK_CONTROL) {
\r
719 highlightActionsEnabled = false;
\r
726 private boolean cancelCurrentAction() {
\r
727 if (currentAction != null) {
\r
728 currentAction = null;
\r
735 private boolean splitTarget(Object target) {
\r
736 if (target instanceof RouteLine) {
\r
737 RouteLine rLine = (RouteLine)target;
\r
738 RouteGraph before = rg.copy();
\r
739 rg.split(rLine, rLine.isHorizontal() ? mouseX : mouseY);
\r
740 setRouteGraphAndFireChanges(before, rg);
\r
747 private boolean deleteTarget(Object target) {
\r
748 boolean changed = false;
\r
749 if (target instanceof RouteLine) {
\r
750 RouteLine line = (RouteLine) target;
\r
751 RouteGraph before = rg.copy();
\r
753 changed = setRouteGraphAndFireChanges(before, rg);
\r
755 else if (target instanceof RouteLink) {
\r
756 RouteGraph before = rg.copy();
\r
757 rg.deleteCorner((RouteLink) target);
\r
758 changed = setRouteGraphAndFireChanges(before, rg);
\r
760 // else if (target instanceof RouteTerminal) {
\r
761 // RouteGraph before = rg.copy();
\r
762 // rg.remove((RouteTerminal) target);
\r
763 // changed = setRouteGraphAndFireChanges(before, rg);
\r
771 * A version of MoveAction that snaps movements using the specified
\r
774 static class SnappingMoveAction extends MoveAction {
\r
776 private ISnapAdvisor snapAdvisor;
\r
777 private Point2D point = new Point2D.Double();
\r
779 public SnappingMoveAction(RouteGraph rg, Object target, ISnapAdvisor snapAdvisor) {
\r
781 this.snapAdvisor = snapAdvisor;
\r
784 protected void move(RouteGraph rg, Object target, double x, double y) {
\r
785 point.setLocation(x, y);
\r
786 snapAdvisor.snap(point);
\r
787 super.move(rg, target, point.getX(), point.getY());
\r
790 public static MoveAction create(RouteGraph rg, double x, double y, double tolerance, TargetFilter filter, ISnapAdvisor snapAdvisor) {
\r
791 Object target = rg.pick(x, y, tolerance, RouteGraph.PICK_LINES | RouteGraph.PICK_INTERIOR_POINTS);
\r
792 if (target != null && (filter == null || filter.accept(target))) {
\r
793 if (snapAdvisor != null)
\r
794 return new SnappingMoveAction(rg, target, snapAdvisor);
\r
795 return new MoveAction(rg, target);
\r
803 public int getEventMask() {
\r
804 return EventTypes.CommandMask | EventTypes.KeyMask | EventTypes.MouseMask;
\r