Source for org.jfree.chart.plot.PlotRenderingInfo

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2006, 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-2006, 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.3 2006/12/01 13:51:07 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:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  44:  * 01-Dec-2006 : Implemented clone() method properly (DG);
  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.io.SerialUtilities;
  59: import org.jfree.util.ObjectUtilities;
  60: 
  61: /**
  62:  * Stores information about the dimensions of a plot and its subplots.
  63:  */
  64: public class PlotRenderingInfo implements Cloneable, Serializable {
  65: 
  66:     /** For serialization. */
  67:     private static final long serialVersionUID = 8446720134379617220L;
  68:     
  69:     /** The owner of this info. */
  70:     private ChartRenderingInfo owner;
  71:     
  72:     /** The plot area. */
  73:     private transient Rectangle2D plotArea;
  74:     
  75:     /** The data area. */
  76:     private transient Rectangle2D dataArea;
  77:     
  78:     /**
  79:      * Storage for the plot rendering info objects belonging to the subplots. 
  80:      */
  81:     private List subplotInfo;
  82:       
  83:     /**
  84:      * Creates a new instance.
  85:      * 
  86:      * @param owner  the owner (<code>null</code> permitted).
  87:      */
  88:     public PlotRenderingInfo(ChartRenderingInfo owner) {
  89:         this.owner = owner;        
  90:         this.dataArea = new Rectangle2D.Double();
  91:         this.subplotInfo = new java.util.ArrayList();    
  92:     }
  93:     
  94:     /**
  95:      * Returns the owner (as specified in the constructor).
  96:      * 
  97:      * @return The owner (possibly <code>null</code>).
  98:      */
  99:     public ChartRenderingInfo getOwner() {
 100:         return this.owner;
 101:     }
 102:     
 103:     /**
 104:      * Returns the plot area (in Java2D space).
 105:      * 
 106:      * @return The plot area (possibly <code>null</code>).
 107:      *
 108:      * @see #setPlotArea(Rectangle2D)
 109:      */
 110:     public Rectangle2D getPlotArea() {
 111:         return this.plotArea;
 112:     }
 113:     
 114:     /**
 115:      * Sets the plot area.
 116:      * 
 117:      * @param area  the plot area (in Java2D space, <code>null</code> 
 118:      *     permitted but discouraged)
 119:      * 
 120:      * @see #getPlotArea()
 121:      */
 122:     public void setPlotArea(Rectangle2D area) {
 123:         this.plotArea = area;
 124:     }
 125:     
 126:     /**
 127:      * Returns the plot's data area (in Java2D space).
 128:      * 
 129:      * @return The data area (possibly <code>null</code>).
 130:      * 
 131:      * @see #setDataArea(Rectangle2D)
 132:      */
 133:     public Rectangle2D getDataArea() {
 134:         return this.dataArea;
 135:     }
 136:     
 137:     /**
 138:      * Sets the data area.
 139:      * 
 140:      * @param area  the data area (in Java2D space, <code>null</code> permitted
 141:      *     but discouraged).
 142:      * 
 143:      * @see #getDataArea()
 144:      */
 145:     public void setDataArea(Rectangle2D area) {
 146:         this.dataArea = area;
 147:     }
 148:     
 149:     /**
 150:      * Returns the number of subplots (possibly zero).
 151:      * 
 152:      * @return The subplot count.
 153:      */
 154:     public int getSubplotCount() {
 155:         return this.subplotInfo.size();
 156:     }
 157:     
 158:     /**
 159:      * Adds the info for a subplot.
 160:      * 
 161:      * @param info  the subplot info.
 162:      * 
 163:      * @see #getSubplotInfo(int)
 164:      */
 165:     public void addSubplotInfo(PlotRenderingInfo info) {
 166:         this.subplotInfo.add(info);    
 167:     }
 168:     
 169:     /**
 170:      * Returns the info for a subplot.
 171:      * 
 172:      * @param index  the subplot index.
 173:      * 
 174:      * @return The info.
 175:      * 
 176:      * @see #addSubplotInfo(PlotRenderingInfo)
 177:      */
 178:     public PlotRenderingInfo getSubplotInfo(int index) {
 179:         return (PlotRenderingInfo) this.subplotInfo.get(index);
 180:     }
 181:     
 182:     /**
 183:      * Returns the index of the subplot that contains the specified
 184:      * (x, y) point (the "source" point).  The source point will usually
 185:      * come from a mouse click on a {@link org.jfree.chart.ChartPanel},
 186:      * and this method is then used to determine the subplot that 
 187:      * contains the source point.
 188:      * 
 189:      * @param source  the source point (in Java2D space).
 190:      * 
 191:      * @return The subplot index (or -1 if no subplot contains the point).
 192:      */
 193:     public int getSubplotIndex(Point2D source) {
 194:         int subplotCount = getSubplotCount();
 195:         for (int i = 0; i < subplotCount; i++) {
 196:             PlotRenderingInfo info = getSubplotInfo(i);
 197:             Rectangle2D area = info.getDataArea();
 198:             if (area.contains(source)) {
 199:                 return i;
 200:             }
 201:         }
 202:         return -1;
 203:     }
 204:     
 205:     /**
 206:      * Tests this instance for equality against an arbitrary object.
 207:      * 
 208:      * @param obj  the object (<code>null</code> permitted).
 209:      * 
 210:      * @return A boolean.
 211:      */
 212:     public boolean equals(Object obj) {
 213:         if (this == obj) {
 214:             return true;   
 215:         }
 216:         if (!(obj instanceof PlotRenderingInfo)) {
 217:             return false;
 218:         }
 219:         PlotRenderingInfo that = (PlotRenderingInfo) obj;
 220:         if (!ObjectUtilities.equal(this.dataArea, that.dataArea)) {
 221:             return false;   
 222:         }
 223:         if (!ObjectUtilities.equal(this.plotArea, that.plotArea)) {
 224:             return false;   
 225:         }
 226:         if (!ObjectUtilities.equal(this.subplotInfo, that.subplotInfo)) {
 227:             return false;   
 228:         }
 229:         return true;   
 230:     }
 231:     
 232:     /**
 233:      * Returns a clone of this object.
 234:      * 
 235:      * @return A clone.
 236:      * 
 237:      * @throws CloneNotSupportedException if there is a problem cloning.
 238:      */
 239:     public Object clone() throws CloneNotSupportedException {
 240:         PlotRenderingInfo clone = (PlotRenderingInfo) super.clone();
 241:         if (this.plotArea != null) {
 242:             clone.plotArea = (Rectangle2D) this.plotArea.clone();
 243:         }
 244:         if (this.dataArea != null) {
 245:             clone.dataArea = (Rectangle2D) this.dataArea.clone();
 246:         }
 247:         clone.subplotInfo = new java.util.ArrayList(this.subplotInfo.size());
 248:         for (int i = 0; i < this.subplotInfo.size(); i++) {
 249:             PlotRenderingInfo info 
 250:                     = (PlotRenderingInfo) this.subplotInfo.get(i);
 251:             clone.subplotInfo.add(info.clone());
 252:         }
 253:         return clone;
 254:     }
 255:     
 256:     /**
 257:      * Provides serialization support.
 258:      *
 259:      * @param stream  the output stream.
 260:      *
 261:      * @throws IOException  if there is an I/O error.
 262:      */
 263:     private void writeObject(ObjectOutputStream stream) throws IOException {
 264:         stream.defaultWriteObject();
 265:         SerialUtilities.writeShape(this.dataArea, stream);
 266:         SerialUtilities.writeShape(this.plotArea, stream);
 267:     }
 268: 
 269:     /**
 270:      * Provides serialization support.
 271:      *
 272:      * @param stream  the input stream.
 273:      *
 274:      * @throws IOException  if there is an I/O error.
 275:      * @throws ClassNotFoundException  if there is a classpath problem.
 276:      */
 277:     private void readObject(ObjectInputStream stream) 
 278:             throws IOException, ClassNotFoundException {
 279:         stream.defaultReadObject();
 280:         this.dataArea = (Rectangle2D) SerialUtilities.readShape(stream);
 281:         this.plotArea = (Rectangle2D) SerialUtilities.readShape(stream);
 282:     }
 283: 
 284: }