1 package org.simantics.diagram.flag;
5 import java.awt.geom.AffineTransform;
6 import java.awt.geom.Rectangle2D;
8 import org.simantics.databoard.util.Bean;
9 import org.simantics.diagram.adapter.FlagTextInfo;
10 import org.simantics.diagram.adapter.SVGImageInfo;
11 import org.simantics.diagram.elements.TextNode;
12 import org.simantics.g2d.element.ElementUtils;
13 import org.simantics.g2d.element.IElement;
14 import org.simantics.g2d.element.SceneGraphNodeKey;
15 import org.simantics.g2d.element.handler.Outline;
16 import org.simantics.g2d.element.handler.SceneGraph;
17 import org.simantics.g2d.element.handler.Text;
18 import org.simantics.g2d.elementclass.FlagClass;
19 import org.simantics.g2d.elementclass.FlagClass.Type;
20 import org.simantics.g2d.utils.Alignment;
21 import org.simantics.scenegraph.Node;
22 import org.simantics.scenegraph.g2d.G2DParentNode;
23 import org.simantics.scenegraph.g2d.nodes.FlagNode;
24 import org.simantics.scenegraph.g2d.nodes.SVGNode;
25 import org.simantics.ui.colors.Colors;
26 import org.simantics.ui.fonts.Fonts;
27 import org.simantics.utils.datastructures.hints.IHintContext.Key;
28 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
30 public class FlagSceneGraph implements SceneGraph {
32 private static final long serialVersionUID = 35208146123929197L;
34 public static final FlagSceneGraph INSTANCE = new FlagSceneGraph();
37 * References an array of {@link FlagTextInfo} instances and
38 * {@link SVGImageInfo}.
40 public static final Key KEY_FLAG_VISUALS = new KeyOf(Bean[].class, "FLAG_MONITORS");
42 public static final Key KEY_VISUAL_SG_NODE = new SceneGraphNodeKey(Node.class, "FLAG_VISUAL_NODE");
44 private static final String VISUAL_ROOT = "visual";
47 public void cleanup(IElement e) {
48 ElementUtils.removePossibleNode(e, FlagClass.KEY_SG_NODE);
49 ElementUtils.removePossibleNode(e, KEY_VISUAL_SG_NODE);
53 public void init(IElement e, G2DParentNode parent) {
54 Bean[] flagVisuals = e.getHint(KEY_FLAG_VISUALS);
55 if (flagVisuals == null || flagVisuals.length == 0) {
56 Color fc = ElementUtils.getFillColor(e, Color.WHITE);
57 Color bc = ElementUtils.getBorderColor(e, Color.BLACK);
58 Color tc = ElementUtils.getTextColor(e, Color.BLACK);
60 Outline outline = e.getElementClass().getSingleItem(Outline.class);
61 Shape shape = outline.getElementShape(e);
62 Type type = FlagClass.getType(e);
63 double dir = FlagClass.getDirection(e);
64 double width = e.getHint(FlagClass.KEY_FLAG_WIDTH);
65 double height = e.getHint(FlagClass.KEY_FLAG_HEIGHT);
66 double beakAngle = e.getHint(FlagClass.KEY_FLAG_BEAK_ANGLE);
68 String[] flagText = e.getHint(FlagClass.KEY_FLAG_TEXT);
69 if (flagText == null) {
71 Text t = e.getElementClass().getAtMostOneItemOfClass(Text.class);
73 String text = t.getText(e);
75 flagText = new String[] { text };
80 //flagText = new String[] { String.format("%3.1f", dir) + " deg", "FOO"};
82 Rectangle2D textArea = e.getHint(FlagClass.KEY_FLAG_TEXT_AREA);
83 if (textArea == null) {
84 double beakLength = FlagClass.getBeakLength(height, beakAngle);
85 textArea = type == Type.In
86 ? new Rectangle2D.Double(-width-beakLength, -height*0.5, width, height)
87 : new Rectangle2D.Double(0, -height*0.5, width, height);
90 Alignment horizAlign = ElementUtils.getHintOrDefault(e, FlagClass.KEY_TEXT_HORIZONTAL_ALIGN, Alignment.LEADING);
91 Alignment vertAlign = ElementUtils.getHintOrDefault(e, FlagClass.KEY_TEXT_VERTICAL_ALIGN, Alignment.CENTER);
93 ElementUtils.removePossibleNode(e, KEY_VISUAL_SG_NODE);
94 e.removeHint(KEY_VISUAL_SG_NODE);
96 FlagNode flag = ElementUtils.getOrCreateNode(e, parent, FlagClass.KEY_SG_NODE, ElementUtils.generateNodeId(e), FlagNode.class);
108 horizAlign.ordinal(),
109 vertAlign.ordinal());
110 AffineTransform at = ElementUtils.getTransform(e);
111 if(at != null) flag.setTransform(at);
115 ElementUtils.removePossibleNode(e, FlagClass.KEY_SG_NODE);
116 e.removeHint(FlagClass.KEY_SG_NODE);
118 G2DParentNode par = ElementUtils.getOrCreateNode(e, parent, KEY_VISUAL_SG_NODE, VISUAL_ROOT, G2DParentNode.class);
119 if (par.getNodeCount() > 0)
122 AffineTransform at = ElementUtils.getTransform(e);
123 if(at != null) par.setTransform(at);
125 for (Bean visual : flagVisuals) {
126 if (visual instanceof FlagTextInfo) {
127 FlagTextInfo info = (FlagTextInfo) visual;
129 String text = info.text;
131 TextNode cellNode = par.getOrCreateNode(info.id, TextNode.class);
132 if (info.width >= 0.0f)
133 cellNode.setFixedWidth(info.width);
134 cellNode.setWrapText(info.wrapText);
135 cellNode.setHorizontalAlignment((byte)info.hAlignment.ordinal());
136 cellNode.setVerticalAlignment((byte)info.vAlignment.ordinal());
137 if (info.borderWidth >= 0.0f)
138 cellNode.setBorderWidth(info.borderWidth);
139 if (info.borderColor != null && info.borderWidth > 0.0f)
140 cellNode.setBorderColor(Colors.awt(info.borderColor));
141 if (info.backgroundColor != null)
142 cellNode.setBackgroundColor(Colors.awt(info.backgroundColor));
143 if (info.font != null)
144 cellNode.setFont(Fonts.awt(info.font));
145 if (info.color != null)
146 cellNode.setColor(Colors.awt(info.color));
147 if (info.transform != null)
148 cellNode.setTransform(new AffineTransform(info.transform));
150 cellNode.setText(text);
151 } else if (visual instanceof SVGImageInfo) {
152 SVGImageInfo info = (SVGImageInfo) visual;
153 SVGNode svgNode = par.getOrCreateNode(info.id, SVGNode.class);
154 if (info.svgDocument != null && !info.svgDocument.isEmpty())
155 svgNode.setData(info.svgDocument);
156 if (info.transform != null)
157 svgNode.setTransform(new AffineTransform(info.transform));