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