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