]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.trend/src/org/simantics/trend/impl/TextNode.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.trend / src / org / simantics / trend / impl / TextNode.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.trend.impl;
13
14 import java.awt.Color;\r
15 import java.awt.Font;\r
16 import java.awt.Graphics2D;\r
17 import java.awt.font.GlyphVector;\r
18 import java.awt.geom.AffineTransform;\r
19 import java.awt.geom.Rectangle2D;\r
20 \r
21 import org.simantics.g2d.utils.GridUtil;\r
22 import org.simantics.scenegraph.ExportableWidget.OutputWidget;\r
23 \r
24 /**\r
25  * TextNode, the text is centered and scaled to fit inside the bounds.\r
26  *\r
27  * @author toni.kalajainen\r
28  */\r
29 @OutputWidget("text")
30 public class TextNode extends TrendGraphicalNode {
31
32     private static final long serialVersionUID = 8508750881358776559L;\r
33 \r
34     String      text   = null;\r
35     Font        font   = null;\r
36     Color       color  = Color.BLACK;\r
37     \r
38     transient double sx, sy, tw, th, tx, ty;\r
39 \r
40     @SyncField("font")\r
41     public void setFont(Font font) {\r
42         this.font = font;\r
43     }\r
44     \r
45     @SyncField("text")\r
46     public void setText(String text) {\r
47         this.text = text;\r
48     }\r
49     \r
50     @SyncField("color")\r
51     public void setColor(Color color) {\r
52         this.color = color;\r
53     }\r
54     \r
55     public void layout() {\r
56                 GlyphVector glyphVector = font.createGlyphVector(GridUtil.frc, text);\r
57         Rectangle2D tr = glyphVector.getVisualBounds().getBounds2D();\r
58         tw = tr.getWidth();\r
59         th = tr.getHeight();\r
60         sx = getWidth() / tw;\r
61         sy = getHeight() / th;\r
62         tx = -tr.getX();\r
63         ty = -tr.getY();\r
64         \r
65         // Fix horiz scale param sx\r
66         if (sx<1.0) {\r
67                 // Text is too wide\r
68                 // Scale-down to fit rectangle width\r
69                 tw *= sx;\r
70                 th *= sx;\r
71                 sy *= sx;\r
72                 tx *= sx;\r
73                 ty *= sx;\r
74         } else {\r
75                 // Text is ok horizontally. No need to scale.\r
76                 sx = 1.0;\r
77         }\r
78         \r
79         // Fix vert scale param sy\r
80         if (sy<1.0) {\r
81                 // Text is too tall\r
82                 // Scale-down to fit rectangle height\r
83                 tw *= sy;\r
84                 th *= sy;\r
85                 sx *= sy;\r
86                 tx *= sy;\r
87                 ty *= sy;\r
88         } else {\r
89                 // Text is ok vertically. No need to scale.\r
90                 sy = 1.0;\r
91         }\r
92         \r
93         tx += (bounds.getWidth() - tw) / 2;\r
94         ty += (bounds.getHeight() - th) / 2;            \r
95     }\r
96 \r
97         @Override\r
98         protected void doRender(Graphics2D g) {\r
99         if (text == null || bounds == null || font == null || color == null)\r
100             return;\r
101 \r
102         g.setFont(font);\r
103         g.setColor(color);\r
104         \r
105         AffineTransform at = g.getTransform();\r
106         \r
107         g.translate( bounds.getX(), bounds.getY() );\r
108         g.translate(tx, ty);\r
109         g.scale(sx, sy);\r
110 \r
111         g.drawString(text, 0, 0);\r
112         g.setTransform( at );\r
113         }    \r
114     
115 }