X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Felements%2FTextElementHandler.java;h=a39ef4f5790ccd7556d976350d6883c8818a7277;hb=704e41fc543094311b7e70f2d705b2eba9f6574b;hp=2e9f1bf9f22810bfbde9d57a676b4357ebf36c45;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextElementHandler.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextElementHandler.java index 2e9f1bf9f..a39ef4f57 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextElementHandler.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/TextElementHandler.java @@ -1,108 +1,108 @@ -/******************************************************************************* - * 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 - */ -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(); - } - +/******************************************************************************* + * 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 + */ +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