--- /dev/null
+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
+ ()
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;
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 {
}
}
+ @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;
+ }
+
+ };
+
+ }
+
}