Source for org.jfree.chart.plot.PieLabelRecord

   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:  * PieLabelRecord.java
  29:  * -------------------
  30:  * (C) Copyright 2004, 2007, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 08-Mar-2004 : Version 1 (DG);
  38:  * 14-Jun-2007 : Implemented Serializable, updated API docs (DG);
  39:  * 21-Nov-2007 : Implemented equals() to shut up FindBugs (DG);
  40:  *
  41:  */
  42: 
  43: package org.jfree.chart.plot;
  44: 
  45: import java.io.Serializable;
  46: 
  47: import org.jfree.text.TextBox;
  48: 
  49: /**
  50:  * A structure that retains information about the label for a section in a pie 
  51:  * chart.
  52:  */
  53: public class PieLabelRecord implements Comparable, Serializable {
  54:     
  55:     /** The section key. */
  56:     private Comparable key;
  57:     
  58:     /** The angle of the centre of the section (in radians). */
  59:     private double angle;
  60:     
  61:     /** The base y-coordinate. */
  62:     private double baseY;
  63:     
  64:     /** The allocated y-coordinate. */
  65:     private double allocatedY;
  66: 
  67:     /** The label. */
  68:     private TextBox label;
  69:     
  70:     /** The label height. */
  71:     private double labelHeight;
  72:     
  73:     /** The gap. */
  74:     private double gap;
  75:     
  76:     /** The link percent. */
  77:     private double linkPercent;
  78:     
  79:     /**
  80:      * Creates a new record.
  81:      * 
  82:      * @param key  the section key.
  83:      * @param angle  the angle to the middle of the section (in radians).
  84:      * @param baseY  the base y-coordinate.
  85:      * @param label  the section label.
  86:      * @param labelHeight  the label height (in Java2D units).
  87:      * @param gap  the offset to the left.
  88:      * @param linkPercent  the link percent.
  89:      */
  90:     public PieLabelRecord(Comparable key, double angle, double baseY, 
  91:                           TextBox label, double labelHeight, double gap, 
  92:                           double linkPercent) {
  93:         this.key = key;
  94:         this.angle = angle;
  95:         this.baseY = baseY;
  96:         this.allocatedY = baseY;
  97:         this.label = label;
  98:         this.labelHeight = labelHeight;
  99:         this.gap = gap;
 100:         this.linkPercent = linkPercent;
 101:     }
 102:     
 103:     /**
 104:      * Returns the base y-coordinate.  This is where the label will appear if 
 105:      * there is no overlapping of labels.
 106:      * 
 107:      * @return The base y-coordinate.
 108:      */
 109:     public double getBaseY() {
 110:         return this.baseY;   
 111:     }
 112:     
 113:     /**
 114:      * Sets the base y-coordinate.
 115:      * 
 116:      * @param base  the base y-coordinate.
 117:      */
 118:     public void setBaseY(double base) {
 119:         this.baseY = base;   
 120:     }
 121:     
 122:     /**
 123:      * Returns the lower bound of the label.
 124:      * 
 125:      * @return The lower bound.
 126:      */
 127:     public double getLowerY() {
 128:         return this.allocatedY - this.labelHeight / 2.0;   
 129:     }
 130:     
 131:     /**
 132:      * Returns the upper bound of the label.
 133:      * 
 134:      * @return The upper bound.
 135:      */
 136:     public double getUpperY() {
 137:         return this.allocatedY + this.labelHeight / 2.0;   
 138:     }
 139:     
 140:     /**
 141:      * Returns the angle of the middle of the section, in radians.
 142:      * 
 143:      * @return The angle, in radians.
 144:      */
 145:     public double getAngle() {
 146:         return this.angle;   
 147:     }
 148:     
 149:     /**
 150:      * Returns the key for the section that the label applies to.
 151:      * 
 152:      * @return The key.
 153:      */
 154:     public Comparable getKey() {
 155:         return this.key;   
 156:     }
 157:     
 158:     /**
 159:      * Returns the label.
 160:      * 
 161:      * @return The label.
 162:      */
 163:     public TextBox getLabel() {
 164:         return this.label;   
 165:     }
 166:     
 167:     /**
 168:      * Returns the label height (you could derive this from the label itself,
 169:      * but we cache the value so it can be retrieved quickly).
 170:      * 
 171:      * @return The label height (in Java2D units).
 172:      */
 173:     public double getLabelHeight() {
 174:         return this.labelHeight;   
 175:     }
 176:     
 177:     /**
 178:      * Returns the allocated y-coordinate.
 179:      * 
 180:      * @return The allocated y-coordinate.
 181:      */
 182:     public double getAllocatedY() {
 183:         return this.allocatedY;   
 184:     }
 185:     
 186:     /**
 187:      * Sets the allocated y-coordinate.
 188:      * 
 189:      * @param y  the y-coordinate.
 190:      */
 191:     public void setAllocatedY(double y) {
 192:         this.allocatedY = y;   
 193:     }
 194:     
 195:     /**
 196:      * Returns the gap.
 197:      * 
 198:      * @return The gap.
 199:      */
 200:     public double getGap() {
 201:         return this.gap;   
 202:     }
 203:     
 204:     /**
 205:      * Returns the link percent.
 206:      * 
 207:      * @return The link percent.
 208:      */
 209:     public double getLinkPercent() {
 210:         return this.linkPercent;   
 211:     }
 212:     
 213:     /**
 214:      * Compares this object to an arbitrary object.
 215:      * 
 216:      * @param obj  the object to compare against.
 217:      * 
 218:      * @return An integer that specifies the relative order of the two objects.
 219:      */
 220:     public int compareTo(Object obj) {
 221:         int result = 0;
 222:         if (obj instanceof PieLabelRecord) {
 223:             PieLabelRecord plr = (PieLabelRecord) obj;
 224:             if (this.baseY < plr.baseY) {
 225:                 result = -1;   
 226:             }
 227:             else if (this.baseY > plr.baseY) {
 228:                 result = 1;   
 229:             }
 230:         }
 231:         return result;
 232:     }
 233:     
 234:     /**
 235:      * Tests this record for equality with an arbitrary object.
 236:      * 
 237:      * @param obj  the object (<code>null</code> permitted).
 238:      * 
 239:      * @return A boolean.
 240:      */
 241:     public boolean equals(Object obj) {
 242:         if (obj == this) {
 243:             return true;
 244:         }
 245:         if (!(obj instanceof PieLabelRecord)) {
 246:             return false;
 247:         }
 248:         PieLabelRecord that = (PieLabelRecord) obj;
 249:         if (!this.key.equals(that.key)) {
 250:             return false;
 251:         }
 252:         if (this.angle != that.angle) {
 253:             return false;
 254:         }
 255:         if (this.gap != that.gap) {
 256:             return false;
 257:         }
 258:         if (this.allocatedY != that.allocatedY) {
 259:             return false;
 260:         }
 261:         if (this.baseY != that.baseY) {
 262:             return false;
 263:         }
 264:         if (this.labelHeight != that.labelHeight) {
 265:             return false;
 266:         }
 267:         if (this.linkPercent != that.linkPercent) {
 268:             return false;
 269:         }
 270:         if (!this.label.equals(that.label)) {
 271:             return false;
 272:         }
 273:         return true;
 274:     }
 275:     
 276:     /**
 277:      * Returns a string describing the object.  This is used for debugging only.
 278:      * 
 279:      * @return A string.
 280:      */
 281:     public String toString() {
 282:         return this.baseY + ", " + this.key.toString();   
 283:     }
 284: }