    001/* ===========================================================
    002 * Orson Charts : a 3D chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
    006 *
    007 * http://www.object-refinery.com/orsoncharts/index.html
    008 *
    009 * This program is free software: you can redistribute it and/or modify
    010 * it under the terms of the GNU General Public License as published by
    011 * the Free Software Foundation, either version 3 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This program is distributed in the hope that it will be useful,
    015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    017 * GNU General Public License for more details.
    018 *
    019 * You should have received a copy of the GNU General Public License
    020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    021 *
    022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    023 * Other names may be trademarks of their respective owners.]
    024 *
    025 * If you do not wish to be bound by the terms of the GPL, an alternative
    026 * commercial license can be purchased.  For details, please see visit the
    027 * Orson Charts home page:
    028 *
    029 * http://www.object-refinery.com/orsoncharts/index.html
    030 *
    031 */
    032
    033package com.orsoncharts;
    034
    035import java.awt.Color;
    036import java.awt.Font;
    037import com.orsoncharts.interaction.InteractiveElementType;
    038import com.orsoncharts.table.GridElement;
    039import com.orsoncharts.table.HAlign;
    040import com.orsoncharts.table.TableElement;
    041import com.orsoncharts.table.TextElement;
    042import com.orsoncharts.util.Anchor2D;
    043
    044/**
    045 * Some utility methods for creating chart titles.
    046 */
    047public class TitleUtils {
    048
    049    /** The default title font. */
    050    public static final Font DEFAULT_TITLE_FONT = new Font("Dialog", Font.BOLD,
    051            20);
    052
    053    /** The default foreground color for titles. */
    054    public static final Color DEFAULT_TITLE_COLOR = Color.BLACK;
    055
    056    /** The default sub-title font. */
    057    public static final Font DEFAULT_SUBTITLE_FONT = new Font("Dialog",
    058            Font.PLAIN, 12);
    059
    060    private TitleUtils() {
    061        // no need to instantiate this class
    062    }
    063
    064    /**
    065     * Creates a chart title using the default font and alignment.
    066     *
    067     * @param title  the chart title ({@code null} not permitted).
    068     *
    069     * @return The chart title.
    070     */
    071    public static TableElement createTitle(String title) {
    072        return createTitle(title, null);
    073    }
    074
    075    /**
    076     * Creates a chart title and subtitle using default fonts and left
    077     * alignment.  The {@code subtitle} is optional.
    078     *
    079     * @param title  the title text ({@code null} not permitted).
    080     * @param subtitle  the subtitle text ({@code null} permitted).
    081     *
    082     * @return A composite title.
    083     */
    084    public static TableElement createTitle(String title, String subtitle) {
    085        return createTitle(title, subtitle, TitleAnchor.TOP_LEFT);
    086    }
    087
    088    /**
    089     * Creates a chart title and subtitle (optional) using default fonts and
    090     * alignment that is standard for the specified anchor point (that is, left
    091     * alignment when the title is anchored left, center alignment when the
    092     * title is anchored centrally, and right alignment when the title is
    093     * anchored to the right).
    094     *
    095     * @param title  the title text ({@code null} not permitted).
    096     * @param subtitle  the subtitle text ({@code null} permitted).
    097     * @param anchor  the anchor point ({@code null} not permitted).
    098     *
    099     * @return A composite title.
    100     */
    101    public static TableElement createTitle(String title, String subtitle,
    102            Anchor2D anchor) {
    103        HAlign alignment = HAlign.LEFT;
    104        if (anchor.getRefPt().isHorizontalCenter()) {
    105            alignment = HAlign.CENTER;
    106        } else if (anchor.getRefPt().isRight()) {
    107            alignment = HAlign.RIGHT;
    108        }
    109        return createTitle(title, DEFAULT_TITLE_FONT, subtitle,
    110                DEFAULT_SUBTITLE_FONT, alignment);
    111    }
    112
    113    /**
    114     * Creates a chart title and subtitle using the specified fonts and
    115     * alignment.
    116     *
    117     * @param title  the title text ({@code null} not permitted).
    118     * @param titleFont  the title font ({@code null} not permitted).
    119     * @param subtitle  the subtitle text ({@code null} permitted).
    120     * @param subtitleFont  the subtitle font ({@code null} permitted).
    121     * @param alignment  the horizontal alignment ({@code null} not
    122     *     permitted).
    123     *
    124     * @return A chart title (never {@code null}).
    125     */
    126    public static TableElement createTitle(String title, Font titleFont,
    127            String subtitle, Font subtitleFont, HAlign alignment) {
    128
    129        TextElement t = new TextElement(title, titleFont);
    130        t.setHorizontalAligment(alignment);
    131        t.setColor(DEFAULT_TITLE_COLOR);
    132        t.setTag("CHART_TITLE");
    133        t.setProperty(TableElement.CLASS, InteractiveElementType.TITLE);
    134        if (subtitle == null) {
    135            return t;
    136        }
    137        if (subtitleFont == null) {
    138            throw new IllegalArgumentException(
    139                    "A subtitleFont is required when there is a subtitle.");
    140        }
    141        GridElement compositeTitle = new GridElement();
    142        TextElement st = new TextElement(subtitle, subtitleFont);
    143        st.setHorizontalAligment(alignment);
    144        st.setColor(DEFAULT_TITLE_COLOR);
    145        st.setTag("CHART_SUBTITLE");
    146        st.setProperty(TableElement.CLASS, InteractiveElementType.SUBTITLE);
    147        compositeTitle.setElement(t, "R1", "C1");
    148        compositeTitle.setElement(st, "R2", "C1");
    149        return compositeTitle;
    150    }
    151
    152}