]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/RouteGraphConnectionClassFactory.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / RouteGraphConnectionClassFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2016 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  *     Semantum Oy - refactoring
12  *******************************************************************************/
13 package org.simantics.diagram.adapter;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.Set;
19
20 import org.simantics.db.AsyncReadGraph;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.Session;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.procedure.AsyncProcedure;
26 import org.simantics.diagram.adapter.RouteGraphUtils.BackendConnection;
27 import org.simantics.diagram.connection.RouteGraph;
28 import org.simantics.diagram.connection.rendering.ConnectionStyle;
29 import org.simantics.diagram.connection.rendering.StyledRouteGraphRenderer;
30 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;
31 import org.simantics.diagram.content.ResourceTerminal;
32 import org.simantics.diagram.stubs.DiagramResource;
33 import org.simantics.diagram.ui.DiagramModelHints;
34 import org.simantics.g2d.canvas.ICanvasContext;
35 import org.simantics.g2d.connection.ConnectionEntity;
36 import org.simantics.g2d.diagram.IDiagram;
37 import org.simantics.g2d.diagram.handler.DataElementMap;
38 import org.simantics.g2d.diagram.handler.Topology.Connection;
39 import org.simantics.g2d.diagram.handler.Topology.Terminal;
40 import org.simantics.g2d.element.ElementClass;
41 import org.simantics.g2d.element.ElementHints;
42 import org.simantics.g2d.element.IElement;
43 import org.simantics.g2d.element.handler.TerminalTopology;
44 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
45 import org.simantics.g2d.elementclass.RouteGraphConnectionClass;
46 import org.simantics.g2d.utils.TopologicalSelectionExpander;
47 import org.simantics.layer0.Layer0;
48 import org.simantics.modeling.ModelingResources;
49 import org.simantics.scenegraph.g2d.nodes.connection.IRouteGraphListener;
50 import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphChangeEvent;
51 import org.simantics.structural.stubs.StructuralResource2;
52 import org.simantics.structural2.modelingRules.IModelingRules;
53
54 import gnu.trove.set.hash.THashSet;
55
56 /**
57  * An element class for single connection entity elements. A connection entity
58  * consists of connection edge segments and branch points as its children.
59  * 
60  * @author Tuukka Lehtonen
61  */
62 public class RouteGraphConnectionClassFactory extends SyncElementFactory {
63
64     public static final ElementClass   CLASS = RouteGraphConnectionClass.CLASS;
65
66     public static final ILineEndStyle  HEAD  = RouteGraphUtils.HEAD;
67     public static final ILineEndStyle  TAIL  = RouteGraphUtils.TAIL;
68
69     protected Layer0                   L0;
70     protected DiagramResource          DIA;
71     protected StructuralResource2      STR;
72     protected ModelingResources        MOD;
73
74     public RouteGraphConnectionClassFactory(ReadGraph graph) {
75         this.L0 = Layer0.getInstance(graph);
76         this.DIA = DiagramResource.getInstance(graph);
77         this.STR = StructuralResource2.getInstance(graph);
78         this.MOD = ModelingResources.getInstance(graph);
79     }
80
81     @Override
82     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType,
83             final AsyncProcedure<ElementClass> procedure) {
84         procedure.execute(graph, CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
85     }
86
87     @Override
88     protected Resource getElementClassBaseType(AsyncReadGraph graph) {
89         return DIA.Connection;
90     }
91
92     @Override
93     public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource connection,
94             IElement element) throws DatabaseException {
95
96         IModelingRules modelingRules = diagram.getHint(DiagramModelHints.KEY_MODELING_RULES);
97         Resource diagramRuntime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
98
99         Set<BackendConnection> backendConnections = new THashSet<>();
100         RouteGraph rg = RouteGraphUtils.load(graph, diagramRuntime, connection, canvas, diagram, element, modelingRules, backendConnections);
101
102         // Load connection line style.
103         ConnectionStyle style = RouteGraphUtils.readConnectionStyle(graph, modelingRules, connection, STR, DIA);
104         StyledRouteGraphRenderer renderer = RouteGraphUtils.getRenderer(graph, style);
105
106         // Finish element load
107         element.setHint(RouteGraphConnectionClass.KEY_ROUTEGRAPH, rg);
108         element.setHint(RouteGraphConnectionClass.KEY_RENDERER, renderer);
109         element.setHint(RouteGraphConnectionClass.KEY_PICK_TOLERANCE, 0.5);
110
111         // Initialize ConnectionEntity in element
112         element.setHint(ElementHints.KEY_CONNECTION_ENTITY, new CE(diagram, connection, element, backendConnections));
113
114         // Setup graph writeback support for route graph modifications
115         Session session = graph.getSession();
116         element.setHint(RouteGraphConnectionClass.KEY_RG_LISTENER, new IRouteGraphListener() {
117             @Override
118             public void routeGraphChanged(RouteGraphChangeEvent event) {
119                 RouteGraphUtils.scheduleSynchronize(session, connection, event);
120             }
121         });
122     }
123
124     /**
125      * Must have this in order for {@link TopologicalSelectionExpander} to work.
126      * Otherwise this is pretty useless and should be deprecated altogether.
127      * 
128      * @see ElementHints#KEY_CONNECTION_ENTITY
129      */
130     public static class CE implements ConnectionEntity {
131
132         /**
133          * Needed to gain access to {@link DataElementMap}.
134          */
135         final IDiagram               diagram;
136
137         /**
138          * The connection instance resource in the graph database back-end.
139          */
140         final Resource               connection;
141
142         /**
143          * The current element mapped to connection. 
144          */
145         IElement                     connectionElement;
146
147         /**
148          * @see #getTerminalConnections(Collection)
149          */
150         final Set<BackendConnection> backendConnections;
151
152         /**
153          * Cache.
154          */
155         transient Set<Connection>    terminalConnections;
156
157         public CE(IDiagram diagram, Resource connection, IElement connectionElement, Set<BackendConnection> backendConnections) {
158             if (connectionElement == null)
159                 throw new NullPointerException("null connection element");
160             this.diagram = diagram;
161             this.connection = connection;
162             this.connectionElement = connectionElement;
163             this.backendConnections = backendConnections;
164             IElement ce = getConnection0();
165             if (ce != null)
166                 this.connectionElement = ce;
167         }
168
169         public IElement getConnection0() {
170             DataElementMap dem = diagram.getDiagramClass().getSingleItem(DataElementMap.class);
171             IElement connectionElement = dem.getElement(diagram, connection);
172             return connectionElement;
173         }
174
175         @Override
176         public IElement getConnection() {
177             IElement c = getConnection0();
178             if (c == null)
179                 c = this.connectionElement;
180             return c;
181         }
182
183         @Override
184         public Collection<IElement> getBranchPoints(Collection<IElement> result) {
185             return result != null ? result : Collections.<IElement> emptyList();
186         }
187
188         @Override
189         public Collection<IElement> getSegments(Collection<IElement> result) {
190             return result != null ? result : Collections.<IElement> emptyList();
191         }
192
193         @Override
194         public Collection<Connection> getTerminalConnections(Collection<Connection> result) {
195             if (terminalConnections == null)
196                 terminalConnections = calculateTerminalConnections();
197             if (result == null)
198                 result = new ArrayList<Connection>(terminalConnections);
199             else
200                 result.addAll(terminalConnections);
201             return terminalConnections;
202         }
203
204         private Set<Connection> calculateTerminalConnections() {
205             Set<Connection> result = new THashSet<Connection>(backendConnections.size());
206             DataElementMap dem = diagram.getDiagramClass().getSingleItem(DataElementMap.class);
207             IElement connectionElement = dem.getElement(diagram, connection);
208             if (connectionElement == null)
209                 throw new NullPointerException("connection is not mapped");
210             ArrayList<Terminal> ts = new ArrayList<Terminal>();
211             for (BackendConnection bc : backendConnections) {
212                 IElement e = dem.getElement(diagram, bc.node);
213                 if (e == null)
214                     continue;
215                 TerminalTopology tt = e.getElementClass().getSingleItem(TerminalTopology.class);
216                 ts.clear();
217                 tt.getTerminals(e, ts);
218                 for (Terminal t : ts) {
219                     if (t instanceof ResourceTerminal) {
220                         ResourceTerminal rt = (ResourceTerminal) t;
221                         if (bc.terminal.equals(rt.getResource())) {
222                             result.add(new Connection(connectionElement, bc.end, e, t));
223                             break;
224                         }
225                     }
226                 }
227             }
228             return result;
229         }
230
231         @Override
232         public void setListener(ConnectionListener listener) {
233             throw new UnsupportedOperationException();
234         }
235
236         @Override
237         public String toString() {
238             return getClass().getSimpleName() + "[resource=" + connection + ", connectionElement=" + getConnection()
239                     + "]";
240         }
241
242     }
243
244 }