]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextElementHandler.java
a39ef4f5790ccd7556d976350d6883c8818a7277
[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
56     @Override
57     public Rectangle2D getBounds(IElement e, Rectangle2D size) {
58         return calculateBounds(e, size, horizontalAlignment, SCALE, paddingX, paddingY);
59     }
60     
61     public static Rectangle2D calculateBounds(
62             IElement e,
63             Rectangle2D size, 
64             Alignment horizontalAlignment, 
65             double scale,
66             double paddingX,
67             double paddingY) {
68         
69         TextNode node = (TextNode) e.getHint(SG_NODE);
70         Rectangle2D b = e.getHint(ElementHints.KEY_BOUNDS);
71
72         if (size == null)
73             size = new Rectangle2D.Double();
74         if (node != null)
75             size.setRect(node.getBoundsInLocal());
76         else if(b != null && ElementUtils.getHintOrDefault(e, ElementHints.KEY_RESIZABLE, false))
77             size.setRect(b);
78         else {
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);
83             else {
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();   
87                 size.setFrame(
88                         getAlignedXCoordinate(bounds, horizontalAlignment) * scale - paddingX,
89                         bounds.getY() * scale -paddingY, 
90                         bounds.getWidth()* scale + paddingX + paddingX, 
91                         bounds.getHeight()* scale + paddingY + paddingY);
92             }
93         }
94         return size;
95         
96     }
97     
98     
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();
104         }
105         return bounds.getX();
106     }
107
108 }