From: Hannu Niemistö Date: Wed, 3 May 2017 13:38:56 +0000 (+0300) Subject: (refs #7180) Configurable font for FlagNode X-Git-Tag: v1.29.0~79 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=6b0f2d47b9137d0f92e30b50175195ee878485bc;p=simantics%2Fplatform.git (refs #7180) Configurable font for FlagNode Change-Id: I01e041e83ed8d66ad203107c2b4ff1d670756e5f --- diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/FlagClassFactory.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/FlagClassFactory.java index d0a44984d..30efa54f0 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/FlagClassFactory.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/FlagClassFactory.java @@ -11,6 +11,7 @@ *******************************************************************************/ package org.simantics.diagram.adapter; +import java.awt.Font; import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; @@ -25,7 +26,6 @@ import java.util.regex.PatternSyntaxException; import org.simantics.databoard.Bindings; import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; import org.simantics.databoard.util.Bean; -import org.simantics.datatypes.literal.Font; import org.simantics.datatypes.literal.RGB; import org.simantics.db.AsyncReadGraph; import org.simantics.db.ReadGraph; @@ -203,6 +203,11 @@ public class FlagClassFactory extends SyncElementFactory { e.setHint(FlagClass.KEY_FLAG_TEXT, flagText); flagTextIsSet = true; } + + Font flagFont = info.getFont(); + if(flagFont != null) { + e.setHint(FlagClass.KEY_FLAG_FONT, flagFont); + } if (info.getTextArea() != null) { e.setHint(FlagClass.KEY_FLAG_TEXT_AREA, info.getTextArea()); diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagInfoBuilder.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagInfoBuilder.java index 6372267a4..080ca9df1 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagInfoBuilder.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagInfoBuilder.java @@ -11,6 +11,7 @@ *******************************************************************************/ package org.simantics.diagram.flag; +import java.awt.Font; import java.awt.Shape; import java.awt.geom.Rectangle2D; @@ -33,6 +34,7 @@ public class FlagInfoBuilder { private Rectangle2D textArea; private Alignment horizontalAlignment = Alignment.LEADING; private Alignment verticalAlignment = Alignment.CENTER; + private Font font; public static FlagInfoBuilder fill(Type type) { return new FlagInfoBuilder().type(type); @@ -42,7 +44,7 @@ public class FlagInfoBuilder { } public FlagInfo create() { - return new FlagInfoImpl(shape, text, type, textArea, horizontalAlignment, verticalAlignment); + return new FlagInfoImpl(shape, text, type, textArea, horizontalAlignment, verticalAlignment, font); } public Shape shape() { @@ -53,6 +55,10 @@ public class FlagInfoBuilder { return text; } + public Font font() { + return font; + } + public Type type() { return type; } @@ -78,6 +84,11 @@ public class FlagInfoBuilder { this.text = text; return this; } + + public FlagInfoBuilder font(Font font) { + this.font = font; + return this; + } public FlagInfoBuilder type(Type type) { this.type = type; diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagInfoImpl.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagInfoImpl.java index bad06dbbd..53db4209d 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagInfoImpl.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagInfoImpl.java @@ -11,6 +11,7 @@ *******************************************************************************/ package org.simantics.diagram.flag; +import java.awt.Font; import java.awt.Shape; import java.awt.geom.Rectangle2D; @@ -29,15 +30,17 @@ public class FlagInfoImpl implements FlagInfo { private final Rectangle2D textArea; private final Alignment horizontalAlignment; private final Alignment verticalAlignment; + private final Font font; public FlagInfoImpl(Shape shape, String[] text, Type type, Rectangle2D textArea, - Alignment horizontalAlignment, Alignment verticalAlignment) { + Alignment horizontalAlignment, Alignment verticalAlignment, Font font) { this.shape = shape; this.text = text; this.type = type; this.textArea = textArea; this.horizontalAlignment = horizontalAlignment; this.verticalAlignment = verticalAlignment; + this.font = font; } @Override @@ -70,4 +73,8 @@ public class FlagInfoImpl implements FlagInfo { return verticalAlignment; } + @Override + public Font getFont() { + return font; + } } diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagSceneGraph.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagSceneGraph.java index cd8661350..9895dd39f 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagSceneGraph.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagSceneGraph.java @@ -1,6 +1,7 @@ package org.simantics.diagram.flag; import java.awt.Color; +import java.awt.Font; import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; @@ -90,6 +91,8 @@ public class FlagSceneGraph implements SceneGraph { Alignment horizAlign = ElementUtils.getHintOrDefault(e, FlagClass.KEY_TEXT_HORIZONTAL_ALIGN, Alignment.LEADING); Alignment vertAlign = ElementUtils.getHintOrDefault(e, FlagClass.KEY_TEXT_VERTICAL_ALIGN, Alignment.CENTER); + Font font = ElementUtils.getHintOrDefault(e, FlagClass.KEY_FLAG_FONT, FlagNode.DEFAULT_FONT); + ElementUtils.removePossibleNode(e, KEY_VISUAL_SG_NODE); e.removeHint(KEY_VISUAL_SG_NODE); @@ -106,7 +109,8 @@ public class FlagSceneGraph implements SceneGraph { (float) beakAngle, textArea, horizAlign.ordinal(), - vertAlign.ordinal()); + vertAlign.ordinal(), + font); AffineTransform at = ElementUtils.getTransform(e); if(at != null) flag.setTransform(at); diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/IFlagType.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/IFlagType.java index 8ee0ff93d..8176d1e79 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/IFlagType.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/IFlagType.java @@ -11,6 +11,7 @@ *******************************************************************************/ package org.simantics.diagram.flag; +import java.awt.Font; import java.awt.Shape; import java.awt.geom.Rectangle2D; @@ -35,6 +36,7 @@ public interface IFlagType { FlagClass.Type getType(); Alignment getHorizontalAlignment(); Alignment getVerticalAlignment(); + Font getFont(); } /** diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/FlagClass.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/FlagClass.java index 604fa15a5..7c60894bc 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/FlagClass.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/FlagClass.java @@ -13,6 +13,7 @@ package org.simantics.g2d.elementclass; import java.awt.BasicStroke; import java.awt.Color; +import java.awt.Font; import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.Path2D; @@ -101,6 +102,7 @@ public class FlagClass { public static final Key KEY_SHAPE = new KeyOf(Shape.class, "SHAPE"); public static final Key KEY_TEXT_HORIZONTAL_ALIGN = new KeyOf(Alignment.class, "TEXT_HORIZONTAL_ALIGN"); public static final Key KEY_TEXT_VERTICAL_ALIGN = new KeyOf(Alignment.class, "TEXT_VERTICAL_ALIGN"); + public static final Key KEY_FLAG_FONT = new KeyOf(Font.class, "FLAG_FONT"); public static final Key KEY_SG_NODE = new SceneGraphNodeKey(Node.class, "FLAG_SG_NODE"); @@ -631,6 +633,8 @@ public class FlagClass { Alignment horizAlign = ElementUtils.getHintOrDefault(e, KEY_TEXT_HORIZONTAL_ALIGN, Alignment.LEADING); Alignment vertAlign = ElementUtils.getHintOrDefault(e, KEY_TEXT_VERTICAL_ALIGN, Alignment.CENTER); + Font font = ElementUtils.getHintOrDefault(e, KEY_FLAG_FONT, FlagNode.DEFAULT_FONT); + FlagNode flag = ElementUtils.getOrCreateNode(e, parent, KEY_SG_NODE, ElementUtils.generateNodeId(e), FlagNode.class); flag.init(shape, flagText, @@ -644,7 +648,8 @@ public class FlagClass { (float) beakAngle, textArea, horizAlign.ordinal(), - vertAlign.ordinal()); + vertAlign.ordinal(), + font); AffineTransform at = ElementUtils.getTransform(e); if(at != null) flag.setTransform(at); diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/FlagNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/FlagNode.java index 4468996fb..598e13009 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/FlagNode.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/FlagNode.java @@ -49,7 +49,7 @@ public class FlagNode extends G2DNode { static transient final BasicStroke STROKE = new BasicStroke(0.25f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER); - final transient Font FONT = Font.decode("Arial 12"); + public final static Font DEFAULT_FONT = Font.decode("Arial 12"); protected boolean visible; @@ -66,6 +66,7 @@ public class FlagNode extends G2DNode { protected Rectangle2D textArea; protected byte hAlign; protected byte vAlign; + protected Font font = DEFAULT_FONT; private transient final Point2D origin = new Point2D.Double(); private transient final Point2D xa = new Point2D.Double(); @@ -85,8 +86,8 @@ public class FlagNode extends G2DNode { return visible; } - @SyncField({"visible", "flagShape", "flagText", "stroke", "border", "fill", "textColor", "width", "height", "direction", "beakAngle", "textSize", "hAlign", "vAlign"}) - public void init(Shape flagShape, String[] flagText, Stroke stroke, Color border, Color fill, Color textColor, float width, float height, double direction, float beakAngle, Rectangle2D textArea, int hAlign, int vAlign) { + @SyncField({"visible", "flagShape", "flagText", "stroke", "border", "fill", "textColor", "width", "height", "direction", "beakAngle", "textSize", "hAlign", "vAlign", "font"}) + public void init(Shape flagShape, String[] flagText, Stroke stroke, Color border, Color fill, Color textColor, float width, float height, double direction, float beakAngle, Rectangle2D textArea, int hAlign, int vAlign, Font font) { this.visible = true; this.flagShape = flagShape; this.flagText = flagText; @@ -101,6 +102,7 @@ public class FlagNode extends G2DNode { this.textArea = textArea; this.hAlign = (byte) hAlign; this.vAlign = (byte) vAlign; + this.font = font; resetCaches(); } @@ -165,8 +167,7 @@ public class FlagNode extends G2DNode { } // Paint flag text - Font f = FONT; - g.setFont(f); + g.setFont(font); g.setColor(textColor); AffineTransform orig = g.getTransform(); @@ -240,7 +241,7 @@ public class FlagNode extends G2DNode { System.out.println("transform: " + g.getTransform()); } - FontMetrics fm = g.getFontMetrics(f); + FontMetrics fm = g.getFontMetrics(font); double fontHeight = fm.getHeight(); if (textLayout == null || (float) scale != lastViewScale) @@ -254,7 +255,7 @@ public class FlagNode extends G2DNode { textHeight = 0; for (int i = 0; i < flagText.length; ++i) { String txt = flagText[i].isEmpty() ? " " : flagText[i]; - textLayout[i] = new TextLayout(txt, f, frc); + textLayout[i] = new TextLayout(txt, font, frc); rects[i] = textLayout[i].getBounds(); // If the bb height is not overridden with the font height