    001/* ===========================================================
    002 * JFreeChart : a free chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C) Copyright 2000-2011, 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-2009, 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    public abstract void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
    139                              ValueAxis domainAxis, ValueAxis rangeAxis,
    140                              int rendererIndex,
    141                              PlotRenderingInfo info);
    142
    143    /**
    144     * A utility method for adding an {@link XYAnnotationEntity} to
    145     * a {@link PlotRenderingInfo} instance.
    146     *
    147     * @param info  the plot rendering info (<code>null</code> permitted).
    148     * @param hotspot  the hotspot area.
    149     * @param rendererIndex  the renderer index.
    150     * @param toolTipText  the tool tip text.
    151     * @param urlText  the URL text.
    152     */
    153    protected void addEntity(PlotRenderingInfo info,
    154                             Shape hotspot, int rendererIndex,
    155                             String toolTipText, String urlText) {
    156        if (info == null) {
    157            return;
    158        }
    159        EntityCollection entities = info.getOwner().getEntityCollection();
    160        if (entities == null) {
    161            return;
    162        }
    163        XYAnnotationEntity entity = new XYAnnotationEntity(hotspot,
    164                rendererIndex, toolTipText, urlText);
    165        entities.add(entity);
    166    }
    167
    168    /**
    169     * Tests this annotation for equality with an arbitrary object.
    170     *
    171     * @param obj  the object (<code>null</code> permitted).
    172     *
    173     * @return A boolean.
    174     */
    175    public boolean equals(Object obj) {
    176        if (obj == this) {
    177            return true;
    178        }
    179        if (!(obj instanceof AbstractXYAnnotation)) {
    180            return false;
    181        }
    182        AbstractXYAnnotation that = (AbstractXYAnnotation) obj;
    183        if (!ObjectUtilities.equal(this.toolTipText, that.toolTipText)) {
    184            return false;
    185        }
    186        if (!ObjectUtilities.equal(this.url, that.url)) {
    187            return false;
    188        }
    189        return true;
    190    }
    191
    192    /**
    193     * Returns a hash code for this instance.
    194     *
    195     * @return A hash code.
    196     */
    197    public int hashCode() {
    198        int result = 193;
    199        if (this.toolTipText != null) {
    200            result = 37 * result + this.toolTipText.hashCode();
    201        }
    202        if (this.url != null) {
    203            result = 37 * result + this.url.hashCode();
    204        }
    205        return result;
    206    }
    207
    208}