001/* ===========================================================
002 * Orson Charts : a 3D chart library for the Java(tm) platform
003 * ===========================================================
004 * 
005 * (C)opyright 2013-2022, by David Gilbert.  All rights reserved.
006 * 
007 * https://github.com/jfree/orson-charts
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 org.jfree.chart3d.plot;
034
035import java.awt.Font;
036import java.io.Serializable;
037
038import org.jfree.chart3d.data.DefaultKeyedValues;
039import org.jfree.chart3d.internal.Args;
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<K extends Comparable<K>> 
050        implements FontSource<K>, Serializable {
051
052    private static Font DEFAULT_FONT = new Font("Dialog", Font.PLAIN, 12);
053    
054    /** Storage for the fonts assigned to keys. */
055    private DefaultKeyedValues<K, Font> fonts;
056
057    /** The default font (never {@code null}). */
058    private Font defaultFont;
059    
060    /**
061     * Creates a new instance with default fonts.
062     */
063    public StandardFontSource() {
064        this(DEFAULT_FONT);
065    }
066    
067    /**
068     * Creates a new font source with the specified default font.
069     * 
070     * @param defaultFont  the default font ({@code null} not permitted). 
071     */
072    public StandardFontSource(Font defaultFont) {
073        Args.nullNotPermitted(defaultFont, "defaultFont");
074        this.defaultFont = defaultFont;
075        this.fonts = new DefaultKeyedValues<>();
076    }
077    
078    /**
079     * Returns the default font.  The default value is {@link #DEFAULT_FONT}.
080     * 
081     * @return The default font (never {@code null}). 
082     */
083    public Font getDefaultFont() {
084        return this.defaultFont;
085    }
086    
087    /**
088     * Sets the default font.
089     * 
090     * @param font  the font ({@code null} not permitted).
091     */
092    public void setDefaultFont(Font font) {
093        Args.nullNotPermitted(font, "font");
094        this.defaultFont = font;
095    }
096    
097    /**
098     * Returns the font for the specified key.
099     * 
100     * @param key  the key ({@code null} not permitted).
101     * 
102     * @return The font (never {@code null}). 
103     */
104    @Override
105    public Font getFont(K key) {
106        Font result = this.fonts.getValue(key);
107        if (result != null) {
108            return result;
109        } else {
110            return this.defaultFont;
111        }
112    }
113    
114    /**
115     * Sets the font associated with the specified key.
116     * 
117     * @param key  the key ({@code null} not permitted).
118     * @param font  the font ({@code null} permitted).
119     */
120    @Override
121    public void setFont(K key, Font font) {
122        if (font != null) {
123            this.fonts.put(key, font);
124        } else {
125            this.fonts.remove(key);
126        }
127    }
128    
129    /**
130     * Clears existing font settings and sets the default font to the 
131     * supplied value.  This method is used by the framework and is not
132     * normally called by client code.
133     * 
134     * @param font  the font ({@code null} not permitted).
135     * 
136     * @since 1.2
137     */
138    @Override
139    public void style(Font font) {
140        Args.nullNotPermitted(font, "font");
141        this.defaultFont = font;
142        this.fonts.clear();
143    }
144
145    /**
146     * Tests this paint source for equality with an arbitrary object.
147     * 
148     * @param obj  the object ({@code null} permitted).
149     * 
150     * @return A boolean. 
151     */
152    @Override
153    public boolean equals(Object obj) {
154        if (obj == this) {
155            return true;
156        }
157        if (!(obj instanceof StandardFontSource)) {
158            return false;
159        }
160        StandardFontSource that = (StandardFontSource) obj;
161        if (!this.defaultFont.equals(that.defaultFont)) {
162            return false;
163        }
164        if (!this.fonts.equals(that.fonts)) {
165            return false;
166        }
167        return true;
168    }
169
170}