]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Font property for DIA.TextElement 17/917/4
authorAntti Villberg <antti.villberg@semantum.fi>
Tue, 5 Sep 2017 12:13:51 +0000 (15:13 +0300)
committerAntti Villberg <antti.villberg@semantum.fi>
Wed, 6 Sep 2017 06:32:01 +0000 (09:32 +0300)
refs #7466

Change-Id: I3ee21138f8488d207ec07a00f6ceaf177880577f

bundles/org.simantics.diagram.ontology/graph/Diagram.pgraph
bundles/org.simantics.diagram.ontology/graph/DiagramElements.pgraph
bundles/org.simantics.diagram/META-INF/MANIFEST.MF
bundles/org.simantics.diagram/build.properties
bundles/org.simantics.diagram/scl/Simantics/Diagram/Internal/DiagramOntologyEntryPoint.scl [new file with mode: 0644]
bundles/org.simantics.diagram/src/org/simantics/diagram/function/All.java

index 42aa3b0ef905524aedd53cb00e6bcc93edc45517..ac3926b595740e1c8856c97e06ad12a713171d93 100644 (file)
@@ -9,6 +9,11 @@ DIA = <http://www.simantics.org/Diagram-2.2> : L0.Ontology
     @L0.new
     L0.HasResourceClass "org.simantics.diagram.stubs.DiagramResource"
 
+DIA.SCLMain : L0.SCLModule
+    L0.SCLModule.definition """
+        include "Simantics/Diagram/Internal/DiagramOntologyEntryPoint"
+        """
+
 //// Import / Export
 
 DIA.DiagramSubgraphExtent : L0X.SubgraphExtent
index 7fb684aefb49b564b31f8436d901e6338c573019..87f6da68f046ad60d5634e82e4ca796dab734f9d 100644 (file)
@@ -2,6 +2,7 @@ L0 = <http://www.simantics.org/Layer0-1.1>
 L0X = <http://www.simantics.org/Layer0X-1.1>
 G2D = <http://www.simantics.org/G2D-1.1>
 DIA = <http://www.simantics.org/Diagram-2.2>
+SEL = <http://www.simantics.org/SelectionView-1.2>
 
 // SHAPE ELEMENT (NOT USED, DEPRECATE?)
 
@@ -39,7 +40,17 @@ DIA.TextElement <T DIA.Element <T DIA.ColorProvider <T DIA.FontProvider
     @L0.assert G2D.HasVerticalAlignment G2D.Alignment.Baseline
     @L0.assert G2D.HasStrokeWidth
         0 : L0.Float
-
+    >-- DIA.TextElement.font ==> "String" <R L0.HasProperty <R G2D.HasFont : SEL.GenericParameterType
+      L0.HasLabel "Font"
+      SEL.HasDisplayValue
+        _ : SEL.DisplayValue
+          SEL.HasCustomModifier
+            DIA.Functions.fontModifier : L0.Function
+          L0.valueAccessor
+            _ : L0.SCLValue
+              L0.SCLValue.expression "textFontValueAccessor"
+              L0.HasValueType "ValueAccessor" 
+              
 DIA.HasText <R L0.HasProperty : L0.FunctionalRelation
     L0.HasLabel "Text"
     --> L0.String
index 758aa4869a657e59abf396e14ca79840755f2a30..a82491e1128a538a35dd18dcdfd53d2a3ad866ab 100644 (file)
@@ -45,7 +45,8 @@ Require-Bundle: org.simantics.utils.thread.swt,
  org.eclipse.e4.ui.services,
  org.eclipse.e4.core.contexts,
  org.eclipse.e4.ui.workbench,
- org.slf4j.api;bundle-version="1.7.20"
+ org.slf4j.api;bundle-version="1.7.20",
+ org.simantics.browsing.ui
 Export-Package: org.simantics.diagram,
  org.simantics.diagram.adapter,
  org.simantics.diagram.connection,
index 87675f07aea1a75f12322fe9e6ef080d1fc05f15..a45c45910cdbdd7f2f5125c528f7ad560c1f6a6c 100644 (file)
@@ -15,4 +15,5 @@ bin.includes = META-INF/,\
                .,\
                plugin.xml,\
                icons/,\
