]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagSceneGraph.java
Merge "(refs #7178) Validator for build.properties"
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / flag / FlagSceneGraph.java
1 package org.simantics.diagram.flag;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.Shape;
6 import java.awt.geom.AffineTransform;
7 import java.awt.geom.Rectangle2D;
8
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.TextNode;
13 import org.simantics.g2d.element.ElementUtils;
14 import org.simantics.g2d.element.IElement;
15 import org.simantics.g2d.element.SceneGraphNodeKey;
16 import org.simantics.g2d.element.handler.Outline;
17 import org.simantics.g2d.element.handler.SceneGraph;
18 import org.simantics.g2d.element.handler.Text;
19 import org.simantics.g2d.elementclass.FlagClass;
20 import org.simantics.g2d.elementclass.FlagClass.Type;
21 import org.simantics.g2d.utils.Alignment;
22 import org.simantics.scenegraph.Node;
23 import org.simantics.scenegraph.g2d.G2DParentNode;
24 import org.simantics.scenegraph.g2d.nodes.FlagNode;
25 import org.simantics.scenegraph.g2d.nodes.SVGNode;
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;
30
31 public class FlagSceneGraph implements SceneGraph {
32
33     private static final long serialVersionUID = 35208146123929197L;
34
35     public static final FlagSceneGraph INSTANCE           = new FlagSceneGraph();
36
37     /**
38      * References an array of {@link FlagTextInfo} instances and
39      * {@link SVGImageInfo}.
40      */
41     public static final Key            KEY_FLAG_VISUALS  = new KeyOf(Bean[].class, "FLAG_MONITORS");
42
43     public static final Key            KEY_VISUAL_SG_NODE = new SceneGraphNodeKey(Node.class, "FLAG_VISUAL_NODE");
44
45     private static final String        VISUAL_ROOT        = "visual";
46
47     @Override
48     public void cleanup(IElement e) {
49         ElementUtils.removePossibleNode(e, FlagClass.KEY_SG_NODE);
50         ElementUtils.removePossibleNode(e, KEY_VISUAL_SG_NODE);
51     }
52
53     @Override
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);
60
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);
68
69             String[] flagText = e.getHint(FlagClass.KEY_FLAG_TEXT);
70             if (flagText == null) {
71                 // fallback option.
72                 Text t = e.getElementClass().getAtMostOneItemOfClass(Text.class);
73                 if (t != null) {
74                     String text = t.getText(e);
75                     if (text != null)
76                         flagText = new String[] { text };
77                 }
78             }
79
80             // DEBUG TEXT
81             //flagText = new String[] { String.format("%3.1f", dir) + " deg", "FOO"};
82
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);
89             }
90
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);
93
94             Font font  = ElementUtils.getHintOrDefault(e, FlagClass.KEY_FLAG_FONT, FlagNode.DEFAULT_FONT);
95
96             ElementUtils.removePossibleNode(e, KEY_VISUAL_SG_NODE);
97             e.removeHint(KEY_VISUAL_SG_NODE);
98
99             FlagNode flag = ElementUtils.getOrCreateNode(e, parent, FlagClass.KEY_SG_NODE, ElementUtils.generateNodeId(e), FlagNode.class);
100             flag.init(shape,
101                     flagText,
102                     FlagClass.STROKE,
103                     bc,
104                     fc,
105                     tc,
106                     (float) width,
107                     (float) height,
108                     (float) dir,
109                     (float) beakAngle,
110                     textArea,
111                     horizAlign.ordinal(),
112                     vertAlign.ordinal(),
113                     font);
114             AffineTransform at = ElementUtils.getTransform(e);
115             if(at != null) flag.setTransform(at);
116
117         } else {
118
119             ElementUtils.removePossibleNode(e, FlagClass.KEY_SG_NODE);
120             e.removeHint(FlagClass.KEY_SG_NODE);
121
122             G2DParentNode par = ElementUtils.getOrCreateNode(e, parent, KEY_VISUAL_SG_NODE, VISUAL_ROOT, G2DParentNode.class);
123             if (par.getNodeCount() > 0)
124                 par.removeNodes();
125
126             AffineTransform at = ElementUtils.getTransform(e);
127             if(at != null) par.setTransform(at);
128
129             for (Bean visual : flagVisuals) {
130                 if (visual instanceof FlagTextInfo) {
131                     FlagTextInfo info = (FlagTextInfo) visual;
132
133                     String text = info.text;
134
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));
153                     if (text != null)
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));
162                 }
163             }
164         }
165     }
166
167 }