]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram.connection/src/org/simantics/diagram/connection/actions/MoveAction.java
Round corners between non-axis-aligned connection lines properly
[simantics/platform.git] / bundles / org.simantics.diagram.connection / src / org / simantics / diagram / connection / actions / MoveAction.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.connection.actions;
13
14 import gnu.trove.map.hash.THashMap;
15
16 import java.awt.Graphics2D;
17
18 import org.simantics.diagram.connection.RouteGraph;
19 import org.simantics.diagram.connection.RouteLine;
20 import org.simantics.diagram.connection.RouteLink;
21 import org.simantics.diagram.connection.RouteTerminal;
22 import org.simantics.diagram.connection.rendering.IRouteGraphRenderer;
23
24 public class MoveAction implements IAction {
25
26     protected RouteGraph rg;
27     protected Object target;
28     protected RouteGraph movedRg;
29
30     public MoveAction(RouteGraph rg, Object target) {
31         this.rg = rg;
32         this.target = target;
33     }
34
35     @Override
36     public void render(Graphics2D g, IRouteGraphRenderer renderer, double x, double y) {
37         THashMap<Object, Object> map = new THashMap<Object, Object>();
38         movedRg = rg.copy(map);
39         move(movedRg, map.get(target), x, y);
40         renderer.render(g, movedRg);
41     }
42
43     public RouteGraph getRouteGraph() {
44         return movedRg == null ? rg : movedRg;
45     }
46
47     public Object getTarget() {
48         return target;
49     }
50
51     public void finish(double x, double y) {
52         move(rg, target, x, y);
53         movedRg = null;
54     }
55
56     protected void move(RouteGraph rg, Object target, double x, double y) {
57         moveTarget(rg, target, x, y);
58     }
59
60     private static void moveTarget(RouteGraph rg, Object target, double x, double y) {
61         if(target instanceof RouteLine) {
62             RouteLine line = (RouteLine)target;
63             rg.setLocation(line, x, y);
64         }
65         else if(target instanceof RouteTerminal) {
66             RouteTerminal terminal = (RouteTerminal)target;
67             rg.setLocation(terminal, x, y);
68         }
69         else if(target instanceof RouteLink) {
70             RouteLink link = (RouteLink)target;
71             RouteLine a = link.getA();
72             RouteLine b = link.getB();
73             rg.setLocation(a, x, y);
74             rg.setLocation(b, x, y);
75         }
76     }
77
78     public static MoveAction create(RouteGraph rg, double x, double y) {
79         return create(rg, x, y, TOLERANCE);
80     }
81
82     public static MoveAction create(RouteGraph rg, double x, double y, double tolerance) {
83         Object target = rg.pick(x, y, tolerance);
84         if(target == null)
85             return null;
86         else
87             return new MoveAction(rg, target);
88     }
89
90     public static MoveAction create(RouteGraph rg, double x, double y, double tolerance, TargetFilter filter) {
91         Object target = rg.pick(x, y, tolerance);
92         if (target != null && (filter == null || filter.accept(target)))
93             return new MoveAction(rg, target);
94         return null;
95     }
96
97     public static interface TargetFilter {
98         boolean accept(Object target);
99     }
100
101 }