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