]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java
Enhancements to district functionalities and code
[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         
41         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
42         
43         // 1. Get diagram edge to construct
44         Resource edge = getOrCreateEdge(graph, diagramResource);
45         
46         // 2. Add vertices
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);
50         
51         graph.claim(edge, DN.HasStartVertex, startVertex);
52         graph.claim(edge, DN.HasEndVertex, endVertex);
53         
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);
57             
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);
62             });
63         }
64         
65         return edge;
66     }
67     public void create(WriteGraph graph,  double[] start, double[] end, double padding) throws DatabaseException {
68         
69         Resource edge = create(graph, diagramResource, start, end, padding);
70         // 7. Put the element on all the currently active layers if possible.
71         if (glm != null) {
72             putOnActiveLayer(graph, edge);
73         }
74         
75         Layer0Utils.addCommentMetadata(graph, "Added edge " + edge);
76         graph.markUndoPoint();
77     }
78     
79     private void putOnActiveLayer(WriteGraph graph, Resource res) throws DatabaseException {
80         glm.removeFromAllLayers(graph, res);
81         glm.putElementOnVisibleLayers(diagram, graph, res);
82     }
83
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) {
95                     vertex = vertx;
96                 }
97             }
98         }
99         if (vertex == null) {
100             vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords); 
101         }
102         return vertex;
103     }
104
105     private static Resource getOrCreateEdge(WriteGraph graph, Resource diagramResource) throws DatabaseException {
106         return DistrictNetworkUtil.createEdge(graph, diagramResource);
107     }
108
109 }