--- /dev/null
+/*******************************************************************************\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
+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
+@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
+
+}