]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/ArrowLengthStyle.java
2e8c3458164770959409e9aa7fce6f87b2276532
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / profile / ArrowLengthStyle.java
1 package org.simantics.district.network.profile;
2
3 import java.util.HashSet;
4 import java.util.List;
5 import java.util.Set;
6
7 import org.simantics.Simantics;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
11 import org.simantics.db.common.request.ResourceRead;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.layer0.Layer0;
14 import org.simantics.scenegraph.INode;
15 import org.simantics.scenegraph.g2d.G2DSceneGraph;
16 import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
17 import org.simantics.scenegraph.profile.EvaluationContext;
18 import org.simantics.scenegraph.profile.common.ProfileVariables;
19
20 public class ArrowLengthStyle extends ThrottledStyleBase<Double> {
21
22         private static final Double PENDING = Double.NaN;
23         private static final Double ONE = 1.0;
24
25         @Override
26         public Double calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
27                 Resource diagram = graph.getSingleObject(groupItem, Layer0.getInstance(graph).PartOf);
28                 Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
29                 if (!edgesToUse.contains(groupItem))
30                         return null;
31                 
32                 DiagramSettings ds = graph.syncRequest(new DiagramSettingsRequest(runtimeDiagram), TransientCacheListener.instance());
33                 // Prevent PendingVariableExceptions from coming through
34                 boolean wasSynchronous = graph.setSynchronous(true);
35                 try {
36                         Double length = ONE;
37                         if (ds.arrowLengthProperty.isPresent()) {
38                                 length = Simantics.applySCLRead(graph, ds.arrowLengthProperty.get(), groupItem);
39         //                      System.out.println("read thickness: " + thickness + " : " + ds.arrowLengthProperty);
40                                 if (length == null) {
41                                         length = ONE;
42                                 } else {
43                                         length = length * ds.arrowLengthGain + ds.arrowLengthBias;
44                                 }
45                         }
46                         
47                         return length;
48                 }
49                 finally {
50                         graph.setSynchronous(wasSynchronous);
51                 }
52         }
53
54         @Override
55         public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Double value) {
56 //              System.out.println("apply: " + node + " : " + value);
57                 ConnectionNode n = (ConnectionNode) node;
58                 if (value == PENDING) {
59                         ((G2DSceneGraph)node.getRootNode()).setPending(node);
60                 } else {
61                         ((G2DSceneGraph)node.getRootNode()).clearPending(node);
62                 }
63                 for (INode nn : n.getNodes())
64                         ProfileVariables.claimNodeProperty(nn, "arrowLength", value, observer);
65         }
66
67         @Override
68         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
69                 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
70                 ConnectionNode n = (ConnectionNode) node;
71                 for (INode nn : n.getNodes())
72                         ProfileVariables.claimNodeProperty(nn, "arrowLength", null, evaluationContext);
73         }
74
75         private static final class MidBranchEdgeSetRequest extends ResourceRead<Set<Resource>> {
76                 private MidBranchEdgeSetRequest(Resource resource) {
77                         super(resource);
78                 }
79         
80                 @Override
81                 public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
82                         List<Resource> edges = Simantics.applySCL("Simantics/District/Algorithm", "midBranchEdges", graph, resource);
83                         return new HashSet<>(edges);
84                 }
85         }
86
87 }