From: Jussi Koskela Date: Thu, 21 Mar 2019 10:48:21 +0000 (+0200) Subject: Possibility to define label color decorator rules in SCL X-Git-Tag: v1.43.0~136^2~172 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=67918d39fd9a67b659dcb118412fe0313a8fc0e8;p=simantics%2Fplatform.git Possibility to define label color decorator rules in SCL gitlab #279 Change-Id: I9e34cb3af0434492ec26795f4f044c4e2484ecce --- diff --git a/bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph b/bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph index e3c551d0a..47931cc4f 100644 --- a/bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph +++ b/bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph @@ -26,6 +26,12 @@ MOD.SCLLabelRule -- MOD.SCLImageRule.getImages ==> "Resource -> [(String,ImageDescriptor)]" -- MOD.SCLLabelForegroundColorRule.getColor ==> "Resource -> Maybe (Double, Double, Double) -> String -> Integer -> Maybe (Double, Double, Double)]" -- MOD.SCLLabelBackgroundColorRule.getColor ==> "Resource -> Maybe (Double, Double, Double) -> String -> Integer -> Maybe (Double, Double, Double)]" MOD.SCLAction.action ==> "Resource -> ()" [(String,ImageDescriptor)]" +MOD.sclLabelForegroundColorRule : L0.Template + @template %action %expression + %action : MOD.SCLLabelForegroundColorRule + MOD.SCLLabelForegroundColorRule.getColor _ : MOD.SCLValue + L0.SCLValue.expression %expression + L0.HasValueType "Resource -> Maybe (Double, Double, Double) -> String -> Integer -> Maybe (Double, Double, Double)" + +MOD.sclLabelBackgroundColorRule : L0.Template + @template %action %expression + %action : MOD.SCLLabelBackgroundColorRule + MOD.SCLLabelBackgroundColorRule.getColor _ : MOD.SCLValue + L0.SCLValue.expression %expression + L0.HasValueType "Resource -> Maybe (Double, Double, Double) -> String -> Integer -> Maybe (Double, Double, Double)" + MOD.sclAction : L0.Template @template %action %expression %action : MOD.SCLAction diff --git a/bundles/org.simantics.modeling.ui/scl/Simantics/Testing/BrowseContext.scl b/bundles/org.simantics.modeling.ui/scl/Simantics/Testing/BrowseContext.scl index 106d93bb3..590ddd21c 100644 --- a/bundles/org.simantics.modeling.ui/scl/Simantics/Testing/BrowseContext.scl +++ b/bundles/org.simantics.modeling.ui/scl/Simantics/Testing/BrowseContext.scl @@ -78,6 +78,8 @@ importJava "org.simantics.browsing.ui.content.ImageDecorator" where importJava "org.eclipse.jface.resource.DeviceResourceDescriptor" where data FontDescriptor + +importJava "org.eclipse.jface.resource.ColorDescriptor" where data ColorDescriptor importJava "org.eclipse.jface.resource.FontDescriptor" where @@ -85,6 +87,11 @@ importJava "org.eclipse.jface.resource.FontDescriptor" where @JavaName createFrom createFontDescriptorFrom :: String -> Integer -> Integer -> FontDescriptor +importJava "org.simantics.modeling.ColorDescriptorUtil" where + colorDescriptorAsHex :: ColorDescriptor -> String + colorDescriptorAsTuple3 :: ColorDescriptor -> (Double, Double, Double) + colorDescriptor :: (Double, Double, Double) -> ColorDescriptor + defaultFontDescriptor = createFontDescriptorFrom "Arial" 12 0 importJava "org.simantics.browsing.ui.content.LabelDecorator" where diff --git a/bundles/org.simantics.modeling/META-INF/MANIFEST.MF b/bundles/org.simantics.modeling/META-INF/MANIFEST.MF index 78a1d3b59..c1d891b0a 100644 --- a/bundles/org.simantics.modeling/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.modeling/META-INF/MANIFEST.MF @@ -40,7 +40,8 @@ Require-Bundle: org.simantics.simulation;bundle-version="1.0.0", org.slf4j.api, org.simantics.graphfile.ontology, org.apache.batik, - org.simantics.graph.compiler + org.simantics.graph.compiler, + org.simantics.browsing.ui;bundle-version="1.1.0" Export-Package: org.simantics.modeling, org.simantics.modeling.actions, org.simantics.modeling.adapters, diff --git a/bundles/org.simantics.modeling/adapters.xml b/bundles/org.simantics.modeling/adapters.xml index 0e104229a..7693586d8 100644 --- a/bundles/org.simantics.modeling/adapters.xml +++ b/bundles/org.simantics.modeling/adapters.xml @@ -197,6 +197,22 @@ + + + + + + + + + + + + + + diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/ColorDescriptorUtil.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/ColorDescriptorUtil.java new file mode 100644 index 000000000..3f02f54f7 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/ColorDescriptorUtil.java @@ -0,0 +1,23 @@ +package org.simantics.modeling; + +import org.eclipse.jface.resource.ColorDescriptor; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Display; +import org.simantics.scl.runtime.tuple.Tuple3; + +public class ColorDescriptorUtil { + public static ColorDescriptor colorDescriptor(Tuple3 color) { + return ColorDescriptor.createFrom(new RGB((int)(((double)color.c0) * 255), (int)(((double)color.c1) * 255), (int)(((double)color.c2) * 255))); + } + + public static Tuple3 colorDescriptorAsTuple3(ColorDescriptor descriptor) { + Color color = ((ColorDescriptor)descriptor).createColor(Display.getDefault()); + return new Tuple3(color.getRed(), color.getGreen(), color.getBlue()); + } + + public synchronized static String colorDescriptorAsHex(ColorDescriptor descriptor) { + Color color = ((ColorDescriptor)descriptor).createColor(Display.getDefault()); + return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue()); + } +} diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelBackgroundColorRule.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelBackgroundColorRule.java new file mode 100644 index 000000000..04b1e1873 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelBackgroundColorRule.java @@ -0,0 +1,26 @@ +package org.simantics.modeling.adapters; + +import org.simantics.browsing.ui.content.LabelDecorator; +import org.simantics.browsing.ui.model.labeldecorators.AbstractLabelDecorator; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.modeling.ModelingResources; + +public class SCLLabelBackgroundColorRule extends SCLLabelColorRule { + + public SCLLabelBackgroundColorRule(ReadGraph graph, Resource rule) { + super(graph, rule, ModelingResources.getInstance(graph).SCLLabelBackgroundColorRule_getColor); + } + + @Override + public LabelDecorator getLabelDecorator(ReadGraph graph, Object content) throws DatabaseException { + return new AbstractLabelDecorator() { + + @Override + public Color decorateBackground(Color color, String column, int itemIndex) { + return decorateColor(graph, content, color, column, itemIndex); + } + }; + } +} \ No newline at end of file diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelColorRule.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelColorRule.java new file mode 100644 index 000000000..db3cf4b94 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelColorRule.java @@ -0,0 +1,50 @@ +package org.simantics.modeling.adapters; + +import org.eclipse.jface.resource.ColorDescriptor; +import org.eclipse.swt.graphics.RGB; +import org.simantics.Simantics; +import org.simantics.browsing.ui.model.labeldecorators.LabelDecorationRule; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.modeling.ColorDescriptorUtil; +import org.simantics.scl.runtime.function.Function; +import org.simantics.scl.runtime.tuple.Tuple3; + +public abstract class SCLLabelColorRule implements LabelDecorationRule { + + private Resource rule; + private Resource predicate; + + public SCLLabelColorRule(ReadGraph graph, Resource rule, Resource predicate) { + this.rule = rule; + this.predicate = predicate; + } + + @Override + public boolean isCompatible(Class contentType) { + return contentType.equals(Resource.class); + } + + @SuppressWarnings("unchecked") + protected Color decorateColor(ReadGraph graph, Object content, Color color, String column, int itemIndex) { + try { + Variable ruleVariable = Variables.getVariable(graph, rule); + Function fn = ruleVariable.getPossiblePropertyValue(graph, predicate); + if (fn != null) { + Tuple3 prevColor = color != null ? ColorDescriptorUtil.colorDescriptorAsTuple3((ColorDescriptor)color) : null; + Tuple3 result = Simantics.applySCLRead(graph, fn, content, prevColor, column, itemIndex); + int r = (int)(((double)result.c0) * 255); + int g = (int)(((double)result.c1) * 255); + int b = (int)(((double)result.c2) * 255); + return (Color)ColorDescriptor.createFrom(new RGB(r, g, b)); + } else { + return color; + } + } catch (DatabaseException e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelForegroundColorRule.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelForegroundColorRule.java new file mode 100644 index 000000000..bf9c45874 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelForegroundColorRule.java @@ -0,0 +1,26 @@ +package org.simantics.modeling.adapters; + +import org.simantics.browsing.ui.content.LabelDecorator; +import org.simantics.browsing.ui.model.labeldecorators.AbstractLabelDecorator; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.modeling.ModelingResources; + +public class SCLLabelForegroundColorRule extends SCLLabelColorRule { + + public SCLLabelForegroundColorRule(ReadGraph graph, Resource rule) { + super(graph, rule, ModelingResources.getInstance(graph).SCLLabelForegroundColorRule_getColor); + } + + @Override + public LabelDecorator getLabelDecorator(ReadGraph graph, Object content) throws DatabaseException { + return new AbstractLabelDecorator() { + + @Override + public Color decorateForeground(Color color, String column, int itemIndex) { + return decorateColor(graph, content, color, column, itemIndex); + } + }; + } +} \ No newline at end of file