Source for org.jfree.chart.axis.CategoryLabelPosition

   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:  * CategoryLabelPosition.java
  29:  * --------------------------
  30:  * (C) Copyright 2003-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:  * 31-Oct-2003 : Version 1 (DG);
  38:  * 17-Feb-2004 : Added new constructor (DG);
  39:  * 23-Mar-2004 : Added width calculation parameters (DG);
  40:  * 07-Jan-2005 : Fixed bug in equals() method (DG);
  41:  * 11-Jan-2005 : Removed deprecated constructor in preparation for the 1.0.0 
  42:  *               release (DG);
  43:  *
  44:  */
  45: 
  46: package org.jfree.chart.axis;
  47: 
  48: import java.io.Serializable;
  49: 
  50: import org.jfree.text.TextBlockAnchor;
  51: import org.jfree.ui.RectangleAnchor;
  52: import org.jfree.ui.TextAnchor;
  53: 
  54: /**
  55:  * The attributes that control the position of the labels for the categories on 
  56:  * a {@link CategoryAxis}. Instances of this class are immutable and other 
  57:  * JFreeChart classes rely upon this.
  58:  */
  59: public class CategoryLabelPosition implements Serializable {
  60: 
  61:     /** For serialization. */
  62:     private static final long serialVersionUID = 5168681143844183864L;
  63:     
  64:     /** The category anchor point. */
  65:     private RectangleAnchor categoryAnchor;
  66:     
  67:     /** The text block anchor. */
  68:     private TextBlockAnchor labelAnchor;
  69:     
  70:     /** The rotation anchor. */
  71:     private TextAnchor rotationAnchor;
  72: 
  73:     /** The rotation angle (in radians). */    
  74:     private double angle;
  75:     
  76:     /** The width calculation type. */
  77:     private CategoryLabelWidthType widthType;
  78:     
  79:     /** 
  80:      * The maximum label width as a percentage of the category space or the 
  81:      * range space. 
  82:      */
  83:     private float widthRatio;
  84:     
  85:     /**
  86:      * Creates a new position record with default settings.
  87:      */
  88:     public CategoryLabelPosition() {
  89:         this(RectangleAnchor.CENTER, TextBlockAnchor.BOTTOM_CENTER, 
  90:                 TextAnchor.CENTER, 0.0, CategoryLabelWidthType.CATEGORY, 0.95f);
  91:     }
  92:     
  93:     /**
  94:      * Creates a new category label position record.
  95:      * 
  96:      * @param categoryAnchor  the category anchor (<code>null</code> not 
  97:      *                        permitted).
  98:      * @param labelAnchor  the label anchor (<code>null</code> not permitted).
  99:      */
 100:     public CategoryLabelPosition(RectangleAnchor categoryAnchor, 
 101:                                  TextBlockAnchor labelAnchor) {
 102:         // argument checking delegated...
 103:         this(categoryAnchor, labelAnchor, TextAnchor.CENTER, 0.0, 
 104:                 CategoryLabelWidthType.CATEGORY, 0.95f);
 105:     }
 106:     
 107:     /**
 108:      * Creates a new category label position record.
 109:      * 
 110:      * @param categoryAnchor  the category anchor (<code>null</code> not 
 111:      *                        permitted).
 112:      * @param labelAnchor  the label anchor (<code>null</code> not permitted).
 113:      * @param widthType  the width type (<code>null</code> not permitted).
 114:      * @param widthRatio  the maximum label width as a percentage (of the 
 115:      *                    category space or the range space).
 116:      */
 117:     public CategoryLabelPosition(RectangleAnchor categoryAnchor, 
 118:                                  TextBlockAnchor labelAnchor,
 119:                                  CategoryLabelWidthType widthType,
 120:                                  float widthRatio) {
 121:         // argument checking delegated...
 122:         this(categoryAnchor, labelAnchor, TextAnchor.CENTER, 0.0, widthType, 
 123:                 widthRatio);
 124:     }
 125:     
 126:     /**
 127:      * Creates a new position record.  The item label anchor is a point 
 128:      * relative to the data item (dot, bar or other visual item) on a chart.  
 129:      * The item label is aligned by aligning the text anchor with the item 
 130:      * label anchor.
 131:      * 
 132:      * @param categoryAnchor  the category anchor (<code>null</code> not 
 133:      *                        permitted).
 134:      * @param labelAnchor  the label anchor (<code>null</code> not permitted).
 135:      * @param rotationAnchor  the rotation anchor (<code>null</code> not 
 136:      *                        permitted).
 137:      * @param angle  the rotation angle (<code>null</code> not permitted).
 138:      * @param widthType  the width type (<code>null</code> not permitted).
 139:      * @param widthRatio  the maximum label width as a percentage (of the 
 140:      *                    category space or the range space).
 141:      */
 142:     public CategoryLabelPosition(RectangleAnchor categoryAnchor, 
 143:                                  TextBlockAnchor labelAnchor,
 144:                                  TextAnchor rotationAnchor,
 145:                                  double angle,
 146:                                  CategoryLabelWidthType widthType,
 147:                                  float widthRatio) {
 148:         
 149:         if (categoryAnchor == null) {
 150:             throw new IllegalArgumentException(
 151:                     "Null 'categoryAnchor' argument.");
 152:         }
 153:         if (labelAnchor == null) {
 154:             throw new IllegalArgumentException(
 155:                     "Null 'labelAnchor' argument.");
 156:         }
 157:         if (rotationAnchor == null) {
 158:             throw new IllegalArgumentException(
 159:                     "Null 'rotationAnchor' argument.");
 160:         }
 161:         if (widthType == null) {
 162:             throw new IllegalArgumentException("Null 'widthType' argument.");   
 163:         }
 164:         
 165:         this.categoryAnchor = categoryAnchor;
 166:         this.labelAnchor = labelAnchor;
 167:         this.rotationAnchor = rotationAnchor;
 168:         this.angle = angle;
 169:         this.widthType = widthType;
 170:         this.widthRatio = widthRatio;
 171:     
 172:     }
 173:     
 174:     /**
 175:      * Returns the item label anchor.
 176:      * 
 177:      * @return The item label anchor (never <code>null</code>).
 178:      */
 179:     public RectangleAnchor getCategoryAnchor() {
 180:         return this.categoryAnchor;
 181:     }
 182:     
 183:     /**
 184:      * Returns the text block anchor.
 185:      * 
 186:      * @return The text block anchor (never <code>null</code>).
 187:      */
 188:     public TextBlockAnchor getLabelAnchor() {
 189:         return this.labelAnchor;
 190:     }
 191:     
 192:     /**
 193:      * Returns the rotation anchor point.
 194:      * 
 195:      * @return The rotation anchor point (never <code>null</code>).
 196:      */
 197:     public TextAnchor getRotationAnchor() {
 198:         return this.rotationAnchor;
 199:     }
 200:     
 201:     /**
 202:      * Returns the angle of rotation for the label.
 203:      * 
 204:      * @return The angle (in radians).
 205:      */
 206:     public double getAngle() {
 207:         return this.angle;
 208:     }
 209:     
 210:     /**
 211:      * Returns the width calculation type.
 212:      * 
 213:      * @return The width calculation type (never <code>null</code>).
 214:      */
 215:     public CategoryLabelWidthType getWidthType() {
 216:         return this.widthType;   
 217:     }
 218:     
 219:     /**
 220:      * Returns the ratio used to calculate the maximum category label width.
 221:      * 
 222:      * @return The ratio.
 223:      */
 224:     public float getWidthRatio() {
 225:         return this.widthRatio;   
 226:     }
 227:     
 228:     /**
 229:      * Tests this instance for equality with an arbitrary object.
 230:      * 
 231:      * @param obj  the object (<code>null</code> permitted).
 232:      * 
 233:      * @return A boolean.
 234:      */
 235:     public boolean equals(Object obj) {
 236:         if (obj == this) {
 237:             return true;
 238:         }
 239:         if (!(obj instanceof CategoryLabelPosition)) {
 240:             return false;
 241:         }
 242:         CategoryLabelPosition that = (CategoryLabelPosition) obj;
 243:         if (!this.categoryAnchor.equals(that.categoryAnchor)) {
 244:             return false;
 245:         }
 246:         if (!this.labelAnchor.equals(that.labelAnchor)) {
 247:             return false;
 248:         }
 249:         if (!this.rotationAnchor.equals(that.rotationAnchor)) {
 250:             return false;
 251:         }
 252:         if (this.angle != that.angle) {
 253:             return false;
 254:         }
 255:         if (this.widthType != that.widthType) {
 256:             return false;
 257:         }
 258:         if (this.widthRatio != that.widthRatio) {
 259:             return false;
 260:         }
 261:         return true;
 262:     }
 263:     
 264:     /**
 265:      * Returns a hash code for this object.
 266:      * 
 267:      * @return A hash code.
 268:      */
 269:     public int hashCode() {
 270:         int result = 19;
 271:         result = 37 * result + this.categoryAnchor.hashCode();
 272:         result = 37 * result + this.labelAnchor.hashCode();
 273:         result = 37 * result + this.rotationAnchor.hashCode();
 274:         return result;
 275:     }
 276: 
 277: }