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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.element.handler.impl;
\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
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
31 * @author Toni Kalajainen
\r
33 public class TextPainter implements SceneGraph {
\r
38 private static final long serialVersionUID = -4156239790629253738L;
\r
40 static final Font FONT = Font.decode("Arial 6");
\r
42 public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");
\r
45 public void cleanup(IElement e) {
\r
46 Node node = e.removeHint(SG_NODE);
\r
52 public void init(IElement e, G2DParentNode parent) {
\r
53 TextNode node = (TextNode) e.getHint(SG_NODE);
\r
55 node = parent.addNode(TextNode.class);
\r
56 e.setHint(SG_NODE, node);
\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
64 Text t = e.getElementClass().getAtMostOneItemOfClass(Text.class);
\r
67 String text = t.getText(e);
\r
70 AffineTransform at = ElementUtils.getTransform(e);
\r
76 node.setTransform(at);
\r