]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
70d1ad55dfaea2eccf673bdbc528c08457e0c83e
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.elements2.connections;\r
13 \r
14 import java.awt.BasicStroke;\r
15 import java.awt.Color;\r
16 import java.awt.Shape;\r
17 import java.awt.Stroke;\r
18 import java.awt.geom.AffineTransform;\r
19 import java.awt.geom.Rectangle2D;\r
20 import java.util.ArrayList;\r
21 import java.util.Collection;\r
22 import java.util.Collections;\r
23 import java.util.HashMap;\r
24 import java.util.HashSet;\r
25 import java.util.Map;\r
26 import java.util.Set;\r
27 \r
28 import org.simantics.databoard.Bindings;\r
29 import org.simantics.db.AsyncReadGraph;\r
30 import org.simantics.db.ReadGraph;\r
31 import org.simantics.db.Resource;\r
32 import org.simantics.db.Session;\r
33 import org.simantics.db.Statement;\r
34 import org.simantics.db.common.procedure.adapter.TransientCacheListener;\r
35 import org.simantics.db.common.utils.NameUtils;\r
36 import org.simantics.db.exception.DatabaseException;\r
37 import org.simantics.db.procedure.AsyncProcedure;\r
38 import org.simantics.diagram.adapter.SyncElementFactory;\r
39 import org.simantics.diagram.connection.ConnectionVisuals;\r
40 import org.simantics.diagram.connection.RouteGraph;\r
41 import org.simantics.diagram.connection.RouteGraphConnectionClass;\r
42 import org.simantics.diagram.connection.RouteLine;\r
43 import org.simantics.diagram.connection.RouteNode;\r
44 import org.simantics.diagram.connection.RouteTerminal;\r
45 import org.simantics.diagram.connection.rendering.ConnectionStyle;\r
46 import org.simantics.diagram.connection.rendering.StyledRouteGraphRenderer;\r
47 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;\r
48 import org.simantics.diagram.content.EdgeResource;\r
49 import org.simantics.diagram.content.ResourceTerminal;\r
50 import org.simantics.diagram.content.TerminalMap;\r
51 import org.simantics.diagram.query.DiagramRequests;\r
52 import org.simantics.diagram.stubs.DiagramResource;\r
53 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
54 import org.simantics.diagram.synchronization.graph.RouteGraphConnection;\r
55 import org.simantics.diagram.ui.DiagramModelHints;\r
56 import org.simantics.g2d.canvas.ICanvasContext;\r
57 import org.simantics.g2d.connection.ConnectionEntity;\r
58 import org.simantics.g2d.diagram.DiagramHints;\r
59 import org.simantics.g2d.diagram.IDiagram;\r
60 import org.simantics.g2d.diagram.handler.DataElementMap;\r
61 import org.simantics.g2d.diagram.handler.Topology.Connection;\r
62 import org.simantics.g2d.diagram.handler.Topology.Terminal;\r
63 import org.simantics.g2d.element.ElementClass;\r
64 import org.simantics.g2d.element.ElementHints;\r
65 import org.simantics.g2d.element.ElementUtils;\r
66 import org.simantics.g2d.element.IElement;\r
67 import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;\r
68 import org.simantics.g2d.element.handler.TerminalTopology;\r
69 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
70 import org.simantics.g2d.routing.algorithm2.Router4;\r
71 import org.simantics.g2d.utils.TopologicalSelectionExpander;\r
72 import org.simantics.layer0.Layer0;\r
73 import org.simantics.modeling.ModelingResources;\r
74 import org.simantics.scenegraph.g2d.nodes.connection.IRouteGraphListener;\r
75 import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphChangeEvent;\r
76 import org.simantics.scenegraph.utils.GeometryUtils;\r
77 import org.simantics.structural.stubs.StructuralResource2;\r
78 import org.simantics.structural2.modelingRules.CPTerminal;\r
79 import org.simantics.structural2.modelingRules.IAttachmentRelationMap;\r
80 import org.simantics.structural2.modelingRules.IModelingRules;\r
81 import org.simantics.sysdyn.SysdynResource;\r
82 import org.simantics.sysdyn.ui.elements2.ValveFactory.ValveSceneGraph;\r
83 /**\r
84  * An element class for Sysdyn Flow elements.\r
85  * Copied from RouteGraphConnectionClassFactory and adapted to Flow needs\r
86  * \r
87  * @author Teemu Lempinen\r
88  * \r
89  */\r
90 public class RouteFlowConnectionFactory extends SyncElementFactory {\r
91 \r
92     public static final ElementClass CLASS = RouteFlowEdgeClass.FLOW_CLASS;\r
93 \r
94     Layer0                             L0;\r
95     DiagramResource                    DIA;\r
96     StructuralResource2                STR;\r
97     ModelingResources                  MOD;\r
98 \r
99     public RouteFlowConnectionFactory(ReadGraph graph) {\r
100         this.L0 = Layer0.getInstance(graph);\r
101         this.DIA = DiagramResource.getInstance(graph);\r
102         this.STR = StructuralResource2.getInstance(graph);\r
103         this.MOD = ModelingResources.getInstance(graph);\r
104     }\r
105 \r
106     @Override\r
107     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {\r
108         SysdynResource sr = graph.getService(SysdynResource.class);\r
109         graph.forSingleType(elementType, sr.FlowConnection, new AsyncProcedure<Resource>() {\r
110             @Override\r
111             public void exception(AsyncReadGraph graph, Throwable throwable) {\r
112                 procedure.exception(graph, throwable);\r
113             }\r
114             @Override\r
115             public void execute(AsyncReadGraph graph, Resource connectionType) {\r
116                 procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(connectionType)));\r
117             }\r
118         });\r
119     }\r
120     \r
121     @Override\r
122     public void load(ReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource connection,\r
123             IElement element) throws DatabaseException {\r
124         \r
125         // Do we need this?\r
126         element.setHint(DiagramHints.ROUTE_ALGORITHM, new Router4(false));\r
127         \r
128         IModelingRules modelingRules = diagram.getHint(DiagramModelHints.KEY_MODELING_RULES);\r
129 \r
130         IElement mappedElement = ElementUtils.getByData(diagram, connection);\r
131         if (mappedElement == null)\r
132             // FIXME: With undo this seems to happen, don't know why yet!\r
133             return;\r
134 \r
135         RouteGraph rg = new RouteGraph();\r
136 \r
137         Set<Resource> nodes = new HashSet<Resource>();\r
138         Set<EdgeResource> links = new HashSet<EdgeResource>();\r
139         Map<Object, RouteNode> nodeByData = new HashMap<Object, RouteNode>();\r
140 \r
141         // Needed to support ConnectionEntity#getTerminalConnections\r
142         Set<BackendConnection> backendonnections = new HashSet<BackendConnection>();\r
143 \r
144         // Load all route graph interior RouteNodes: route lines and points\r
145         for (Resource interiorNode : graph.getObjects(connection, DIA.HasInteriorRouteNode)) {\r
146             if (graph.isInstanceOf(interiorNode, DIA.RouteLine)) {\r
147                 Boolean isHorizontal = graph.getRelatedValue(interiorNode, DIA.IsHorizontal, Bindings.BOOLEAN);\r
148                 Double position = graph.getRelatedValue(interiorNode, DIA.HasPosition, Bindings.DOUBLE);\r
149                 RouteLine line = rg.addLine(isHorizontal, position);\r
150                 line.setData( RouteGraphConnection.serialize(graph, interiorNode) );\r
151 \r
152                 nodes.add( interiorNode );\r
153                 nodeByData.put( interiorNode, line );\r
154 \r
155                 for (Resource connectedTo : graph.getObjects(interiorNode, DIA.AreConnected)) {\r
156                     links.add( new EdgeResource(interiorNode, connectedTo) );\r
157                 }\r
158             } else if (graph.isInstanceOf(interiorNode, DIA.RoutePoint)) {\r
159                 // Not supported yet. Ignore.\r
160             }\r
161         }\r
162 \r
163         Rectangle2D bounds = new Rectangle2D.Double();\r
164 \r
165         // Load all node terminal connections as RouteTerminals\r
166         for (Statement toConnector : graph.getStatements(connection, DIA.HasConnector)) {\r
167             Resource connector = toConnector.getObject();\r
168             Resource attachmentRelation = toConnector.getPredicate();\r
169 \r
170             Statement terminalStm = findTerminalStatement(graph, STR, connection, connector);\r
171             if (terminalStm == null)\r
172                 // Ignore broken connector: attached to the connection but not to any terminal.\r
173                 continue;\r
174 \r
175             Resource terminalElement = terminalStm.getObject();\r
176             Resource terminalElementType = graph.getPossibleType(terminalElement, DIA.Element);\r
177             if (terminalElementType == null)\r
178                 // Ignore non-element terminal elements\r
179                 continue;\r
180 \r
181             Resource connectionRelation = graph.getInverse(terminalStm.getPredicate());\r
182 \r
183             // Discover node and terminal this connector is connected to.\r
184             TerminalMap terminals = graph.syncRequest(DiagramRequests.elementTypeTerminals(terminalElementType),\r
185                     TransientCacheListener.<TerminalMap> instance());\r
186             Resource terminal = terminals.getTerminal(connectionRelation);\r
187             if (terminal == null) {\r
188                 System.err.println(getClass().getSimpleName()\r
189                         + ": Could not find terminal for connection point "\r
190                         + NameUtils.getSafeName(graph, connectionRelation, true)\r
191                         + " in element "\r
192                         + NameUtils.getSafeName(graph, terminalElement, true)); \r
193                 continue;\r
194             }\r
195 \r
196             double[] position = graph.getRelatedValue(connector, DIA.HasRelativeLocation, Bindings.DOUBLE_ARRAY);\r
197             if (position.length != 2)\r
198                 position = new double[] { 0, 0 };\r
199 \r
200             //System.out.println("terminalStm: " + NameUtils.toString(graph, terminalStm));\r
201             AffineTransform terminalElementTr = getWorldTransform(graph, terminalElement);\r
202 \r
203             double x = terminalElementTr.getTranslateX();\r
204             double y = terminalElementTr.getTranslateY();\r
205             double minx = x-1, miny = y-1, maxx = x+1, maxy = y+1;\r
206             int direction = 0x0;\r
207 \r
208             // Use modelingRules to ascertain the proper attachmentRelation\r
209             // for this terminal connection, if available.\r
210             if (modelingRules != null) {\r
211                 // Get attachmentRelation from modelingRules if possible.\r
212                 IAttachmentRelationMap map = modelingRules.getAttachmentRelations(graph, connection);\r
213                 Resource att = map.get(graph, new CPTerminal(terminalElement, terminal));\r
214                 if (att != null) {\r
215                     //System.out.println("modeling rules attachment: " + NameUtils.getSafeLabel(graph, att));\r
216                     attachmentRelation = att;\r
217                 }\r
218             }\r
219             //System.out.println("attachment: " + NameUtils.getSafeLabel(graph, attachmentRelation));\r
220 \r
221             // Get element bounds to decide allowed terminal direction(s)\r
222             IElement te = graph.syncRequest(DiagramRequests.getElement(canvas, diagram, terminalElement, null));\r
223             \r
224             ElementUtils.getElementBounds(te, bounds);\r
225             \r
226             {\r
227                 Shape shp = org.simantics.g2d.utils.GeometryUtils.transformShape(bounds, terminalElementTr);\r
228                 bounds.setFrame(shp.getBounds2D());\r
229             }\r
230             \r
231             // Valve behaves differently. The flow must start inside the valve bounds\r
232             if(te.getElementClass().containsClass(ValveSceneGraph.class)) {\r
233                 bounds.setFrame(new Rectangle2D.Double(bounds.getCenterX() - 1, bounds.getCenterY() - 1, 2, 2));\r
234             }\r
235 \r
236             x = bounds.getCenterX();\r
237             y = bounds.getCenterY();\r
238             \r
239             // Expand bounds by 4mm to make the connections enter the terminals\r
240             // at a straight angle and from a distance instead of coming in\r
241             // "horizontally".\r
242             GeometryUtils.expandRectangle(bounds, 4);\r
243 \r
244             minx = bounds.getMinX();\r
245             miny = bounds.getMinY();\r
246             maxx = bounds.getMaxX();\r
247             maxy = bounds.getMaxY();\r
248             \r
249 \r
250             Integer allowedDirections = graph.getPossibleRelatedValue(terminal, DIA.Terminal_AllowedDirections, Bindings.INTEGER);\r
251             \r
252             // Valve behaves differently. Allowed directions depend on the orientation of the valve\r
253             if(te.getElementClass().containsClass(ValveSceneGraph.class)) {\r
254                 SysdynResource sr = SysdynResource.getInstance(graph);\r
255                 if(graph.hasStatement(terminalElement, sr.ValveSymbol_orientation, sr.Vertical)) {\r
256                     allowedDirections = 10; // Directions up and down (1010)\r
257                 } else {\r
258                     allowedDirections = 5; // Directions left and right (0101)\r
259                 }\r
260             }\r
261             if (allowedDirections != null) {\r
262                 direction |= allowedDirections;\r
263             } else {\r
264                 direction |= RouteGraphConnectionClass.shortestDirectionOutOfBounds(x, y, bounds);\r
265             }\r
266 \r
267             backendonnections.add(\r
268                     new BackendConnection(\r
269                             toEdgeEnd(graph, attachmentRelation, EdgeEnd.Begin),\r
270                             terminalElement,\r
271                             terminal)\r
272                     );\r
273 \r
274             if (direction == 0)\r
275                 // Accept any horizontal/vertical direction if nothing is defined\r
276                 direction = 0xf;\r
277 \r
278             //System.out.println("load line style: " + NameUtils.getSafeLabel(graph, attachmentRelation));\r
279             ILineEndStyle endStyle = loadLineEndStyle(graph, te, attachmentRelation, new Rectangle2D.Double(\r
280                     bounds.getX(), \r
281                     bounds.getY(), \r
282                     bounds.getWidth(), \r
283                     bounds.getHeight())\r
284             );\r
285 \r
286             RouteTerminal routeTerminal = rg.addTerminal(x, y, minx, miny, maxx, maxy, direction, endStyle);\r
287             routeTerminal.setData( RouteGraphConnection.serialize(graph, connector) );\r
288 \r
289             nodes.add( connector );\r
290             nodeByData.put( connector, routeTerminal );\r
291 \r
292             for (Resource connectedTo : graph.getObjects(connector, DIA.AreConnected)) {\r
293                 links.add( new EdgeResource(connectedTo, connector) );\r
294             }\r
295         }\r
296 \r
297         // Finish route graph loading by Linking route nodes together\r
298         for (EdgeResource link : links) {\r
299             RouteNode n1 = nodeByData.get(link.first());\r
300             RouteNode n2 = nodeByData.get(link.second());\r
301             if (n1 == null || n2 == null) {\r
302                 System.err.println("Stray connection link found: " + link.toString(graph));\r
303                 continue;\r
304             }\r
305             rg.link(n1, n2);\r
306         }\r
307 \r
308         // Load connection line style\r
309         ConnectionStyle style = readConnectionStyle(graph, modelingRules, connection, element);\r
310         StyledRouteGraphRenderer renderer = new StyledRouteGraphRenderer(style);\r
311 \r
312         // Finish element load\r
313         element.setHint(RouteGraphConnectionClass.KEY_ROUTEGRAPH, rg);\r
314         element.setHint(RouteGraphConnectionClass.KEY_RENDERER, renderer);\r
315         element.setHint(RouteGraphConnectionClass.KEY_PICK_TOLERANCE, 0.5);\r
316 \r
317         // Initialize ConnectionEntity in element\r
318         // NOTE: MUST use the mapped element with class CE, not the connection (element) were loading into.\r
319         // GDS will synchronize element into mappedElement in a controlled manner.\r
320         element.setHint(ElementHints.KEY_CONNECTION_ENTITY, new CE(connection, mappedElement, backendonnections));\r
321 \r
322         // Setup graph writeback support for route graph modifications\r
323         final Session session = graph.getSession();\r
324         element.setHint(RouteGraphConnectionClass.KEY_RG_LISTENER, new IRouteGraphListener() {\r
325             @Override\r
326             public void routeGraphChanged(RouteGraphChangeEvent event) {\r
327                 scheduleSynchronize(session, connection, event);\r
328             }\r
329         });\r
330     }\r
331 \r
332     private EdgeEnd toEdgeEnd(ReadGraph graph, Resource attachmentRelation, EdgeEnd defaultValue)\r
333             throws DatabaseException {\r
334         if (graph.isSubrelationOf(attachmentRelation, DIA.IsTailConnectorOf))\r
335             return EdgeEnd.Begin;\r
336         if (graph.isSubrelationOf(attachmentRelation, DIA.IsHeadConnectorOf))\r
337             return EdgeEnd.End;\r
338         return defaultValue;\r
339     }\r
340 \r
341     private ConnectionStyle readConnectionStyle(ReadGraph graph, IModelingRules modelingRules, Resource connection,\r
342             IElement element) throws DatabaseException {\r
343         Resource connectionType = null;\r
344         if (modelingRules != null)\r
345             connectionType = modelingRules.getConnectionType(graph, connection);\r
346         if (connectionType == null)\r
347             connectionType = graph.getPossibleObject(connection, STR.HasConnectionType);\r
348 \r
349         ConnectionVisuals cv = null;\r
350         if (connectionType != null)\r
351             cv = graph.syncRequest(DiagramRequests.getConnectionVisuals(connectionType),\r
352                     TransientCacheListener.<ConnectionVisuals> instance());\r
353 \r
354         Color lineColor = cv != null ? cv.toColor() : null;\r
355         if (lineColor == null)\r
356             lineColor = Color.DARK_GRAY;\r
357         Stroke lineStroke = cv != null ? cv.stroke : null;\r
358         if (lineStroke == null)\r
359             lineStroke = new BasicStroke(0.1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10, null, 0);\r
360 \r
361         return new FlowConnectionStyle(\r
362                 lineColor,\r
363                 lineStroke);\r
364     }\r
365 \r
366     /**\r
367      * @param graph\r
368      * @param STR\r
369      * @param connection\r
370      * @param connector\r
371      * @return connection relation statement from diagram connection connector\r
372      *         to a node\r
373      * @throws DatabaseException\r
374      */\r
375     private static Statement findTerminalStatement(ReadGraph graph, StructuralResource2 STR, Resource connection,\r
376             Resource connector) throws DatabaseException {\r
377         for (Statement stm : graph.getStatements(connector, STR.Connects)) {\r
378             if (connection.equals(stm.getObject()))\r
379                 continue;\r
380             return stm;\r
381         }\r
382         return null;\r
383     }\r
384 \r
385     public ILineEndStyle loadLineEndStyle(ReadGraph graph, IElement te, Resource attachmentRelation, Rectangle2D bounds)\r
386             throws DatabaseException {\r
387         ILineEndStyle style;\r
388         // TODO: change bounds according to terminal type: Very small rectangle for Valves, Text box size for Stocks and Clouds\r
389         if(te.getElementClass().containsClass(ValveSceneGraph.class)) {\r
390             GeometryUtils.expandRectangle(bounds, -4);\r
391             style =  new FlowArrowLineStyle("none 0 0 0", bounds);\r
392         } else {\r
393             if (graph.isSubrelationOf(attachmentRelation, DIA.HasHeadConnector)) {\r
394                 GeometryUtils.expandRectangle(bounds, -2.5);\r
395                 style = new FlowArrowLineStyle("fill 2 2 -1.5", bounds);\r
396             } else {\r
397                 GeometryUtils.expandRectangle(bounds, -4);\r
398                 style =  new FlowArrowLineStyle("none 0 0 0", bounds);\r
399             }\r
400         }\r
401         return style;\r
402     }\r
403 \r
404     /**\r
405      * @param graph\r
406      * @param element\r
407      * @return\r
408      * @throws DatabaseException\r
409      */\r
410     private static AffineTransform getWorldTransform(ReadGraph graph, Resource element) throws DatabaseException {\r
411         ModelingResources MOD = ModelingResources.getInstance(graph);\r
412         AffineTransform result = DiagramGraphUtil.getAffineTransform(graph, element);\r
413         while (true) {\r
414             Resource parentComponent = graph.getPossibleObject(element, MOD.HasParentComponent);\r
415             if (parentComponent == null)\r
416                 return result;\r
417             element = graph.getPossibleObject(parentComponent, MOD.ComponentToElement);\r
418             if (element == null)\r
419                 return result;\r
420             AffineTransform tr = DiagramGraphUtil.getAffineTransform(graph, element);\r
421             tr.setToTranslation(tr.getTranslateX(), tr.getTranslateY());\r
422             result.preConcatenate(tr);\r
423         }\r
424     }\r
425 \r
426 \r
427     protected void scheduleSynchronize(Session session, Resource connection, RouteGraphChangeEvent event) {\r
428         session.asyncRequest(RouteGraphConnection.synchronizer(connection, event));\r
429     }\r
430 \r
431     /**\r
432      * Must have this in order for {@link TopologicalSelectionExpander} to work.\r
433      * Otherwise this is pretty useless and should be deprecated altogether.\r
434      * \r
435      * @see ElementHints#KEY_CONNECTION_ENTITY\r
436      */\r
437     static class CE implements ConnectionEntity {\r
438 \r
439         /**\r
440          * The connection instance resource in the graph backend.\r
441          */\r
442         final Resource               connection;\r
443 \r
444         /**\r
445          * The connection entity element which is a part of the diagram.\r
446          */\r
447         final IElement               connectionElement;\r
448 \r
449         /**\r
450          * @see #getTerminalConnections(Collection)\r
451          */\r
452         final Set<BackendConnection> backendConnections;\r
453 \r
454         /**\r
455          * Cache.\r
456          */\r
457         Set<Connection>              terminalConnections;\r
458 \r
459         CE(Resource connection, IElement connectionElement, Set<BackendConnection> backendConnections) {\r
460             this.connection = connection;\r
461             this.connectionElement = connectionElement;\r
462             this.backendConnections = backendConnections;\r
463         }\r
464 \r
465         @Override\r
466         public IElement getConnection() {\r
467             return connectionElement;\r
468         }\r
469 \r
470         public Object getConnectionObject() {\r
471             return connection;\r
472         }\r
473 \r
474         public IElement getConnectionElement() {\r
475             return connectionElement;\r
476         }\r
477 \r
478         @Override\r
479         public Collection<IElement> getBranchPoints(Collection<IElement> result) {\r
480             return result != null ? result : Collections.<IElement> emptyList();\r
481         }\r
482 \r
483         @Override\r
484         public Collection<IElement> getSegments(Collection<IElement> result) {\r
485             return result != null ? result : Collections.<IElement> emptyList();\r
486         }\r
487 \r
488         @Override\r
489         public Collection<Connection> getTerminalConnections(Collection<Connection> result) {\r
490             if (terminalConnections == null)\r
491                 terminalConnections = calculateTerminalConnections();\r
492             if (result == null)\r
493                 result = new ArrayList<Connection>(terminalConnections);\r
494             else\r
495                 result.addAll(terminalConnections);\r
496             return terminalConnections;\r
497         }\r
498 \r
499         private Set<Connection> calculateTerminalConnections() {\r
500             IDiagram diagram = connectionElement.getDiagram();\r
501             DataElementMap dem = diagram.getDiagramClass().getSingleItem(DataElementMap.class);\r
502             Set<Connection> result = new HashSet<Connection>();\r
503             ArrayList<Terminal> ts = new ArrayList<Terminal>();\r
504             for (BackendConnection bc : backendConnections) {\r
505                 IElement e = dem.getElement(diagram, bc.node);\r
506                 if (e == null)\r
507                     continue;\r
508                 TerminalTopology tt = e.getElementClass().getSingleItem(TerminalTopology.class);\r
509                 tt.getTerminals(e, ts);\r
510                 for (Terminal t : ts) {\r
511                     if (t instanceof ResourceTerminal) {\r
512                         ResourceTerminal rt = (ResourceTerminal) t;\r
513                         if (bc.terminal.equals(rt.getResource())) {\r
514                             result.add(new Connection(connectionElement, bc.end, e, t));\r
515                             break;\r
516                         }\r
517                     }\r
518                 }\r
519             }\r
520             return result;\r
521         }\r
522 \r
523         @Override\r
524         public void setListener(ConnectionListener listener) {\r
525             throw new UnsupportedOperationException();\r
526         }\r
527 \r
528         @Override\r
529         public String toString() {\r
530             return getClass().getSimpleName() + "[resource=" + connection + ", connectionElement=" + connectionElement\r
531                     + "]";\r
532         }\r
533 \r
534     }\r
535 \r
536     public static class BackendConnection {\r
537         public final Resource node;\r
538         public final Resource terminal;\r
539         public final EdgeEnd  end;\r
540         public BackendConnection(EdgeEnd end, Resource node, Resource terminal) {\r
541             assert end != null;\r
542             assert node != null;\r
543             assert terminal != null;\r
544             this.end = end;\r
545             this.node = node;\r
546             this.terminal = terminal;\r
547         }\r
548         @Override\r
549         public boolean equals(Object obj) {\r
550             if (this == obj)\r
551                 return true;\r
552             if (!(obj instanceof Connection))\r
553                 return false;\r
554             Connection other = (Connection) obj;\r
555             return other.terminal == terminal\r
556                     && other.node == node\r
557                     && other.end == end;\r
558         }\r
559         @Override\r
560         public int hashCode() {\r
561             final int prime = 31;\r
562             int result = 1;\r
563             result = prime * result + end.hashCode();\r
564             result = prime * result + ((node == null) ? 0 : node.hashCode());\r
565             result = prime * result + ((terminal == null) ? 0 : terminal.hashCode());\r
566             return result;\r
567         }\r
568         @Override\r
569         public String toString() {\r
570             return "BackendConnection[node=" + node + ", terminal=" + terminal + ", end=" + end + "]";\r
571         }\r
572     }\r
573 \r
574 }\r
575 \r