001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -------------------------
028 * AbstractXYAnnotation.java
029 * -------------------------
030 * (C) Copyright 2004-2013, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   Peter Kolb (patch 2809117);
034 *
035 * Changes:
036 * --------
037 * 29-Sep-2004 : Version 1 (DG);
038 * ------------- JFREECHART 1.0.x ---------------------------------------------
039 * 06-Mar-2007 : Implemented hashCode() (DG);
040 * 24-Jun-2009 : Now extends AbstractAnnotation (see patch 2809117 by PK) (DG);
041 *
042 */
043
044package org.jfree.chart.annotations;
045
046import java.awt.Graphics2D;
047import java.awt.Shape;
048import java.awt.geom.Rectangle2D;
049
050import org.jfree.chart.axis.ValueAxis;
051import org.jfree.chart.entity.EntityCollection;
052import org.jfree.chart.entity.XYAnnotationEntity;
053import org.jfree.chart.plot.PlotRenderingInfo;
054import org.jfree.chart.plot.XYPlot;
055import org.jfree.util.ObjectUtilities;
056
057/**
058 * The interface that must be supported by annotations that are to be added to
059 * an {@link XYPlot}.
060 */
061public abstract class AbstractXYAnnotation extends AbstractAnnotation
062        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        super();
075        this.toolTipText = null;
076        this.url = null;
077    }
078
079    /**
080     * Returns the tool tip text for the annotation.  This will be displayed in
081     * a {@link org.jfree.chart.ChartPanel} when the mouse pointer hovers over
082     * the annotation.
083     *
084     * @return The tool tip text (possibly <code>null</code>).
085     *
086     * @see #setToolTipText(String)
087     */
088    public String getToolTipText() {
089        return this.toolTipText;
090    }
091
092    /**
093     * Sets the tool tip text for the annotation.
094     *
095     * @param text  the tool tip text (<code>null</code> permitted).
096     *
097     * @see #getToolTipText()
098     */
099    public void setToolTipText(String text) {
100        this.toolTipText = text;
101    }
102
103    /**
104     * Returns the URL for the annotation.  This URL will be used to provide
105     * hyperlinks when an HTML image map is created for the chart.
106     *
107     * @return The URL (possibly <code>null</code>).
108     *
109     * @see #setURL(String)
110     */
111    public String getURL() {
112        return this.url;
113    }
114
115    /**
116     * Sets the URL for the annotation.
117     *
118     * @param url  the URL (<code>null</code> permitted).
119     *
120     * @see #getURL()
121     */
122    public void setURL(String url) {
123        this.url = url;
124    }
125
126    /**
127     * Draws the annotation.
128     *
129     * @param g2  the graphics device.
130     * @param plot  the plot.
131     * @param dataArea  the data area.
132     * @param domainAxis  the domain axis.
133     * @param rangeAxis  the range axis.
134     * @param rendererIndex  the renderer index.
135     * @param info  if supplied, this info object will be populated with
136     *              entity information.
137     */
138    @Override
139    public abstract void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
140                              ValueAxis domainAxis, ValueAxis rangeAxis,
141                              int rendererIndex,
142                              PlotRenderingInfo info);
143
144    /**
145     * A utility method for adding an {@link XYAnnotationEntity} to
146     * a {@link PlotRenderingInfo} instance.
147     *
148     * @param info  the plot rendering info (<code>null</code> permitted).
149     * @param hotspot  the hotspot area.
150     * @param rendererIndex  the renderer index.
151     * @param toolTipText  the tool tip text.
152     * @param urlText  the URL text.
153     */
154    protected void addEntity(PlotRenderingInfo info,
155                             Shape hotspot, int rendererIndex,
156                             String toolTipText, String urlText) {
157        if (info == null) {
158            return;
159        }
160        EntityCollection entities = info.getOwner().getEntityCollection();
161        if (entities == null) {
162            return;
163        }
164        XYAnnotationEntity entity = new XYAnnotationEntity(hotspot,
165                rendererIndex, toolTipText, urlText);
166        entities.add(entity);
167    }
168
169    /**
170     * Tests this annotation for equality with an arbitrary object.
171     *
172     * @param obj  the object (<code>null</code> permitted).
173     *
174     * @return A boolean.
175     */
176    @Override
177    public boolean equals(Object obj) {
178        if (obj == this) {
179            return true;
180        }
181        if (!(obj instanceof AbstractXYAnnotation)) {
182            return false;
183        }
184        AbstractXYAnnotation that = (AbstractXYAnnotation) obj;
185        if (!ObjectUtilities.equal(this.toolTipText, that.toolTipText)) {
186            return false;
187        }
188        if (!ObjectUtilities.equal(this.url, that.url)) {
189            return false;
190        }
191        return true;
192    }
193
194    /**
195     * Returns a hash code for this instance.
196     *
197     * @return A hash code.
198     */
199    @Override
200    public int hashCode() {
201        int result = 193;
202        if (this.toolTipText != null) {
203            result = 37 * result + this.toolTipText.hashCode();
204        }
205        if (this.url != null) {
206            result = 37 * result + this.url.hashCode();
207        }
208        return result;
209    }
210
211}