]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/TextPainter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / TextPainter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.element.handler.impl;\r
13 \r
14 import java.awt.Color;\r
15 import java.awt.Font;\r
16 import java.awt.geom.AffineTransform;\r
17 import java.awt.geom.Rectangle2D;\r
18 \r
19 import org.simantics.g2d.element.ElementUtils;\r
20 import org.simantics.g2d.element.IElement;\r
21 import org.simantics.g2d.element.SceneGraphNodeKey;\r
22 import org.simantics.g2d.element.handler.InternalSize;\r
23 import org.simantics.g2d.element.handler.SceneGraph;\r
24 import org.simantics.g2d.element.handler.Text;\r
25 import org.simantics.scenegraph.Node;\r
26 import org.simantics.scenegraph.g2d.G2DParentNode;\r
27 import org.simantics.scenegraph.g2d.nodes.TextNode;\r
28 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
29 \r
30 /**\r
31  * @author Toni Kalajainen\r
32  */\r
33 public class TextPainter implements SceneGraph {\r
34 \r
35     /**\r
36      * \r
37      */\r
38     private static final long serialVersionUID = -4156239790629253738L;\r
39     \r
40     static final Font FONT = Font.decode("Arial 6");\r
41 \r
42     public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");\r
43 \r
44     @Override\r
45     public void cleanup(IElement e) {\r
46         Node node = e.removeHint(SG_NODE);\r
47         if (node != null)\r
48             node.remove();\r
49     }\r
50 \r
51     @Override\r
52     public void init(IElement e, G2DParentNode parent) {\r
53         TextNode node = (TextNode) e.getHint(SG_NODE);\r
54         if (node == null) {\r
55             node = parent.addNode(TextNode.class);\r
56             e.setHint(SG_NODE, node);\r
57         }\r
58 \r
59         Color bc = ElementUtils.getTextColor(e, Color.BLACK);\r
60         InternalSize b = e.getElementClass().getSingleItem(InternalSize.class);\r
61         Rectangle2D r = b.getBounds(e, null);\r
62         if (r ==  null)\r
63             return;\r
64         Text t = e.getElementClass().getAtMostOneItemOfClass(Text.class);\r
65         if (t == null)\r
66             return;\r
67         String text = t.getText(e);\r
68         if (text == null)\r
69             return;\r
70         AffineTransform at = ElementUtils.getTransform(e);\r
71         \r
72         node.setFont(FONT);\r
73         node.setColor(bc);\r
74         node.setBounds(r);\r
75         node.setText(text);\r
76         node.setTransform(at);\r
77     }\r
78 \r
79 }\r