Source for org.jfree.chart.annotations.AbstractXYAnnotation

   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:  * AbstractXYAnnotation.java
  29:  * -------------------------
  30:  * (C) Copyright 2004-2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes:
  36:  * --------
  37:  * 29-Sep-2004 : Version 1 (DG);
  38:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  39:  * 06-Mar-2007 : Implemented hashCode() (DG);
  40:  * 
  41:  */
  42: 
  43: package org.jfree.chart.annotations;
  44: 
  45: import java.awt.Graphics2D;
  46: import java.awt.Shape;
  47: import java.awt.geom.Rectangle2D;
  48: 
  49: import org.jfree.chart.axis.ValueAxis;
  50: import org.jfree.chart.entity.EntityCollection;
  51: import org.jfree.chart.entity.XYAnnotationEntity;
  52: import org.jfree.chart.plot.PlotRenderingInfo;
  53: import org.jfree.chart.plot.XYPlot;
  54: import org.jfree.util.ObjectUtilities;
  55: 
  56: /**
  57:  * The interface that must be supported by annotations that are to be added to 
  58:  * an {@link XYPlot}.
  59:  */
  60: public abstract class AbstractXYAnnotation implements XYAnnotation {
  61: 
  62:     /** The tool tip text. */
  63:     private String toolTipText;
  64:     
  65:     /** The URL. */
  66:     private String url;
  67:     
  68:     /**
  69:      * Creates a new instance that has no tool tip or URL specified.
  70:      */
  71:     protected AbstractXYAnnotation() {
  72:         this.toolTipText = null;    
  73:         this.url = null;
  74:     }
  75:     
  76:     /**
  77:      * Returns the tool tip text for the annotation.  This will be displayed in
  78:      * a {@link org.jfree.chart.ChartPanel} when the mouse pointer hovers over 
  79:      * the annotation.
  80:      * 
  81:      * @return The tool tip text (possibly <code>null</code>).
  82:      * 
  83:      * @see #setToolTipText(String)
  84:      */
  85:     public String getToolTipText() {
  86:         return this.toolTipText;
  87:     }
  88:     
  89:     /**
  90:      * Sets the tool tip text for the annotation.
  91:      * 
  92:      * @param text  the tool tip text (<code>null</code> permitted).
  93:      * 
  94:      * @see #getToolTipText()
  95:      */
  96:     public void setToolTipText(String text) {
  97:         this.toolTipText = text;
  98:     }
  99:     
 100:     /**
 101:      * Returns the URL for the annotation.  This URL will be used to provide
 102:      * hyperlinks when an HTML image map is created for the chart.
 103:      * 
 104:      * @return The URL (possibly <code>null</code>).
 105:      * 
 106:      * @see #setURL(String)
 107:      */
 108:     public String getURL() {
 109:         return this.url;
 110:     }
 111:     
 112:     /**
 113:      * Sets the URL for the annotation.
 114:      * 
 115:      * @param url  the URL (<code>null</code> permitted).
 116:      * 
 117:      * @see #getURL()
 118:      */
 119:     public void setURL(String url) {
 120:         this.url = url;
 121:     }
 122:     
 123:     /**
 124:      * Draws the annotation.
 125:      *
 126:      * @param g2  the graphics device.
 127:      * @param plot  the plot.
 128:      * @param dataArea  the data area.
 129:      * @param domainAxis  the domain axis.
 130:      * @param rangeAxis  the range axis.
 131:      * @param rendererIndex  the renderer index.
 132:      * @param info  if supplied, this info object will be populated with
 133:      *              entity information.
 134:      */
 135:     public abstract void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
 136:                               ValueAxis domainAxis, ValueAxis rangeAxis, 
 137:                               int rendererIndex,
 138:                               PlotRenderingInfo info);
 139: 
 140:     /**
 141:      * A utility method for adding an {@link XYAnnotationEntity} to 
 142:      * a {@link PlotRenderingInfo} instance.
 143:      * 
 144:      * @param info  the plot rendering info (<code>null</code> permitted).
 145:      * @param hotspot  the hotspot area.
 146:      * @param rendererIndex  the renderer index.
 147:      * @param toolTipText  the tool tip text.
 148:      * @param urlText  the URL text.
 149:      */
 150:     protected void addEntity(PlotRenderingInfo info, 
 151:                              Shape hotspot, int rendererIndex, 
 152:                              String toolTipText, String urlText) {
 153:         if (info == null) {
 154:             return;  
 155:         }
 156:         EntityCollection entities = info.getOwner().getEntityCollection();
 157:         if (entities == null) {
 158:             return;
 159:         }
 160:         XYAnnotationEntity entity = new XYAnnotationEntity(hotspot, 
 161:                 rendererIndex, toolTipText, urlText);
 162:         entities.add(entity);
 163:     }
 164: 
 165:     /**
 166:      * Tests this annotation for equality with an arbitrary object.
 167:      * 
 168:      * @param obj  the object (<code>null</code> permitted).
 169:      * 
 170:      * @return A boolean.
 171:      */
 172:     public boolean equals(Object obj) {
 173:         if (obj == this) {
 174:             return true;
 175:         }
 176:         if (!(obj instanceof AbstractXYAnnotation)) {
 177:             return false;
 178:         }
 179:         AbstractXYAnnotation that = (AbstractXYAnnotation) obj;
 180:         if (!ObjectUtilities.equal(this.toolTipText, that.toolTipText)) {
 181:             return false;
 182:         }
 183:         if (!ObjectUtilities.equal(this.url, that.url)) {
 184:             return false;
 185:         }
 186:         return true;
 187:     }
 188:     
 189:     /**
 190:      * Returns a hash code for this instance.
 191:      * 
 192:      * @return A hash code.
 193:      */
 194:     public int hashCode() {
 195:         int result = 193;
 196:         if (this.toolTipText != null) {
 197:             result = 37 * result + this.toolTipText.hashCode();
 198:         }
 199:         if (this.url != null) {
 200:             result = 37 * result + this.url.hashCode();            
 201:         }
 202:         return result;
 203:     }
 204:     
 205: }