/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------------
* PeriodAxisLabelInfo.java
* ------------------------
* (C) Copyright 2004, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* $Id: PeriodAxisLabelInfo.java,v 1.1 2004/08/31 14:30:24 mungady Exp $
*
* Changes
* -------
* 01-Jun-2004 : Version 1 (DG);
*
*/
package org.jfree.chart.axis;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.awt.Stroke;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.io.SerialUtilities;
import org.jfree.ui.Spacer;
/**
* A record that contains information for one "band" of date labels in a {@link PeriodAxis}.
*/
public class PeriodAxisLabelInfo implements Cloneable, Serializable {
/** The subclass of {@link RegularTimePeriod} to use for this band. */
private Class periodClass;
/** Controls the gaps around the band. */
private Spacer spacer;
/** The date formatter. */
private DateFormat dateFormat;
/** The label font. */
private Font labelFont;
/** The label paint. */
private transient Paint labelPaint;
/** A flag that controls whether or not dividers are visible. */
private boolean drawDividers;
/** The stroke used to draw the dividers. */
private transient Stroke dividerStroke;
/** The paint used to draw the dividers. */
private transient Paint dividerPaint;
/**
* Creates a new instance.
*
* @param periodClass the subclass of {@link RegularTimePeriod} to use (<code>null</code>
* not permitted).
* @param dateFormat the date formatter (<code>null</code> not permitted).
*/
public PeriodAxisLabelInfo(Class periodClass, String dateFormat) {
this(
periodClass, dateFormat, new Spacer(Spacer.ABSOLUTE, 2, 2, 2, 2),
new Font("SansSerif", Font.PLAIN, 10), Color.black,
true, new BasicStroke(0.5f), Color.gray
);
}
/**
* Creates a new instance.
*
* @param periodClass the subclass of {@link RegularTimePeriod} to use (<code>null</code>
* not permitted).
* @param dateFormat the date formatter (<code>null</code> not permitted).
* @param spacer controls the space around the band.
* @param labelFont the label font.
* @param labelPaint the label paint.
* @param drawDividers a flag that controls whether dividers are drawn.
* @param dividerStroke the stroke used to draw the dividers.
* @param dividerPaint the paint used to draw the dividers.
*/
public PeriodAxisLabelInfo(Class periodClass, String dateFormat,
Spacer spacer,
Font labelFont, Paint labelPaint,
boolean drawDividers, Stroke dividerStroke, Paint dividerPaint) {
this.periodClass = periodClass;
this.dateFormat = new SimpleDateFormat(dateFormat);
this.spacer = spacer;
this.labelFont = labelFont;
this.labelPaint = labelPaint;
this.drawDividers = drawDividers;
this.dividerStroke = dividerStroke;
this.dividerPaint = dividerPaint;
}
/**
* Returns the subclass of {@link RegularTimePeriod} that should be used to generate the
* date labels.
*
* @return The class.
*/
public Class getPeriodClass() {
return this.periodClass;
}
/**
* Returns the date formatter.
*
* @return The date formatter (never <code>null</code>).
*/
public DateFormat getDateFormat() {
return this.dateFormat;
}
/**
* Returns the spacer for the band.
*
* @return The spacer.
*/
public Spacer getSpacer() {
return this.spacer;
}
/**
* Returns the label font.
*
* @return The label font (never <code>null</code>).
*/
public Font getLabelFont() {
return this.labelFont;
}
/**
* Returns the label paint.
*
* @return The label paint.
*/
public Paint getLabelPaint() {
return this.labelPaint;
}
/**
* Returns a flag that controls whether or not dividers are drawn.
*
* @return A flag.
*/
public boolean getDrawDividers() {
return this.drawDividers;
}
/**
* Returns the stroke used to draw the dividers.
*
* @return The stroke.
*/
public Stroke getDividerStroke() {
return this.dividerStroke;
}
/**
* Returns the paint used to draw the dividers.
*
* @return The paint.
*/
public Paint getDividerPaint() {
return this.dividerPaint;
}
/**
* Creates a time period that includes the specified millisecond, assuming the given time
* zone.
*
* @param millisecond the time.
* @param zone the time zone.
*
* @return The time period.
*/
public RegularTimePeriod createInstance(Date millisecond, TimeZone zone) {
RegularTimePeriod result = null;
try {
Constructor c = this.periodClass.getDeclaredConstructor(
new Class[] {Date.class, TimeZone.class}
);
result = (RegularTimePeriod) c.newInstance(new Object[] {millisecond, zone});
}
catch (Exception e) {
// do nothing
}
return result;
}
/**
* Tests this object for equality with an arbitrary object.
*
* @param obj the object to test against (<code>null</code> permitted).
*
* @return A boolean.
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof PeriodAxisLabelInfo) {
PeriodAxisLabelInfo info = (PeriodAxisLabelInfo) obj;
if (!info.periodClass.equals(this.periodClass)) {
return false;
}
if (!info.dateFormat.equals(this.dateFormat)) {
return false;
}
if (!info.spacer.equals(this.spacer)) {
return false;
}
if (!info.labelFont.equals(this.labelFont)) {
return false;
}
if (!info.labelPaint.equals(this.labelPaint)) {
return false;
}
if (info.drawDividers != this.drawDividers) {
return false;
}
if (!info.dividerStroke.equals(this.dividerStroke)) {
return false;
}
if (!info.dividerPaint.equals(this.dividerPaint)) {
return false;
}
return true;
}
return false;
}
/**
* Returns a clone of the object.
*
* @return A clone.
*
* @throws CloneNotSupportedException if cloning is not supported.
*/
public Object clone() throws CloneNotSupportedException {
Object clone = (PeriodAxisLabelInfo) super.clone();
return clone;
}
/**
* Provides serialization support.
*
* @param stream the output stream.
*
* @throws IOException if there is an I/O error.
*/
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
SerialUtilities.writePaint(this.labelPaint, stream);
SerialUtilities.writeStroke(this.dividerStroke, stream);
SerialUtilities.writePaint(this.dividerPaint, stream);
}
/**
* Provides serialization support.
*
* @param stream the input stream.
*
* @throws IOException if there is an I/O error.
* @throws ClassNotFoundException if there is a classpath problem.
*/
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
this.labelPaint = SerialUtilities.readPaint(stream);
this.dividerStroke = SerialUtilities.readStroke(stream);
this.dividerPaint = SerialUtilities.readPaint(stream);
}
}