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