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 * ExtendedCategoryAxis.java
029 * -------------------------
030 * (C) Copyright 2003-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.axis;
038
039import java.awt.Color;
040import java.awt.Font;
041import java.awt.Graphics2D;
042import java.awt.Paint;
043import java.io.IOException;
044import java.io.ObjectInputStream;
045import java.io.ObjectOutputStream;
046import java.util.HashMap;
047import java.util.Map;
048
049import org.jfree.chart.event.AxisChangeEvent;
050import org.jfree.chart.text.TextBlock;
051import org.jfree.chart.text.TextFragment;
052import org.jfree.chart.text.TextLine;
053import org.jfree.chart.ui.RectangleEdge;
054import org.jfree.chart.util.PaintUtils;
055import org.jfree.chart.util.Args;
056import org.jfree.chart.util.SerialUtils;
057
058/**
059 * An extended version of the {@link CategoryAxis} class that supports
060 * sublabels on the axis.
061 */
062public class ExtendedCategoryAxis extends CategoryAxis {
063
064    /** For serialization. */
065    static final long serialVersionUID = -3004429093959826567L;
066
067    /** Storage for the sublabels. */
068    private Map sublabels;
069
070    /** The sublabel font. */
071    private Font sublabelFont;
072
073    /** The sublabel paint. */
074    private transient Paint sublabelPaint;
075
076    /**
077     * Creates a new axis.
078     *
079     * @param label  the axis label.
080     */
081    public ExtendedCategoryAxis(String label) {
082        super(label);
083        this.sublabels = new HashMap();
084        this.sublabelFont = new Font("SansSerif", Font.PLAIN, 10);
085        this.sublabelPaint = Color.BLACK;
086    }
087
088    /**
089     * Returns the font for the sublabels.
090     *
091     * @return The font (never {@code null}).
092     *
093     * @see #setSubLabelFont(Font)
094     */
095    public Font getSubLabelFont() {
096        return this.sublabelFont;
097    }
098
099    /**
100     * Sets the font for the sublabels and sends an {@link AxisChangeEvent} to
101     * all registered listeners.
102     *
103     * @param font  the font ({@code null} not permitted).
104     *
105     * @see #getSubLabelFont()
106     */
107    public void setSubLabelFont(Font font) {
108        Args.nullNotPermitted(font, "font");
109        this.sublabelFont = font;
110        notifyListeners(new AxisChangeEvent(this));
111    }
112
113    /**
114     * Returns the paint for the sublabels.
115     *
116     * @return The paint (never {@code null}).
117     *
118     * @see #setSubLabelPaint(Paint)
119     */
120    public Paint getSubLabelPaint() {
121        return this.sublabelPaint;
122    }
123
124    /**
125     * Sets the paint for the sublabels and sends an {@link AxisChangeEvent}
126     * to all registered listeners.
127     *
128     * @param paint  the paint ({@code null} not permitted).
129     *
130     * @see #getSubLabelPaint()
131     */
132    public void setSubLabelPaint(Paint paint) {
133        Args.nullNotPermitted(paint, "paint");
134        this.sublabelPaint = paint;
135        notifyListeners(new AxisChangeEvent(this));
136    }
137
138    /**
139     * Adds a sublabel for a category.
140     *
141     * @param category  the category.
142     * @param label  the label.
143     */
144    public void addSubLabel(Comparable category, String label) {
145        this.sublabels.put(category, label);
146    }
147
148    /**
149     * Overrides the default behaviour by adding the sublabel to the text
150     * block that is used for the category label.
151     *
152     * @param category  the category.
153     * @param width  the width (not used yet).
154     * @param edge  the location of the axis.
155     * @param g2  the graphics device.
156     *
157     * @return A label.
158     */
159    @Override
160    protected TextBlock createLabel(Comparable category, float width,
161                                    RectangleEdge edge, Graphics2D g2) {
162        TextBlock label = super.createLabel(category, width, edge, g2);
163        String s = (String) this.sublabels.get(category);
164        if (s != null) {
165            if (edge == RectangleEdge.TOP || edge == RectangleEdge.BOTTOM) {
166                TextLine line = new TextLine(s, this.sublabelFont,
167                        this.sublabelPaint);
168                label.addLine(line);
169            }
170            else if (edge == RectangleEdge.LEFT
171                    || edge == RectangleEdge.RIGHT) {
172                TextLine line = label.getLastLine();
173                if (line != null) {
174                    line.addFragment(new TextFragment("  " + s,
175                            this.sublabelFont, this.sublabelPaint));
176                }
177            }
178        }
179        return label;
180    }
181
182    /**
183     * Tests this axis for equality with an arbitrary object.
184     *
185     * @param obj  the object ({@code null} permitted).
186     *
187     * @return A boolean.
188     */
189    @Override
190    public boolean equals(Object obj) {
191        if (obj == this) {
192            return true;
193        }
194        if (!(obj instanceof ExtendedCategoryAxis)) {
195            return false;
196        }
197        ExtendedCategoryAxis that = (ExtendedCategoryAxis) obj;
198        if (!this.sublabelFont.equals(that.sublabelFont)) {
199            return false;
200        }
201        if (!PaintUtils.equal(this.sublabelPaint, that.sublabelPaint)) {
202            return false;
203        }
204        if (!this.sublabels.equals(that.sublabels)) {
205            return false;
206        }
207        return super.equals(obj);
208    }
209
210    /**
211     * Returns a clone of this axis.
212     *
213     * @return A clone.
214     *
215     * @throws CloneNotSupportedException if there is a problem cloning.
216     */
217    @Override
218    public Object clone() throws CloneNotSupportedException {
219        ExtendedCategoryAxis clone = (ExtendedCategoryAxis) super.clone();
220        clone.sublabels = new HashMap(this.sublabels);
221        return clone;
222    }
223
224    /**
225     * Provides serialization support.
226     *
227     * @param stream  the output stream.
228     *
229     * @throws IOException  if there is an I/O error.
230     */
231    private void writeObject(ObjectOutputStream stream) throws IOException {
232        stream.defaultWriteObject();
233        SerialUtils.writePaint(this.sublabelPaint, stream);
234    }
235
236    /**
237     * Provides serialization support.
238     *
239     * @param stream  the input stream.
240     *
241     * @throws IOException  if there is an I/O error.
242     * @throws ClassNotFoundException  if there is a classpath problem.
243     */
244    private void readObject(ObjectInputStream stream)
245        throws IOException, ClassNotFoundException {
246        stream.defaultReadObject();
247        this.sublabelPaint = SerialUtils.readPaint(stream);
248    }
249
250}