From 4573db95e4d5054feaa18c2baa9ae75ec5338577 Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Tue, 5 Sep 2017 15:13:51 +0300 Subject: [PATCH] Font property for DIA.TextElement refs #7466 Change-Id: I3ee21138f8488d207ec07a00f6ceaf177880577f --- .../graph/Diagram.pgraph | 5 + .../graph/DiagramElements.pgraph | 13 ++- .../META-INF/MANIFEST.MF | 3 +- .../org.simantics.diagram/build.properties | 3 +- .../Internal/DiagramOntologyEntryPoint.scl | 54 +++++++++++ .../org/simantics/diagram/function/All.java | 93 ++++++++++++++++++- 6 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 bundles/org.simantics.diagram/scl/Simantics/Diagram/Internal/DiagramOntologyEntryPoint.scl diff --git a/bundles/org.simantics.diagram.ontology/graph/Diagram.pgraph b/bundles/org.simantics.diagram.ontology/graph/Diagram.pgraph index 42aa3b0ef..ac3926b59 100644 --- a/bundles/org.simantics.diagram.ontology/graph/Diagram.pgraph +++ b/bundles/org.simantics.diagram.ontology/graph/Diagram.pgraph @@ -9,6 +9,11 @@ DIA = : 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 diff --git a/bundles/org.simantics.diagram.ontology/graph/DiagramElements.pgraph b/bundles/org.simantics.diagram.ontology/graph/DiagramElements.pgraph index 7fb684aef..87f6da68f 100644 --- a/bundles/org.simantics.diagram.ontology/graph/DiagramElements.pgraph +++ b/bundles/org.simantics.diagram.ontology/graph/DiagramElements.pgraph @@ -2,6 +2,7 @@ L0 = L0X = G2D = DIA = +SEL = // SHAPE ELEMENT (NOT USED, DEPRECATE?) @@ -39,7 +40,17 @@ DIA.TextElement -- DIA.TextElement.font ==> "String" L0.String diff --git a/bundles/org.simantics.diagram/META-INF/MANIFEST.MF b/bundles/org.simantics.diagram/META-INF/MANIFEST.MF index 758aa4869..a82491e11 100644 --- a/bundles/org.simantics.diagram/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.diagram/META-INF/MANIFEST.MF @@ -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, diff --git a/bundles/org.simantics.diagram/build.properties b/bundles/org.simantics.diagram/build.properties index 87675f07a..a45c45910 100644 --- a/bundles/org.simantics.diagram/build.properties +++ b/bundles/org.simantics.diagram/build.properties @@ -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 index 000000000..e395a1fde --- /dev/null +++ b/bundles/org.simantics.diagram/scl/Simantics/Diagram/Internal/DiagramOntologyEntryPoint.scl @@ -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 -> 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 -> () +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 + () diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/function/All.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/function/All.java index 67c136496..6cd4c542f 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/function/All.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/function/All.java @@ -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 applyCallback) { + + Control ctrl = (Control) parentControl; + FontData[] initialValue = null; + + try { + String font = Simantics.getSession().syncRequest(new UniqueRead() { + @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; + } + + }; + + } + } -- 2.43.2