Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2005, 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: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ---------------------- 28: * PlotRenderingInfo.java 29: * ---------------------- 30: * (C) Copyright 2003-2005, by Object Refinery Limited. 31: * 32: * Original Author: David Gilbert (for Object Refinery Limited); 33: * Contributor(s): -; 34: * 35: * $Id: PlotRenderingInfo.java,v 1.5.2.2 2005/11/02 12:56:10 mungady Exp $ 36: * 37: * Changes 38: * ------- 39: * 16-Sep-2003 : Version 1 (DG); 40: * 23-Sep-2003 : Added Javadocs (DG); 41: * 12-Nov-2004 : Added getSubplotCount() and findSubplot() methods (DG); 42: * 01-Nov-2005 : Made 'owner' non-transient to fix bug 1344048 (DG); 43: * 44: */ 45: 46: package org.jfree.chart.plot; 47: 48: import java.awt.geom.Point2D; 49: import java.awt.geom.Rectangle2D; 50: import java.io.IOException; 51: import java.io.ObjectInputStream; 52: import java.io.ObjectOutputStream; 53: import java.io.Serializable; 54: import java.util.List; 55: 56: import org.jfree.chart.ChartRenderingInfo; 57: import org.jfree.io.SerialUtilities; 58: import org.jfree.util.ObjectUtilities; 59: 60: /** 61: * Stores information about the dimensions of a plot and its subplots. 62: */ 63: public class PlotRenderingInfo implements Cloneable, Serializable { 64: 65: /** For serialization. */ 66: private static final long serialVersionUID = 8446720134379617220L; 67: 68: /** The owner of this info. */ 69: private ChartRenderingInfo owner; 70: 71: /** The plot area. */ 72: private transient Rectangle2D plotArea; 73: 74: /** The data area. */ 75: private transient Rectangle2D dataArea; 76: 77: /** 78: * Storage for the plot rendering info objects belonging to the subplots. 79: */ 80: private List subplotInfo; 81: 82: /** 83: * Default constructor. 84: * 85: * @param owner the owner. 86: */ 87: public PlotRenderingInfo(ChartRenderingInfo owner) { 88: this.owner = owner; 89: this.dataArea = new Rectangle2D.Double(); 90: this.subplotInfo = new java.util.ArrayList(); 91: } 92: 93: /** 94: * Returns the owner. 95: * 96: * @return The owner. 97: */ 98: public ChartRenderingInfo getOwner() { 99: return this.owner; 100: } 101: 102: /** 103: * Returns the plot area (in Java2D space). 104: * 105: * @return The plot area. 106: */ 107: public Rectangle2D getPlotArea() { 108: return this.plotArea; 109: } 110: 111: /** 112: * Sets the plot area. 113: * 114: * @param area the plot area (in Java2D space) 115: */ 116: public void setPlotArea(Rectangle2D area) { 117: this.plotArea = area; 118: } 119: 120: /** 121: * Returns the plot's data area (in Java2D space). 122: * 123: * @return The data area. 124: */ 125: public Rectangle2D getDataArea() { 126: return this.dataArea; 127: } 128: 129: /** 130: * Sets the data area. 131: * 132: * @param area the data area (in Java2D space). 133: */ 134: public void setDataArea(Rectangle2D area) { 135: this.dataArea = area; 136: } 137: 138: /** 139: * Returns the number of subplots. 140: * 141: * @return The subplot count. 142: */ 143: public int getSubplotCount() { 144: return this.subplotInfo.size(); 145: } 146: 147: /** 148: * Adds the info for a subplot. 149: * 150: * @param info the subplot info. 151: */ 152: public void addSubplotInfo(PlotRenderingInfo info) { 153: this.subplotInfo.add(info); 154: } 155: 156: /** 157: * Returns the info for a subplot. 158: * 159: * @param index the subplot index. 160: * 161: * @return The info. 162: */ 163: public PlotRenderingInfo getSubplotInfo(int index) { 164: return (PlotRenderingInfo) this.subplotInfo.get(index); 165: } 166: 167: /** 168: * Returns the index of the subplot that contains the specified 169: * (x, y) point (the "source" point). The source point will usually 170: * come from a mouse click on a {@link org.jfree.chart.ChartPanel}, 171: * and this method is then used to determine the subplot that 172: * contains the source point. 173: * 174: * @param source the source point (in Java2D space). 175: * 176: * @return The subplot index (or -1 if no subplot contains the point). 177: */ 178: public int getSubplotIndex(Point2D source) { 179: int subplotCount = getSubplotCount(); 180: for (int i = 0; i < subplotCount; i++) { 181: PlotRenderingInfo info = getSubplotInfo(i); 182: Rectangle2D area = info.getDataArea(); 183: if (area.contains(source)) { 184: return i; 185: } 186: } 187: return -1; 188: } 189: 190: /** 191: * Tests this instance for equality against an arbitrary object. 192: * 193: * @param obj the object (<code>null</code> permitted). 194: * 195: * @return A boolean. 196: */ 197: public boolean equals(Object obj) { 198: if (this == obj) { 199: return true; 200: } 201: if (!(obj instanceof PlotRenderingInfo)) { 202: return false; 203: } 204: PlotRenderingInfo that = (PlotRenderingInfo) obj; 205: if (!ObjectUtilities.equal(this.dataArea, that.dataArea)) { 206: return false; 207: } 208: if (!ObjectUtilities.equal(this.plotArea, that.plotArea)) { 209: return false; 210: } 211: if (!ObjectUtilities.equal(this.subplotInfo, that.subplotInfo)) { 212: return false; 213: } 214: return true; 215: } 216: 217: /** 218: * Returns a clone of this object. 219: * 220: * @return A clone. 221: * 222: * @throws CloneNotSupportedException if there is a problem cloning. 223: */ 224: public Object clone() throws CloneNotSupportedException { 225: return super.clone(); 226: } 227: 228: /** 229: * Provides serialization support. 230: * 231: * @param stream the output stream. 232: * 233: * @throws IOException if there is an I/O error. 234: */ 235: private void writeObject(ObjectOutputStream stream) throws IOException { 236: stream.defaultWriteObject(); 237: SerialUtilities.writeShape(this.dataArea, stream); 238: SerialUtilities.writeShape(this.plotArea, stream); 239: } 240: 241: /** 242: * Provides serialization support. 243: * 244: * @param stream the input stream. 245: * 246: * @throws IOException if there is an I/O error. 247: * @throws ClassNotFoundException if there is a classpath problem. 248: */ 249: private void readObject(ObjectInputStream stream) 250: throws IOException, ClassNotFoundException { 251: stream.defaultReadObject(); 252: this.dataArea = (Rectangle2D) SerialUtilities.readShape(stream); 253: this.plotArea = (Rectangle2D) SerialUtilities.readShape(stream); 254: } 255: 256: }