Source for org.jfree.chart.title.ShortTextTitle

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it
  10:  * under the terms of the GNU Lesser General Public License as published by
  11:  * the Free Software Foundation; either version 2.1 of the License, or
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
  22:  * USA.
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25:  * in the United States and other countries.]
  26:  *
  27:  * -------------------
  28:  * ShortTextTitle.java
  29:  * -------------------
  30:  * (C) Copyright 2008, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 28-Apr-2008 : Version 1 (DG);
  38:  *
  39:  */
  40: 
  41: package org.jfree.chart.title;
  42: 
  43: import java.awt.FontMetrics;
  44: import java.awt.Graphics2D;
  45: import java.awt.geom.Rectangle2D;
  46: 
  47: import org.jfree.chart.block.LengthConstraintType;
  48: import org.jfree.chart.block.RectangleConstraint;
  49: import org.jfree.data.Range;
  50: import org.jfree.text.TextUtilities;
  51: import org.jfree.ui.Size2D;
  52: import org.jfree.ui.TextAnchor;
  53: 
  54: /**
  55:  * A text title that is only displayed if the entire text will be visible
  56:  * without line wrapping.  It is only intended for use with short titles - for
  57:  * general purpose titles, you should use the {@link TextTitle} class.
  58:  *
  59:  * @since 1.0.10
  60:  *
  61:  * @see TextTitle
  62:  */
  63: public class ShortTextTitle extends TextTitle {
  64: 
  65:     /**
  66:      * Creates a new title.
  67:      *
  68:      * @param text  the text (<code>null</code> not permitted).
  69:      */
  70:     public ShortTextTitle(String text) {
  71:         setText(text);
  72:     }
  73: 
  74:     /**
  75:      * Performs a layout for this title, subject to the supplied constraint,
  76:      * and returns the dimensions required for the title (if the title
  77:      * cannot be displayed in the available space, this method will return
  78:      * zero width and height for the dimensions).
  79:      *
  80:      * @param g2  the graphics target.
  81:      * @param constraint  the layout constraints.
  82:      *
  83:      * @return The dimensions for the title.
  84:      */
  85:     public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
  86:         RectangleConstraint cc = toContentConstraint(constraint);
  87:         LengthConstraintType w = cc.getWidthConstraintType();
  88:         LengthConstraintType h = cc.getHeightConstraintType();
  89:         Size2D contentSize = null;
  90:         if (w == LengthConstraintType.NONE) {
  91:             if (h == LengthConstraintType.NONE) {
  92:                 contentSize = arrangeNN(g2);
  93:             }
  94:             else if (h == LengthConstraintType.RANGE) {
  95:                 throw new RuntimeException("Not yet implemented.");
  96:             }
  97:             else if (h == LengthConstraintType.FIXED) {
  98:                 throw new RuntimeException("Not yet implemented.");
  99:             }
 100:         }
 101:         else if (w == LengthConstraintType.RANGE) {
 102:             if (h == LengthConstraintType.NONE) {
 103:                 contentSize = arrangeRN(g2, cc.getWidthRange());
 104:             }
 105:             else if (h == LengthConstraintType.RANGE) {
 106:                 contentSize = arrangeRR(g2, cc.getWidthRange(),
 107:                         cc.getHeightRange());
 108:             }
 109:             else if (h == LengthConstraintType.FIXED) {
 110:                 throw new RuntimeException("Not yet implemented.");
 111:             }
 112:         }
 113:         else if (w == LengthConstraintType.FIXED) {
 114:             if (h == LengthConstraintType.NONE) {
 115:                 contentSize = arrangeFN(g2, cc.getWidth());
 116:             }
 117:             else if (h == LengthConstraintType.RANGE) {
 118:                 throw new RuntimeException("Not yet implemented.");
 119:             }
 120:             else if (h == LengthConstraintType.FIXED) {
 121:                 throw new RuntimeException("Not yet implemented.");
 122:             }
 123:         }
 124:         if (contentSize.width <= 0.0 || contentSize.height <= 0.0) {
 125:             return new Size2D(0.0, 0.0);
 126:         }
 127:         else {
 128:             return new Size2D(calculateTotalWidth(contentSize.getWidth()),
 129:                     calculateTotalHeight(contentSize.getHeight()));
 130:         }
 131:     }
 132: 
 133:     /**
 134:      * Arranges the content for this title assuming no bounds on the width
 135:      * or the height, and returns the required size.
 136:      *
 137:      * @param g2  the graphics target.
 138:      *
 139:      * @return The content size.
 140:      */
 141:     protected Size2D arrangeNN(Graphics2D g2) {
 142:         Range max = new Range(0.0, Float.MAX_VALUE);
 143:         return arrangeRR(g2, max, max);
 144:     }
 145: 
 146:     /**
 147:      * Arranges the content for this title assuming a range constraint for the
 148:      * width and no bounds on the height, and returns the required size.
 149:      *
 150:      * @param g2  the graphics target.
 151:      * @param widthRange  the range for the width.
 152:      *
 153:      * @return The content size.
 154:      */
 155:     protected Size2D arrangeRN(Graphics2D g2, Range widthRange) {
 156:         Size2D s = arrangeNN(g2);
 157:         if (widthRange.contains(s.getWidth())) {
 158:             return s;
 159:         }
 160:         double ww = widthRange.constrain(s.getWidth());
 161:         return arrangeFN(g2, ww);
 162:     }
 163: 
 164:     /**
 165:      * Arranges the content for this title assuming a fixed width and no bounds
 166:      * on the height, and returns the required size.  This will reflect the
 167:      * fact that a text title positioned on the left or right of a chart will
 168:      * be rotated by 90 degrees.
 169:      *
 170:      * @param g2  the graphics target.
 171:      * @param w  the width.
 172:      *
 173:      * @return The content size.
 174:      */
 175:     protected Size2D arrangeFN(Graphics2D g2, double w) {
 176:         g2.setFont(getFont());
 177:         FontMetrics fm = g2.getFontMetrics(getFont());
 178:         Rectangle2D bounds = TextUtilities.getTextBounds(getText(), g2, fm);
 179:         if (bounds.getWidth() <= w) {
 180:             return new Size2D(w, bounds.getHeight());
 181:         }
 182:         else {
 183:             return new Size2D(0.0, 0.0);
 184:         }
 185:     }
 186: 
 187:     /**
 188:      * Returns the content size for the title.
 189:      *
 190:      * @param g2  the graphics device.
 191:      * @param widthRange  the width range.
 192:      * @param heightRange  the height range.
 193:      *
 194:      * @return The content size.
 195:      */
 196:     protected Size2D arrangeRR(Graphics2D g2, Range widthRange,
 197:             Range heightRange) {
 198: 
 199:         g2.setFont(getFont());
 200:         FontMetrics fm = g2.getFontMetrics(getFont());
 201:         Rectangle2D bounds = TextUtilities.getTextBounds(getText(), g2, fm);
 202:         if (bounds.getWidth() <= widthRange.getUpperBound()
 203:                 && bounds.getHeight() <= heightRange.getUpperBound()) {
 204:             return new Size2D(bounds.getWidth(), bounds.getHeight());
 205:         }
 206:         else {
 207:             return new Size2D(0.0, 0.0);
 208:         }
 209:     }
 210: 
 211:     /**
 212:      * Draws the title using the current font and paint.
 213:      *
 214:      * @param g2  the graphics target.
 215:      * @param area  the title area.
 216:      * @param params  optional parameters (ignored here).
 217:      */
 218:     public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
 219:         if (area.isEmpty()) {
 220:             return null;
 221:         }
 222:         area = trimMargin(area);
 223:         drawBorder(g2, area);
 224:         area = trimBorder(area);
 225:         area = trimPadding(area);
 226:         g2.setFont(getFont());
 227:         g2.setPaint(getPaint());
 228:         TextUtilities.drawAlignedString(getText(), g2, (float) area.getMinX(),
 229:                 (float) area.getMinY(), TextAnchor.TOP_LEFT);
 230: 
 231:         return null;
 232:     }
 233: 
 234: }