1 package org.simantics.district.network.ui;
3 import java.awt.geom.Rectangle2D;
4 import java.util.Collection;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.common.request.ObjectsWithType;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.util.Layer0Utils;
12 import org.simantics.diagram.stubs.DiagramResource;
13 import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
14 import org.simantics.diagram.synchronization.SynchronizationHints;
15 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
16 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
17 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
18 import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
19 import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil;
20 import org.simantics.district.network.DistrictNetworkUtil;
21 import org.simantics.district.network.ontology.DistrictNetworkResource;
22 import org.simantics.g2d.diagram.IDiagram;
23 import org.simantics.layer0.Layer0;
25 public class DNEdgeBuilder {
27 private Resource diagramResource;
28 private IDiagram diagram;
29 private GraphLayerManager glm;
31 public DNEdgeBuilder(Resource diagramResource, IDiagram diagram) {
32 this.diagramResource = diagramResource;
33 this.diagram = diagram;
35 IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
36 glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
39 public static Resource create(WriteGraph graph, Resource diagramResource, double[] start, double[] end, double padding) throws DatabaseException {
40 return create(graph, diagramResource, null, start, end, padding);
43 public static Resource create(WriteGraph graph, Resource diagramResource, Resource mapping, double[] start, double[] end, double padding) throws DatabaseException {
45 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
47 // 1. Get diagram edge to construct
48 Resource edge = getOrCreateEdge(graph, diagramResource, mapping);
51 Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
52 Resource startVertex = getOrCreateVertex(graph, diagramResource, vertices, start, padding);
53 Resource endVertex = getOrCreateVertex(graph, diagramResource, vertices, end, padding);
55 graph.claim(edge, DN.HasStartVertex, startVertex);
56 graph.claim(edge, DN.HasEndVertex, endVertex);
58 // We need to put GraphLayer to newLayers so...
59 for (Resource layer : graph.getObjects(diagramResource, DiagramResource.getInstance(graph).HasLayer)) {
60 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
62 GraphLayer gl = layerUtil.loadLayer(graph, layer);
63 gl.forEachTag(tag -> {
64 DiagramGraphUtil.tag(graph, startVertex, tag, true);
65 DiagramGraphUtil.tag(graph, endVertex, tag, true);
71 public void create(WriteGraph graph, double[] start, double[] end, double padding) throws DatabaseException {
73 Resource edge = create(graph, diagramResource, start, end, padding);
74 // 7. Put the element on all the currently active layers if possible.
76 putOnActiveLayer(graph, edge);
79 Layer0Utils.addCommentMetadata(graph, "Added edge " + edge);
80 graph.markUndoPoint();
83 private void putOnActiveLayer(WriteGraph graph, Resource res) throws DatabaseException {
84 glm.removeFromAllLayers(graph, res);
85 glm.putElementOnVisibleLayers(diagram, graph, res);
88 private static Resource getOrCreateVertex(WriteGraph graph, Resource diagramResource, Collection<Resource> vertices, double[] coords, double padding) throws DatabaseException {
89 Resource vertex = null;
90 double halfPadding = padding / 2;
91 double maxDistance = Double.MAX_VALUE;
92 for (Resource vertx : vertices) {
93 double[] existingCoords = graph.getRelatedValue2(vertx, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
94 Rectangle2D existing = new Rectangle2D.Double(existingCoords[0] - halfPadding, existingCoords[1] - halfPadding, padding, padding);
95 Rectangle2D tobecreated = new Rectangle2D.Double(coords[0] - halfPadding, coords[1] - halfPadding, padding, padding);
96 if (existing.intersects(tobecreated)) {
97 double dist = Math.sqrt((Math.pow(coords[0] - existingCoords[0], 2) + (Math.pow(coords[1] - existingCoords[1], 2))));
98 if (dist <= maxDistance) {
103 if (vertex == null) {
104 vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords);
109 private static Resource getOrCreateEdge(WriteGraph graph, Resource diagramResource, Resource mapping) throws DatabaseException {
110 return DistrictNetworkUtil.createEdge(graph, diagramResource, mapping);