]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Possibility to define label color decorator rules in SCL 94/2794/1
authorJussi Koskela <jussi.koskela@semantum.fi>
Thu, 21 Mar 2019 10:48:21 +0000 (12:48 +0200)
committerJussi Koskela <jussi.koskela@semantum.fi>
Thu, 21 Mar 2019 10:48:21 +0000 (12:48 +0200)
gitlab #279

Change-Id: I9e34cb3af0434492ec26795f4f044c4e2484ecce

bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph
bundles/org.simantics.modeling.ui/scl/Simantics/Testing/BrowseContext.scl
bundles/org.simantics.modeling/META-INF/MANIFEST.MF
bundles/org.simantics.modeling/adapters.xml
bundles/org.simantics.modeling/src/org/simantics/modeling/ColorDescriptorUtil.java [new file with mode: 0644]
bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelBackgroundColorRule.java [new file with mode: 0644]
bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelColorRule.java [new file with mode: 0644]
bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelForegroundColorRule.java [new file with mode: 0644]

index e3c551d0a2f45faf7d361a98e6c90bc77fa3eae1..47931cc4f1172e7eb0bc631471114d8fd648209b 100644 (file)
@@ -26,6 +26,12 @@ MOD.SCLLabelRule <T VP.VisualsRule
 MOD.SCLImageRule <T VP.VisualsRule
   >-- MOD.SCLImageRule.getImages ==> "Resource -> <ReadGraph> [(String,ImageDescriptor)]" <R L0.HasProperty : L0.FunctionalRelation
 
+MOD.SCLLabelForegroundColorRule <T VP.VisualsRule
+  >-- MOD.SCLLabelForegroundColorRule.getColor ==> "Resource -> Maybe (Double, Double, Double) -> String -> Integer -> <ReadGraph> Maybe (Double, Double, Double)]" <R L0.HasProperty : L0.FunctionalRelation
+
+MOD.SCLLabelBackgroundColorRule <T VP.VisualsRule
+  >-- MOD.SCLLabelBackgroundColorRule.getColor ==> "Resource -> Maybe (Double, Double, Double) -> String -> Integer -> <ReadGraph> Maybe (Double, Double, Double)]" <R L0.HasProperty : L0.FunctionalRelation
+
 MOD.SCLAction <T ACT.Action
   --> MOD.SCLAction.action ==> "Resource -> <Proc> ()" <R L0.HasProperty : L0.FunctionalRelation 
 
@@ -392,6 +398,20 @@ MOD.sclImageRule : L0.Template
             L0.SCLValue.expression %expression
             L0.HasValueType "Resource -> <ReadGraph> [(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 -> <ReadGraph> 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 -> <ReadGraph> Maybe (Double, Double, Double)"
+
 MOD.sclAction : L0.Template
     @template %action %expression
         %action : MOD.SCLAction
index 106d93bb322410cbc6e2d2d4728c53174bda04e2..590ddd21ce8f658d40b133dd6cb0fddd3be8607d 100644 (file)
@@ -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 -> <Proc> String
+    colorDescriptorAsTuple3 :: ColorDescriptor -> <Proc> (Double, Double, Double)
+    colorDescriptor :: (Double, Double, Double) -> <Proc> ColorDescriptor
+
 defaultFontDescriptor = createFontDescriptorFrom "Arial" 12 0 
 
 importJava "org.simantics.browsing.ui.content.LabelDecorator" where
index 78a1d3b59a54522562be445e34c7beae4c4358d2..c1d891b0aed6c0fad748cffca51d84d6ca8ecdf8 100644 (file)
@@ -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,
index 0e104229aaee577d25d988f58be4e1863e6f0597..7693586d8029b79dcb5ccc80d838e3821644e791 100644 (file)
         </type>
     </target>    
 
+    <target interface="org.simantics.browsing.ui.model.visuals.VisualsRule">
+        <type uri="http://www.simantics.org/Modeling-0.0/SCLLabelForegroundColorRule"
+            class="org.simantics.modeling.adapters.SCLLabelForegroundColorRule">
+            <graph />
+            <this />
+        </type>
+    </target>
+
+    <target interface="org.simantics.browsing.ui.model.visuals.VisualsRule">
+        <type uri="http://www.simantics.org/Modeling-0.0/SCLLabelBackgroundColorRule"
+            class="org.simantics.modeling.adapters.SCLLabelBackgroundColorRule">
+            <graph />
+            <this />
+        </type>
+    </target>
+
     <target interface="org.simantics.browsing.ui.model.tests.Test">
         <type uri="http://www.simantics.org/Modeling-0.0/SCLTest"
             class="org.simantics.modeling.adapters.SCLTest">
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 (file)
index 0000000..3f02f54
--- /dev/null
@@ -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 (file)
index 0000000..04b1e18
--- /dev/null
@@ -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> 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 (file)
index 0000000..db3cf4b
--- /dev/null
@@ -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> 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 (file)
index 0000000..bf9c458
--- /dev/null
@@ -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> Color decorateForeground(Color color, String column, int itemIndex) {
+                return decorateColor(graph, content, color, column, itemIndex);
+            }
+        };
+    }
+}
\ No newline at end of file