Source for org.jfree.chart.annotations.TextAnnotation

   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:  * TextAnnotation.java
  29:  * -------------------
  30:  * (C) Copyright 2002-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes:
  36:  * --------
  37:  * 28-Aug-2002 : Version 1 (DG);
  38:  * 07-Nov-2002 : Fixed errors reported by Checkstyle, added accessor 
  39:  *               methods (DG);
  40:  * 13-Jan-2003 : Reviewed Javadocs (DG);
  41:  * 26-Mar-2003 : Implemented Serializable (DG);
  42:  * 02-Jun-2003 : Added anchor and rotation settings (DG);
  43:  * 19-Aug-2003 : Added equals() method and implemented Cloneable (DG);
  44:  * 29-Sep-2004 : Updated equals() method (DG);
  45:  * 06-Jun-2005 : Fixed equals() method to work with GradientPaint (DG);
  46:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  47:  * 16-Jan-2007 : Added argument checks, fixed hashCode() method and updated
  48:  *               API docs (DG);
  49:  * 
  50:  */
  51: 
  52: package org.jfree.chart.annotations;
  53: 
  54: import java.awt.Color;
  55: import java.awt.Font;
  56: import java.awt.Paint;
  57: import java.io.IOException;
  58: import java.io.ObjectInputStream;
  59: import java.io.ObjectOutputStream;
  60: import java.io.Serializable;
  61: 
  62: import org.jfree.chart.HashUtilities;
  63: import org.jfree.io.SerialUtilities;
  64: import org.jfree.ui.TextAnchor;
  65: import org.jfree.util.ObjectUtilities;
  66: import org.jfree.util.PaintUtilities;
  67: 
  68: /**
  69:  * A base class for text annotations.  This class records the content but not 
  70:  * the location of the annotation.
  71:  */
  72: public class TextAnnotation implements Serializable {
  73: 
  74:     /** For serialization. */
  75:     private static final long serialVersionUID = 7008912287533127432L;
  76:     
  77:     /** The default font. */
  78:     public static final Font DEFAULT_FONT 
  79:         = new Font("SansSerif", Font.PLAIN, 10);
  80: 
  81:     /** The default paint. */
  82:     public static final Paint DEFAULT_PAINT = Color.black;
  83:     
  84:     /** The default text anchor. */
  85:     public static final TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.CENTER;
  86: 
  87:     /** The default rotation anchor. */    
  88:     public static final TextAnchor DEFAULT_ROTATION_ANCHOR = TextAnchor.CENTER;
  89:     
  90:     /** The default rotation angle. */
  91:     public static final double DEFAULT_ROTATION_ANGLE = 0.0;
  92: 
  93:     /** The text. */
  94:     private String text;
  95: 
  96:     /** The font. */
  97:     private Font font;
  98: 
  99:     /** The paint. */
 100:     private transient Paint paint;
 101:     
 102:     /** The text anchor. */
 103:     private TextAnchor textAnchor;
 104:     
 105:     /** The rotation anchor. */
 106:     private TextAnchor rotationAnchor;
 107:     
 108:     /** The rotation angle. */
 109:     private double rotationAngle;
 110: 
 111:     /**
 112:      * Creates a text annotation with default settings.
 113:      *
 114:      * @param text  the text (<code>null</code> not permitted).
 115:      */
 116:     protected TextAnnotation(String text) {
 117:         if (text == null) {
 118:             throw new IllegalArgumentException("Null 'text' argument.");
 119:         }
 120:         this.text = text;
 121:         this.font = DEFAULT_FONT;
 122:         this.paint = DEFAULT_PAINT;
 123:         this.textAnchor = DEFAULT_TEXT_ANCHOR;
 124:         this.rotationAnchor = DEFAULT_ROTATION_ANCHOR;
 125:         this.rotationAngle = DEFAULT_ROTATION_ANGLE;
 126:     }
 127: 
 128:     /**
 129:      * Returns the text for the annotation.
 130:      *
 131:      * @return The text (never <code>null</code>).
 132:      * 
 133:      * @see #setText(String)
 134:      */
 135:     public String getText() {
 136:         return this.text;
 137:     }
 138: 
 139:     /**
 140:      * Sets the text for the annotation.
 141:      * 
 142:      * @param text  the text (<code>null</code> not permitted).
 143:      * 
 144:      * @see #getText()
 145:      */
 146:     public void setText(String text) {
 147:         if (text == null) {
 148:             throw new IllegalArgumentException("Null 'text' argument.");
 149:         }
 150:         this.text = text;
 151:     }
 152:     
 153:     /**
 154:      * Returns the font for the annotation.
 155:      *
 156:      * @return The font (never <code>null</code>).
 157:      * 
 158:      * @see #setFont(Font)
 159:      */
 160:     public Font getFont() {
 161:         return this.font;
 162:     }
 163: 
 164:     /**
 165:      * Sets the font for the annotation.
 166:      * 
 167:      * @param font  the font (<code>null</code> not permitted).
 168:      * 
 169:      * @see #getFont()
 170:      */
 171:     public void setFont(Font font) {
 172:         if (font == null) {
 173:             throw new IllegalArgumentException("Null 'font' argument.");
 174:         }
 175:         this.font = font;
 176:     }
 177:     
 178:     /**
 179:      * Returns the paint for the annotation.
 180:      *
 181:      * @return The paint (never <code>null</code>).
 182:      * 
 183:      * @see #setPaint(Paint)
 184:      */
 185:     public Paint getPaint() {
 186:         return this.paint;
 187:     }
 188:     
 189:     /**
 190:      * Sets the paint for the annotation.
 191:      * 
 192:      * @param paint  the paint (<code>null</code> not permitted).
 193:      * 
 194:      * @see #getPaint()
 195:      */
 196:     public void setPaint(Paint paint) {
 197:         if (paint == null) {
 198:             throw new IllegalArgumentException("Null 'paint' argument.");
 199:         }
 200:         this.paint = paint;
 201:     }
 202: 
 203:     /**
 204:      * Returns the text anchor.
 205:      * 
 206:      * @return The text anchor.
 207:      * 
 208:      * @see #setTextAnchor(TextAnchor)
 209:      */
 210:     public TextAnchor getTextAnchor() {
 211:         return this.textAnchor;
 212:     }
 213:     
 214:     /**
 215:      * Sets the text anchor (the point on the text bounding rectangle that is 
 216:      * aligned to the (x, y) coordinate of the annotation).
 217:      * 
 218:      * @param anchor  the anchor point (<code>null</code> not permitted).
 219:      * 
 220:      * @see #getTextAnchor()
 221:      */
 222:     public void setTextAnchor(TextAnchor anchor) {
 223:         if (anchor == null) {
 224:             throw new IllegalArgumentException("Null 'anchor' argument.");
 225:         }
 226:         this.textAnchor = anchor;
 227:     }
 228:     
 229:     /**
 230:      * Returns the rotation anchor.
 231:      * 
 232:      * @return The rotation anchor point (never <code>null</code>).
 233:      * 
 234:      * @see #setRotationAnchor(TextAnchor)
 235:      */
 236:     public TextAnchor getRotationAnchor() {
 237:         return this.rotationAnchor;
 238:     }
 239:     
 240:     /**
 241:      * Sets the rotation anchor point.
 242:      * 
 243:      * @param anchor  the anchor (<code>null</code> not permitted).
 244:      * 
 245:      * @see #getRotationAnchor()
 246:      */
 247:     public void setRotationAnchor(TextAnchor anchor) {
 248:         this.rotationAnchor = anchor;    
 249:     }
 250:     
 251:     /**
 252:      * Returns the rotation angle in radians.
 253:      * 
 254:      * @return The rotation angle.
 255:      * 
 256:      * @see #setRotationAngle(double)
 257:      */
 258:     public double getRotationAngle() {
 259:         return this.rotationAngle; 
 260:     }
 261:     
 262:     /**
 263:      * Sets the rotation angle.  The angle is measured clockwise in radians.
 264:      * 
 265:      * @param angle  the angle (in radians).
 266:      * 
 267:      * @see #getRotationAngle()
 268:      */
 269:     public void setRotationAngle(double angle) {
 270:         this.rotationAngle = angle;    
 271:     }
 272:     
 273:     /**
 274:      * Tests this object for equality with an arbitrary object.
 275:      * 
 276:      * @param obj  the object (<code>null</code> permitted).
 277:      * 
 278:      * @return <code>true</code> or <code>false</code>.
 279:      */
 280:     public boolean equals(Object obj) {
 281:         if (obj == this) {
 282:             return true;
 283:         }
 284:         // now try to reject equality...
 285:         if (!(obj instanceof TextAnnotation)) {
 286:             return false;
 287:         }
 288:         TextAnnotation that = (TextAnnotation) obj;
 289:         if (!ObjectUtilities.equal(this.text, that.getText())) {
 290:             return false;
 291:         }
 292:         if (!ObjectUtilities.equal(this.font, that.getFont())) {
 293:             return false;
 294:         }
 295:         if (!PaintUtilities.equal(this.paint, that.getPaint())) {
 296:             return false;
 297:         }
 298:         if (!ObjectUtilities.equal(this.textAnchor, that.getTextAnchor())) {
 299:             return false;
 300:         }
 301:         if (!ObjectUtilities.equal(this.rotationAnchor, 
 302:                 that.getRotationAnchor())) {
 303:             return false;
 304:         }
 305:         if (this.rotationAngle != that.getRotationAngle()) {
 306:             return false;
 307:         }
 308:         
 309:         // seem to be the same...
 310:         return true;
 311:             
 312:     }
 313:     
 314:     /**
 315:      * Returns a hash code for this instance.
 316:      * 
 317:      * @return A hash code.
 318:      */
 319:     public int hashCode() {
 320:         int result = 193;
 321:         result = 37 * result + this.font.hashCode();
 322:         result = 37 * result + HashUtilities.hashCodeForPaint(this.paint);
 323:         result = 37 * result + this.rotationAnchor.hashCode();
 324:         long temp = Double.doubleToLongBits(this.rotationAngle);
 325:         result = 37 * result + (int) (temp ^ (temp >>> 32));
 326:         result = 37 * result + this.text.hashCode();
 327:         result = 37 * result + this.textAnchor.hashCode();
 328:         return result;
 329:     }
 330:     
 331:     /**
 332:      * Provides serialization support.
 333:      *
 334:      * @param stream  the output stream.
 335:      *
 336:      * @throws IOException if there is an I/O error.
 337:      */
 338:     private void writeObject(ObjectOutputStream stream) throws IOException {
 339:         stream.defaultWriteObject();
 340:         SerialUtilities.writePaint(this.paint, stream);
 341:     }
 342: 
 343:     /**
 344:      * Provides serialization support.
 345:      *
 346:      * @param stream  the input stream.
 347:      *
 348:      * @throws IOException  if there is an I/O error.
 349:      * @throws ClassNotFoundException  if there is a classpath problem.
 350:      */
 351:     private void readObject(ObjectInputStream stream) 
 352:         throws IOException, ClassNotFoundException {
 353:         stream.defaultReadObject();
 354:         this.paint = SerialUtilities.readPaint(stream);
 355:     }
 356: 
 357: }