-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
package org.simantics.trend.impl;
-import java.awt.Color;\r
-import java.awt.Font;\r
-import java.awt.Graphics2D;\r
-import java.awt.font.GlyphVector;\r
-import java.awt.geom.AffineTransform;\r
-import java.awt.geom.Rectangle2D;\r
-\r
-import org.simantics.g2d.utils.GridUtil;\r
-import org.simantics.scenegraph.ExportableWidget.OutputWidget;\r
-\r
-/**\r
- * TextNode, the text is centered and scaled to fit inside the bounds.\r
- *\r
- * @author toni.kalajainen\r
- */\r
+import java.awt.Color;
+import java.awt.Font;
+import java.awt.Graphics2D;
+import java.awt.font.GlyphVector;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+
+import org.simantics.g2d.utils.GridUtil;
+import org.simantics.scenegraph.ExportableWidget.OutputWidget;
+
+/**
+ * TextNode, the text is centered and scaled to fit inside the bounds.
+ *
+ * @author toni.kalajainen
+ */
@OutputWidget("text")
public class TextNode extends TrendGraphicalNode {
- private static final long serialVersionUID = 8508750881358776559L;\r
-\r
- String text = null;\r
- Font font = null;\r
- Color color = Color.BLACK;\r
- \r
- transient double sx, sy, tw, th, tx, ty;\r
-\r
- @SyncField("font")\r
- public void setFont(Font font) {\r
- this.font = font;\r
- }\r
- \r
- @SyncField("text")\r
- public void setText(String text) {\r
- this.text = text;\r
- }\r
- \r
- @SyncField("color")\r
- public void setColor(Color color) {\r
- this.color = color;\r
- }\r
- \r
- public void layout() {\r
- GlyphVector glyphVector = font.createGlyphVector(GridUtil.frc, text);\r
- Rectangle2D tr = glyphVector.getVisualBounds().getBounds2D();\r
- tw = tr.getWidth();\r
- th = tr.getHeight();\r
- sx = getWidth() / tw;\r
- sy = getHeight() / th;\r
- tx = -tr.getX();\r
- ty = -tr.getY();\r
- \r
- // Fix horiz scale param sx\r
- if (sx<1.0) {\r
- // Text is too wide\r
- // Scale-down to fit rectangle width\r
- tw *= sx;\r
- th *= sx;\r
- sy *= sx;\r
- tx *= sx;\r
- ty *= sx;\r
- } else {\r
- // Text is ok horizontally. No need to scale.\r
- sx = 1.0;\r
- }\r
- \r
- // Fix vert scale param sy\r
- if (sy<1.0) {\r
- // Text is too tall\r
- // Scale-down to fit rectangle height\r
- tw *= sy;\r
- th *= sy;\r
- sx *= sy;\r
- tx *= sy;\r
- ty *= sy;\r
- } else {\r
- // Text is ok vertically. No need to scale.\r
- sy = 1.0;\r
- }\r
- \r
- tx += (bounds.getWidth() - tw) / 2;\r
- ty += (bounds.getHeight() - th) / 2; \r
- }\r
-\r
- @Override\r
- protected void doRender(Graphics2D g) {\r
- if (text == null || bounds == null || font == null || color == null)\r
- return;\r
-\r
- g.setFont(font);\r
- g.setColor(color);\r
- \r
- AffineTransform at = g.getTransform();\r
- \r
- g.translate( bounds.getX(), bounds.getY() );\r
- g.translate(tx, ty);\r
- g.scale(sx, sy);\r
-\r
- g.drawString(text, 0, 0);\r
- g.setTransform( at );\r
- } \r
+ private static final long serialVersionUID = 8508750881358776559L;
+
+ String text = null;
+ Font font = null;
+ Color color = Color.BLACK;
+
+ transient double sx, sy, tw, th, tx, ty;
+
+ @SyncField("font")
+ public void setFont(Font font) {
+ this.font = font;
+ }
+
+ @SyncField("text")
+ public void setText(String text) {
+ this.text = text;
+ }
+
+ @SyncField("color")
+ public void setColor(Color color) {
+ this.color = color;
+ }
+
+ public void layout() {
+ GlyphVector glyphVector = font.createGlyphVector(GridUtil.frc, text);
+ Rectangle2D tr = glyphVector.getVisualBounds().getBounds2D();
+ tw = tr.getWidth();
+ th = tr.getHeight();
+ sx = getWidth() / tw;
+ sy = getHeight() / th;
+ tx = -tr.getX();
+ ty = -tr.getY();
+
+ // Fix horiz scale param sx
+ if (sx<1.0) {
+ // Text is too wide
+ // Scale-down to fit rectangle width
+ tw *= sx;
+ th *= sx;
+ sy *= sx;
+ tx *= sx;
+ ty *= sx;
+ } else {
+ // Text is ok horizontally. No need to scale.
+ sx = 1.0;
+ }
+
+ // Fix vert scale param sy
+ if (sy<1.0) {
+ // Text is too tall
+ // Scale-down to fit rectangle height
+ tw *= sy;
+ th *= sy;
+ sx *= sy;
+ tx *= sy;
+ ty *= sy;
+ } else {
+ // Text is ok vertically. No need to scale.
+ sy = 1.0;
+ }
+
+ tx += (bounds.getWidth() - tw) / 2;
+ ty += (bounds.getHeight() - th) / 2;
+ }
+
+ @Override
+ protected void doRender(Graphics2D g) {
+ if (text == null || bounds == null || font == null || color == null)
+ return;
+
+ g.setFont(font);
+ g.setColor(color);
+
+ AffineTransform at = g.getTransform();
+
+ g.translate( bounds.getX(), bounds.getY() );
+ g.translate(tx, ty);
+ g.scale(sx, sy);
+
+ g.drawString(text, 0, 0);
+ g.setTransform( at );
+ }
}