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 * Changes:
036 * --------
037 * 29-Sep-2004 : Version 1 (DG);
038 * ------------- JFREECHART 1.0.x ---------------------------------------------
039 * 06-Mar-2007 : Implemented hashCode() (DG);
040 * 
041 */
042
043package org.jfree.chart.annotations;
044
045import java.awt.Graphics2D;
046import java.awt.Shape;
047import java.awt.geom.Rectangle2D;
048
049import org.jfree.chart.axis.ValueAxis;
050import org.jfree.chart.entity.EntityCollection;
051import org.jfree.chart.entity.XYAnnotationEntity;
052import org.jfree.chart.plot.PlotRenderingInfo;
053import org.jfree.chart.plot.XYPlot;
054import org.jfree.util.ObjectUtilities;
055
056/**
057 * The interface that must be supported by annotations that are to be added to 
058 * an {@link XYPlot}.
059 */
060public abstract class AbstractXYAnnotation implements XYAnnotation {
061
062    /** The tool tip text. */
063    private String toolTipText;
064    
065    /** The URL. */
066    private String url;
067    
068    /**
069     * Creates a new instance that has no tool tip or URL specified.
070     */
071    protected AbstractXYAnnotation() {
072        this.toolTipText = null;    
073        this.url = null;
074    }
075    
076    /**
077     * Returns the tool tip text for the annotation.  This will be displayed in
078     * a {@link org.jfree.chart.ChartPanel} when the mouse pointer hovers over 
079     * the annotation.
080     * 
081     * @return The tool tip text (possibly <code>null</code>).
082     * 
083     * @see #setToolTipText(String)
084     */
085    public String getToolTipText() {
086        return this.toolTipText;
087    }
088    
089    /**
090     * Sets the tool tip text for the annotation.
091     * 
092     * @param text  the tool tip text (<code>null</code> permitted).
093     * 
094     * @see #getToolTipText()
095     */
096    public void setToolTipText(String text) {
097        this.toolTipText = text;
098    }
099    
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}