]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/VertexSizeStyle.java
First draft of vertex size adjusting district network diagram profiles
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / profile / VertexSizeStyle.java
1 package org.simantics.district.network.profile;
2
3 import org.simantics.Simantics;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.diagram.profile.StyleBase;
9 import org.simantics.scenegraph.INode;
10 import org.simantics.scenegraph.g2d.G2DSceneGraph;
11 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
12 import org.simantics.scenegraph.profile.EvaluationContext;
13 import org.simantics.scenegraph.profile.common.ProfileVariables;
14
15 public class VertexSizeStyle extends StyleBase<Double> {
16
17         private static final Double PENDING = Double.NaN;
18         private static final Double ONE = 1.0;
19
20         @Override
21         public Double calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
22                 DiagramSettings ds = graph.syncRequest(new DiagramSettingsRequest(runtimeDiagram), TransientCacheAsyncListener.instance());
23                 Double scaling = ONE;
24                 if (ds.vertexScaleProperty.isPresent()) {
25                         scaling = Simantics.applySCLRead(graph, ds.vertexScaleProperty.get(), groupItem);
26                         if (scaling == null) {
27                                 scaling = ONE;
28                         } else {
29 //                              System.out.println("read vertex scaling: " + scaling + " : " + ds.vertexScaleProperty);
30                                 scaling = scaling * ds.vertexScaleGain + ds.vertexScaleBias;
31 //                              System.out.println("RESULT: " + scaling);
32                         }
33                 }
34                 return scaling;
35         }
36
37         @Override
38         public void applyStyleForNode(EvaluationContext observer, INode node, Double value) {
39                 //System.out.println("apply: " + node + " : " + value);
40                 SingleElementNode n = (SingleElementNode) node;
41                 if (value == PENDING) {
42                         ((G2DSceneGraph)node.getRootNode()).setPending(node);
43                 } else {
44                         ((G2DSceneGraph)node.getRootNode()).clearPending(node);
45                 }
46                 if (value == null)
47                         value = ONE;
48                 for (INode nn : n.getNodes())
49                         ProfileVariables.claimNodeProperty(nn, "size", value, observer);
50         }
51
52         @Override
53         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
54                 ((G2DSceneGraph)node.getRootNode()).clearPending(node);
55                 SingleElementNode n = (SingleElementNode) node;
56                 for (INode nn : n.getNodes())
57                         ProfileVariables.claimNodeProperty(nn, "size", ONE, evaluationContext);
58         }
59
60 }