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 {
41 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
43 // 1. Get diagram edge to construct
44 Resource edge = getOrCreateEdge(graph, diagramResource);
47 Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
48 Resource startVertex = getOrCreateVertex(graph, diagramResource, vertices, start, padding);
49 Resource endVertex = getOrCreateVertex(graph, diagramResource, vertices, end, padding);
51 graph.claim(edge, DN.HasStartVertex, startVertex);
52 graph.claim(edge, DN.HasEndVertex, endVertex);
54 // We need to put GraphLayer to newLayers so...
55 for (Resource layer : graph.getObjects(diagramResource, DiagramResource.getInstance(graph).HasLayer)) {
56 IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
58 GraphLayer gl = layerUtil.loadLayer(graph, layer);
59 gl.forEachTag(tag -> {
60 DiagramGraphUtil.tag(graph, startVertex, tag, true);
61 DiagramGraphUtil.tag(graph, endVertex, tag, true);
67 public void create(WriteGraph graph, double[] start, double[] end, double padding) throws DatabaseException {
69 Resource edge = create(graph, diagramResource, start, end, padding);
70 // 7. Put the element on all the currently active layers if possible.
72 putOnActiveLayer(graph, edge);
75 Layer0Utils.addCommentMetadata(graph, "Added edge " + edge);
76 graph.markUndoPoint();
79 private void putOnActiveLayer(WriteGraph graph, Resource res) throws DatabaseException {
80 glm.removeFromAllLayers(graph, res);
81 glm.putElementOnVisibleLayers(diagram, graph, res);
84 private static Resource getOrCreateVertex(WriteGraph graph, Resource diagramResource, Collection<Resource> vertices, double[] coords, double padding) throws DatabaseException {
85 Resource vertex = null;
86 double halfPadding = padding / 2;
87 double maxDistance = Double.MAX_VALUE;
88 for (Resource vertx : vertices) {
89 double[] existingCoords = graph.getRelatedValue2(vertx, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
90 Rectangle2D existing = new Rectangle2D.Double(existingCoords[0] - halfPadding, existingCoords[1] - halfPadding, padding, padding);
91 Rectangle2D tobecreated = new Rectangle2D.Double(coords[0] - halfPadding, coords[1] - halfPadding, padding, padding);
92 if (existing.intersects(tobecreated)) {
93 double dist = Math.sqrt((Math.pow(coords[0] - existingCoords[0], 2) + (Math.pow(coords[1] - existingCoords[1], 2))));
94 if (dist <= maxDistance) {
100 vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords);
105 private static Resource getOrCreateEdge(WriteGraph graph, Resource diagramResource) throws DatabaseException {
106 return DistrictNetworkUtil.createEdge(graph, diagramResource);