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 * DateTitle.java
029 * --------------
030 * (C) Copyright 2000-present, by David Berry and Contributors.
031 *
032 * Original Author:  David Berry;
033 * Contributor(s):   David Gilbert;
034 *
035 */
036
037package org.jfree.chart.title;
038
039import java.awt.Color;
040import java.awt.Font;
041import java.awt.Paint;
042import java.io.Serializable;
043import java.text.DateFormat;
044import java.util.Date;
045import java.util.Locale;
046
047import org.jfree.chart.ui.HorizontalAlignment;
048import org.jfree.chart.ui.RectangleEdge;
049import org.jfree.chart.ui.RectangleInsets;
050import org.jfree.chart.ui.VerticalAlignment;
051
052/**
053 * A chart title that displays the date.
054 * <p>
055 * Keep in mind that a chart can have several titles, and that they can appear
056 * at the top, left, right or bottom of the chart - a {@code DateTitle}
057 * will commonly appear at the bottom of a chart, although you can place it
058 * anywhere.
059 * <P>
060 * By specifying the locale, dates are formatted to the correct standard for
061 * the given locale. For example, a date would appear as "January 17, 2000" in
062 * the US, but "17 January 2000" in most European locales.
063 */
064public class DateTitle extends TextTitle implements Serializable {
065
066    /** For serialization. */
067    private static final long serialVersionUID = -465434812763159881L;
068
069    /**
070     * Creates a new chart title that displays the current date in the default
071     * (LONG) format for the locale, positioned to the bottom right of the
072     * chart.
073     * <P>
074     * The color will be black in 12 point, plain Helvetica font (maps to Arial
075     * on Win32 systems without Helvetica).
076     */
077    public DateTitle() {
078        this(DateFormat.LONG);
079    }
080
081    /**
082     * Creates a new chart title that displays the current date with the
083     * specified style (for the default locale).
084     * <P>
085     * The date style should be one of:  {@code SHORT},
086     * {@code MEDIUM}, {@code LONG} or {@code FULL}
087     * (defined in {@code java.util.DateFormat}).
088     *
089     * @param style  the date style.
090     */
091    public DateTitle(int style) {
092        this(style, Locale.getDefault(), new Font("Dialog", Font.PLAIN, 12),
093                Color.BLACK);
094    }
095
096    /**
097     * Creates a new chart title that displays the current date.
098     * <p>
099     * The date style should be one of:  {@code SHORT},
100     * {@code MEDIUM}, {@code LONG} or {@code FULL} (defined
101     * in {@code java.util.DateFormat}).
102     * <P>
103     * For the locale, you can use {@code Locale.getDefault()} for the
104     * default locale.
105     *
106     * @param style  the date style.
107     * @param locale  the locale.
108     * @param font  the font.
109     * @param paint  the text color.
110     */
111    public DateTitle(int style, Locale locale, Font font, Paint paint) {
112        this(style, locale, font, paint, RectangleEdge.BOTTOM,
113                HorizontalAlignment.RIGHT, VerticalAlignment.CENTER,
114                Title.DEFAULT_PADDING);
115    }
116
117    /**
118     * Creates a new chart title that displays the current date.
119     * <p>
120     * The date style should be one of:  {@code SHORT},
121     * {@code MEDIUM}, {@code LONG} or {@code FULL} (defined
122     * in {@code java.util.DateFormat}).
123     * <P>
124     * For the locale, you can use {@code Locale.getDefault()} for the
125     * default locale.
126     *
127     * @param style  the date style.
128     * @param locale  the locale.
129     * @param font  the font (not null).
130     * @param paint  the text color (not null).
131     * @param position  the relative location of this title (use constants in
132     *                  Title).
133     * @param horizontalAlignment  the horizontal text alignment of this title
134     *                             (use constants in Title).
135     * @param verticalAlignment  the vertical text alignment of this title (use
136     *                           constants in Title).
137     * @param padding  determines the blank space around the outside of the
138     *                 title (not null).
139     */
140    public DateTitle(int style, Locale locale, Font font, Paint paint,
141            RectangleEdge position, HorizontalAlignment horizontalAlignment,
142            VerticalAlignment verticalAlignment, RectangleInsets padding) {
143        super(DateFormat.getDateInstance(style, locale).format(new Date()),
144                font, paint, position, horizontalAlignment, verticalAlignment,
145                padding);
146    }
147
148    /**
149     * Set the format of the date.
150     * <P>
151     * The date style should be one of:  {@code SHORT},
152     * {@code MEDIUM}, {@code LONG} or {@code FULL} (defined
153     * in {@code java.util.DateFormat}).
154     * <P>
155     * For the locale, you can use {@code Locale.getDefault()} for the
156     * default locale.
157     *
158     * @param style  the date style.
159     * @param locale  the locale.
160     */
161    public void setDateFormat(int style, Locale locale) {
162        setText(DateFormat.getDateInstance(style, locale).format(new Date()));
163    }
164
165}