1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.connection.actions;
14 import gnu.trove.map.hash.THashMap;
16 import java.awt.Graphics2D;
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;
24 public class MoveAction implements IAction {
26 protected RouteGraph rg;
27 protected Object target;
28 protected RouteGraph movedRg;
30 public MoveAction(RouteGraph rg, Object target) {
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);
43 public RouteGraph getRouteGraph() {
44 return movedRg == null ? rg : movedRg;
47 public Object getTarget() {
51 public void finish(double x, double y) {
52 move(rg, target, x, y);
56 protected void move(RouteGraph rg, Object target, double x, double y) {
57 moveTarget(rg, target, x, y);
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);
65 else if(target instanceof RouteTerminal) {
66 RouteTerminal terminal = (RouteTerminal)target;
67 rg.setLocation(terminal, x, y);
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);
78 public static MoveAction create(RouteGraph rg, double x, double y) {
79 return create(rg, x, y, TOLERANCE);
82 public static MoveAction create(RouteGraph rg, double x, double y, double tolerance) {
83 Object target = rg.pick(x, y, tolerance);
87 return new MoveAction(rg, target);
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);
97 public static interface TargetFilter {
98 boolean accept(Object target);