-/*******************************************************************************\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.diagram.elements;\r
-\r
-import java.awt.Font;\r
-import java.awt.font.FontRenderContext;\r
-import java.awt.font.TextLayout;\r
-import java.awt.geom.AffineTransform;\r
-import java.awt.geom.Rectangle2D;\r
-\r
-import org.simantics.g2d.element.ElementHints;\r
-import org.simantics.g2d.element.ElementUtils;\r
-import org.simantics.g2d.element.IElement;\r
-import org.simantics.g2d.element.handler.InternalSize;\r
-import org.simantics.g2d.utils.Alignment;\r
-\r
-/**\r
- * ElementHandler for text elements\r
- * In-line editing supported.\r
- * \r
- * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
- */\r
-public class TextElementHandler extends TextElementNoBounds implements InternalSize {\r
-\r
- private static final long serialVersionUID = 2560598115620905271L;\r
-\r
- public static final TextElementHandler INSTANCE = new TextElementHandler();\r
-\r
- public TextElementHandler() {\r
- super();\r
- }\r
-\r
- public TextElementHandler(double originX, double originY, Alignment horizontalAlignment) {\r
- super(originX, originY, horizontalAlignment);\r
- }\r
-\r
- public TextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth) {\r
- super(originX, originY, horizontalAlignment, borderWidth);\r
- }\r
-\r
- public TextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth,\r
- double paddingX, double paddingY, boolean editable) {\r
- super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable);\r
- }\r
- \r
-\r
- @Override\r
- public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
- return calculateBounds(e, size, horizontalAlignment, SCALE, paddingX, paddingY);\r
- }\r
- \r
- public static Rectangle2D calculateBounds(\r
- IElement e,\r
- Rectangle2D size, \r
- Alignment horizontalAlignment, \r
- double scale,\r
- double paddingX,\r
- double paddingY) {\r
- \r
- TextNode node = (TextNode) e.getHint(SG_NODE);\r
- Rectangle2D b = e.getHint(ElementHints.KEY_BOUNDS);\r
-\r
- if (size == null)\r
- size = new Rectangle2D.Double();\r
- if (node != null)\r
- size.setRect(node.getBoundsInLocal());\r
- else if(b != null && ElementUtils.getHintOrDefault(e, ElementHints.KEY_RESIZABLE, false))\r
- size.setRect(b);\r
- else {\r
- String text = e.getHint(ElementHints.KEY_TEXT);\r
- Font font = e.getHint(ElementHints.KEY_FONT);\r
- if(text == null || font == null)\r
- size.setFrame(0, 0, 0, 0);\r
- else {\r
- FontRenderContext FRC = new FontRenderContext(new AffineTransform(), true, true);\r
- TextLayout tl = new TextLayout(text, font, FRC);\r
- Rectangle2D bounds = tl.getLogicalHighlightShape(0, text.length()).getBounds2D(); \r
- size.setFrame(\r
- getAlignedXCoordinate(bounds, horizontalAlignment) * scale - paddingX,\r
- bounds.getY() * scale -paddingY, \r
- bounds.getWidth()* scale + paddingX + paddingX, \r
- bounds.getHeight()* scale + paddingY + paddingY);\r
- }\r
- }\r
- return size;\r
- \r
- }\r
- \r
- \r
- public static double getAlignedXCoordinate(Rectangle2D bounds, Alignment horizontalAlignment) {\r
- if(horizontalAlignment == Alignment.CENTER) {\r
- return bounds.getX() - bounds.getWidth() / 2;\r
- } else if(horizontalAlignment == Alignment.TRAILING) {\r
- return - bounds.getWidth();\r
- }\r
- return bounds.getX();\r
- }\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.diagram.elements;
+
+import java.awt.Font;
+import java.awt.font.FontRenderContext;
+import java.awt.font.TextLayout;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+
+import org.simantics.g2d.element.ElementHints;
+import org.simantics.g2d.element.ElementUtils;
+import org.simantics.g2d.element.IElement;
+import org.simantics.g2d.element.handler.InternalSize;
+import org.simantics.g2d.utils.Alignment;
+
+/**
+ * ElementHandler for text elements
+ * In-line editing supported.
+ *
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
+ */
+public class TextElementHandler extends TextElementNoBounds implements InternalSize {
+
+ private static final long serialVersionUID = 2560598115620905271L;
+
+ public static final TextElementHandler INSTANCE = new TextElementHandler();
+
+ public TextElementHandler() {
+ super();
+ }
+
+ public TextElementHandler(double originX, double originY, Alignment horizontalAlignment) {
+ super(originX, originY, horizontalAlignment);
+ }
+
+ public TextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth) {
+ super(originX, originY, horizontalAlignment, borderWidth);
+ }
+
+ public TextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth,
+ double paddingX, double paddingY, boolean editable) {
+ super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable);
+ }
+
+
+ @Override
+ public Rectangle2D getBounds(IElement e, Rectangle2D size) {
+ return calculateBounds(e, size, horizontalAlignment, SCALE, paddingX, paddingY);
+ }
+
+ public static Rectangle2D calculateBounds(
+ IElement e,
+ Rectangle2D size,
+ Alignment horizontalAlignment,
+ double scale,
+ double paddingX,
+ double paddingY) {
+
+ TextNode node = (TextNode) e.getHint(SG_NODE);
+ Rectangle2D b = e.getHint(ElementHints.KEY_BOUNDS);
+
+ if (size == null)
+ size = new Rectangle2D.Double();
+ if (node != null)
+ size.setRect(node.getBoundsInLocal());
+ else if(b != null && ElementUtils.getHintOrDefault(e, ElementHints.KEY_RESIZABLE, false))
+ size.setRect(b);
+ else {
+ String text = e.getHint(ElementHints.KEY_TEXT);
+ Font font = e.getHint(ElementHints.KEY_FONT);
+ if(text == null || font == null)
+ size.setFrame(0, 0, 0, 0);
+ else {
+ FontRenderContext FRC = new FontRenderContext(new AffineTransform(), true, true);
+ TextLayout tl = new TextLayout(text, font, FRC);
+ Rectangle2D bounds = tl.getLogicalHighlightShape(0, text.length()).getBounds2D();
+ size.setFrame(
+ getAlignedXCoordinate(bounds, horizontalAlignment) * scale - paddingX,
+ bounds.getY() * scale -paddingY,
+ bounds.getWidth()* scale + paddingX + paddingX,
+ bounds.getHeight()* scale + paddingY + paddingY);
+ }
+ }
+ return size;
+
+ }
+
+
+ public static double getAlignedXCoordinate(Rectangle2D bounds, Alignment horizontalAlignment) {
+ if(horizontalAlignment == Alignment.CENTER) {
+ return bounds.getX() - bounds.getWidth() / 2;
+ } else if(horizontalAlignment == Alignment.TRAILING) {
+ return - bounds.getWidth();
+ }
+ return bounds.getX();
+ }
+
}
\ No newline at end of file