1 package org.simantics.district.network.ui;
3 import java.awt.geom.Rectangle2D;
4 import java.util.ArrayList;
5 import java.util.Collection;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.Resource;
10 import org.simantics.db.WriteGraph;
11 import org.simantics.db.common.request.ObjectsWithType;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.util.Layer0Utils;
14 import org.simantics.diagram.stubs.DiagramResource;
15 import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
16 import org.simantics.diagram.synchronization.SynchronizationHints;
17 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
18 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
19 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
20 import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
21 import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil;
22 import org.simantics.district.network.DistrictNetworkUtil;
23 import org.simantics.district.network.ontology.DistrictNetworkResource;
24 import org.simantics.g2d.diagram.IDiagram;
25 import org.simantics.layer0.Layer0;
27 public class DNEdgeBuilder {
29 private Resource diagramResource;
30 private IDiagram diagram;
31 private GraphLayerManager glm;
33 public DNEdgeBuilder(Resource diagramResource, IDiagram diagram) {
34 this.diagramResource = diagramResource;
35 this.diagram = diagram;
37 IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
38 glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
41 public static Resource create(WriteGraph graph, Resource diagramResource, double[] start, double[] end, double padding) throws DatabaseException {
42 Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
43 List<ResourceVertex> vv = new ArrayList<>(vertices.size());
44 for (Resource vertex : vertices) {
45 double[] existingCoords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
46 vv.add(new ResourceVertex(vertex, existingCoords));
48 return create(graph, vv, diagramResource, null, start, end, padding);
51 public static Resource create(WriteGraph graph, Collection<ResourceVertex> vertices, Resource diagramResource, Resource mapping, double[] start, double[] end, double padding) throws DatabaseException {
53 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
55 // 1. Get diagram edge to construct
56 Resource edge = getOrCreateEdge(graph, diagramResource, mapping);
59 Resource startVertex = getOrCreateVertex(graph, diagramResource, vertices, start, padding, null);
60 Resource endVertex = getOrCreateVertex(graph, diagramResource, vertices, end, padding, startVertex);
62 graph.claim(edge, DN.HasStartVertex, startVertex);
63 graph.claim(edge, DN.HasEndVertex, endVertex);
65 // We need to put GraphLayer to newLayers so...
66 // for (Resource layer : graph.getObjects(diagramResource, DiagramResource.getInstance(graph).HasLayer)) {
67 // IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class);
69 // GraphLayer gl = layerUtil.loadLayer(graph, layer);
70 // gl.forEachTag(tag -> {
71 // DiagramGraphUtil.tag(graph, startVertex, tag, true);
72 // DiagramGraphUtil.tag(graph, endVertex, tag, true);
78 public void create(WriteGraph graph, double[] start, double[] end, double padding) throws DatabaseException {
80 Resource edge = create(graph, diagramResource, start, end, padding);
81 // 7. Put the element on all the currently active layers if possible.
83 putOnActiveLayer(graph, edge);
86 Layer0Utils.addCommentMetadata(graph, "Added edge " + edge);
87 graph.markUndoPoint();
90 private void putOnActiveLayer(WriteGraph graph, Resource res) throws DatabaseException {
91 glm.removeFromAllLayers(graph, res);
92 glm.putElementOnVisibleLayers(diagram, graph, res);
95 private static Resource getOrCreateVertex(WriteGraph graph, Resource diagramResource, Collection<ResourceVertex> vertices, double[] coords, double padding, Resource startVertex) throws DatabaseException {
96 Resource vertex = null;
97 double halfPadding = padding / 2;
98 double maxDistance = Double.MAX_VALUE;
99 for (ResourceVertex vertx : vertices) {
100 Rectangle2D existing = new Rectangle2D.Double(vertx.coords[0] - halfPadding, vertx.coords[1] - halfPadding, padding, padding);
101 Rectangle2D tobecreated = new Rectangle2D.Double(coords[0] - halfPadding, coords[1] - halfPadding, padding, padding);
102 if (existing.intersects(tobecreated)) {
103 double dist = Math.sqrt((Math.pow(coords[0] - vertx.coords[0], 2) + (Math.pow(coords[1] - vertx.coords[1], 2))));
104 if (dist <= maxDistance && !vertx.vertex.equals(startVertex)) {
105 vertex = vertx.vertex;
110 if (vertex == null) {
111 vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords);
112 vertices.add(new ResourceVertex(vertex, coords));
117 private static Resource getOrCreateEdge(WriteGraph graph, Resource diagramResource, Resource mapping) throws DatabaseException {
118 return DistrictNetworkUtil.createEdge(graph, diagramResource, mapping);
121 public static class ResourceVertex {
123 final Resource vertex;
124 final double[] coords;
126 public ResourceVertex(Resource vertex, double[] coords) {
127 this.vertex = vertex;
128 this.coords = coords;