-package org.simantics.district.network.ui;\r
-\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.ServiceException;\r
-import org.simantics.db.layer0.util.Layer0Utils;\r
-import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;\r
-import org.simantics.diagram.synchronization.SynchronizationHints;\r
-import org.simantics.diagram.synchronization.graph.AddElement;\r
-import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;\r
-import org.simantics.diagram.synchronization.graph.layer.GraphLayer;\r
-import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;\r
-import org.simantics.diagram.ui.DiagramModelHints;\r
-import org.simantics.district.network.DistrictNetworkUtil;\r
-import org.simantics.district.network.ontology.DistrictNetworkResource;\r
-import org.simantics.g2d.diagram.IDiagram;\r
-\r
-public class DNEdgeBuilder {\r
- \r
- private Resource diagramResource;\r
- private IDiagram diagram;\r
- private GraphLayerManager glm;\r
-\r
- public DNEdgeBuilder(Resource diagramResource, IDiagram diagram) {\r
- this.diagramResource = diagramResource;\r
- this.diagram = diagram;\r
- \r
- IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);\r
- glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);\r
- }\r
-\r
- public void create(WriteGraph graph, double[] start, double[] end) throws DatabaseException {\r
- \r
- DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);\r
- \r
- // 1. Get diagram edge to construct\r
- Resource edge = getOrCreateEdge(graph);\r
- \r
- // 1.1 Give running name to connection and increment the counter attached to the diagram.\r
- AddElement.claimFreshElementName(graph, diagramResource, edge);\r
- \r
- // 2. Add vertices\r
- Resource startVertex = getOrCreateVertex(graph, start);\r
- Resource endVertex = getOrCreateVertex(graph, end);\r
- \r
- graph.claim(edge, DN.HasStartVertex, startVertex);\r
- graph.claim(edge, DN.HasEndVertex, endVertex);\r
- \r
- // 7. Put the element on all the currently active layers if possible.\r
- if (glm != null) {\r
- putOnActiveLayer(graph, edge);\r
- putOnActiveLayer(graph, startVertex);\r
- putOnActiveLayer(graph, endVertex);\r
- }\r
- \r
- Layer0Utils.addCommentMetadata(graph, "Added edge " + edge);\r
- graph.markUndoPoint();\r
- }\r
- \r
- private void putOnActiveLayer(WriteGraph graph, Resource res) throws DatabaseException {\r
- glm.removeFromAllLayers(graph, res);\r
- glm.putElementOnVisibleLayers(diagram, graph, res);\r
- }\r
-\r
- private Resource getOrCreateVertex(WriteGraph graph, double[] coords) throws DatabaseException {\r
- // TODO: check if vertex exists already, for now create always new\r
- Resource vertex = null;\r
- if (vertex == null) {\r
- vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords); \r
- AddElement.claimFreshElementName(graph, diagramResource, vertex);\r
- }\r
- return vertex;\r
- }\r
-\r
- private Resource getOrCreateEdge(WriteGraph graph) throws DatabaseException {\r
- return DistrictNetworkUtil.createEdge(graph, diagramResource);\r
- }\r
-\r
-}\r
+package org.simantics.district.network.ui;
+
+import java.awt.geom.Rectangle2D;
+import java.util.Collection;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ObjectsWithType;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
+import org.simantics.diagram.synchronization.SynchronizationHints;
+import org.simantics.diagram.synchronization.graph.AddElement;
+import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
+import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
+import org.simantics.district.network.DistrictNetworkUtil;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.layer0.Layer0;
+
+public class DNEdgeBuilder {
+
+ private Resource diagramResource;
+ private IDiagram diagram;
+ private GraphLayerManager glm;
+
+ public DNEdgeBuilder(Resource diagramResource, IDiagram diagram) {
+ this.diagramResource = diagramResource;
+ this.diagram = diagram;
+
+ IModifiableSynchronizationContext context = diagram.getHint(SynchronizationHints.CONTEXT);
+ glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER);
+ }
+
+ public void create(WriteGraph graph, double[] start, double[] end) throws DatabaseException {
+
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+
+ // 1. Get diagram edge to construct
+ Resource edge = getOrCreateEdge(graph);
+
+ // 1.1 Give running name to connection and increment the counter attached to the diagram.
+ AddElement.claimFreshElementName(graph, diagramResource, edge);
+
+ // 2. Add vertices
+ Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
+ Resource startVertex = getOrCreateVertex(graph, vertices, start);
+ Resource endVertex = getOrCreateVertex(graph, vertices, end);
+
+ graph.claim(edge, DN.HasStartVertex, startVertex);
+ graph.claim(edge, DN.HasEndVertex, endVertex);
+
+ // 7. Put the element on all the currently active layers if possible.
+ if (glm != null) {
+ putOnActiveLayer(graph, edge);
+ putOnActiveLayer(graph, startVertex);
+ putOnActiveLayer(graph, endVertex);
+ }
+
+ Layer0Utils.addCommentMetadata(graph, "Added edge " + edge);
+ graph.markUndoPoint();
+ }
+
+ private void putOnActiveLayer(WriteGraph graph, Resource res) throws DatabaseException {
+ glm.removeFromAllLayers(graph, res);
+ glm.putElementOnVisibleLayers(diagram, graph, res);
+ }
+
+ private Resource getOrCreateVertex(WriteGraph graph, Collection<Resource> vertices, double[] coords) throws DatabaseException {
+ Resource vertex = null;
+ for (Resource vertx : vertices) {
+ double[] existingCoords = graph.getRelatedValue2(vertx, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
+ Rectangle2D existing = new Rectangle2D.Double(existingCoords[0], existingCoords[1], 1, 1);
+ Rectangle2D tobecreated = new Rectangle2D.Double(coords[0], coords[1], 1, 1);
+ if (existing.intersects(tobecreated)) {
+ vertex = vertx;
+ break;
+ }
+ }
+ if (vertex == null) {
+ vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords);
+ AddElement.claimFreshElementName(graph, diagramResource, vertex);
+ }
+ return vertex;
+ }
+
+ private Resource getOrCreateEdge(WriteGraph graph) throws DatabaseException {
+ return DistrictNetworkUtil.createEdge(graph, diagramResource);
+ }
+
+}