1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.elements;
\r
14 import java.awt.Font;
\r
15 import java.awt.font.FontRenderContext;
\r
16 import java.awt.font.TextLayout;
\r
17 import java.awt.geom.AffineTransform;
\r
18 import java.awt.geom.Rectangle2D;
\r
20 import org.simantics.g2d.element.ElementHints;
\r
21 import org.simantics.g2d.element.IElement;
\r
22 import org.simantics.g2d.element.handler.InternalSize;
\r
23 import org.simantics.g2d.utils.Alignment;
\r
25 public class HoverTextElementHandler extends HoverTextElementNoBounds implements InternalSize {
\r
27 private static final long serialVersionUID = 8800738238681432901L;
\r
29 public static final HoverTextElementHandler INSTANCE = new HoverTextElementHandler();
\r
31 public HoverTextElementHandler() {
\r
35 public HoverTextElementHandler(double originX, double originY, Alignment horizontalAlignment) {
\r
36 super(originX, originY, horizontalAlignment);
\r
39 public HoverTextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth) {
\r
40 super(originX, originY, horizontalAlignment, borderWidth);
\r
43 public HoverTextElementHandler(double originX, double originY, Alignment horizontalAlignment, double borderWidth,
\r
44 double paddingX, double paddingY, boolean editable) {
\r
45 super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable);
\r
49 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
\r
50 HoverTextNode node = (HoverTextNode) e.getHint(SG_NODE);
\r
52 size = new Rectangle2D.Double();
\r
54 size.setRect(node.getBoundsInLocal());
\r
56 String text = e.getHint(ElementHints.KEY_TEXT);
\r
57 Font font = e.getHint(ElementHints.KEY_FONT);
\r
58 if(text == null || font == null)
\r
59 size.setFrame(0, 0, 0, 0);
\r
61 FontRenderContext FRC = new FontRenderContext(new AffineTransform(), true, true);
\r
62 TextLayout tl = new TextLayout(text, font, FRC);
\r
63 Rectangle2D bounds = tl.getLogicalHighlightShape(0, text.length()).getBounds2D();
\r
65 bounds.getX() * SCALE - paddingX,
\r
66 bounds.getY() * SCALE -paddingY,
\r
67 bounds.getWidth()* SCALE + paddingX + paddingX,
\r
68 bounds.getHeight()* SCALE + paddingY + paddingY);
\r