001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -------------------------
028: * ExtendedCategoryAxis.java
029: * -------------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: ExtendedCategoryAxis.java,v 1.4.2.2 2007/03/21 11:16:59 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 07-Nov-2003 : Version 1 (DG);
040: * 07-Jan-2004 : Updated the createLabel() method (DG);
041: * 29-Jan-2004 : Added paint attribute (DG);
042: * ------------- JFREECHART 1.0.x ---------------------------------------------
043: * 21-Mar-2007 : Implemented equals(), clone() and fixed serialization (DG);
044: *
045: */
046:
047: package org.jfree.chart.axis;
048:
049: import java.awt.Color;
050: import java.awt.Font;
051: import java.awt.Graphics2D;
052: import java.awt.Paint;
053: import java.io.IOException;
054: import java.io.ObjectInputStream;
055: import java.io.ObjectOutputStream;
056: import java.util.HashMap;
057: import java.util.Map;
058:
059: import org.jfree.chart.event.AxisChangeEvent;
060: import org.jfree.io.SerialUtilities;
061: import org.jfree.text.TextBlock;
062: import org.jfree.text.TextFragment;
063: import org.jfree.text.TextLine;
064: import org.jfree.ui.RectangleEdge;
065: import org.jfree.util.PaintUtilities;
066:
067: /**
068: * An extended version of the {@link CategoryAxis} class that supports
069: * sublabels on the axis.
070: */
071: public class ExtendedCategoryAxis extends CategoryAxis {
072:
073: /** Storage for the sublabels. */
074: private Map sublabels;
075:
076: /** The sublabel font. */
077: private Font sublabelFont;
078:
079: /** The sublabel paint. */
080: private transient Paint sublabelPaint;
081:
082: /**
083: * Creates a new axis.
084: *
085: * @param label the axis label.
086: */
087: public ExtendedCategoryAxis(String label) {
088: super (label);
089: this .sublabels = new HashMap();
090: this .sublabelFont = new Font("SansSerif", Font.PLAIN, 10);
091: this .sublabelPaint = Color.black;
092: }
093:
094: /**
095: * Returns the font for the sublabels.
096: *
097: * @return The font (never <code>null</code>).
098: *
099: * @see #setSubLabelFont(Font)
100: */
101: public Font getSubLabelFont() {
102: return this .sublabelFont;
103: }
104:
105: /**
106: * Sets the font for the sublabels and sends an {@link AxisChangeEvent} to
107: * all registered listeners.
108: *
109: * @param font the font (<code>null</code> not permitted).
110: *
111: * @see #getSubLabelFont()
112: */
113: public void setSubLabelFont(Font font) {
114: if (font == null) {
115: throw new IllegalArgumentException("Null 'font' argument.");
116: }
117: this .sublabelFont = font;
118: notifyListeners(new AxisChangeEvent(this ));
119: }
120:
121: /**
122: * Returns the paint for the sublabels.
123: *
124: * @return The paint (never <code>null</code>).
125: *
126: * @see #setSubLabelPaint(Paint)
127: */
128: public Paint getSubLabelPaint() {
129: return this .sublabelPaint;
130: }
131:
132: /**
133: * Sets the paint for the sublabels and sends an {@link AxisChangeEvent}
134: * to all registered listeners.
135: *
136: * @param paint the paint (<code>null</code> not permitted).
137: *
138: * @see #getSubLabelPaint()
139: */
140: public void setSubLabelPaint(Paint paint) {
141: if (paint == null) {
142: throw new IllegalArgumentException("Null 'paint' argument.");
143: }
144: this .sublabelPaint = paint;
145: notifyListeners(new AxisChangeEvent(this ));
146: }
147:
148: /**
149: * Adds a sublabel for a category.
150: *
151: * @param category the category.
152: * @param label the label.
153: */
154: public void addSubLabel(Comparable category, String label) {
155: this .sublabels.put(category, label);
156: }
157:
158: /**
159: * Overrides the default behaviour by adding the sublabel to the text
160: * block that is used for the category label.
161: *
162: * @param category the category.
163: * @param width the width (not used yet).
164: * @param edge the location of the axis.
165: * @param g2 the graphics device.
166: *
167: * @return A label.
168: */
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
175: || edge == RectangleEdge.BOTTOM) {
176: TextLine line = new TextLine(s, this .sublabelFont,
177: this .sublabelPaint);
178: label.addLine(line);
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: public boolean equals(Object obj) {
199: if (obj == this ) {
200: return true;
201: }
202: if (!(obj instanceof ExtendedCategoryAxis)) {
203: return false;
204: }
205: ExtendedCategoryAxis that = (ExtendedCategoryAxis) obj;
206: if (!this .sublabelFont.equals(that.sublabelFont)) {
207: return false;
208: }
209: if (!PaintUtilities.equal(this .sublabelPaint,
210: 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: public Object clone() throws CloneNotSupportedException {
227: ExtendedCategoryAxis clone = (ExtendedCategoryAxis) super
228: .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)
241: throws IOException {
242: stream.defaultWriteObject();
243: SerialUtilities.writePaint(this .sublabelPaint, stream);
244: }
245:
246: /**
247: * Provides serialization support.
248: *
249: * @param stream the input stream.
250: *
251: * @throws IOException if there is an I/O error.
252: * @throws ClassNotFoundException if there is a classpath problem.
253: */
254: private void readObject(ObjectInputStream stream)
255: throws IOException, ClassNotFoundException {
256: stream.defaultReadObject();
257: this.sublabelPaint = SerialUtilities.readPaint(stream);
258: }
259:
260: }
|