]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/ArrowLengthStyle.java
Static information label profile definition.
[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.Set;
4
5 import org.simantics.Simantics;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.layer0.Layer0;
11 import org.simantics.scenegraph.INode;
12 import org.simantics.scenegraph.g2d.G2DSceneGraph;
13 import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
14 import org.simantics.scenegraph.profile.EvaluationContext;
15 import org.simantics.scenegraph.profile.common.ProfileVariables;
16
17 public class ArrowLengthStyle extends ThrottledStyleBase<Double> {
18
19         private static final Double PENDING = Double.NaN;
20
21         @Override
22         public Double calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
23                 Resource diagram = graph.getSingleObject(groupItem, Layer0.getInstance(graph).PartOf);
24                 Set<Resource> edgesToUse = graph.syncRequest(new MidBranchEdgeSetRequest(diagram), TransientCacheListener.instance());
25                 if (!edgesToUse.contains(groupItem))
26                         return null;
27                 
28                 DiagramSettings ds = graph.syncRequest(new DiagramSettingsRequest(runtimeDiagram), TransientCacheListener.instance());
29                 // Prevent PendingVariableExceptions from coming through
30                 boolean wasSynchronous = graph.setSynchronous(true);
31                 try {
32                         if (ds.arrowLengthProperty.isPresent()) {
33                                 Double length = Simantics.applySCLRead(graph, ds.arrowLengthProperty.get(), groupItem);
34                                 return length != null ? length * ds.arrowLengthGain + ds.arrowLengthBias : null;
35                         }
36                         else {
37                                 return null;
38                         }
39                 }
40                 finally {
41                         graph.setSynchronous(wasSynchronous);
42                 }
43         }
44
45         @Override
46         public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Double value) {
47 //              System.out.println("apply: " + node + " : " + value);
48                 ConnectionNode n = (ConnectionNode) node;
49                 if (value == PENDING) {
50                         ((G2DSceneGraph)node.getRootNode()).setPending(node);
51                 } else {
52                         ((G2DSceneGraph)node.getRootNode()).clearPending(node);
53                 }
54                 for (INode nn : n.getNodes())
55                         ProfileVariables.claimNodeProperty(nn, "arrowLength", value, observer);
56         }
57
58         @Override
59         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
60                 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
61                 ConnectionNode n = (ConnectionNode) node;
62                 for (INode nn : n.getNodes())
63                         ProfileVariables.claimNodeProperty(nn, "arrowLength", null, evaluationContext);
64         }
65 }