]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelColorRule.java
Merge "Added effectful Kleisli composition to Prelude."
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / SCLLabelColorRule.java
1 package org.simantics.modeling.adapters;
2
3 import org.eclipse.jface.resource.ColorDescriptor;
4 import org.eclipse.swt.graphics.RGB;
5 import org.simantics.Simantics;
6 import org.simantics.browsing.ui.model.labeldecorators.LabelDecorationRule;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.variable.Variable;
11 import org.simantics.db.layer0.variable.Variables;
12 import org.simantics.modeling.ColorDescriptorUtil;
13 import org.simantics.scl.runtime.function.Function;
14 import org.simantics.scl.runtime.tuple.Tuple3;
15
16 public abstract class SCLLabelColorRule implements LabelDecorationRule {
17
18     private Resource rule;
19     private Resource predicate;
20     
21     public SCLLabelColorRule(ReadGraph graph, Resource rule, Resource predicate) {
22         this.rule = rule;
23         this.predicate = predicate;
24     }
25     
26     @Override
27     public boolean isCompatible(Class<?> contentType) {
28         return contentType.equals(Resource.class);
29     }
30
31     @SuppressWarnings("unchecked")
32     protected <Color> Color decorateColor(ReadGraph graph, Object content, Color color, String column, int itemIndex) {
33         try {
34             Variable ruleVariable = Variables.getVariable(graph, rule);
35             Function fn = ruleVariable.getPossiblePropertyValue(graph, predicate);
36             if (fn != null) {
37                 Tuple3 prevColor = color != null ? ColorDescriptorUtil.colorDescriptorAsTuple3((ColorDescriptor)color) : null;
38                 Tuple3 result = Simantics.applySCLRead(graph, fn, content, prevColor, column, itemIndex);
39                 int r = (int)(((double)result.c0) * 255);
40                 int g = (int)(((double)result.c1) * 255);
41                 int b = (int)(((double)result.c2) * 255);
42                 return (Color)ColorDescriptor.createFrom(new RGB(r, g, b));
43             } else {
44                 return color;
45             }
46         } catch (DatabaseException e) {
47             throw new RuntimeException(e);
48         }
49     }
50 }