// XAAL toolkit // Copyright (C) 2009 Ville Karavirta // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . package xaal.objects.graphical; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Iterator; import xaal.objects.Xaal; import xaal.util.XaalConstants; /** * @author vkaravir * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class Text extends GraphicalPrimitive implements Cloneable { private Coordinate coord; public static final String ALIGNMENT_CENTERED = "centered"; public static final String ALIGNMENT_LEFT = "left"; public static final String ALIGNMENT_RIGHT = "right"; private String alignment = ALIGNMENT_LEFT; private String defaultText; private boolean boxed = false; private HashMap contents; /** * */ public Text(Xaal xaalDoc) { super(xaalDoc); contents = new HashMap(); } public Text(Xaal xaalDoc, String text) { this(xaalDoc); setContents(text); } public Object clone() throws CloneNotSupportedException { Text copy = (Text) super.clone(); if (coord!=null) copy.coord = (Coordinate)coord.clone(); return copy; } /** * @param coordinate */ public void setCoordinate(Coordinate coordinate) { coord = coordinate; } public Coordinate getCoordinate() { return coord; } /** * @param a */ public void setAlignment(String a) { if (ALIGNMENT_CENTERED.equals( a ) && !ALIGNMENT_LEFT.equals( a ) && !ALIGNMENT_RIGHT.equals( a )) { // TODO centralized error handling } else { this.alignment = a; } } public String getAlignment() { return alignment; } /** * @param string */ public void setContents(String string) { defaultText = string; } public void addContents(String string, String lang) { contents.put(lang, string); } /** * @return Returns the boxed. */ public boolean isBoxed() { return boxed; } /** * @param boxed * The boxed to set. */ public void setBoxed(boolean boxed) { this.boxed = boxed; } /** * @return */ public String getContents() { if (defaultText == null) return getContents(XaalConstants.getDefaultLanguage()); return defaultText; } public String getContents(String lang) { Object o = contents.get(lang); if (o != null) return o.toString(); else return null; } public Iterator getLanguages() { return contents.keySet().iterator(); } public boolean equalsIgnoreId(GraphicalPrimitive gp) { if (!(gp instanceof Text) || !super.equalsIgnoreId(gp)) return false; Text txt= (Text) gp; if (txt.getContents().equals(getContents()) && txt.getCoordinate().equalsIgnoreId(getCoordinate())) //TODO multiple languages + other properties return true; return false; } public void addCoordinate(Coordinate c) { setCoordinate(c); } public Rectangle getBounds() { if (getContents() == null || getContents().equals("")) { return new Rectangle(getCoordinate().getAbsoluteX(), getCoordinate().getAbsoluteY(), 0, 0); } Graphics graphics = new BufferedImage(1,1,BufferedImage.TYPE_BYTE_GRAY).getGraphics(); java.awt.Font font = getStyle().getFont().getAwtFont(); FontRenderContext frc = ((Graphics2D) graphics).getFontRenderContext(); TextLayout layout = new TextLayout(getContents(), font, frc); int textWidth = graphics.getFontMetrics(getStyle().getFont().getAwtFont()).stringWidth(getContents()); int textHeight = graphics.getFontMetrics(getStyle().getFont().getAwtFont()).getHeight(); // return new Rectangle(getCoordinate().getAbsoluteX(), getCoordinate().getAbsoluteY(), textWidth, textHeight); Rectangle2D bounds = layout.getBounds(); return new Rectangle(getCoordinate().getAbsoluteX(), getCoordinate().getAbsoluteY() - (int)Math.round(bounds.getHeight()), (int)Math.round(bounds.getWidth()), (int)Math.round(bounds.getHeight())); } }