]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/connection/RemoveLineAction.java
Merge changes I7c81eac8,I15581be9
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / connection / RemoveLineAction.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.scenegraph.g2d.nodes.connection;
13
14 import gnu.trove.map.hash.THashMap;
15
16 import org.simantics.diagram.connection.RouteGraph;
17 import org.simantics.diagram.connection.RouteLine;
18 import org.simantics.diagram.connection.RouteTerminal;
19
20 /**
21  * @author Tuukka Lehtonen
22  */
23 public class RemoveLineAction {
24
25     RouteGraph rg;
26     RouteGraph rgc;
27     RouteTerminal terminal;
28
29     public RemoveLineAction(RouteGraph rg, RouteGraph rgc, RouteTerminal terminal) {
30         this.rg = rg;
31         this.rgc = rgc;
32         this.terminal = terminal;
33     }
34
35     public RouteGraph getOriginalRouteGraph() {
36         return rg;
37     }
38
39     public RouteGraph getRouteGraph() {
40         return rgc;
41     }
42
43     /**
44      * @param rg
45      * @param l the route line to remove
46      * @param x
47      * @param y
48      * @return the performed action or <code>null</code> if no action to perform
49      */
50     public static RemoveLineAction perform(RouteGraph rg, RouteLine line, double x, double y) {
51         // Can't remove segments from connections that have only two terminals.
52         if (rg.isSimpleConnection() || rg.getTerminals().size() <= 2)
53             return null;
54
55         THashMap<Object, Object> map = new THashMap<Object, Object>();
56         RouteGraph rgc = rg.copy(map);
57         RouteLine lc = (RouteLine) map.get(line);
58
59         RouteTerminal terminal = lc.getTerminal();
60         if (terminal == null)
61             return null;
62
63         rgc.disconnect(terminal);
64         rgc.remove(terminal);
65
66         return new RemoveLineAction(rg, rgc, terminal);
67     }
68
69 }