]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/DNElementColorStyle.java
f8f6ba5c5760d8ddd2c1e118382dc3b35269dfa4
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / profile / DNElementColorStyle.java
1 package org.simantics.district.network.profile;
2
3 import java.awt.Color;
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.TransientCacheAsyncListener;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.diagram.profile.StyleBase;
11 import org.simantics.scenegraph.INode;
12 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
13 import org.simantics.scenegraph.profile.EvaluationContext;
14 import org.simantics.scenegraph.profile.common.ProfileVariables;
15
16 /**
17  * @author Tuukka Lehtonen
18  * @since 1.35.0
19  */
20 public class DNElementColorStyle extends ThrottledStyleBase<Color> {
21
22         private static final boolean DEBUG = false;
23
24         @Override
25         public Color calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
26                 DiagramSettings ds = graph.syncRequest(new DiagramSettingsRequest(runtimeDiagram), TransientCacheAsyncListener.instance());
27                 // Prevent PendingVariableExceptions from coming through
28                 boolean wasSynchronous = graph.setSynchronous(true);
29                 try {
30                         if (ds.elementColoringFunction.isPresent()) {
31                                 if (DEBUG)
32                                         System.out.print("elementColoringFunction: " + ds.elementColoringFunction + "(" + groupItem + "): ");
33                                 Double t = Simantics.applySCLRead(graph, ds.elementColoringFunction.get(), groupItem);
34                                 if (DEBUG)
35                                         System.out.print(t);
36                                 if (t != null) {
37                                         Color result = ds.coloringGradient.get(t);
38                                         //System.out.println("color(" + t + "): " + result);
39                                         return result;
40                                 }
41                         }
42                 }
43                 finally {
44                         graph.setSynchronous(wasSynchronous);
45                 }
46                 return null;
47         }
48
49         @Override
50         public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Color color) {
51                 SingleElementNode n = (SingleElementNode) node;
52                 for (INode nn : n.getNodes())
53                         ProfileVariables.claimNodeProperty(nn, "dynamicColor", color, observer);
54         }
55
56         @Override
57         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
58                 SingleElementNode n = (SingleElementNode) node;
59                 for (INode nn : n.getNodes())
60                         ProfileVariables.claimNodeProperty(nn, "dynamicColor", null, evaluationContext);
61         }
62
63 }