001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ------------------------- 028 * AbstractXYAnnotation.java 029 * ------------------------- 030 * (C) Copyright 2004-2007, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * $Id: AbstractXYAnnotation.java,v 1.3.2.3 2007/03/06 16:12:18 mungady Exp $ 036 * 037 * Changes: 038 * -------- 039 * 29-Sep-2004 : Version 1 (DG); 040 * ------------- JFREECHART 1.0.x --------------------------------------------- 041 * 06-Mar-2007 : Implemented hashCode() (DG); 042 * 043 */ 044 045 package org.jfree.chart.annotations; 046 047 import java.awt.Graphics2D; 048 import java.awt.Shape; 049 import java.awt.geom.Rectangle2D; 050 051 import org.jfree.chart.axis.ValueAxis; 052 import org.jfree.chart.entity.EntityCollection; 053 import org.jfree.chart.entity.XYAnnotationEntity; 054 import org.jfree.chart.plot.PlotRenderingInfo; 055 import org.jfree.chart.plot.XYPlot; 056 import org.jfree.util.ObjectUtilities; 057 058 /** 059 * The interface that must be supported by annotations that are to be added to 060 * an {@link XYPlot}. 061 */ 062 public abstract class AbstractXYAnnotation implements XYAnnotation { 063 064 /** The tool tip text. */ 065 private String toolTipText; 066 067 /** The URL. */ 068 private String url; 069 070 /** 071 * Creates a new instance that has no tool tip or URL specified. 072 */ 073 protected AbstractXYAnnotation() { 074 this.toolTipText = null; 075 this.url = null; 076 } 077 078 /** 079 * Returns the tool tip text for the annotation. This will be displayed in 080 * a {@link org.jfree.chart.ChartPanel} when the mouse pointer hovers over 081 * the annotation. 082 * 083 * @return The tool tip text (possibly <code>null</code>). 084 * 085 * @see #setToolTipText(String) 086 */ 087 public String getToolTipText() { 088 return this.toolTipText; 089 } 090 091 /** 092 * Sets the tool tip text for the annotation. 093 * 094 * @param text the tool tip text (<code>null</code> permitted). 095 * 096 * @see #getToolTipText() 097 */ 098 public void setToolTipText(String text) { 099 this.toolTipText = text; 100 } 101 102 /** 103 * Returns the URL for the annotation. This URL will be used to provide 104 * hyperlinks when an HTML image map is created for the chart. 105 * 106 * @return The URL (possibly <code>null</code>). 107 * 108 * @see #setURL(String) 109 */ 110 public String getURL() { 111 return this.url; 112 } 113 114 /** 115 * Sets the URL for the annotation. 116 * 117 * @param url the URL (<code>null</code> permitted). 118 * 119 * @see #getURL() 120 */ 121 public void setURL(String url) { 122 this.url = url; 123 } 124 125 /** 126 * Draws the annotation. 127 * 128 * @param g2 the graphics device. 129 * @param plot the plot. 130 * @param dataArea the data area. 131 * @param domainAxis the domain axis. 132 * @param rangeAxis the range axis. 133 * @param rendererIndex the renderer index. 134 * @param info if supplied, this info object will be populated with 135 * entity information. 136 */ 137 public abstract void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, 138 ValueAxis domainAxis, ValueAxis rangeAxis, 139 int rendererIndex, 140 PlotRenderingInfo info); 141 142 /** 143 * A utility method for adding an {@link XYAnnotationEntity} to 144 * a {@link PlotRenderingInfo} instance. 145 * 146 * @param info the plot rendering info (<code>null</code> permitted). 147 * @param hotspot the hotspot area. 148 * @param rendererIndex the renderer index. 149 * @param toolTipText the tool tip text. 150 * @param urlText the URL text. 151 */ 152 protected void addEntity(PlotRenderingInfo info, 153 Shape hotspot, int rendererIndex, 154 String toolTipText, String urlText) { 155 if (info == null) { 156 return; 157 } 158 EntityCollection entities = info.getOwner().getEntityCollection(); 159 if (entities == null) { 160 return; 161 } 162 XYAnnotationEntity entity = new XYAnnotationEntity(hotspot, 163 rendererIndex, toolTipText, urlText); 164 entities.add(entity); 165 } 166 167 /** 168 * Tests this annotation for equality with an arbitrary object. 169 * 170 * @param obj the object (<code>null</code> permitted). 171 * 172 * @return A boolean. 173 */ 174 public boolean equals(Object obj) { 175 if (obj == this) { 176 return true; 177 } 178 if (!(obj instanceof AbstractXYAnnotation)) { 179 return false; 180 } 181 AbstractXYAnnotation that = (AbstractXYAnnotation) obj; 182 if (!ObjectUtilities.equal(this.toolTipText, that.toolTipText)) { 183 return false; 184 } 185 if (!ObjectUtilities.equal(this.url, that.url)) { 186 return false; 187 } 188 return true; 189 } 190 191 /** 192 * Returns a hash code for this instance. 193 * 194 * @return A hash code. 195 */ 196 public int hashCode() { 197 int result = 193; 198 if (this.toolTipText != null) { 199 result = 37 * result + this.toolTipText.hashCode(); 200 } 201 if (this.url != null) { 202 result = 37 * result + this.url.hashCode(); 203 } 204 return result; 205 } 206 207 }