    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.plot;
    034
    035import java.awt.Font;
    036import java.io.Serializable;
    037
    038import com.orsoncharts.data.DefaultKeyedValues;
    039import com.orsoncharts.util.ArgChecks;
    040
    041/**
    042 * A standard implementation of the {@link FontSource} interface.
    043 * <br><br>
    044 * NOTE: This class is serializable, but the serialization format is subject
    045 * to change in future releases and should not be relied upon for persisting
    046 * instances of this class.
    047 */
    048@SuppressWarnings("serial")
    049public final class StandardFontSource implements FontSource, Serializable {
    050
    051    private static Font DEFAULT_FONT = new Font("Dialog", Font.PLAIN, 12);
    052
    053    /** Storage for the fonts assigned to keys. */
    054    private DefaultKeyedValues<Font> fonts;
    055
    056    /** The default font (never {@code null}). */
    057    private Font defaultFont;
    058
    059    /**
    060     * Creates a new instance with default fonts.
    061     */
    062    public StandardFontSource() {
    063        this(DEFAULT_FONT);
    064    }
    065
    066    /**
    067     * Creates a new font source with the specified default font.
    068     *
    069     * @param defaultFont  the default font ({@code null} not permitted).
    070     */
    071    public StandardFontSource(Font defaultFont) {
    072        ArgChecks.nullNotPermitted(defaultFont, "defaultFont");
    073        this.defaultFont = defaultFont;
    074        this.fonts = new DefaultKeyedValues<Font>();
    075    }
    076
    077    /**
    078     * Returns the default font.  The default value is {@link #DEFAULT_FONT}.
    079     *
    080     * @return The default font (never {@code null}).
    081     */
    082    public Font getDefaultFont() {
    083        return this.defaultFont;
    084    }
    085
    086    /**
    087     * Sets the default font.
    088     *
    089     * @param font  the font ({@code null} not permitted).
    090     */
    091    public void setDefaultFont(Font font) {
    092        ArgChecks.nullNotPermitted(font, "font");
    093        this.defaultFont = font;
    094    }
    095
    096    /**
    097     * Returns the font for the specified key.
    098     *
    099     * @param key  the key ({@code null} not permitted).
    100     *
    101     * @return The font (never {@code null}).
    102     */
    103    @Override
    104    public Font getFont(Comparable<?> key) {
    105        Font result = this.fonts.getValue(key);
    106        if (result != null) {
    107            return result;
    108        } else {
    109            return this.defaultFont;
    110        }
    111    }
    112
    113    /**
    114     * Sets the font associated with the specified key.
    115     *
    116     * @param key  the key ({@code null} not permitted).
    117     * @param font  the font ({@code null} permitted).
    118     */
    119    @Override
    120    public void setFont(Comparable<?> key, Font font) {
    121        if (font != null) {
    122            this.fonts.put(key, font);
    123        } else {
    124            this.fonts.remove(key);
    125        }
    126    }
    127
    128    /**
    129     * Clears existing font settings and sets the default font to the
    130     * supplied value.  This method is used by the framework and is not
    131     * normally called by client code.
    132     *
    133     * @param font  the font ({@code null} not permitted).
    134     *
    135     * @since 1.2
    136     */
    137    @Override
    138    public void style(Font font) {
    139        ArgChecks.nullNotPermitted(font, "font");
    140        this.defaultFont = font;
    141        this.fonts.clear();
    142    }
    143
    144    /**
    145     * Tests this paint source for equality with an arbitrary object.
    146     *
    147     * @param obj  the object ({@code null} permitted).
    148     *
    149     * @return A boolean.
    150     */
    151    @Override
    152    public boolean equals(Object obj) {
    153        if (obj == this) {
    154            return true;
    155        }
    156        if (!(obj instanceof StandardFontSource)) {
    157            return false;
    158        }
    159        StandardFontSource that = (StandardFontSource) obj;
    160        if (!this.defaultFont.equals(that.defaultFont)) {
    161            return false;
    162        }
    163        if (!this.fonts.equals(that.fonts)) {
    164            return false;
    165        }
    166        return true;
    167    }
    168
    169}