]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/profile/DNElementColorStyle.java
Add support for gradients in dynamic visualisations
[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 import java.util.Map;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.variable.Variable;
12 import org.simantics.db.layer0.variable.Variables;
13 import org.simantics.district.network.ontology.DistrictNetworkResource;
14 import org.simantics.district.network.visualisations.model.DynamicColorContribution;
15 import org.simantics.district.network.visualisations.model.DynamicColorMap;
16 import org.simantics.district.network.visualisations.model.DynamicVisualisation;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.modeling.ModelingResources;
19 import org.simantics.scenegraph.INode;
20 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
21 import org.simantics.scenegraph.profile.EvaluationContext;
22 import org.simantics.scenegraph.profile.common.ProfileVariables;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * @author Tuukka Lehtonen
28  * @since 1.35.0
29  */
30 public class DNElementColorStyle extends ThrottledStyleBase<Color> {
31
32     private static final Logger LOGGER = LoggerFactory.getLogger(DNElementColorStyle.class);
33
34     private static final boolean DEBUG = false;
35
36         @Override
37         public Color calculateThrottledStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
38             
39         DynamicVisualisation dv = graph.syncRequest(new RuntimeDynamicVisualisationsRequest(runtimeDiagram),
40                 TransientCacheAsyncListener.instance());
41
42         // Prevent PendingVariableExceptions from coming through
43         boolean wasSynchronous = graph.setSynchronous(true);
44         try {
45             if (dv != null) {
46                 Layer0 L0 = Layer0.getInstance(graph);
47                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
48                 ModelingResources MOD = ModelingResources.getInstance(graph);
49                 Resource mapping = graph.getSingleObject(groupItem, DN.HasMapping);
50             
51                 Map<String, DynamicColorContribution> colorContributions = dv.getColorContributions();
52                 String mappingName = graph.getRelatedValue(mapping, L0.HasName);
53                 DynamicColorContribution dcc = colorContributions.get(mappingName);
54                 if (dcc != null && dcc.isUsed()) {
55     
56                     Resource mappedComponent = graph.getPossibleObject(groupItem, DN.MappedComponent);
57                     if (mappedComponent != null) {
58     
59                         Resource component = graph.getSingleObject(mappedComponent, MOD.ElementToComponent);
60                         Variable variable = Variables.getVariable(graph, component);
61                         Variable possibleActiveVariable = Variables.possibleActiveVariable(graph, variable);
62                         if (possibleActiveVariable != null) {
63     
64                             Variable module = possibleActiveVariable.getPossibleChild(graph, dcc.getModuleName());
65                             if (module != null) {
66                                 Variable attribute = module.getPossibleProperty(graph, dcc.getAttributeName());
67                                 if (attribute != null) {
68                                     Double possibleValue = attribute.getPossibleValue(graph, Bindings.DOUBLE);
69                                     if (possibleValue != null) {
70                                         // here we do the adjusting according to spec in #15038
71                                         double adjustedValue = possibleValue.doubleValue() * dcc.getVariableGain() + dcc.getVariableBias();
72                                         DynamicColorMap defaultColorMap = dcc.getDefaultColorMap();
73                                         Color color = defaultColorMap.getColor(adjustedValue, dv.getColorBarOptions().isUseGradients(), dcc.getDefaultMin(), dcc.getDefaultMax());
74                                         return color;
75                                     } else {
76                                         LOGGER.warn("No value for {}", attribute.getURI(graph));
77                                     }
78                                 } else {
79                                     LOGGER.warn("Wrong attribute name {} for {} !!", dcc.getAttributeName(), module.getURI(graph));
80                                 }
81                             } else {
82                                 LOGGER.warn("Wrong modulename {} for {} !!", dcc.getModuleName(), possibleActiveVariable.getURI(graph));
83                             }
84                         } else {
85                             LOGGER.debug("No active experiment for {}", variable.getURI(graph));
86                         }
87                     } else {
88                         LOGGER.debug("No mapped component for {} to calculate dynamic color style", groupItem);
89                     }
90                 }
91             }
92                 }
93                 finally {
94                         graph.setSynchronous(wasSynchronous);
95                 }
96                 return null;
97         }
98
99         @Override
100         public void applyThrottledStyleForNode(EvaluationContext observer, INode node, Color color) {
101                 SingleElementNode n = (SingleElementNode) node;
102                 for (INode nn : n.getNodes())
103                         ProfileVariables.claimNodeProperty(nn, "dynamicColor", color, observer);
104         }
105
106         @Override
107         protected void cleanupStyleForNode(EvaluationContext evaluationContext, INode node) {
108                 SingleElementNode n = (SingleElementNode) node;
109                 for (INode nn : n.getNodes())
110                         ProfileVariables.claimNodeProperty(nn, "dynamicColor", null, evaluationContext);
111         }
112
113 }