]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/handler/Topology.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / diagram / handler / Topology.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.diagram.handler;\r
13 \r
14 import java.util.Collection;\r
15 \r
16 import org.simantics.g2d.diagram.handler.impl.TopologyImpl;\r
17 import org.simantics.g2d.element.IElement;\r
18 import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;\r
19 import org.simantics.g2d.element.handler.TerminalTopology;\r
20 import org.simantics.g2d.element.handler.impl.ObjectTerminal;\r
21 \r
22 /**\r
23  * \r
24  * TODO Remove Connection. Replace with Individual hints.\r
25  * \r
26  * @see TopologyImpl Default implementation.\r
27  * @author Toni Kalajainen\r
28  */\r
29 public interface Topology extends DiagramHandler {\r
30 \r
31     /**\r
32      * @See TerminalImpl\r
33      * @see ObjectTerminal\r
34      */\r
35     public interface Terminal {}\r
36 \r
37     /**\r
38      * @param edge\r
39      * @param end Begin or End\r
40      * @return <code>null</code> if the specified end of the specified edge is\r
41      *         not connected\r
42      */\r
43     Connection getConnection(IElement edge, EdgeEnd end);\r
44 \r
45     /**\r
46      * Get all the connections attached to the specified terminal of the\r
47      * specified node.\r
48      * \r
49      * @param node\r
50      * @param terminal\r
51      * @param connections\r
52      */\r
53     void getConnections(IElement node, Terminal terminal, Collection<Connection> connections);\r
54 \r
55     /**\r
56      * Connect one end of a connection to an element terminal.\r
57      * \r
58      * @param edge the edge to connect to the specified node, must belong to the\r
59      *        same diagram as the node\r
60      * @param end\r
61      * @param node the node to connect to the specified edge, must belong to the\r
62      *        same diagram as the edge\r
63      * @param terminal a terminal of node, retrieved using {@link TerminalTopology}\r
64      */\r
65     void connect(IElement edge, EdgeEnd end, IElement node, Terminal terminal);\r
66 \r
67     /**\r
68      * Disconnect one end of a connection from an element terminal.\r
69      * \r
70      * @param edge the edge to connect to the specified node, must belong to the\r
71      *        same diagram as the node\r
72      * @param end the end of the edge to disconnect\r
73      * @param node the node to disconnect the specified edge from. Must belong to the\r
74      *        same diagram as the edge.\r
75      * @param terminal a terminal of node, retrieved using {@link TerminalTopology}\r
76      */\r
77     void disconnect(IElement edge, EdgeEnd end, IElement node, Terminal terminal);\r
78 \r
79     /**\r
80      * A hint value class for representing a single edge end to node terminal\r
81      * connection.\r
82      */\r
83     public static class Connection {\r
84         public final Terminal terminal;\r
85         public final IElement edge;\r
86         public final IElement node;\r
87         public final EdgeEnd  end;\r
88         public Connection(IElement edge, EdgeEnd end, IElement node, Terminal terminal) {\r
89             assert edge != null;\r
90             assert end != null;\r
91             assert (terminal == null && node == null) || (node != null && terminal != null);\r
92             this.edge = edge;\r
93             this.end = end;\r
94             this.node = node;\r
95             this.terminal = terminal;\r
96         }\r
97         @Override\r
98         public boolean equals(Object obj) {\r
99             if (this == obj)\r
100                 return true;\r
101             if (!(obj instanceof Connection))\r
102                 return false;\r
103             Connection other = (Connection) obj;\r
104             return other.terminal == terminal &&\r
105             other.edge == edge &&\r
106             other.node == node &&\r
107             other.end == end;\r
108         }\r
109         @Override\r
110         public int hashCode() {\r
111             final int prime = 31;\r
112             int result = 1;\r
113             result = prime * result + edge.hashCode();\r
114             result = prime * result + end.hashCode();\r
115             result = prime * result + ((node == null) ? 0 : node.hashCode());\r
116             result = prime * result + ((terminal == null) ? 0 : terminal.hashCode());\r
117             return result;\r
118         }\r
119         @Override\r
120         public String toString() {\r
121             return "Topology.Connection[edge=" + edge + ", node=" + node + ", terminal=" + terminal + ", end=" + end + "]";\r
122         }\r
123     }\r
124 \r
125 }\r