Source for org.jfree.chart.annotations.XYDrawableAnnotation

   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:  * XYDrawableAnnotation.java
  29:  * -------------------------
  30:  * (C) Copyright 2003-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: XYDrawableAnnotation.java,v 1.6.2.2 2007/03/06 16:12:18 mungady Exp $
  36:  *
  37:  * Changes:
  38:  * --------
  39:  * 21-May-2003 : Version 1 (DG);
  40:  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
  41:  * 30-Sep-2004 : Added support for tool tips and URLs (DG);
  42:  *
  43:  */
  44: 
  45: package org.jfree.chart.annotations;
  46: 
  47: import java.awt.Graphics2D;
  48: import java.awt.geom.Rectangle2D;
  49: import java.io.Serializable;
  50: 
  51: import org.jfree.chart.axis.ValueAxis;
  52: import org.jfree.chart.plot.Plot;
  53: import org.jfree.chart.plot.PlotOrientation;
  54: import org.jfree.chart.plot.PlotRenderingInfo;
  55: import org.jfree.chart.plot.XYPlot;
  56: import org.jfree.ui.Drawable;
  57: import org.jfree.ui.RectangleEdge;
  58: import org.jfree.util.ObjectUtilities;
  59: import org.jfree.util.PublicCloneable;
  60: 
  61: /**
  62:  * A general annotation that can be placed on an {@link XYPlot}.
  63:  */
  64: public class XYDrawableAnnotation extends AbstractXYAnnotation
  65:                                   implements Cloneable, PublicCloneable, 
  66:                                              Serializable {
  67: 
  68:     /** For serialization. */
  69:     private static final long serialVersionUID = -6540812859722691020L;
  70:     
  71:     /** The x-coordinate. */
  72:     private double x;
  73: 
  74:     /** The y-coordinate. */
  75:     private double y;
  76: 
  77:     /** The width. */
  78:     private double width;
  79: 
  80:     /** The height. */
  81:     private double height;
  82: 
  83:     /** The drawable object. */
  84:     private Drawable drawable;
  85: 
  86:     /**
  87:      * Creates a new annotation to be displayed within the given area.
  88:      *
  89:      * @param x  the x-coordinate for the area.
  90:      * @param y  the y-coordinate for the area.
  91:      * @param width  the width of the area.
  92:      * @param height  the height of the area.
  93:      * @param drawable  the drawable object (<code>null</code> not permitted).
  94:      */
  95:     public XYDrawableAnnotation(double x, double y, double width, double height,
  96:                                 Drawable drawable) {
  97: 
  98:         if (drawable == null) {
  99:             throw new IllegalArgumentException("Null 'drawable' argument.");
 100:         }
 101:         this.x = x;
 102:         this.y = y;
 103:         this.width = width;
 104:         this.height = height;
 105:         this.drawable = drawable;
 106: 
 107:     }
 108: 
 109:     /**
 110:      * Draws the annotation.
 111:      *
 112:      * @param g2  the graphics device.
 113:      * @param plot  the plot.
 114:      * @param dataArea  the data area.
 115:      * @param domainAxis  the domain axis.
 116:      * @param rangeAxis  the range axis.
 117:      * @param rendererIndex  the renderer index.
 118:      * @param info  if supplied, this info object will be populated with
 119:      *              entity information.
 120:      */
 121:     public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
 122:                      ValueAxis domainAxis, ValueAxis rangeAxis, 
 123:                      int rendererIndex,
 124:                      PlotRenderingInfo info) {
 125: 
 126:         PlotOrientation orientation = plot.getOrientation();
 127:         RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
 128:                 plot.getDomainAxisLocation(), orientation);
 129:         RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
 130:                 plot.getRangeAxisLocation(), orientation);
 131:         float j2DX = (float) domainAxis.valueToJava2D(this.x, dataArea, 
 132:                 domainEdge);
 133:         float j2DY = (float) rangeAxis.valueToJava2D(this.y, dataArea, 
 134:                 rangeEdge);
 135:         Rectangle2D area = new Rectangle2D.Double(j2DX - this.width / 2.0, 
 136:                 j2DY - this.height / 2.0, this.width, this.height);
 137:         this.drawable.draw(g2, area);
 138:         String toolTip = getToolTipText();
 139:         String url = getURL();
 140:         if (toolTip != null || url != null) {
 141:             addEntity(info, area, rendererIndex, toolTip, url);
 142:         }
 143:         
 144:     }
 145: 
 146:     /**
 147:      * Tests this annotation for equality with an arbitrary object.
 148:      * 
 149:      * @param obj  the object to test against.
 150:      * 
 151:      * @return <code>true</code> or <code>false</code>.
 152:      */
 153:     public boolean equals(Object obj) {
 154:         
 155:         if (obj == this) { // simple case
 156:             return true;
 157:         }      
 158:         // now try to reject equality...
 159:         if (!super.equals(obj)) {
 160:             return false;
 161:         }
 162:         if (!(obj instanceof XYDrawableAnnotation)) {
 163:             return false;
 164:         }
 165:         XYDrawableAnnotation that = (XYDrawableAnnotation) obj;
 166:         if (this.x != that.x) {
 167:             return false;
 168:         }
 169:         if (this.y != that.y) {
 170:             return false;
 171:         }
 172:         if (this.width != that.width) {
 173:             return false;
 174:         }
 175:         if (this.height != that.height) {
 176:             return false;
 177:         }
 178:         if (!ObjectUtilities.equal(this.drawable, that.drawable)) {
 179:             return false;
 180:         }
 181:         // seem to be the same... 
 182:         return true;
 183:         
 184:     }
 185:     
 186:     /**
 187:      * Returns a hash code.
 188:      * 
 189:      * @return A hash code.
 190:      */
 191:     public int hashCode() {
 192:         int result;
 193:         long temp;
 194:         temp = Double.doubleToLongBits(this.x);
 195:         result = (int) (temp ^ (temp >>> 32));
 196:         temp = Double.doubleToLongBits(this.y);
 197:         result = 29 * result + (int) (temp ^ (temp >>> 32));
 198:         temp = Double.doubleToLongBits(this.width);
 199:         result = 29 * result + (int) (temp ^ (temp >>> 32));
 200:         temp = Double.doubleToLongBits(this.height);
 201:         result = 29 * result + (int) (temp ^ (temp >>> 32));
 202:         return result;
 203:     }
 204:     
 205:     /**
 206:      * Returns a clone of the annotation.
 207:      * 
 208:      * @return A clone.
 209:      * 
 210:      * @throws CloneNotSupportedException  if the annotation can't be cloned.
 211:      */
 212:     public Object clone() throws CloneNotSupportedException {
 213:         return super.clone();
 214:     }
 215: 
 216: }