1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.elements;
15 import java.awt.font.FontRenderContext;
16 import java.awt.font.TextLayout;
17 import java.awt.geom.AffineTransform;
18 import java.awt.geom.Rectangle2D;
20 import org.simantics.g2d.element.ElementHints;
21 import org.simantics.g2d.element.ElementUtils;
22 import org.simantics.g2d.element.IElement;
23 import org.simantics.g2d.element.handler.InternalSize;
24 import org.simantics.g2d.utils.Alignment;
27 * ElementHandler for text elements
28 * In-line editing supported.
30 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
32 public class TextElementHandler extends TextElementNoBounds implements InternalSize {
34 private static final long serialVersionUID = 2560598115620905271L;
36 public static final TextElementHandler INSTANCE = new TextElementHandler();
38 public TextElementHandler() {
42 public TextElementHandler(double originX, double originY, Alignment horizontalAlignment) {
43 super(originX, originY, horizontalAlignment);
46 public TextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth) {
47 super(originX, originY, horizontalAlignment, borderWidth);
50 public TextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth,
51 double paddingX, double paddingY, boolean editable) {
52 super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable);
57 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
58 return calculateBounds(e, size, horizontalAlignment, SCALE, paddingX, paddingY);
61 public static Rectangle2D calculateBounds(
64 Alignment horizontalAlignment,
69 TextNode node = (TextNode) e.getHint(SG_NODE);
70 Rectangle2D b = e.getHint(ElementHints.KEY_BOUNDS);
73 size = new Rectangle2D.Double();
75 size.setRect(node.getBoundsInLocal());
76 else if(b != null && ElementUtils.getHintOrDefault(e, ElementHints.KEY_RESIZABLE, false))
79 String text = e.getHint(ElementHints.KEY_TEXT);
80 Font font = e.getHint(ElementHints.KEY_FONT);
81 if(text == null || font == null)
82 size.setFrame(0, 0, 0, 0);
84 FontRenderContext FRC = new FontRenderContext(new AffineTransform(), true, true);
85 TextLayout tl = new TextLayout(text, font, FRC);
86 Rectangle2D bounds = tl.getLogicalHighlightShape(0, text.length()).getBounds2D();
88 getAlignedXCoordinate(bounds, horizontalAlignment) * scale - paddingX,
89 bounds.getY() * scale -paddingY,
90 bounds.getWidth()* scale + paddingX + paddingX,
91 bounds.getHeight()* scale + paddingY + paddingY);
99 public static double getAlignedXCoordinate(Rectangle2D bounds, Alignment horizontalAlignment) {
100 if(horizontalAlignment == Alignment.CENTER) {
101 return bounds.getX() - bounds.getWidth() / 2;
102 } else if(horizontalAlignment == Alignment.TRAILING) {
103 return - bounds.getWidth();
105 return bounds.getX();