]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextElementHandler.java
192b0ea5d09588c783561dc1133d5ad571af5823
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / elements / TextElementHandler.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.diagram.elements;
13
14 import java.awt.Font;
15 import java.awt.font.FontRenderContext;
16 import java.awt.font.TextLayout;
17 import java.awt.geom.AffineTransform;
18 import java.awt.geom.Rectangle2D;
19
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;
25
26 /**
27  * ElementHandler for text elements
28  * In-line editing supported.
29  * 
30  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
31  */
32 public class TextElementHandler extends TextElementNoBounds implements InternalSize {
33
34     private static final long serialVersionUID = 2560598115620905271L;
35
36     public static final TextElementHandler INSTANCE         = new TextElementHandler();
37
38     public TextElementHandler() {
39         super();
40     }
41
42     public TextElementHandler(double originX, double originY, Alignment horizontalAlignment) {
43         super(originX, originY, horizontalAlignment);
44     }
45
46     public TextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth) {
47         super(originX, originY, horizontalAlignment, borderWidth);
48     }
49
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);
53     }
54     
55     public TextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth,
56                         double paddingX, double paddingY, boolean editable, double scale) {
57                 super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable, scale);
58         }
59
60         @Override
61     public Rectangle2D getBounds(IElement e, Rectangle2D size) {
62         return calculateBounds(e, size, horizontalAlignment, scale, paddingX, paddingY);
63     }
64     
65     public static Rectangle2D calculateBounds(
66             IElement e,
67             Rectangle2D size, 
68             Alignment horizontalAlignment, 
69             double scale,
70             double paddingX,
71             double paddingY) {
72         
73         TextNode node = (TextNode) e.getHint(SG_NODE);
74         Rectangle2D b = e.getHint(ElementHints.KEY_BOUNDS);
75
76         if (size == null)
77             size = new Rectangle2D.Double();
78         if (node != null)
79             size.setRect(node.getBoundsInLocal());
80         else if(b != null && ElementUtils.getHintOrDefault(e, ElementHints.KEY_RESIZABLE, false))
81             size.setRect(b);
82         else {
83             String text = e.getHint(ElementHints.KEY_TEXT);
84             Font font = e.getHint(ElementHints.KEY_FONT);
85             if(text == null || font == null)
86                 size.setFrame(0, 0, 0, 0);
87             else {
88                 FontRenderContext FRC = new FontRenderContext(new AffineTransform(), true, true);
89                 TextLayout tl = new TextLayout(text, font, FRC);
90                 Rectangle2D bounds = tl.getLogicalHighlightShape(0, text.length()).getBounds2D();   
91                 size.setFrame(
92                         getAlignedXCoordinate(bounds, horizontalAlignment) * scale - paddingX,
93                         bounds.getY() * scale -paddingY, 
94                         bounds.getWidth()* scale + paddingX + paddingX, 
95                         bounds.getHeight()* scale + paddingY + paddingY);
96             }
97         }
98         return size;
99         
100     }
101     
102     
103     public static double getAlignedXCoordinate(Rectangle2D bounds, Alignment horizontalAlignment) {
104         if(horizontalAlignment == Alignment.CENTER) {
105             return bounds.getX() - bounds.getWidth() / 2;
106         } else if(horizontalAlignment == Alignment.TRAILING) {
107             return - bounds.getWidth();
108         }
109         return bounds.getX();
110     }
111
112 }