    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, 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     * ChartRenderingInfo.java
    029     * -----------------------
    030     * (C) Copyright 2002-2005, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: ChartRenderingInfo.java,v 1.4.2.3 2005/11/30 11:48:25 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 22-Jan-2002 : Version 1 (DG);
    040     * 05-Feb-2002 : Added a new constructor, completed Javadoc comments (DG);
    041     * 05-Mar-2002 : Added a clear() method (DG);
    042     * 23-May-2002 : Renamed DrawInfo --> ChartRenderingInfo (DG);
    043     * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
    044     * 17-Sep-2003 : Added PlotRenderingInfo (DG);
    045     * 01-Nov-2005 : Updated equals() method (DG);
    046     * 30-Nov-2005 : Removed get/setPlotArea() (DG);
    047     *
    048     */
    049
    050    package org.jfree.chart;
    051
    052    import java.awt.geom.Rectangle2D;
    053    import java.io.IOException;
    054    import java.io.ObjectInputStream;
    055    import java.io.ObjectOutputStream;
    056    import java.io.Serializable;
    057
    058    import org.jfree.chart.entity.EntityCollection;
    059    import org.jfree.chart.entity.StandardEntityCollection;
    060    import org.jfree.chart.plot.PlotRenderingInfo;
    061    import org.jfree.io.SerialUtilities;
    062    import org.jfree.util.ObjectUtilities;
    063
    064    /**
    065     * A structure for storing rendering information from one call to the
    066     * JFreeChart.draw() method.
    067     * <P>
    068     * An instance of the {@link JFreeChart} class can draw itself within an
    069     * arbitrary rectangle on any <code>Graphics2D</code>.  It is assumed that
    070     * client code will sometimes render the same chart in more than one view, so
    071     * the {@link JFreeChart} instance does not retain any information about its
    072     * rendered dimensions.  This information can be useful sometimes, so you have
    073     * the option to collect the information at each call to
    074     * <code>JFreeChart.draw()</code>, by passing an instance of this
    075     * <code>ChartRenderingInfo</code> class.
    076     */
    077    public class ChartRenderingInfo implements Cloneable, Serializable {
    078
    079        /** For serialization. */
    080        private static final long serialVersionUID = 2751952018173406822L;
    081
    082        /** The area in which the chart is drawn. */
    083        private transient Rectangle2D chartArea;
    084
    085        /** Rendering info for the chart's plot (and subplots, if any). */
    086        private PlotRenderingInfo plotInfo;
    087
    088        /**
    089         * Storage for the chart entities.  Since retaining entity information for
    090         * charts with a large number of data points consumes a lot of memory, it
    091         * is intended that you can set this to <code>null</code> to prevent the
    092         * information being collected.
    093         */
    094        private EntityCollection entities;
    095
    096        /**
    097         * Constructs a new ChartRenderingInfo structure that can be used to
    098         * collect information about the dimensions of a rendered chart.
    099         */
    100        public ChartRenderingInfo() {
    101            this(new StandardEntityCollection());
    102        }
    103
    104        /**
    105         * Constructs a new instance. If an entity collection is supplied, it will
    106         * be populated with information about the entities in a chart.  If it is
    107         * <code>null</code>, no entity information (including tool tips) will
    108         * be collected.
    109         *
    110         * @param entities  an entity collection (<code>null</code> permitted).
    111         */
    112        public ChartRenderingInfo(EntityCollection entities) {
    113            this.chartArea = new Rectangle2D.Double();
    114            this.plotInfo = new PlotRenderingInfo(this);
    115            this.entities = entities;
    116        }
    117
    118        /**
    119         * Returns the area in which the chart was drawn.
    120         *
    121         * @return The area in which the chart was drawn.
    122         */
    123        public Rectangle2D getChartArea() {
    124            return this.chartArea;
    125        }
    126
    127        /**
    128         * Sets the area in which the chart was drawn.
    129         *
    130         * @param area  the chart area.
    131         */
    132        public void setChartArea(Rectangle2D area) {
    133            this.chartArea.setRect(area);
    134        }
    135
    136        /**
    137         * Returns the collection of entities maintained by this instance.
    138         *
    139         * @return The entity collection (possibly <code>null</code>.
    140         */
    141        public EntityCollection getEntityCollection() {
    142            return this.entities;
    143        }
    144
    145        /**
    146         * Sets the entity collection.
    147         *
    148         * @param entities  the entity collection (<code>null</code> permitted).
    149         */
    150        public void setEntityCollection(EntityCollection entities) {
    151            this.entities = entities;
    152        }
    153
    154        /**
    155         * Clears the information recorded by this object.
    156         */
    157        public void clear() {
    158            this.chartArea.setRect(0.0, 0.0, 0.0, 0.0);
    159            this.plotInfo = new PlotRenderingInfo(this);
    160            if (this.entities != null) {
    161                this.entities.clear();
    162            }
    163        }
    164
    165        /**
    166         * Returns the rendering info for the chart's plot.
    167         *
    168         * @return The rendering info for the plot.
    169         */
    170        public PlotRenderingInfo getPlotInfo() {
    171            return this.plotInfo;
    172        }
    173
    174        /**
    175         * Tests this object for equality with an arbitrary object.
    176         *
    177         * @param obj  the object to test against (<code>null</code> permitted).
    178         *
    179         * @return A boolean.
    180         */
    181        public boolean equals(Object obj) {
    182            if (obj == this) {
    183                return true;
    184            }
    185            if (!(obj instanceof ChartRenderingInfo)) {
    186                return false;
    187            }
    188            ChartRenderingInfo that = (ChartRenderingInfo) obj;
    189            if (!ObjectUtilities.equal(this.chartArea, that.chartArea)) {
    190                return false;
    191            }
    192            if (!ObjectUtilities.equal(this.plotInfo, that.plotInfo)) {
    193                return false;
    194            }
    195            return true;
    196        }
    197
    198        /**
    199         * Returns a clone of this object.
    200         *
    201         * @return A clone.
    202         *
    203         * @throws CloneNotSupportedException if the object cannot be cloned.
    204         */
    205        public Object clone() throws CloneNotSupportedException {
    206            return super.clone();
    207        }
    208
    209        /**
    210         * Provides serialization support.
    211         *
    212         * @param stream  the output stream.
    213         *
    214         * @throws IOException  if there is an I/O error.
    215         */
    216        private void writeObject(ObjectOutputStream stream) throws IOException {
    217            stream.defaultWriteObject();
    218            SerialUtilities.writeShape(this.chartArea, stream);
    219        }
    220
    221        /**
    222         * Provides serialization support.
    223         *
    224         * @param stream  the input stream.
    225         *
    226         * @throws IOException  if there is an I/O error.
    227         * @throws ClassNotFoundException  if there is a classpath problem.
    228         */
    229        private void readObject(ObjectInputStream stream)
    230            throws IOException, ClassNotFoundException {
    231            stream.defaultReadObject();
    232            this.chartArea = (Rectangle2D) SerialUtilities.readShape(stream);
    233        }
    234
    235    }