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