]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/DistrictNetworkStaticInfoStyle.java
Enable static info labels for vertex elements
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / nodes / DistrictNetworkStaticInfoStyle.java
index 49d524a4b2f9525b9fcde4e4f95c96f466a5e59b..20e8ecbc4fa30727f0f4fa14a1029150350be62c 100644 (file)
@@ -55,13 +55,17 @@ public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkSta
                        throws DatabaseException {
                DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
                
-               if (!graph.isInstanceOf(mapElement, DN.Edge))
+               boolean isEdge = graph.isInstanceOf(mapElement, DN.Edge);
+               boolean isVertex = graph.isInstanceOf(mapElement, DN.Vertex);
+               if (!isEdge && !isVertex)
                        return null;
                
-               Resource diagram = graph.getSingleObject(mapElement, Layer0.getInstance(graph).PartOf);
-               Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
-               if (!edgesToUse.contains(mapElement))
-                       return null;
+               if (isEdge) {
+                       Resource diagram = graph.getSingleObject(mapElement, Layer0.getInstance(graph).PartOf);
+                       Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
+                       if (!edgesToUse.contains(mapElement))
+                               return null;
+               }
                
                DiagramResource DIA = DiagramResource.getInstance(graph);
                StructuralResource2 STR = StructuralResource2.getInstance(graph);
@@ -90,14 +94,23 @@ public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkSta
                        result = null;
                }
                
-               Resource v1 = graph.getSingleObject(mapElement, DN.HasStartVertex);
-               double[] coords1 = graph.getRelatedValue(v1, DIA.HasLocation);
-               Resource v2 = graph.getSingleObject(mapElement, DN.HasEndVertex);
-               double[] coords2 = graph.getRelatedValue(v2, DIA.HasLocation);
-               Point2D p1 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords1[0], coords1[1]), null);
-               Point2D p2 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords2[0], coords2[1]), null);
+               if (isVertex) {
+                       double[] coords = graph.getRelatedValue(mapElement, DIA.HasLocation);
+                       Point2D p = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords[0], coords[1]), null);
+                       return new StyleResult(p, p, result);
+               }
+               else if (isEdge) {
+                       Resource v1 = graph.getSingleObject(mapElement, DN.HasStartVertex);
+                       double[] coords1 = graph.getRelatedValue(v1, DIA.HasLocation);
+                       Resource v2 = graph.getSingleObject(mapElement, DN.HasEndVertex);
+                       double[] coords2 = graph.getRelatedValue(v2, DIA.HasLocation);
+                       Point2D p1 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords1[0], coords1[1]), null);
+                       Point2D p2 = DistrictNetworkNodeUtils.calculatePoint2D(new Point2D.Double(coords2[0], coords2[1]), null);
+                       
+                       return new StyleResult(p1, p2, result);
+               }
                
-               return new StyleResult(p1, p2, result);
+               return null;
        }
        
        @Override
@@ -114,11 +127,17 @@ public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkSta
                Point2D p1 = result.p1;
                Point2D p2 = result.p2;
                
-               double sign = Math.signum(p1.getX() - p2.getX());
-               Point2D.Double origin = new Point2D.Double(0.5 * (p1.getX() + p2.getX()), 0.5 * (p1.getY() + p2.getY()));
-               Point2D direction = new Point2D.Double(0.5 * sign * (p1.getX() - p2.getX()), 0.5 * sign * (p1.getY() - p2.getY()));
+               if (p1.equals(p2)) {
+                       node.setLocation(p1, new Point2D.Double(1.0, 0.0));
+               }
+               else {
+                       double sign = Math.signum(p1.getX() - p2.getX());
+                       Point2D.Double origin = new Point2D.Double(0.5 * (p1.getX() + p2.getX()), 0.5 * (p1.getY() + p2.getY()));
+                       Point2D direction = new Point2D.Double(0.5 * sign * (p1.getX() - p2.getX()), 0.5 * sign * (p1.getY() - p2.getY()));
+                       
+                       node.setLocation(origin, direction);
+               }
                
-               node.setLocation(origin, direction);
                node.setInfo(result.info);
        }
        
@@ -155,6 +174,6 @@ public class DistrictNetworkStaticInfoStyle extends StyleBase<DistrictNetworkSta
        
        @Override
        protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
-               ProfileVariables.denyChild(node, DistrictNetworkStaticInfoNode.NODE_KEY);
+               ProfileVariables.denyChild(node, "*", DistrictNetworkStaticInfoNode.NODE_KEY);
        }
 }