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;
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))
31 DiagramSettings ds = graph.syncRequest(new DiagramSettingsRequest(runtimeDiagram), TransientCacheListener.instance());
32 // Prevent PendingVariableExceptions from coming through
33 boolean wasSynchronous = graph.setSynchronous(true);
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;
44 graph.setSynchronous(wasSynchronous);
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);
55 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
57 for (INode nn : n.getNodes())
58 ProfileVariables.claimNodeProperty(nn, "arrowLength", value, observer);
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);
69 private static final class MidBranchEdgeSetRequest extends ResourceRead<Set<Resource>> {
70 private MidBranchEdgeSetRequest(Resource resource) {
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);