package org.simantics.district.network.ui; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ServiceException; import org.simantics.db.layer0.util.Layer0Utils; 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.GraphLayer; import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager; import org.simantics.diagram.ui.DiagramModelHints; import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.g2d.diagram.IDiagram; 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 Resource startVertex = getOrCreateVertex(graph, start); Resource endVertex = getOrCreateVertex(graph, 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, double[] coords) throws DatabaseException { // TODO: check if vertex exists already, for now create always new Resource vertex = null; 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); } }