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