]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/ArrowLengthStyle.java
Merge "Make vertices smaller on map UI & CSV import performance improvements"
[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
24         @Override
25         public Double calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
26                 Resource diagram = graph.getSingleObject(groupItem, Layer0.getInstance(graph).PartOf);
27                 Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
28                 if (!edgesToUse.contains(groupItem))
29                         return null;
30                 
31                 DiagramSettings ds = graph.syncRequest(new DiagramSettingsRequest(runtimeDiagram), TransientCacheListener.instance());
32                 // Prevent PendingVariableExceptions from coming through
33                 boolean wasSynchronous = graph.setSynchronous(true);
34                 try {
35                         if (ds.arrowLengthProperty.isPresent()) {
36                                 Double length = Simantics.applySCLRead(graph, ds.arrowLengthProperty.get(), groupItem);
37                                 return length != null ? length * ds.arrowLengthGain + ds.arrowLengthBias : null;
38                         }
39                         else {
40                                 return null;
41                         }
42                 }
43                 finally {
44                         graph.setSynchronous(wasSynchronous);
45                 }
46         }
47
48         @Override
49         public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Double value) {
50 //              System.out.println("apply: " + node + " : " + value);
51                 ConnectionNode n = (ConnectionNode) node;
52                 if (value == PENDING) {
53                         ((G2DSceneGraph)node.getRootNode()).setPending(node);
54                 } else {
55                         ((G2DSceneGraph)node.getRootNode()).clearPending(node);
56                 }
57                 for (INode nn : n.getNodes())
58                         ProfileVariables.claimNodeProperty(nn, "arrowLength", value, observer);
59         }
60
61         @Override
62         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
63                 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
64                 ConnectionNode n = (ConnectionNode) node;
65                 for (INode nn : n.getNodes())
66                         ProfileVariables.claimNodeProperty(nn, "arrowLength", null, evaluationContext);
67         }
68
69         private static final class MidBranchEdgeSetRequest extends ResourceRead<Set<Resource>> {
70                 private MidBranchEdgeSetRequest(Resource resource) {
71                         super(resource);
72                 }
73         
74                 @Override
75                 public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
76                         List<Resource> edges = Simantics.applySCL("Simantics/District/Algorithm", "midBranchEdges", graph, resource);
77                         return new HashSet<>(edges);
78                 }
79         }
80
81 }