1 /*******************************************************************************
2 * Copyright (c) 2007, 2016 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - refactoring
12 *******************************************************************************/
13 package org.simantics.diagram.adapter;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
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.RouteGraphConnectionClass;
29 import org.simantics.diagram.connection.rendering.ConnectionStyle;
30 import org.simantics.diagram.connection.rendering.StyledRouteGraphRenderer;
31 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;
32 import org.simantics.diagram.content.ResourceTerminal;
33 import org.simantics.diagram.stubs.DiagramResource;
34 import org.simantics.diagram.ui.DiagramModelHints;
35 import org.simantics.g2d.canvas.ICanvasContext;
36 import org.simantics.g2d.connection.ConnectionEntity;
37 import org.simantics.g2d.diagram.IDiagram;
38 import org.simantics.g2d.diagram.handler.DataElementMap;
39 import org.simantics.g2d.diagram.handler.Topology.Connection;
40 import org.simantics.g2d.diagram.handler.Topology.Terminal;
41 import org.simantics.g2d.element.ElementClass;
42 import org.simantics.g2d.element.ElementHints;
43 import org.simantics.g2d.element.IElement;
44 import org.simantics.g2d.element.handler.TerminalTopology;
45 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
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;
54 import gnu.trove.set.hash.THashSet;
57 * An element class for single connection entity elements. A connection entity
58 * consists of connection edge segments and branch points as its children.
60 * @author Tuukka Lehtonen
62 public class RouteGraphConnectionClassFactory extends SyncElementFactory {
64 public static final ElementClass CLASS = RouteGraphConnectionClass.CLASS;
66 public static final ILineEndStyle HEAD = RouteGraphUtils.HEAD;
67 public static final ILineEndStyle TAIL = RouteGraphUtils.TAIL;
70 protected DiagramResource DIA;
71 protected StructuralResource2 STR;
72 protected ModelingResources MOD;
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);
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)));
88 protected Resource getElementClassBaseType(AsyncReadGraph graph) {
89 return DIA.Connection;
93 public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource connection,
94 IElement element) throws DatabaseException {
96 IModelingRules modelingRules = diagram.getHint(DiagramModelHints.KEY_MODELING_RULES);
97 Resource diagramRuntime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);
99 Set<BackendConnection> backendConnections = new THashSet<>();
100 RouteGraph rg = RouteGraphUtils.load(graph, diagramRuntime, connection, canvas, diagram, modelingRules, backendConnections);
102 // Load connection line style.
103 ConnectionStyle style = RouteGraphUtils.readConnectionStyle(graph, modelingRules, connection, STR);
104 StyledRouteGraphRenderer renderer = RouteGraphUtils.getRenderer(graph, style);
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);
111 // Initialize ConnectionEntity in element
112 element.setHint(ElementHints.KEY_CONNECTION_ENTITY, new CE(diagram, connection, element, backendConnections));
114 // Setup graph writeback support for route graph modifications
115 Session session = graph.getSession();
116 element.setHint(RouteGraphConnectionClass.KEY_RG_LISTENER, new IRouteGraphListener() {
118 public void routeGraphChanged(RouteGraphChangeEvent event) {
119 RouteGraphUtils.scheduleSynchronize(session, connection, event);
125 * Must have this in order for {@link TopologicalSelectionExpander} to work.
126 * Otherwise this is pretty useless and should be deprecated altogether.
128 * @see ElementHints#KEY_CONNECTION_ENTITY
130 public static class CE implements ConnectionEntity {
133 * Needed to gain access to {@link DataElementMap}.
135 final IDiagram diagram;
138 * The connection instance resource in the graph database back-end.
140 final Resource connection;
143 * The current element mapped to connection.
145 IElement connectionElement;
148 * @see #getTerminalConnections(Collection)
150 final Set<BackendConnection> backendConnections;
155 transient Set<Connection> terminalConnections;
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();
166 this.connectionElement = ce;
169 public IElement getConnection0() {
170 DataElementMap dem = diagram.getDiagramClass().getSingleItem(DataElementMap.class);
171 IElement connectionElement = dem.getElement(diagram, connection);
172 return connectionElement;
176 public IElement getConnection() {
177 IElement c = getConnection0();
179 c = this.connectionElement;
184 public Collection<IElement> getBranchPoints(Collection<IElement> result) {
185 return result != null ? result : Collections.<IElement> emptyList();
189 public Collection<IElement> getSegments(Collection<IElement> result) {
190 return result != null ? result : Collections.<IElement> emptyList();
194 public Collection<Connection> getTerminalConnections(Collection<Connection> result) {
195 if (terminalConnections == null)
196 terminalConnections = calculateTerminalConnections();
198 result = new ArrayList<Connection>(terminalConnections);
200 result.addAll(terminalConnections);
201 return terminalConnections;
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);
215 TerminalTopology tt = e.getElementClass().getSingleItem(TerminalTopology.class);
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));
232 public void setListener(ConnectionListener listener) {
233 throw new UnsupportedOperationException();
237 public String toString() {
238 return getClass().getSimpleName() + "[resource=" + connection + ", connectionElement=" + getConnection()