    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, 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, 2005, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: AbstractXYAnnotation.java,v 1.3.2.1 2005/10/25 16:51:14 mungady Exp $
    036     *
    037     * Changes:
    038     * --------
    039     * 29-Sep-2004 : Version 1 (DG);
    040     *
    041     */
    042
    043    package org.jfree.chart.annotations;
    044
    045    import java.awt.Graphics2D;
    046    import java.awt.Shape;
    047    import java.awt.geom.Rectangle2D;
    048
    049    import org.jfree.chart.axis.ValueAxis;
    050    import org.jfree.chart.entity.EntityCollection;
    051    import org.jfree.chart.entity.XYAnnotationEntity;
    052    import org.jfree.chart.plot.PlotRenderingInfo;
    053    import org.jfree.chart.plot.XYPlot;
    054    import 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     */
    060    public 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        public String getToolTipText() {
    084            return this.toolTipText;
    085        }
    086
    087        /**
    088         * Sets the tool tip text for the annotation.
    089         *
    090         * @param text  the tool tip text (<code>null</code> permitted).
    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</code>).
    101         */
    102        public String getURL() {
    103            return this.url;
    104        }
    105
    106        /**
    107         * Sets the URL for the annotation.
    108         *
    109         * @param url  the URL (<code>null</code> permitted).
    110         */
    111        public void setURL(String url) {
    112            this.url = url;
    113        }
    114
    115        /**
    116         * Draws the annotation.
    117         *
    118         * @param g2  the graphics device.
    119         * @param plot  the plot.
    120         * @param dataArea  the data area.
    121         * @param domainAxis  the domain axis.
    122         * @param rangeAxis  the range axis.
    123         * @param rendererIndex  the renderer index.
    124         * @param info  if supplied, this info object will be populated with
    125         *              entity information.
    126         */
    127        public abstract void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
    128                                  ValueAxis domainAxis, ValueAxis rangeAxis,
    129                                  int rendererIndex,
    130                                  PlotRenderingInfo info);
    131
    132        /**
    133         * A utility method for adding an {@link XYAnnotationEntity} to
    134         * a {@link PlotRenderingInfo} instance.
    135         *
    136         * @param info  the plot rendering info (<code>null</code> permitted).
    137         * @param hotspot  the hotspot area.
    138         * @param rendererIndex  the renderer index.
    139         * @param toolTipText  the tool tip text.
    140         * @param urlText  the URL text.
    141         */
    142        protected void addEntity(PlotRenderingInfo info,
    143                                 Shape hotspot, int rendererIndex,
    144                                 String toolTipText, String urlText) {
    145            if (info == null) {
    146                return;
    147            }
    148            EntityCollection entities = info.getOwner().getEntityCollection();
    149            if (entities == null) {
    150                return;
    151            }
    152            XYAnnotationEntity entity = new XYAnnotationEntity(
    153                hotspot, rendererIndex, toolTipText, urlText
    154            );
    155            entities.add(entity);
    156        }
    157
    158        /**
    159         * Tests this annotation for equality with an arbitrary object.
    160         *
    161         * @param obj  the object (<code>null</code> permitted).
    162         *
    163         * @return A boolean.
    164         */
    165        public boolean equals(Object obj) {
    166            if (obj == this) {
    167                return true;
    168            }
    169            if (!(obj instanceof AbstractXYAnnotation)) {
    170                return false;
    171            }
    172            AbstractXYAnnotation that = (AbstractXYAnnotation) obj;
    173            if (!ObjectUtilities.equal(this.toolTipText, that.toolTipText)) {
    174                return false;
    175            }
    176            if (!ObjectUtilities.equal(this.url, that.url)) {
    177                return false;
    178            }
    179            return true;
    180        }
    181
    182    }