/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ----------------
* LegendTitle.java
* ----------------
* (C) Copyright 2002-2007, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Pierre-Marie Le Biot;
*
* Changes
* -------
* 25-Nov-2004 : First working version (DG);
* 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
* 08-Feb-2005 : Updated for changes in RectangleConstraint class (DG);
* 11-Feb-2005 : Implemented PublicCloneable (DG);
* 23-Feb-2005 : Replaced chart reference with LegendItemSource (DG);
* 16-Mar-2005 : Added itemFont attribute (DG);
* 17-Mar-2005 : Fixed missing fillShape setting (DG);
* 20-Apr-2005 : Added new draw() method (DG);
* 03-May-2005 : Modified equals() method to ignore sources (DG);
* 13-May-2005 : Added settings for legend item label and graphic padding (DG);
* 09-Jun-2005 : Fixed serialization bug (DG);
* 01-Sep-2005 : Added itemPaint attribute (PMLB);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 20-Jul-2006 : Use new LegendItemBlockContainer to restore support for
* LegendItemEntities (DG);
* 06-Oct-2006 : Add tooltip and URL text to legend item (DG);
* 13-Dec-2006 : Added support for GradientPaint in legend items (DG);
* 16-Mar-2007 : Updated border drawing for changes in AbstractBlock (DG);
* 18-May-2007 : Pass seriesKey and dataset to legend item block (DG);
*
*/
package com.positive.charts.title;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import com.positive.charts.block.Arrangement;
import com.positive.charts.block.Block;
import com.positive.charts.block.BlockContainer;
import com.positive.charts.block.BlockFrame;
import com.positive.charts.block.BorderArrangement;
import com.positive.charts.block.CenterArrangement;
import com.positive.charts.block.ColumnArrangement;
import com.positive.charts.block.FlowArrangement;
import com.positive.charts.block.LabelBlock;
import com.positive.charts.block.RectangleConstraint;
import com.positive.charts.block.RectangleInsets;
import com.positive.charts.common.RectangleEdge;
import com.positive.charts.data.util.PublicCloneable;
import com.positive.charts.data.util.RectangleAnchor;
import com.positive.charts.event.TitleChangeEvent;
import com.positive.charts.legend.LegendItem;
import com.positive.charts.legend.LegendItemCollection;
import com.positive.charts.legend.LegendItemSource;
import com.positive.charts.util.Size2D;
import com.positive.colorchecker.StaticColorChecker;
/**
* A chart title that displays a legend for the data in the chart.
* <P>
* The title can be populated with legend items manually, or you can assign a
* reference to the plot, in which case the legend items will be automatically
* created to match the dataset(s).
*/
public class LegendTitle extends Title implements Cloneable, PublicCloneable,
Serializable {
/** For serialization. */
private static final long serialVersionUID = 2644010518533854633L;
/** The default item font. */
public static final Font DEFAULT_ITEM_FONT = new Font(Display.getCurrent(),
"SansSerif", 10, SWT.NONE);
/** The sources for legend items. */
private LegendItemSource[] sources;
/** The background paint (possibly <code>null</code>). */
private transient Color backgroundPaint;
/** The edge for the legend item graphic relative to the text. */
private RectangleEdge legendItemGraphicEdge;
/** The anchor point for the legend item graphic. */
private RectangleAnchor legendItemGraphicAnchor;
/** The lege7nd item graphic location. */
private RectangleAnchor legendItemGraphicLocation;
/** The padding for the legend item graphic. */
private RectangleInsets legendItemGraphicPadding;
/** The item font. */
private Font itemFont;
/** The item paint. */
private transient Color itemPaint;
/** The padding for the item labels. */
private RectangleInsets itemLabelPadding;
/**
* A container that holds and displays the legend items.
*/
private final BlockContainer items;
/**
* The layout for the legend when it is positioned at the top or bottom of
* the chart.
*/
private final Arrangement hLayout;
/**
* The layout for the legend when it is positioned at the left or right of
* the chart.
*/
private final Arrangement vLayout;
/**
* An optional container for wrapping the legend items (allows for adding a
* title or other text to the legend).
*/
private BlockContainer wrapper;
private final Color DEFAULT_ITEM_PAINT = StaticColorChecker.dublicateColor(SWT.COLOR_BLACK);// Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
/**
* Constructs a new (empty) legend for the specified source.
*
* @param source
* the source.
*/
public LegendTitle(final LegendItemSource source) {
this(source, new FlowArrangement(), new ColumnArrangement());
}
/**
* Creates a new legend title with the specified arrangement.
*
* @param source
* the source.
* @param hLayout
* the horizontal item arrangement (<code>null</code> not
* permitted).
* @param vLayout
* the vertical item arrangement (<code>null</code> not
* permitted).
*/
public LegendTitle(final LegendItemSource source,
final Arrangement hLayout, final Arrangement vLayout) {
this.sources = new LegendItemSource[] { source };
this.items = new BlockContainer(hLayout);
this.hLayout = hLayout;
this.vLayout = vLayout;
this.backgroundPaint = null;
this.legendItemGraphicEdge = RectangleEdge.LEFT;
this.legendItemGraphicAnchor = RectangleAnchor.CENTER;
this.legendItemGraphicLocation = RectangleAnchor.CENTER;
this.legendItemGraphicPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0);
this.itemFont = DEFAULT_ITEM_FONT;
this.itemPaint = this.DEFAULT_ITEM_PAINT;
this.itemLabelPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0);
}
/**
* Arranges the contents of the block, within the given constraints, and
* returns the block size.
*
* @param g2
* the graphics device.
* @param constraint
* the constraint (<code>null</code> not permitted).
*
* @return The block size (in Java2D units, never <code>null</code>).
*/
public Size2D arrange(final GC g2, final RectangleConstraint constraint) {
final Size2D result = new Size2D();
this.fetchLegendItems();
if (this.items.isEmpty()) {
return result;
}
BlockContainer container = this.wrapper;
if (container == null) {
container = this.items;
}
final RectangleConstraint c = this.toContentConstraint(constraint);
final Size2D size = container.arrange(g2, c);
result.height = this.calculateTotalHeight(size.height);
result.width = this.calculateTotalWidth(size.width);
return result;
}
/**
* Creates a legend item block.
*
* @param item
* the legend item.
*
* @return The block.
*/
protected Block createLegendItemBlock(final LegendItem item) {
BlockContainer result = null;
final LegendGraphic lg = new LegendGraphic(item.getShape(), item
.getFillPaint());
// lg.setFillPaintTransformer(item.getFillPaintTransformer());
lg.setShapeFilled(item.isShapeFilled());
lg.setLine(item.getLine());
// lg.setLineStroke(item.getLineStroke());
// lg.setLinePaint(item.getLinePaint());
lg.setLineVisible(item.isLineVisible());
lg.setShapeVisible(item.isShapeVisible());
lg.setShapeOutlineVisible(item.isShapeOutlineVisible());
// lg.setOutlinePaint(item.getOutlinePaint());
// lg.setOutlineStroke(item.getOutlineStroke());
lg.setPadding(this.legendItemGraphicPadding);
final LegendItemBlockContainer legendItem = new LegendItemBlockContainer(
new BorderArrangement(), item.getDataset(), item.getSeriesKey());
lg.setShapeAnchor(this.getLegendItemGraphicAnchor());
lg.setShapeLocation(this.getLegendItemGraphicLocation());
legendItem.add(lg, this.legendItemGraphicEdge);
final LabelBlock labelBlock = new LabelBlock(item.getLabel(),
this.itemFont, this.itemPaint);
labelBlock.setPadding(this.itemLabelPadding);
legendItem.add(labelBlock);
legendItem.setToolTipText(item.getToolTipText());
legendItem.setURLText(item.getURLText());
result = new BlockContainer(new CenterArrangement());
result.add(legendItem);
return result;
}
/**
* Draws the title on a Java 2D graphics device (such as the screen or a
* printer).
*
* @param g2
* the graphics device.
* @param area
* the available area for the title.
*/
public void draw(final GC g2, final Rectangle area) {
this.draw(g2, area, null);
}
/**
* Draws the block within the specified area.
*
* @param g2
* the graphics device.
* @param area
* the area.
* @param params
* ignored (<code>null</code> permitted).
*
* @return An {@link com.positive.charts.block.EntityBlockResult} or
* <code>null</code>.
*/
public Object draw(final GC g2, final Rectangle area, final Object params) {
Rectangle target = new Rectangle(area.x, area.y, area.width,
area.height);
target = this.trimMargin(target);
if (this.backgroundPaint != null) {
g2.setBackground(this.backgroundPaint);
g2.fillRectangle(target);
}
final BlockFrame border = this.getFrame();
border.draw(g2, target);
border.getInsets().trim(target);
BlockContainer container = this.wrapper;
if (container == null) {
container = this.items;
}
target = this.trimPadding(target);
target.x += 2;
return container.draw(g2, target, params);
}
/**
* Tests this title for equality with an arbitrary object.
*
* @param obj
* the object (<code>null</code> permitted).
*
* @return A boolean.
*/
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof LegendTitle)) {
return false;
}
if (!super.equals(obj)) {
return false;
}
final LegendTitle that = (LegendTitle) obj;
if (this.legendItemGraphicEdge != that.legendItemGraphicEdge) {
return false;
}
if (this.legendItemGraphicAnchor != that.legendItemGraphicAnchor) {
return false;
}
if (this.legendItemGraphicLocation != that.legendItemGraphicLocation) {
return false;
}
if (!this.itemFont.equals(that.itemFont)) {
return false;
}
if (!this.itemPaint.equals(that.itemPaint)) {
return false;
}
if (!this.hLayout.equals(that.hLayout)) {
return false;
}
if (!this.vLayout.equals(that.vLayout)) {
return false;
}
return true;
}
/**
* Fetches the latest legend items.
*/
protected void fetchLegendItems() {
this.items.clear();
final RectangleEdge p = this.getPosition();
if (RectangleEdge.isTopOrBottom(p)) {
this.items.setArrangement(this.hLayout);
} else {
this.items.setArrangement(this.vLayout);
}
for (int s = 0; s < this.sources.length; s++) {
final LegendItemCollection legendItems = this.sources[s]
.getLegendItems();
if (legendItems != null) {
for (int i = 0; i < legendItems.getItemCount(); i++) {
final LegendItem item = legendItems.get(i);
final Block block = this.createLegendItemBlock(item);
this.items.add(block);
}
}
}
}
/**
* Returns the background paint.
*
* @return The background paint (possibly <code>null</code>).
*/
public Color getBackgroundPaint() {
return this.backgroundPaint;
}
/**
* Returns the container that holds the legend items.
*
* @return The container for the legend items.
*/
public BlockContainer getItemContainer() {
return this.items;
}
/**
* Returns the item font.
*
* @return The font (never <code>null</code>).
*/
public Font getItemFont() {
return this.itemFont;
}
/**
* Returns the padding used for the items labels.
*
* @return The padding (never <code>null</code>).
*/
public RectangleInsets getItemLabelPadding() {
return this.itemLabelPadding;
}
/**
* Returns the item paint.
*
* @return The paint (never <code>null</code>).
*/
public Color getItemPaint() {
return this.itemPaint;
}
/**
* Returns the legend item graphic anchor.
*
* @return The graphic anchor (never <code>null</code>).
*/
public RectangleAnchor getLegendItemGraphicAnchor() {
return this.legendItemGraphicAnchor;
}
/**
* Returns the location of the shape within each legend item.
*
* @return The location (never <code>null</code>).
*/
public RectangleEdge getLegendItemGraphicEdge() {
return this.legendItemGraphicEdge;
}
/**
* Returns the legend item graphic location.
*
* @return The location (never <code>null</code>).
*/
public RectangleAnchor getLegendItemGraphicLocation() {
return this.legendItemGraphicLocation;
}
/**
* Returns the padding that will be applied to each item graphic.
*
* @return The padding (never <code>null</code>).
*/
public RectangleInsets getLegendItemGraphicPadding() {
return this.legendItemGraphicPadding;
}
/**
* Returns the legend item sources.
*
* @return The sources.
*/
public LegendItemSource[] getSources() {
return this.sources;
}
/**
* 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(final ObjectInputStream stream) throws IOException,
ClassNotFoundException {
stream.defaultReadObject();
// this.backgroundPaint = SerialUtilities.readPaint(stream);
// this.itemPaint = SerialUtilities.readPaint(stream);
}
/**
* Sets the background paint for the legend and sends a
* {@link TitleChangeEvent} to all registered listeners.
*
* @param paint
* the paint (<code>null</code> permitted).
*/
public void setBackgroundPaint(final Color paint) {
this.backgroundPaint = paint;
this.notifyListeners(new TitleChangeEvent(this));
}
/**
* Sets the item font and sends a {@link TitleChangeEvent} to all registered
* listeners.
*
* @param font
* the font (<code>null</code> not permitted).
*/
public void setItemFont(final Font font) {
if (font == null) {
throw new IllegalArgumentException("Null 'font' argument.");
}
this.itemFont = font;
this.notifyListeners(new TitleChangeEvent(this));
}
/**
* Sets the padding used for the item labels in the legend.
*
* @param padding
* the padding (<code>null</code> not permitted).
*/
public void setItemLabelPadding(final RectangleInsets padding) {
if (padding == null) {
throw new IllegalArgumentException("Null 'padding' argument.");
}
this.itemLabelPadding = padding;
this.notifyListeners(new TitleChangeEvent(this));
}
/**
* Sets the item paint.
*
* @param paint
* the paint (<code>null</code> not permitted).
*/
public void setItemPaint(final Color paint) {
if (paint == null) {
throw new IllegalArgumentException("Null 'paint' argument.");
}
this.itemPaint = paint;
this.notifyListeners(new TitleChangeEvent(this));
}
/**
* Sets the anchor point used for the graphic in each legend item.
*
* @param anchor
* the anchor point (<code>null</code> not permitted).
*/
public void setLegendItemGraphicAnchor(final RectangleAnchor anchor) {
if (anchor == null) {
throw new IllegalArgumentException("Null 'anchor' point.");
}
this.legendItemGraphicAnchor = anchor;
}
/**
* Sets the location of the shape within each legend item.
*
* @param edge
* the edge (<code>null</code> not permitted).
*/
public void setLegendItemGraphicEdge(final RectangleEdge edge) {
if (edge == null) {
throw new IllegalArgumentException("Null 'edge' argument.");
}
this.legendItemGraphicEdge = edge;
this.notifyListeners(new TitleChangeEvent(this));
}
/**
* Sets the legend item graphic location.
*
* @param anchor
* the anchor (<code>null</code> not permitted).
*/
public void setLegendItemGraphicLocation(final RectangleAnchor anchor) {
this.legendItemGraphicLocation = anchor;
}
/**
* Sets the padding that will be applied to each item graphic in the legend
* and sends a {@link TitleChangeEvent} to all registered listeners.
*
* @param padding
* the padding (<code>null</code> not permitted).
*/
public void setLegendItemGraphicPadding(final RectangleInsets padding) {
if (padding == null) {
throw new IllegalArgumentException("Null 'padding' argument.");
}
this.legendItemGraphicPadding = padding;
this.notifyListeners(new TitleChangeEvent(this));
}
/**
* Sets the legend item sources and sends a {@link TitleChangeEvent} to all
* registered listeners.
*
* @param sources
* the sources (<code>null</code> not permitted).
*/
public void setSources(final LegendItemSource[] sources) {
if (sources == null) {
throw new IllegalArgumentException("Null 'sources' argument.");
}
this.sources = sources;
this.notifyListeners(new TitleChangeEvent(this));
}
/**
* Sets the wrapper container for the legend.
*
* @param wrapper
* the wrapper container.
*/
public void setWrapper(final BlockContainer wrapper) {
this.wrapper = wrapper;
}
/**
* Provides serialization support.
*
* @param stream
* the output stream.
*
* @throws IOException
* if there is an I/O error.
*/
private void writeObject(final ObjectOutputStream stream)
throws IOException {
stream.defaultWriteObject();
// SerialUtilities.writePaint(this.backgroundPaint, stream);
// SerialUtilities.writePaint(this.itemPaint, stream);
}
}