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