001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -------------------
028 * ShortTextTitle.java
029 * -------------------
030 * (C) Copyright 2008-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.title;
038
039import java.awt.FontMetrics;
040import java.awt.Graphics2D;
041import java.awt.geom.Rectangle2D;
042
043import org.jfree.chart.block.LengthConstraintType;
044import org.jfree.chart.block.RectangleConstraint;
045import org.jfree.chart.text.TextUtils;
046import org.jfree.chart.ui.Size2D;
047import org.jfree.chart.ui.TextAnchor;
048import org.jfree.data.Range;
049
050/**
051 * A text title that is only displayed if the entire text will be visible
052 * without line wrapping.  It is only intended for use with short titles - for
053 * general purpose titles, you should use the {@link TextTitle} class.
054 *
055 * @see TextTitle
056 */
057public class ShortTextTitle extends TextTitle {
058
059    /**
060     * Creates a new title.
061     *
062     * @param text  the text ({@code null} not permitted).
063     */
064    public ShortTextTitle(String text) {
065        setText(text);
066    }
067
068    /**
069     * Performs a layout for this title, subject to the supplied constraint,
070     * and returns the dimensions required for the title (if the title
071     * cannot be displayed in the available space, this method will return
072     * zero width and height for the dimensions).
073     *
074     * @param g2  the graphics target.
075     * @param constraint  the layout constraints.
076     *
077     * @return The dimensions for the title.
078     */
079    @Override
080    public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
081        RectangleConstraint cc = toContentConstraint(constraint);
082        LengthConstraintType w = cc.getWidthConstraintType();
083        LengthConstraintType h = cc.getHeightConstraintType();
084        Size2D contentSize = null;
085        if (w == LengthConstraintType.NONE) {
086            if (h == LengthConstraintType.NONE) {
087                contentSize = arrangeNN(g2);
088            }
089            else if (h == LengthConstraintType.RANGE) {
090                throw new RuntimeException("Not yet implemented.");
091            }
092            else if (h == LengthConstraintType.FIXED) {
093                throw new RuntimeException("Not yet implemented.");
094            }
095        }
096        else if (w == LengthConstraintType.RANGE) {
097            if (h == LengthConstraintType.NONE) {
098                contentSize = arrangeRN(g2, cc.getWidthRange());
099            }
100            else if (h == LengthConstraintType.RANGE) {
101                contentSize = arrangeRR(g2, cc.getWidthRange(),
102                        cc.getHeightRange());
103            }
104            else if (h == LengthConstraintType.FIXED) {
105                throw new RuntimeException("Not yet implemented.");
106            }
107        }
108        else if (w == LengthConstraintType.FIXED) {
109            if (h == LengthConstraintType.NONE) {
110                contentSize = arrangeFN(g2, cc.getWidth());
111            }
112            else if (h == LengthConstraintType.RANGE) {
113                throw new RuntimeException("Not yet implemented.");
114            }
115            else if (h == LengthConstraintType.FIXED) {
116                throw new RuntimeException("Not yet implemented.");
117            }
118        }
119        assert contentSize != null;
120        if (contentSize.width <= 0.0 || contentSize.height <= 0.0) {
121            return new Size2D(0.0, 0.0);
122        }
123        else {
124            return new Size2D(calculateTotalWidth(contentSize.getWidth()),
125                    calculateTotalHeight(contentSize.getHeight()));
126        }
127    }
128
129    /**
130     * Arranges the content for this title assuming no bounds on the width
131     * or the height, and returns the required size.
132     *
133     * @param g2  the graphics target.
134     *
135     * @return The content size.
136     */
137    @Override
138    protected Size2D arrangeNN(Graphics2D g2) {
139        Range max = new Range(0.0, Float.MAX_VALUE);
140        return arrangeRR(g2, max, max);
141    }
142
143    /**
144     * Arranges the content for this title assuming a range constraint for the
145     * width and no bounds on the height, and returns the required size.
146     *
147     * @param g2  the graphics target.
148     * @param widthRange  the range for the width.
149     *
150     * @return The content size.
151     */
152    @Override
153    protected Size2D arrangeRN(Graphics2D g2, Range widthRange) {
154        Size2D s = arrangeNN(g2);
155        if (widthRange.contains(s.getWidth())) {
156            return s;
157        }
158        double ww = widthRange.constrain(s.getWidth());
159        return arrangeFN(g2, ww);
160    }
161
162    /**
163     * Arranges the content for this title assuming a fixed width and no bounds
164     * on the height, and returns the required size.  This will reflect the
165     * fact that a text title positioned on the left or right of a chart will
166     * be rotated by 90 degrees.
167     *
168     * @param g2  the graphics target.
169     * @param w  the width.
170     *
171     * @return The content size.
172     */
173    @Override
174    protected Size2D arrangeFN(Graphics2D g2, double w) {
175        g2.setFont(getFont());
176        FontMetrics fm = g2.getFontMetrics(getFont());
177        Rectangle2D bounds = TextUtils.getTextBounds(getText(), g2, fm);
178        if (bounds.getWidth() <= w) {
179            return new Size2D(w, bounds.getHeight());
180        }
181        else {
182            return new Size2D(0.0, 0.0);
183        }
184    }
185
186    /**
187     * Returns the content size for the title.
188     *
189     * @param g2  the graphics device.
190     * @param widthRange  the width range.
191     * @param heightRange  the height range.
192     *
193     * @return The content size.
194     */
195    @Override
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 = TextUtils.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     * @return {@code null}.
219     */
220    @Override
221    public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
222        if (area.isEmpty()) {
223            return null;
224        }
225        area = trimMargin(area);
226        drawBorder(g2, area);
227        area = trimBorder(area);
228        area = trimPadding(area);
229        g2.setFont(getFont());
230        g2.setPaint(getPaint());
231        TextUtils.drawAlignedString(getText(), g2, (float) area.getMinX(),
232                (float) area.getMinY(), TextAnchor.TOP_LEFT);
233        return null;
234    }
235
236}