Source for org.jfree.chart.entity.PieSectionEntity

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, 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:  * PieSectionEntity.java
  29:  * ---------------------
  30:  * (C) Copyright 2002-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   Richard Atkinson;
  34:  *                   Christian W. Zuckschwerdt;
  35:  *
  36:  * Changes:
  37:  * --------
  38:  * 23-May-2002 : Version 1 (DG);
  39:  * 12-Jun-2002 : Added Javadoc comments (DG);
  40:  * 26-Jun-2002 : Added method to generate AREA tag for image map 
  41:  *               generation (DG);
  42:  * 05-Aug-2002 : Added new constructor to populate URLText
  43:  *               Moved getImageMapAreaTag() to ChartEntity (superclass) (RA);
  44:  * 03-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  45:  * 07-Mar-2003 : Added pie index attribute, since the PiePlot class can create
  46:  *               multiple pie plots within one chart.  Also renamed 'category' 
  47:  *               --> 'sectionKey' and changed the class from Object --> 
  48:  *               Comparable (DG);
  49:  * 30-Jul-2003 : Added PieDataset reference (CZ);
  50:  * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
  51:  * 13-Nov-2007 : Implemented equals() and hashCode() (DG);
  52:  *
  53:  */
  54: 
  55: package org.jfree.chart.entity;
  56: 
  57: import java.awt.Shape;
  58: import java.io.Serializable;
  59: 
  60: import org.jfree.chart.HashUtilities;
  61: import org.jfree.data.general.PieDataset;
  62: import org.jfree.util.ObjectUtilities;
  63: 
  64: /**
  65:  * A chart entity that represents one section within a pie plot.
  66:  */
  67: public class PieSectionEntity extends ChartEntity
  68:                               implements Serializable {
  69: 
  70:     /** For serialization. */
  71:     private static final long serialVersionUID = 9199892576531984162L;
  72:     
  73:     /** The dataset. */
  74:     private PieDataset dataset;
  75:     
  76:     /** The pie index. */
  77:     private int pieIndex;
  78: 
  79:     /** The section index. */
  80:     private int sectionIndex;
  81: 
  82:     /** The section key. */
  83:     private Comparable sectionKey;
  84: 
  85:     /**
  86:      * Creates a new pie section entity.
  87:      *
  88:      * @param area  the area.
  89:      * @param dataset  the pie dataset.
  90:      * @param pieIndex  the pie index (zero-based).
  91:      * @param sectionIndex  the section index (zero-based).
  92:      * @param sectionKey  the section key.
  93:      * @param toolTipText  the tool tip text.
  94:      * @param urlText  the URL text for HTML image maps.
  95:      */
  96:     public PieSectionEntity(Shape area, 
  97:                             PieDataset dataset,
  98:                             int pieIndex, int sectionIndex, 
  99:                             Comparable sectionKey,
 100:                             String toolTipText, String urlText) {
 101: 
 102:         super(area, toolTipText, urlText);
 103:         this.dataset = dataset;
 104:         this.pieIndex = pieIndex;
 105:         this.sectionIndex = sectionIndex;
 106:         this.sectionKey = sectionKey;
 107: 
 108:     }
 109: 
 110:     /**
 111:      * Returns the dataset this entity refers to.
 112:      *
 113:      * @return The dataset.
 114:      * 
 115:      * @see #setDataset(PieDataset)
 116:      */
 117:     public PieDataset getDataset() {
 118:         return this.dataset;
 119:     }
 120: 
 121:     /**
 122:      * Sets the dataset this entity refers to.
 123:      *
 124:      * @param dataset  the dataset.
 125:      * 
 126:      * @see #getDataset()
 127:      */
 128:     public void setDataset(PieDataset dataset) {
 129:         this.dataset = dataset;
 130:     }
 131: 
 132:     /**
 133:      * Returns the pie index.  For a regular pie chart, the section index is 0. 
 134:      * For a pie chart containing multiple pie plots, the pie index is the row 
 135:      * or column index from which the pie data is extracted.
 136:      *
 137:      * @return The pie index.
 138:      * 
 139:      * @see #setPieIndex(int)
 140:      */
 141:     public int getPieIndex() {
 142:         return this.pieIndex;
 143:     }
 144: 
 145:     /**
 146:      * Sets the pie index.
 147:      *
 148:      * @param index  the new index value.
 149:      * 
 150:      * @see #getPieIndex()
 151:      */
 152:     public void setPieIndex(int index) {
 153:         this.pieIndex = index;
 154:     }
 155: 
 156:     /**
 157:      * Returns the section index.
 158:      *
 159:      * @return The section index.
 160:      * 
 161:      * @see #setSectionIndex(int)
 162:      */
 163:     public int getSectionIndex() {
 164:         return this.sectionIndex;
 165:     }
 166: 
 167:     /**
 168:      * Sets the section index.
 169:      *
 170:      * @param index  the section index.
 171:      * 
 172:      * @see #getSectionIndex()
 173:      */
 174:     public void setSectionIndex(int index) {
 175:         this.sectionIndex = index;
 176:     }
 177: 
 178:     /**
 179:      * Returns the section key.
 180:      *
 181:      * @return The section key.
 182:      * 
 183:      * @see #setSectionKey(Comparable)
 184:      */
 185:     public Comparable getSectionKey() {
 186:         return this.sectionKey;
 187:     }
 188: 
 189:     /**
 190:      * Sets the section key.
 191:      *
 192:      * @param key  the section key.
 193:      * 
 194:      * @see #getSectionKey()
 195:      */
 196:     public void setSectionKey(Comparable key) {
 197:         this.sectionKey = key;
 198:     }
 199: 
 200:     /**
 201:      * Tests this entity for equality with an arbitrary object.
 202:      * 
 203:      * @param obj  the object (<code>null</code> permitted).
 204:      * 
 205:      * @return A boolean.
 206:      */
 207:     public boolean equals(Object obj) {
 208:         if (obj == this) {
 209:             return true;
 210:         }
 211:         if (!(obj instanceof PieSectionEntity)) {
 212:             return false;
 213:         }
 214:         PieSectionEntity that = (PieSectionEntity) obj;
 215:         if (!ObjectUtilities.equal(this.dataset, that.dataset)) {
 216:             return false;
 217:         }
 218:         if (this.pieIndex != that.pieIndex) {
 219:             return false;
 220:         }
 221:         if (this.sectionIndex != that.sectionIndex) {
 222:             return false;
 223:         }
 224:         if (!ObjectUtilities.equal(this.sectionKey, that.sectionKey)) {
 225:             return false;
 226:         }
 227:         return super.equals(obj);
 228:     }
 229:     
 230:     /**
 231:      * Returns a hash code for this instance.
 232:      * 
 233:      * @return A hash code.
 234:      */
 235:     public int hashCode() {
 236:         int result = super.hashCode();
 237:         result = HashUtilities.hashCode(result, this.pieIndex);
 238:         result = HashUtilities.hashCode(result, this.sectionIndex);
 239:         return result;
 240:     }
 241:     
 242:     /**
 243:      * Returns a string representing the entity.
 244:      *
 245:      * @return A string representing the entity.
 246:      */
 247:     public String toString() {
 248:         return "PieSection: " + this.pieIndex + ", " + this.sectionIndex + "("
 249:                               + this.sectionKey.toString() + ")";
 250:     }
 251: 
 252: }