001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, 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 * PlotRenderingInfo.java 029 * ---------------------- 030 * (C) Copyright 2003-2008, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 16-Sep-2003 : Version 1 (DG); 038 * 23-Sep-2003 : Added Javadocs (DG); 039 * 12-Nov-2004 : Added getSubplotCount() and findSubplot() methods (DG); 040 * 01-Nov-2005 : Made 'owner' non-transient to fix bug 1344048 (DG); 041 * ------------- JFREECHART 1.0.x --------------------------------------------- 042 * 01-Dec-2006 : Implemented clone() method properly (DG); 043 * 17-Apr-2007 : Fixed bug 1698965 (NPE in CombinedDomainXYPlot) (DG); 044 * 045 */ 046 047package org.jfree.chart.plot; 048 049import java.awt.geom.Point2D; 050import java.awt.geom.Rectangle2D; 051import java.io.IOException; 052import java.io.ObjectInputStream; 053import java.io.ObjectOutputStream; 054import java.io.Serializable; 055import java.util.List; 056 057import org.jfree.chart.ChartRenderingInfo; 058import org.jfree.chart.util.ObjectUtils; 059import org.jfree.chart.util.Args; 060import org.jfree.chart.util.SerialUtils; 061 062/** 063 * Stores information about the dimensions of a plot and its subplots. 064 */ 065public class PlotRenderingInfo implements Cloneable, Serializable { 066 067 /** For serialization. */ 068 private static final long serialVersionUID = 8446720134379617220L; 069 070 /** The owner of this info. */ 071 private ChartRenderingInfo owner; 072 073 /** The plot area. */ 074 private transient Rectangle2D plotArea; 075 076 /** The data area. */ 077 private transient Rectangle2D dataArea; 078 079 /** 080 * Storage for the plot rendering info objects belonging to the subplots. 081 */ 082 private List subplotInfo; 083 084 /** 085 * Creates a new instance. 086 * 087 * @param owner the owner ({@code null} permitted). 088 */ 089 public PlotRenderingInfo(ChartRenderingInfo owner) { 090 this.owner = owner; 091 this.dataArea = new Rectangle2D.Double(); 092 this.subplotInfo = new java.util.ArrayList(); 093 } 094 095 /** 096 * Returns the owner (as specified in the constructor). 097 * 098 * @return The owner (possibly {@code null}). 099 */ 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}). 108 * 109 * @see #setPlotArea(Rectangle2D) 110 */ 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} 119 * permitted but discouraged) 120 * 121 * @see #getPlotArea() 122 */ 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}). 131 * 132 * @see #setDataArea(Rectangle2D) 133 */ 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} permitted 142 * but discouraged). 143 * 144 * @see #getDataArea() 145 */ 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 */ 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 */ 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 */ 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} not 191 * permitted). 192 * 193 * @return The subplot index (or -1 if no subplot contains {@code source}). 194 */ 195 public int getSubplotIndex(Point2D source) { 196 Args.nullNotPermitted(source, "source"); 197 int subplotCount = getSubplotCount(); 198 for (int i = 0; i < subplotCount; i++) { 199 PlotRenderingInfo info = getSubplotInfo(i); 200 Rectangle2D area = info.getDataArea(); 201 if (area.contains(source)) { 202 return i; 203 } 204 } 205 return -1; 206 } 207 208 /** 209 * Tests this instance for equality against an arbitrary object. 210 * 211 * @param obj the object ({@code null} permitted). 212 * 213 * @return A boolean. 214 */ 215 @Override 216 public boolean equals(Object obj) { 217 if (this == obj) { 218 return true; 219 } 220 if (!(obj instanceof PlotRenderingInfo)) { 221 return false; 222 } 223 PlotRenderingInfo that = (PlotRenderingInfo) obj; 224 if (!ObjectUtils.equal(this.dataArea, that.dataArea)) { 225 return false; 226 } 227 if (!ObjectUtils.equal(this.plotArea, that.plotArea)) { 228 return false; 229 } 230 if (!ObjectUtils.equal(this.subplotInfo, that.subplotInfo)) { 231 return false; 232 } 233 return true; 234 } 235 236 /** 237 * Returns a clone of this object. 238 * 239 * @return A clone. 240 * 241 * @throws CloneNotSupportedException if there is a problem cloning. 242 */ 243 @Override 244 public Object clone() throws CloneNotSupportedException { 245 PlotRenderingInfo clone = (PlotRenderingInfo) super.clone(); 246 if (this.plotArea != null) { 247 clone.plotArea = (Rectangle2D) this.plotArea.clone(); 248 } 249 if (this.dataArea != null) { 250 clone.dataArea = (Rectangle2D) this.dataArea.clone(); 251 } 252 clone.subplotInfo = new java.util.ArrayList(this.subplotInfo.size()); 253 for (int i = 0; i < this.subplotInfo.size(); i++) { 254 PlotRenderingInfo info 255 = (PlotRenderingInfo) this.subplotInfo.get(i); 256 clone.subplotInfo.add(info.clone()); 257 } 258 return clone; 259 } 260 261 /** 262 * Provides serialization support. 263 * 264 * @param stream the output stream. 265 * 266 * @throws IOException if there is an I/O error. 267 */ 268 private void writeObject(ObjectOutputStream stream) throws IOException { 269 stream.defaultWriteObject(); 270 SerialUtils.writeShape(this.dataArea, stream); 271 SerialUtils.writeShape(this.plotArea, stream); 272 } 273 274 /** 275 * Provides serialization support. 276 * 277 * @param stream the input stream. 278 * 279 * @throws IOException if there is an I/O error. 280 * @throws ClassNotFoundException if there is a classpath problem. 281 */ 282 private void readObject(ObjectInputStream stream) 283 throws IOException, ClassNotFoundException { 284 stream.defaultReadObject(); 285 this.dataArea = (Rectangle2D) SerialUtils.readShape(stream); 286 this.plotArea = (Rectangle2D) SerialUtils.readShape(stream); 287 } 288 289}