1 package org.simantics.district.network.profile;
3 import java.util.HashSet;
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;
20 public class ArrowLengthStyle extends ThrottledStyleBase<Double> {
22 private static final Double PENDING = Double.NaN;
23 private static final Double ONE = 1.0;
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))
32 DiagramSettings ds = graph.syncRequest(new DiagramSettingsRequest(runtimeDiagram), TransientCacheListener.instance());
33 // Prevent PendingVariableExceptions from coming through
34 boolean wasSynchronous = graph.setSynchronous(true);
37 if (ds.arrowLengthProperty.isPresent()) {
38 length = Simantics.applySCLRead(graph, ds.arrowLengthProperty.get(), groupItem);
39 // System.out.println("read thickness: " + thickness + " : " + ds.arrowLengthProperty);
43 length = length * ds.arrowLengthGain + ds.arrowLengthBias;
50 graph.setSynchronous(wasSynchronous);
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);
61 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
63 for (INode nn : n.getNodes())
64 ProfileVariables.claimNodeProperty(nn, "arrowLength", value, observer);
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);
75 private static final class MidBranchEdgeSetRequest extends ResourceRead<Set<Resource>> {
76 private MidBranchEdgeSetRequest(Resource resource) {
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);