]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java
Initial commit of simantics/district
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / DNEdgeBuilder.java
1 package org.simantics.district.network.ui;\r
2 \r
3 import org.simantics.db.Resource;\r
4 import org.simantics.db.WriteGraph;\r
5 import org.simantics.db.exception.DatabaseException;\r
6 import org.simantics.db.layer0.util.Layer0Utils;\r
7 import org.simantics.diagram.synchronization.graph.AddElement;\r
8 import org.simantics.district.network.DistrictNetworkUtil;\r
9 import org.simantics.district.network.ontology.DistrictNetworkResource;\r
10 \r
11 public class DNEdgeBuilder {\r
12     \r
13     private Resource diagramResource;\r
14 \r
15     public DNEdgeBuilder(Resource diagramResource) {\r
16         this.diagramResource = diagramResource;\r
17     }\r
18 \r
19     public void create(WriteGraph graph, double[] start, double[] end) throws DatabaseException {\r
20         \r
21         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);\r
22         \r
23         // 1. Get diagram edge to construct\r
24         Resource edge = getOrCreateEdge(graph);\r
25         \r
26         // 1.1 Give running name to connection and increment the counter attached to the diagram.\r
27         AddElement.claimFreshElementName(graph, diagramResource, edge);\r
28         \r
29         // 2. Add vertices\r
30         Resource startVertex = getOrCreateVertex(graph, start);\r
31         Resource endVertex = getOrCreateVertex(graph, end);\r
32         \r
33         graph.claim(edge, DN.HasStartVertex, startVertex);\r
34         graph.claim(edge, DN.HasEndVertex, endVertex);\r
35         \r
36         Layer0Utils.addCommentMetadata(graph, "Added edge " + edge);\r
37         graph.markUndoPoint();\r
38     }\r
39 \r
40     private Resource getOrCreateVertex(WriteGraph graph, double[] coords) throws DatabaseException {\r
41         // TODO: check if vertex exists already, for now create always new\r
42         Resource vertex = null;\r
43         if (vertex == null) {\r
44             vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords); \r
45             AddElement.claimFreshElementName(graph, diagramResource, vertex);\r
46         }\r
47         return vertex;\r
48     }\r
49 \r
50     private Resource getOrCreateEdge(WriteGraph graph) throws DatabaseException {\r
51         return DistrictNetworkUtil.createEdge(graph, diagramResource);\r
52     }\r
53 \r
54 }\r