1 /* =========================================================== 2 * JFreeChart : a free chart library for the Java(tm) platform 3 * =========================================================== 4 * 5 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors. 6 * 7 * Project Info: http://www.jfree.org/jfreechart/index.html 8 * 9 * This library is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU Lesser General Public License as published by 11 * the Free Software Foundation; either version 2.1 of the License, or 12 * (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17 * License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22 * USA. 23 * 24 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 25 * Other names may be trademarks of their respective owners.] 26 * 27 * ---------------------- 28 * PlotRenderingInfo.java 29 * ---------------------- 30 * (C) Copyright 2003-2008, by Object Refinery Limited. 31 * 32 * Original Author: David Gilbert (for Object Refinery Limited); 33 * Contributor(s): -; 34 * 35 * Changes 36 * ------- 37 * 16-Sep-2003 : Version 1 (DG); 38 * 23-Sep-2003 : Added Javadocs (DG); 39 * 12-Nov-2004 : Added getSubplotCount() and findSubplot() methods (DG); 40 * 01-Nov-2005 : Made 'owner' non-transient to fix bug 1344048 (DG); 41 * ------------- JFREECHART 1.0.x --------------------------------------------- 42 * 01-Dec-2006 : Implemented clone() method properly (DG); 43 * 17-Apr-2007 : Fixed bug 1698965 (NPE in CombinedDomainXYPlot) (DG); 44 * 45 */ 46 47 package org.jfree.chart.plot; 48 49 import java.awt.geom.Point2D; 50 import java.awt.geom.Rectangle2D; 51 import java.io.IOException; 52 import java.io.ObjectInputStream; 53 import java.io.ObjectOutputStream; 54 import java.io.Serializable; 55 import java.util.List; 56 57 import org.jfree.chart.ChartRenderingInfo; 58 import org.jfree.chart.util.ParamChecks; 59 import org.jfree.io.SerialUtilities; 60 import org.jfree.util.ObjectUtilities; 61 62 /** 63 * Stores information about the dimensions of a plot and its subplots. 64 */ 65 public class PlotRenderingInfo implements Cloneable, Serializable { 66 67 /** For serialization. */ 68 private static final long serialVersionUID = 8446720134379617220L; 69 70 /** The owner of this info. */ 71 private ChartRenderingInfo owner; 72 73 /** The plot area. */ 74 private transient Rectangle2D plotArea; 75 76 /** The data area. */ 77 private transient Rectangle2D dataArea; 78 79 /** 80 * Storage for the plot rendering info objects belonging to the subplots. 81 */ 82 private List subplotInfo; 83 84 /** 85 * Creates a new instance. 86 * 87 * @param owner the owner (<code>null</code> permitted). 88 */ PlotRenderingInfo(ChartRenderingInfo owner)89 public PlotRenderingInfo(ChartRenderingInfo owner) { 90 this.owner = owner; 91 this.dataArea = new Rectangle2D.Double(); 92 this.subplotInfo = new java.util.ArrayList(); 93 } 94 95 /** 96 * Returns the owner (as specified in the constructor). 97 * 98 * @return The owner (possibly <code>null</code>). 99 */ getOwner()100 public ChartRenderingInfo getOwner() { 101 return this.owner; 102 } 103 104 /** 105 * Returns the plot area (in Java2D space). 106 * 107 * @return The plot area (possibly <code>null</code>). 108 * 109 * @see #setPlotArea(Rectangle2D) 110 */ getPlotArea()111 public Rectangle2D getPlotArea() { 112 return this.plotArea; 113 } 114 115 /** 116 * Sets the plot area. 117 * 118 * @param area the plot area (in Java2D space, <code>null</code> 119 * permitted but discouraged) 120 * 121 * @see #getPlotArea() 122 */ setPlotArea(Rectangle2D area)123 public void setPlotArea(Rectangle2D area) { 124 this.plotArea = area; 125 } 126 127 /** 128 * Returns the plot's data area (in Java2D space). 129 * 130 * @return The data area (possibly <code>null</code>). 131 * 132 * @see #setDataArea(Rectangle2D) 133 */ getDataArea()134 public Rectangle2D getDataArea() { 135 return this.dataArea; 136 } 137 138 /** 139 * Sets the data area. 140 * 141 * @param area the data area (in Java2D space, <code>null</code> permitted 142 * but discouraged). 143 * 144 * @see #getDataArea() 145 */ setDataArea(Rectangle2D area)146 public void setDataArea(Rectangle2D area) { 147 this.dataArea = area; 148 } 149 150 /** 151 * Returns the number of subplots (possibly zero). 152 * 153 * @return The subplot count. 154 */ getSubplotCount()155 public int getSubplotCount() { 156 return this.subplotInfo.size(); 157 } 158 159 /** 160 * Adds the info for a subplot. 161 * 162 * @param info the subplot info. 163 * 164 * @see #getSubplotInfo(int) 165 */ addSubplotInfo(PlotRenderingInfo info)166 public void addSubplotInfo(PlotRenderingInfo info) { 167 this.subplotInfo.add(info); 168 } 169 170 /** 171 * Returns the info for a subplot. 172 * 173 * @param index the subplot index. 174 * 175 * @return The info. 176 * 177 * @see #addSubplotInfo(PlotRenderingInfo) 178 */ getSubplotInfo(int index)179 public PlotRenderingInfo getSubplotInfo(int index) { 180 return (PlotRenderingInfo) this.subplotInfo.get(index); 181 } 182 183 /** 184 * Returns the index of the subplot that contains the specified 185 * (x, y) point (the "source" point). The source point will usually 186 * come from a mouse click on a {@link org.jfree.chart.ChartPanel}, 187 * and this method is then used to determine the subplot that 188 * contains the source point. 189 * 190 * @param source the source point (in Java2D space, <code>null</code> not 191 * permitted). 192 * 193 * @return The subplot index (or -1 if no subplot contains 194 * <code>source</code>). 195 */ getSubplotIndex(Point2D source)196 public int getSubplotIndex(Point2D source) { 197 ParamChecks.nullNotPermitted(source, "source"); 198 int subplotCount = getSubplotCount(); 199 for (int i = 0; i < subplotCount; i++) { 200 PlotRenderingInfo info = getSubplotInfo(i); 201 Rectangle2D area = info.getDataArea(); 202 if (area.contains(source)) { 203 return i; 204 } 205 } 206 return -1; 207 } 208 209 /** 210 * Tests this instance for equality against an arbitrary object. 211 * 212 * @param obj the object (<code>null</code> permitted). 213 * 214 * @return A boolean. 215 */ 216 @Override equals(Object obj)217 public boolean equals(Object obj) { 218 if (this == obj) { 219 return true; 220 } 221 if (!(obj instanceof PlotRenderingInfo)) { 222 return false; 223 } 224 PlotRenderingInfo that = (PlotRenderingInfo) obj; 225 if (!ObjectUtilities.equal(this.dataArea, that.dataArea)) { 226 return false; 227 } 228 if (!ObjectUtilities.equal(this.plotArea, that.plotArea)) { 229 return false; 230 } 231 if (!ObjectUtilities.equal(this.subplotInfo, that.subplotInfo)) { 232 return false; 233 } 234 return true; 235 } 236 237 /** 238 * Returns a clone of this object. 239 * 240 * @return A clone. 241 * 242 * @throws CloneNotSupportedException if there is a problem cloning. 243 */ 244 @Override clone()245 public Object clone() throws CloneNotSupportedException { 246 PlotRenderingInfo clone = (PlotRenderingInfo) super.clone(); 247 if (this.plotArea != null) { 248 clone.plotArea = (Rectangle2D) this.plotArea.clone(); 249 } 250 if (this.dataArea != null) { 251 clone.dataArea = (Rectangle2D) this.dataArea.clone(); 252 } 253 clone.subplotInfo = new java.util.ArrayList(this.subplotInfo.size()); 254 for (int i = 0; i < this.subplotInfo.size(); i++) { 255 PlotRenderingInfo info 256 = (PlotRenderingInfo) this.subplotInfo.get(i); 257 clone.subplotInfo.add(info.clone()); 258 } 259 return clone; 260 } 261 262 /** 263 * Provides serialization support. 264 * 265 * @param stream the output stream. 266 * 267 * @throws IOException if there is an I/O error. 268 */ writeObject(ObjectOutputStream stream)269 private void writeObject(ObjectOutputStream stream) throws IOException { 270 stream.defaultWriteObject(); 271 SerialUtilities.writeShape(this.dataArea, stream); 272 SerialUtilities.writeShape(this.plotArea, stream); 273 } 274 275 /** 276 * Provides serialization support. 277 * 278 * @param stream the input stream. 279 * 280 * @throws IOException if there is an I/O error. 281 * @throws ClassNotFoundException if there is a classpath problem. 282 */ readObject(ObjectInputStream stream)283 private void readObject(ObjectInputStream stream) 284 throws IOException, ClassNotFoundException { 285 stream.defaultReadObject(); 286 this.dataArea = (Rectangle2D) SerialUtilities.readShape(stream); 287 this.plotArea = (Rectangle2D) SerialUtilities.readShape(stream); 288 } 289 290 } 291