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