-               adapters.xml
+               adapters.xml,\
+               scl/
diff --git a/bundles/org.simantics.diagram/scl/Simantics/Diagram/Internal/DiagramOntologyEntryPoint.scl b/bundles/org.simantics.diagram/scl/Simantics/Diagram/Internal/DiagramOntologyEntryPoint.scl
new file mode 100644 (file)
index 0000000..e395a1f
--- /dev/null
@@ -0,0 +1,54 @@
+import "Simantics/Variables"
+
+import "http://www.simantics.org/Diagram-2.2" as DIA
+import "http://www.simantics.org/G2D-1.1" as G2D
+
+textFontValueAccessor =
+  createValueAccessor
+    (\self -> decodeFont self)
+    (\self binding -> decodeFont self)
+    (\self value -> encodeFont self value)
+    (\self value binding -> encodeFont self value)
+    standardGetDatatype
+  
+decodeFont :: Variable -> <ReadGraph> String
+decodeFont self = do
+  element = represents $ parent $ parent self
+  match possibleObject element DIA.TextElement.font with
+    Nothing -> ""
+    Just font -> do
+      family = match possibleRelatedValue font G2D.HasFontFamily with
+        Nothing -> "Arial"
+        Just f -> f
+      size = match possibleRelatedValue font G2D.HasFontSize with
+        Nothing -> 12
+        Just s -> s
+      style = match possibleObject font G2D.HasFontStyle with
+        Just G2D.FontStyle.normal_font_style -> "Normal"
+        Just G2D.FontStyle.bold_font_style -> "Bold"
+        Just G2D.FontStyle.italic_font_style -> "Italic"
+        Just G2D.FontStyle.bold_italic_style -> "BoldItalic"
+        Nothing -> "Normal"
+      family + "," + (show size) + "," + style
+
+encodeFont :: Variable -> String -> <WriteGraph> ()
+encodeFont self value = do
+  parts = split "," value
+  match length parts with
+    3 -> do
+      element = represents $ parent $ parent self
+      denyByPredicate element DIA.TextElement.font
+      font = newResource ()
+      family = parts!0
+      size = read (parts!1) :: Integer
+      style = match parts!2 with
+        "Normal" -> G2D.FontStyle.normal_font_style
+        "Bold" -> G2D.FontStyle.bold_font_style
+        "Italic" -> G2D.FontStyle.italic_font_style
+        "BoldItalic" -> G2D.FontStyle.bold_italic_style
+      claim font L0.InstanceOf G2D.Font
+      claimRelatedValue font G2D.HasFontFamily (parts!0)
+      claimRelatedValue font G2D.HasFontSize size
+      claim font G2D.HasFontStyle style
+      claim element DIA.TextElement.font font
+  ()
index 67c136496d44576762a03b301b20a26b1fc8a1c0..6cd4c542f106afe53d4d80a774f4e9ace4e90e28 100644 (file)
@@ -5,11 +5,22 @@ import java.awt.geom.Point2D;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
-
+import java.util.function.Consumer;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.FontData;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.FontDialog;
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.content.Labeler.DialogModifier;
 import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
 import org.simantics.db.common.request.PossibleIndexRoot;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.exception.MissingVariableException;
 import org.simantics.db.layer0.request.PossibleConfiguration;
@@ -26,6 +37,9 @@ import org.simantics.modeling.template2d.ontology.Template2dResource;
 import org.simantics.scenegraph.loader.SceneGraphContext;
 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;
 import org.simantics.scl.reflection.annotations.SCLValue;
+import org.simantics.ui.fonts.Fonts;
+import org.simantics.utils.ui.AdaptionUtils;
+import org.simantics.utils.ui.ErrorLogger;
 
 
 public class All {
@@ -215,4 +229,81 @@ public class All {
         }
     }
 
+    @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
+    public static Object fontModifier(ReadGraph graph, Resource r, final Variable variable) throws DatabaseException {
+       return new DialogModifier() {
+
+               @Override
+               public String getValue() {
+                       return null;
+               }
+
+               @Override
+               public String isValid(String label) {
+                       return null;
+               }
+
+               @Override
+               public void modify(final String label) {
+                       Simantics.getSession().async(new WriteRequest() {
+
+                               @Override
+                               public void perform(WriteGraph graph) throws DatabaseException {
+                                       Variable displayValue = variable.getParent(graph);
+                                       displayValue.setValue(graph, label, Bindings.STRING);
+                               }
+
+                       });
+               }
+
+               public String query(Object parentControl, Object controlItem, int columnIndex, NodeContext context, Consumer<String> applyCallback) {
+                       
+                       Control ctrl = (Control) parentControl;
+                       FontData[] initialValue = null;
+                       
+                       try {
+                               String font = Simantics.getSession().syncRequest(new UniqueRead<String>() {
+                                       @Override
+                                       public String perform(ReadGraph graph) throws DatabaseException {
+                                               Variable v = AdaptionUtils.adaptToSingle(context, Variable.class);
+                                               if(v == null) return null;
+                                               String displayValue = v.getPossiblePropertyValue(graph, "HasDisplayValue", Bindings.STRING);
+                                               return displayValue;
+                                       }
+                               });
+                               if (font != null) {
+                                       String[] fields = font.split(",");
+                                       if (fields.length == 3) {
+                                               int size = 14;
+                                               try {
+                                                       size = Integer.parseInt(fields[1]);
+                                               } catch (NumberFormatException e) {
+                                                       ErrorLogger.defaultLogError(e);
+                                               }
+                                               int style = SWT.NORMAL;
+                                               try {
+                                                       style = Fonts.swtStyle(fields[2]);
+                                               } catch (RuntimeException e) {
+                                                       ErrorLogger.defaultLogError(e);
+                                               }
+                                               initialValue = new FontData[] { new FontData(fields[0], size, style) };
+                                       }
+                               }
+                       } catch (DatabaseException e) {
+                               ErrorLogger.defaultLogError(e);
+                       }
+
+                       FontDialog dialog = new FontDialog(ctrl.getShell());
+                       if (initialValue != null)
+                               dialog.setFontList(initialValue);
+                       FontData font = dialog.open();
+                       if (font != null)
+                               applyCallback.accept(font.getName() + "," + font.getHeight() + "," + Fonts.fromSwtStyle(font.getStyle()));
+                       return null;
+               }
+
+       };
+       
+    }
+    
 }