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