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