]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagSceneGraph.java
Fixed all line endings of the repository
[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.Shape;
5 import java.awt.geom.AffineTransform;
6 import java.awt.geom.Rectangle2D;
7
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;
29
30 public class FlagSceneGraph implements SceneGraph {
31
32     private static final long serialVersionUID = 35208146123929197L;
33
34     public static final FlagSceneGraph INSTANCE           = new FlagSceneGraph();
35
36     /**
37      * References an array of {@link FlagTextInfo} instances and
38      * {@link SVGImageInfo}.
39      */
40     public static final Key            KEY_FLAG_VISUALS  = new KeyOf(Bean[].class, "FLAG_MONITORS");
41
42     public static final Key            KEY_VISUAL_SG_NODE = new SceneGraphNodeKey(Node.class, "FLAG_VISUAL_NODE");
43
44     private static final String        VISUAL_ROOT        = "visual";
45
46     @Override
47     public void cleanup(IElement e) {
48         ElementUtils.removePossibleNode(e, FlagClass.KEY_SG_NODE);
49         ElementUtils.removePossibleNode(e, KEY_VISUAL_SG_NODE);
50     }
51
52     @Override
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);
59
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);
67
68             String[] flagText = e.getHint(FlagClass.KEY_FLAG_TEXT);
69             if (flagText == null) {
70                 // fallback option.
71                 Text t = e.getElementClass().getAtMostOneItemOfClass(Text.class);
72                 if (t != null) {
73                     String text = t.getText(e);
74                     if (text != null)
75                         flagText = new String[] { text };
76                 }
77             }
78
79             // DEBUG TEXT
80             //flagText = new String[] { String.format("%3.1f", dir) + " deg", "FOO"};
81
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);
88             }
89
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);
92
93             ElementUtils.removePossibleNode(e, KEY_VISUAL_SG_NODE);
94             e.removeHint(KEY_VISUAL_SG_NODE);
95
96             FlagNode flag = ElementUtils.getOrCreateNode(e, parent, FlagClass.KEY_SG_NODE, ElementUtils.generateNodeId(e), FlagNode.class);
97             flag.init(shape,
98                     flagText,
99                     FlagClass.STROKE,
100                     bc,
101                     fc,
102                     tc,
103                     (float) width,
104                     (float) height,
105                     (float) dir,
106                     (float) beakAngle,
107                     textArea,
108                     horizAlign.ordinal(),
109                     vertAlign.ordinal());
110             AffineTransform at = ElementUtils.getTransform(e);
111             if(at != null) flag.setTransform(at);
112
113         } else {
114
115             ElementUtils.removePossibleNode(e, FlagClass.KEY_SG_NODE);
116             e.removeHint(FlagClass.KEY_SG_NODE);
117
118             G2DParentNode par = ElementUtils.getOrCreateNode(e, parent, KEY_VISUAL_SG_NODE, VISUAL_ROOT, G2DParentNode.class);
119             if (par.getNodeCount() > 0)
120                 par.removeNodes();
121
122             AffineTransform at = ElementUtils.getTransform(e);
123             if(at != null) par.setTransform(at);
124
125             for (Bean visual : flagVisuals) {
126                 if (visual instanceof FlagTextInfo) {
127                     FlagTextInfo info = (FlagTextInfo) visual;
128
129                     String text = info.text;
130
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));
149                     if (text != null)
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));
158                 }
159             }
160         }
161     }
162
163 }