1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 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.diagram.connection.actions;
\r
14 import gnu.trove.map.hash.THashMap;
\r
16 import java.awt.Graphics2D;
\r
18 import org.simantics.diagram.connection.RouteGraph;
\r
19 import org.simantics.diagram.connection.RouteLine;
\r
20 import org.simantics.diagram.connection.RouteLink;
\r
21 import org.simantics.diagram.connection.RouteTerminal;
\r
22 import org.simantics.diagram.connection.rendering.IRouteGraphRenderer;
\r
24 public class MoveAction implements IAction {
\r
26 protected RouteGraph rg;
\r
27 protected Object target;
\r
28 protected RouteGraph movedRg;
\r
30 public MoveAction(RouteGraph rg, Object target) {
\r
32 this.target = target;
\r
36 public void render(Graphics2D g, IRouteGraphRenderer renderer, double x, double y) {
\r
37 THashMap<Object, Object> map = new THashMap<Object, Object>();
\r
38 movedRg = rg.copy(map);
\r
39 move(movedRg, map.get(target), x, y);
\r
40 renderer.render(g, movedRg);
\r
43 public RouteGraph getRouteGraph() {
\r
44 return movedRg == null ? rg : movedRg;
\r
47 public Object getTarget() {
\r
51 public void finish(double x, double y) {
\r
52 move(rg, target, x, y);
\r
56 protected void move(RouteGraph rg, Object target, double x, double y) {
\r
57 moveTarget(rg, target, x, y);
\r
60 private static void moveTarget(RouteGraph rg, Object target, double x, double y) {
\r
61 if(target instanceof RouteLine) {
\r
62 RouteLine line = (RouteLine)target;
\r
63 rg.setLocation(line, x, y);
\r
65 else if(target instanceof RouteTerminal) {
\r
66 RouteTerminal terminal = (RouteTerminal)target;
\r
67 rg.setLocation(terminal, x, y);
\r
69 else if(target instanceof RouteLink) {
\r
70 RouteLink link = (RouteLink)target;
\r
71 RouteLine a = link.getA();
\r
72 RouteLine b = link.getB();
\r
73 rg.setLocation(a, x, y);
\r
74 rg.setLocation(b, x, y);
\r
78 public static MoveAction create(RouteGraph rg, double x, double y) {
\r
79 return create(rg, x, y, TOLERANCE);
\r
82 public static MoveAction create(RouteGraph rg, double x, double y, double tolerance) {
\r
83 Object target = rg.pick(x, y, tolerance);
\r
87 return new MoveAction(rg, target);
\r
90 public static MoveAction create(RouteGraph rg, double x, double y, double tolerance, TargetFilter filter) {
\r
91 Object target = rg.pick(x, y, tolerance);
\r
92 if (target != null && (filter == null || filter.accept(target)))
\r
93 return new MoveAction(rg, target);
\r
97 public static interface TargetFilter {
\r
98 boolean accept(Object target);
\r