]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java
c2238d8e56eb0e082ce7ce4482cb402a7faba869
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / DNEdgeBuilder.java
1 package org.simantics.district.network.ui;
2
3 import java.awt.geom.Rectangle2D;
4 import java.util.Collection;
5
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;
24
25 public class DNEdgeBuilder {
26     
27     private Resource diagramResource;
28     private IDiagram diagram;
29     private GraphLayerManager glm;
30
31     public DNEdgeBuilder(Resource diagramResource, IDiagram diagram) {
32         this.diagramResource = diagramResource;
33         this.diagram = diagram;
34         
35         IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
36         glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
37     }
38
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);
41     }
42     
43     public static Resource create(WriteGraph graph, Resource diagramResource, Resource mapping, double[] start, double[] end, double padding) throws DatabaseException {
44         
45         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
46         
47         // 1. Get diagram edge to construct
48         Resource edge = getOrCreateEdge(graph, diagramResource, mapping);
49         
50         // 2. Add vertices
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);
54         
55         graph.claim(edge, DN.HasStartVertex, startVertex);
56         graph.claim(edge, DN.HasEndVertex, endVertex);
57         
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);
61             
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);
66             });
67         }
68         
69         return edge;
70     }
71     public void create(WriteGraph graph,  double[] start, double[] end, double padding) throws DatabaseException {
72         
73         Resource edge = create(graph, diagramResource, start, end, padding);
74         // 7. Put the element on all the currently active layers if possible.
75         if (glm != null) {
76             putOnActiveLayer(graph, edge);
77         }
78         
79         Layer0Utils.addCommentMetadata(graph, "Added edge " + edge);
80         graph.markUndoPoint();
81     }
82     
83     private void putOnActiveLayer(WriteGraph graph, Resource res) throws DatabaseException {
84         glm.removeFromAllLayers(graph, res);
85         glm.putElementOnVisibleLayers(diagram, graph, res);
86     }
87
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) {
99                     vertex = vertx;
100                 }
101             }
102         }
103         if (vertex == null) {
104             vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords); 
105         }
106         return vertex;
107     }
108
109     private static Resource getOrCreateEdge(WriteGraph graph, Resource diagramResource, Resource mapping) throws DatabaseException {
110         return DistrictNetworkUtil.createEdge(graph, diagramResource, mapping);
111     }
112
113 }