]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/adapters/DistrictNetworkEdgeElementFactory.java
Add edge geometry for detailed rendering in closer zoom levels
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / adapters / DistrictNetworkEdgeElementFactory.java
index 1003a9b22b0a8cd55646c79f9dd4ad38520ed8bb..3b18dc6f0f3481068853253ae5658ed076e59b78 100644 (file)
@@ -2,6 +2,7 @@ package org.simantics.district.network.ui.adapters;
 
 import java.awt.geom.Point2D;
 
+import org.simantics.databoard.Bindings;
 import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
@@ -28,7 +29,8 @@ import org.simantics.maps.MapScalingTransform;
 public class DistrictNetworkEdgeElementFactory extends SyncElementFactory {
 
     public static final ElementClass CLASS = DistrictNetworkEdgeElement.CLASS;
-    
+    public static final double[] EMPTY = new double[0];
+
     private DistrictNetworkResource DN;
     private DiagramResource DIA;
     
@@ -46,19 +48,30 @@ public class DistrictNetworkEdgeElementFactory extends SyncElementFactory {
     protected Resource getElementClassBaseType(AsyncReadGraph graph) {
         return DN.Edge;
     }
-    
+
     @Override
     public void load(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource edgeResource, IElement element) throws DatabaseException {
         if (!graph.hasStatement(edgeResource))
             return; // already deleted
 
+        if (!graph.hasStatement(edgeResource, DN.HasStartVertex))
+            return; // already deleted
         Resource startVertex = graph.getSingleObject(edgeResource, DN.HasStartVertex);
+        
+        if (!graph.hasStatement(edgeResource, DN.HasEndVertex))
+            return; // already deleted
         Resource endVertex = graph.getSingleObject(edgeResource, DN.HasEndVertex);
 
-        // TODO: Find maybe a better way to apply the scaling 
-        double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation);
-        double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation);
-        DistrictNetworkEdge edge = new DistrictNetworkEdge(new Point2D.Double(startCoords[0], startCoords[1]), new Point2D.Double(endCoords[0], endCoords[1]));
+        double[] startCoords = graph.getRelatedValue2(startVertex, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
+        double[] endCoords = graph.getRelatedValue2(endVertex, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
+        
+        double[] geometry = EMPTY;
+        try {
+            geometry = graph.getPossibleRelatedValue2(edgeResource, DN.Edge_HasGeometry, Bindings.DOUBLE_ARRAY);
+        } catch (Exception e) {
+            // most likely no geometry available
+        }
+        DistrictNetworkEdge edge = new DistrictNetworkEdge(new Point2D.Double(startCoords[0], startCoords[1]), new Point2D.Double(endCoords[0], endCoords[1]), geometry);
 
         Resource mapping = graph.getSingleObject(edgeResource, DistrictNetworkResource.getInstance(graph).HasMapping);
         element.setHint(DistrictNetworkAdditionalColor.KEY_DN_MAPPING_RESOURCE, mapping);