Source for org.jfree.chart.annotations.CategoryLineAnnotation

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2008, 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:  * CategoryLineAnnotation.java
  29:  * ---------------------------
  30:  * (C) Copyright 2005-2008, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes:
  36:  * --------
  37:  * 29-Jul-2005 : Version 1, based on CategoryTextAnnotation (DG);
  38:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  39:  * 06-Mar-2007 : Reimplemented hashCode() (DG);
  40:  * 23-Apr-2008 : Implemented PublicCloneable (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.chart.annotations;
  45: 
  46: import java.awt.BasicStroke;
  47: import java.awt.Color;
  48: import java.awt.Graphics2D;
  49: import java.awt.Paint;
  50: import java.awt.Stroke;
  51: import java.awt.geom.Rectangle2D;
  52: import java.io.IOException;
  53: import java.io.ObjectInputStream;
  54: import java.io.ObjectOutputStream;
  55: import java.io.Serializable;
  56: 
  57: import org.jfree.chart.HashUtilities;
  58: import org.jfree.chart.axis.CategoryAnchor;
  59: import org.jfree.chart.axis.CategoryAxis;
  60: import org.jfree.chart.axis.ValueAxis;
  61: import org.jfree.chart.plot.CategoryPlot;
  62: import org.jfree.chart.plot.Plot;
  63: import org.jfree.chart.plot.PlotOrientation;
  64: import org.jfree.data.category.CategoryDataset;
  65: import org.jfree.io.SerialUtilities;
  66: import org.jfree.ui.RectangleEdge;
  67: import org.jfree.util.ObjectUtilities;
  68: import org.jfree.util.PaintUtilities;
  69: import org.jfree.util.PublicCloneable;
  70: 
  71: /**
  72:  * A line annotation that can be placed on a {@link CategoryPlot}.
  73:  */
  74: public class CategoryLineAnnotation implements CategoryAnnotation,
  75:         Cloneable, PublicCloneable, Serializable {
  76: 
  77:     /** For serialization. */
  78:     static final long serialVersionUID = 3477740483341587984L;
  79: 
  80:     /** The category for the start of the line. */
  81:     private Comparable category1;
  82: 
  83:     /** The value for the start of the line. */
  84:     private double value1;
  85: 
  86:     /** The category for the end of the line. */
  87:     private Comparable category2;
  88: 
  89:     /** The value for the end of the line. */
  90:     private double value2;
  91: 
  92:     /** The line color. */
  93:     private transient Paint paint = Color.black;
  94: 
  95:     /** The line stroke. */
  96:     private transient Stroke stroke = new BasicStroke(1.0f);
  97: 
  98:     /**
  99:      * Creates a new annotation that draws a line between (category1, value1)
 100:      * and (category2, value2).
 101:      *
 102:      * @param category1  the category (<code>null</code> not permitted).
 103:      * @param value1  the value.
 104:      * @param category2  the category (<code>null</code> not permitted).
 105:      * @param value2  the value.
 106:      * @param paint  the line color (<code>null</code> not permitted).
 107:      * @param stroke  the line stroke (<code>null</code> not permitted).
 108:      */
 109:     public CategoryLineAnnotation(Comparable category1, double value1,
 110:                                   Comparable category2, double value2,
 111:                                   Paint paint, Stroke stroke) {
 112:         if (category1 == null) {
 113:             throw new IllegalArgumentException("Null 'category1' argument.");
 114:         }
 115:         if (category2 == null) {
 116:             throw new IllegalArgumentException("Null 'category2' argument.");
 117:         }
 118:         if (paint == null) {
 119:             throw new IllegalArgumentException("Null 'paint' argument.");
 120:         }
 121:         if (stroke == null) {
 122:             throw new IllegalArgumentException("Null 'stroke' argument.");
 123:         }
 124:         this.category1 = category1;
 125:         this.value1 = value1;
 126:         this.category2 = category2;
 127:         this.value2 = value2;
 128:         this.paint = paint;
 129:         this.stroke = stroke;
 130:     }
 131: 
 132:     /**
 133:      * Returns the category for the start of the line.
 134:      *
 135:      * @return The category for the start of the line (never <code>null</code>).
 136:      *
 137:      * @see #setCategory1(Comparable)
 138:      */
 139:     public Comparable getCategory1() {
 140:         return this.category1;
 141:     }
 142: 
 143:     /**
 144:      * Sets the category for the start of the line.
 145:      *
 146:      * @param category  the category (<code>null</code> not permitted).
 147:      *
 148:      * @see #getCategory1()
 149:      */
 150:     public void setCategory1(Comparable category) {
 151:         if (category == null) {
 152:             throw new IllegalArgumentException("Null 'category' argument.");
 153:         }
 154:         this.category1 = category;
 155:     }
 156: 
 157:     /**
 158:      * Returns the y-value for the start of the line.
 159:      *
 160:      * @return The y-value for the start of the line.
 161:      *
 162:      * @see #setValue1(double)
 163:      */
 164:     public double getValue1() {
 165:         return this.value1;
 166:     }
 167: 
 168:     /**
 169:      * Sets the y-value for the start of the line.
 170:      *
 171:      * @param value  the value.
 172:      *
 173:      * @see #getValue1()
 174:      */
 175:     public void setValue1(double value) {
 176:         this.value1 = value;
 177:     }
 178: 
 179:     /**
 180:      * Returns the category for the end of the line.
 181:      *
 182:      * @return The category for the end of the line (never <code>null</code>).
 183:      *
 184:      * @see #setCategory2(Comparable)
 185:      */
 186:     public Comparable getCategory2() {
 187:         return this.category2;
 188:     }
 189: 
 190:     /**
 191:      * Sets the category for the end of the line.
 192:      *
 193:      * @param category  the category (<code>null</code> not permitted).
 194:      *
 195:      * @see #getCategory2()
 196:      */
 197:     public void setCategory2(Comparable category) {
 198:         if (category == null) {
 199:             throw new IllegalArgumentException("Null 'category' argument.");
 200:         }
 201:         this.category2 = category;
 202:     }
 203: 
 204:     /**
 205:      * Returns the y-value for the end of the line.
 206:      *
 207:      * @return The y-value for the end of the line.
 208:      *
 209:      * @see #setValue2(double)
 210:      */
 211:     public double getValue2() {
 212:         return this.value2;
 213:     }
 214: 
 215:     /**
 216:      * Sets the y-value for the end of the line.
 217:      *
 218:      * @param value  the value.
 219:      *
 220:      * @see #getValue2()
 221:      */
 222:     public void setValue2(double value) {
 223:         this.value2 = value;
 224:     }
 225: 
 226:     /**
 227:      * Returns the paint used to draw the connecting line.
 228:      *
 229:      * @return The paint (never <code>null</code>).
 230:      *
 231:      * @see #setPaint(Paint)
 232:      */
 233:     public Paint getPaint() {
 234:         return this.paint;
 235:     }
 236: 
 237:     /**
 238:      * Sets the paint used to draw the connecting line.
 239:      *
 240:      * @param paint  the paint (<code>null</code> not permitted).
 241:      *
 242:      * @see #getPaint()
 243:      */
 244:     public void setPaint(Paint paint) {
 245:         if (paint == null) {
 246:             throw new IllegalArgumentException("Null 'paint' argument.");
 247:         }
 248:         this.paint = paint;
 249:     }
 250: 
 251:     /**
 252:      * Returns the stroke used to draw the connecting line.
 253:      *
 254:      * @return The stroke (never <code>null</code>).
 255:      *
 256:      * @see #setStroke(Stroke)
 257:      */
 258:     public Stroke getStroke() {
 259:         return this.stroke;
 260:     }
 261: 
 262:     /**
 263:      * Sets the stroke used to draw the connecting line.
 264:      *
 265:      * @param stroke  the stroke (<code>null</code> not permitted).
 266:      *
 267:      * @see #getStroke()
 268:      */
 269:     public void setStroke(Stroke stroke) {
 270:         if (stroke == null) {
 271:             throw new IllegalArgumentException("Null 'stroke' argument.");
 272:         }
 273:         this.stroke = stroke;
 274:     }
 275: 
 276:     /**
 277:      * Draws the annotation.
 278:      *
 279:      * @param g2  the graphics device.
 280:      * @param plot  the plot.
 281:      * @param dataArea  the data area.
 282:      * @param domainAxis  the domain axis.
 283:      * @param rangeAxis  the range axis.
 284:      */
 285:     public void draw(Graphics2D g2, CategoryPlot plot, Rectangle2D dataArea,
 286:                      CategoryAxis domainAxis, ValueAxis rangeAxis) {
 287: 
 288:         CategoryDataset dataset = plot.getDataset();
 289:         int catIndex1 = dataset.getColumnIndex(this.category1);
 290:         int catIndex2 = dataset.getColumnIndex(this.category2);
 291:         int catCount = dataset.getColumnCount();
 292: 
 293:         double lineX1 = 0.0f;
 294:         double lineY1 = 0.0f;
 295:         double lineX2 = 0.0f;
 296:         double lineY2 = 0.0f;
 297:         PlotOrientation orientation = plot.getOrientation();
 298:         RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
 299:             plot.getDomainAxisLocation(), orientation);
 300:         RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
 301:             plot.getRangeAxisLocation(), orientation);
 302: 
 303:         if (orientation == PlotOrientation.HORIZONTAL) {
 304:             lineY1 = domainAxis.getCategoryJava2DCoordinate(
 305:                 CategoryAnchor.MIDDLE, catIndex1, catCount, dataArea,
 306:                 domainEdge);
 307:             lineX1 = rangeAxis.valueToJava2D(this.value1, dataArea, rangeEdge);
 308:             lineY2 = domainAxis.getCategoryJava2DCoordinate(
 309:                 CategoryAnchor.MIDDLE, catIndex2, catCount, dataArea,
 310:                 domainEdge);
 311:             lineX2 = rangeAxis.valueToJava2D(this.value2, dataArea, rangeEdge);
 312:         }
 313:         else if (orientation == PlotOrientation.VERTICAL) {
 314:             lineX1 = domainAxis.getCategoryJava2DCoordinate(
 315:                 CategoryAnchor.MIDDLE, catIndex1, catCount, dataArea,
 316:                 domainEdge);
 317:             lineY1 = rangeAxis.valueToJava2D(this.value1, dataArea, rangeEdge);
 318:             lineX2 = domainAxis.getCategoryJava2DCoordinate(
 319:                 CategoryAnchor.MIDDLE, catIndex2, catCount, dataArea,
 320:                 domainEdge);
 321:             lineY2 = rangeAxis.valueToJava2D(this.value2, dataArea, rangeEdge);
 322:         }
 323:         g2.setPaint(this.paint);
 324:         g2.setStroke(this.stroke);
 325:         g2.drawLine((int) lineX1, (int) lineY1, (int) lineX2, (int) lineY2);
 326:     }
 327: 
 328:     /**
 329:      * Tests this object for equality with another.
 330:      *
 331:      * @param obj  the object (<code>null</code> permitted).
 332:      *
 333:      * @return <code>true</code> or <code>false</code>.
 334:      */
 335:     public boolean equals(Object obj) {
 336:         if (obj == this) {
 337:             return true;
 338:         }
 339:         if (!(obj instanceof CategoryLineAnnotation)) {
 340:             return false;
 341:         }
 342:         CategoryLineAnnotation that = (CategoryLineAnnotation) obj;
 343:         if (!this.category1.equals(that.getCategory1())) {
 344:             return false;
 345:         }
 346:         if (this.value1 != that.getValue1()) {
 347:             return false;
 348:         }
 349:         if (!this.category2.equals(that.getCategory2())) {
 350:             return false;
 351:         }
 352:         if (this.value2 != that.getValue2()) {
 353:             return false;
 354:         }
 355:         if (!PaintUtilities.equal(this.paint, that.paint)) {
 356:             return false;
 357:         }
 358:         if (!ObjectUtilities.equal(this.stroke, that.stroke)) {
 359:             return false;
 360:         }
 361:         return true;
 362:     }
 363: 
 364:     /**
 365:      * Returns a hash code for this instance.
 366:      *
 367:      * @return A hash code.
 368:      */
 369:     public int hashCode() {
 370:         int result = 193;
 371:         result = 37 * result + this.category1.hashCode();
 372:         long temp = Double.doubleToLongBits(this.value1);
 373:         result = 37 * result + (int) (temp ^ (temp >>> 32));
 374:         result = 37 * result + this.category2.hashCode();
 375:         temp = Double.doubleToLongBits(this.value2);
 376:         result = 37 * result + (int) (temp ^ (temp >>> 32));
 377:         result = 37 * result + HashUtilities.hashCodeForPaint(this.paint);
 378:         result = 37 * result + this.stroke.hashCode();
 379:         return result;
 380:     }
 381: 
 382:     /**
 383:      * Returns a clone of the annotation.
 384:      *
 385:      * @return A clone.
 386:      *
 387:      * @throws CloneNotSupportedException  this class will not throw this
 388:      *         exception, but subclasses (if any) might.
 389:      */
 390:     public Object clone() throws CloneNotSupportedException {
 391:         return super.clone();
 392:     }
 393: 
 394:     /**
 395:      * Provides serialization support.
 396:      *
 397:      * @param stream  the output stream.
 398:      *
 399:      * @throws IOException if there is an I/O error.
 400:      */
 401:     private void writeObject(ObjectOutputStream stream) throws IOException {
 402:         stream.defaultWriteObject();
 403:         SerialUtilities.writePaint(this.paint, stream);
 404:         SerialUtilities.writeStroke(this.stroke, stream);
 405:     }
 406: 
 407:     /**
 408:      * Provides serialization support.
 409:      *
 410:      * @param stream  the input stream.
 411:      *
 412:      * @throws IOException  if there is an I/O error.
 413:      * @throws ClassNotFoundException  if there is a classpath problem.
 414:      */
 415:     private void readObject(ObjectInputStream stream)
 416:         throws IOException, ClassNotFoundException {
 417:         stream.defaultReadObject();
 418:         this.paint = SerialUtilities.readPaint(stream);
 419:         this.stroke = SerialUtilities.readStroke(stream);
 420:     }
 421: 
 422: }