1 package org.simantics.modeling.adapters;
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;
16 public abstract class SCLLabelColorRule implements LabelDecorationRule {
18 private Resource rule;
19 private Resource predicate;
21 public SCLLabelColorRule(ReadGraph graph, Resource rule, Resource predicate) {
23 this.predicate = predicate;
27 public boolean isCompatible(Class<?> contentType) {
28 return contentType.equals(Resource.class);
31 @SuppressWarnings("unchecked")
32 protected <Color> Color decorateColor(ReadGraph graph, Object content, Color color, String column, int itemIndex) {
34 Variable ruleVariable = Variables.getVariable(graph, rule);
35 Function fn = ruleVariable.getPossiblePropertyValue(graph, predicate);
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));
46 } catch (DatabaseException e) {
47 throw new RuntimeException(e);