    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     * XYTitleAnnotation.java
    029     * ----------------------
    030     * (C) Copyright 2007-2011, by Object Refinery Limited and Contributors.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   Andrew Mickish;
    034     *                   Peter Kolb (patch 2809117);
    035     *
    036     * Changes:
    037     * --------
    038     * 02-Feb-2007 : Version 1 (DG);
    039     * 30-Apr-2007 : Fixed equals() method (DG);
    040     * 26-Feb-2008 : Fixed NullPointerException when drawing chart with a null
    041     *               ChartRenderingInfo - see patch 1901599 by Andrew Mickish (DG);
    042     * 03-Sep-2008 : Moved from experimental to main (DG);
    043     * 24-Jun-2009 : Fire change events (see patch 2809117 by PK) (DG);
    044     *
    045     */
    046
    047    package org.jfree.chart.annotations;
    048
    049    import java.awt.Graphics2D;
    050    import java.awt.geom.Point2D;
    051    import java.awt.geom.Rectangle2D;
    052    import java.io.Serializable;
    053
    054    import org.jfree.chart.HashUtilities;
    055    import org.jfree.chart.axis.AxisLocation;
    056    import org.jfree.chart.axis.ValueAxis;
    057    import org.jfree.chart.block.BlockParams;
    058    import org.jfree.chart.block.EntityBlockResult;
    059    import org.jfree.chart.block.RectangleConstraint;
    060    import org.jfree.chart.event.AnnotationChangeEvent;
    061    import org.jfree.chart.plot.Plot;
    062    import org.jfree.chart.plot.PlotOrientation;
    063    import org.jfree.chart.plot.PlotRenderingInfo;
    064    import org.jfree.chart.plot.XYPlot;
    065    import org.jfree.chart.title.Title;
    066    import org.jfree.chart.util.XYCoordinateType;
    067    import org.jfree.data.Range;
    068    import org.jfree.ui.RectangleAnchor;
    069    import org.jfree.ui.RectangleEdge;
    070    import org.jfree.ui.Size2D;
    071    import org.jfree.util.ObjectUtilities;
    072    import org.jfree.util.PublicCloneable;
    073
    074    /**
    075     * An annotation that allows any {@link Title} to be placed at a location on
    076     * an {@link XYPlot}.
    077     *
    078     * @since 1.0.11
    079     */
    080    public class XYTitleAnnotation extends AbstractXYAnnotation
    081            implements Cloneable, PublicCloneable, Serializable {
    082
    083        /** For serialization. */
    084        private static final long serialVersionUID = -4364694501921559958L;
    085
    086        /** The coordinate type. */
    087        private XYCoordinateType coordinateType;
    088
    089        /** The x-coordinate (in data space). */
    090        private double x;
    091
    092        /** The y-coordinate (in data space). */
    093        private double y;
    094
    095        /** The maximum width. */
    096        private double maxWidth;
    097
    098        /** The maximum height. */
    099        private double maxHeight;
    100
    101        /** The title. */
    102        private Title title;
    103
    104        /**
    105         * The title anchor point.
    106         */
    107        private RectangleAnchor anchor;
    108
    109        /**
    110         * Creates a new annotation to be displayed at the specified (x, y)
    111         * location.
    112         *
    113         * @param x  the x-coordinate (in data space).
    114         * @param y  the y-coordinate (in data space).
    115         * @param title  the title (<code>null</code> not permitted).
    116         */
    117        public XYTitleAnnotation(double x, double y, Title title) {
    118            this(x, y, title, RectangleAnchor.CENTER);
    119        }
    120
    121        /**
    122         * Creates a new annotation to be displayed at the specified (x, y)
    123         * location.
    124         *
    125         * @param x  the x-coordinate (in data space).
    126         * @param y  the y-coordinate (in data space).
    127         * @param title  the title (<code>null</code> not permitted).
    128         * @param anchor  the title anchor (<code>null</code> not permitted).
    129         */
    130        public XYTitleAnnotation(double x, double y, Title title,
    131                RectangleAnchor anchor) {
    132            super();
    133            if (title == null) {
    134                throw new IllegalArgumentException("Null 'title' argument.");
    135            }
    136            if (anchor == null) {
    137                throw new IllegalArgumentException("Null 'anchor' argument.");
    138            }
    139            this.coordinateType = XYCoordinateType.RELATIVE;
    140            this.x = x;
    141            this.y = y;
    142            this.maxWidth = 0.0;
    143            this.maxHeight = 0.0;
    144            this.title = title;
    145            this.anchor = anchor;
    146        }
    147
    148        /**
    149         * Returns the coordinate type (set in the constructor).
    150         *
    151         * @return The coordinate type (never <code>null</code>).
    152         */
    153        public XYCoordinateType getCoordinateType() {
    154            return this.coordinateType;
    155        }
    156
    157        /**
    158         * Returns the x-coordinate for the annotation.
    159         *
    160         * @return The x-coordinate.
    161         */
    162        public double getX() {
    163            return this.x;
    164        }
    165
    166        /**
    167         * Returns the y-coordinate for the annotation.
    168         *
    169         * @return The y-coordinate.
    170         */
    171        public double getY() {
    172            return this.y;
    173        }
    174
    175        /**
    176         * Returns the title for the annotation.
    177         *
    178         * @return The title.
    179         */
    180        public Title getTitle() {
    181            return this.title;
    182        }
    183
    184        /**
    185         * Returns the title anchor for the annotation.
    186         *
    187         * @return The title anchor.
    188         */
    189        public RectangleAnchor getTitleAnchor() {
    190            return this.anchor;
    191        }
    192
    193        /**
    194         * Returns the maximum width.
    195         *
    196         * @return The maximum width.
    197         */
    198        public double getMaxWidth() {
    199            return this.maxWidth;
    200        }
    201
    202        /**
    203         * Sets the maximum width and sends an
    204         * {@link AnnotationChangeEvent} to all registered listeners.
    205         *
    206         * @param max  the maximum width (0.0 or less means no maximum).
    207         */
    208        public void setMaxWidth(double max) {
    209            this.maxWidth = max;
    210            fireAnnotationChanged();
    211        }
    212
    213        /**
    214         * Returns the maximum height.
    215         *
    216         * @return The maximum height.
    217         */
    218        public double getMaxHeight() {
    219            return this.maxHeight;
    220        }
    221
    222        /**
    223         * Sets the maximum height and sends an
    224         * {@link AnnotationChangeEvent} to all registered listeners.
    225         *
    226         * @param max  the maximum height.
    227         */
    228        public void setMaxHeight(double max) {
    229            this.maxHeight = max;
    230            fireAnnotationChanged();
    231        }
    232
    233        /**
    234         * Draws the annotation.  This method is called by the drawing code in the
    235         * {@link XYPlot} class, you don't normally need to call this method
    236         * directly.
    237         *
    238         * @param g2  the graphics device.
    239         * @param plot  the plot.
    240         * @param dataArea  the data area.
    241         * @param domainAxis  the domain axis.
    242         * @param rangeAxis  the range axis.
    243         * @param rendererIndex  the renderer index.
    244         * @param info  if supplied, this info object will be populated with
    245         *              entity information.
    246         */
    247        public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
    248                         ValueAxis domainAxis, ValueAxis rangeAxis,
    249                         int rendererIndex,
    250                         PlotRenderingInfo info) {
    251
    252            PlotOrientation orientation = plot.getOrientation();
    253            AxisLocation domainAxisLocation = plot.getDomainAxisLocation();
    254            AxisLocation rangeAxisLocation = plot.getRangeAxisLocation();
    255            RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
    256                    domainAxisLocation, orientation);
    257            RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
    258                    rangeAxisLocation, orientation);
    259            Range xRange = domainAxis.getRange();
    260            Range yRange = rangeAxis.getRange();
    261            double anchorX = 0.0;
    262            double anchorY = 0.0;
    263            if (this.coordinateType == XYCoordinateType.RELATIVE) {
    264                anchorX = xRange.getLowerBound() + (this.x * xRange.getLength());
    265                anchorY = yRange.getLowerBound() + (this.y * yRange.getLength());
    266            }
    267            else {
    268                anchorX = domainAxis.valueToJava2D(this.x, dataArea, domainEdge);
    269                anchorY = rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge);
    270            }
    271
    272            float j2DX = (float) domainAxis.valueToJava2D(anchorX, dataArea,
    273                    domainEdge);
    274            float j2DY = (float) rangeAxis.valueToJava2D(anchorY, dataArea,
    275                    rangeEdge);
    276            float xx = 0.0f;
    277            float yy = 0.0f;
    278            if (orientation == PlotOrientation.HORIZONTAL) {
    279                xx = j2DY;
    280                yy = j2DX;
    281            }
    282            else if (orientation == PlotOrientation.VERTICAL) {
    283                xx = j2DX;
    284                yy = j2DY;
    285            }
    286
    287            double maxW = dataArea.getWidth();
    288            double maxH = dataArea.getHeight();
    289            if (this.coordinateType == XYCoordinateType.RELATIVE) {
    290                if (this.maxWidth > 0.0) {
    291                    maxW = maxW * this.maxWidth;
    292                }
    293                if (this.maxHeight > 0.0) {
    294                    maxH = maxH * this.maxHeight;
    295                }
    296            }
    297            if (this.coordinateType == XYCoordinateType.DATA) {
    298                maxW = this.maxWidth;
    299                maxH = this.maxHeight;
    300            }
    301            RectangleConstraint rc = new RectangleConstraint(
    302                    new Range(0, maxW), new Range(0, maxH));
    303
    304            Size2D size = this.title.arrange(g2, rc);
    305            Rectangle2D titleRect = new Rectangle2D.Double(0, 0, size.width,
    306                    size.height);
    307            Point2D anchorPoint = RectangleAnchor.coordinates(titleRect,
    308                    this.anchor);
    309            xx = xx - (float) anchorPoint.getX();
    310            yy = yy - (float) anchorPoint.getY();
    311            titleRect.setRect(xx, yy, titleRect.getWidth(), titleRect.getHeight());
    312            BlockParams p = new BlockParams();
    313            if (info != null) {
    314                if (info.getOwner().getEntityCollection() != null) {
    315                    p.setGenerateEntities(true);
    316                }
    317            }
    318            Object result = this.title.draw(g2, titleRect, p);
    319            if (info != null) {
    320                if (result instanceof EntityBlockResult) {
    321                    EntityBlockResult ebr = (EntityBlockResult) result;
    322                    info.getOwner().getEntityCollection().addAll(
    323                            ebr.getEntityCollection());
    324                }
    325                String toolTip = getToolTipText();
    326                String url = getURL();
    327                if (toolTip != null || url != null) {
    328                    addEntity(info, new Rectangle2D.Float(xx, yy,
    329                            (float) size.width, (float) size.height),
    330                            rendererIndex, toolTip, url);
    331                }
    332            }
    333        }
    334
    335        /**
    336         * Tests this object for equality with an arbitrary object.
    337         *
    338         * @param obj  the object (<code>null</code> permitted).
    339         *
    340         * @return A boolean.
    341         */
    342        public boolean equals(Object obj) {
    343            if (obj == this) {
    344                return true;
    345            }
    346            if (!(obj instanceof XYTitleAnnotation)) {
    347                return false;
    348            }
    349            XYTitleAnnotation that = (XYTitleAnnotation) obj;
    350            if (this.coordinateType != that.coordinateType) {
    351                return false;
    352            }
    353            if (this.x != that.x) {
    354                return false;
    355            }
    356            if (this.y != that.y) {
    357                return false;
    358            }
    359            if (this.maxWidth != that.maxWidth) {
    360                return false;
    361            }
    362            if (this.maxHeight != that.maxHeight) {
    363                return false;
    364            }
    365            if (!ObjectUtilities.equal(this.title, that.title)) {
    366                return false;
    367            }
    368            if (!this.anchor.equals(that.anchor)) {
    369                return false;
    370            }
    371            return super.equals(obj);
    372        }
    373
    374        /**
    375         * Returns a hash code for this object.
    376         *
    377         * @return A hash code.
    378         */
    379        public int hashCode() {
    380            int result = 193;
    381            result = HashUtilities.hashCode(result, this.anchor);
    382            result = HashUtilities.hashCode(result, this.coordinateType);
    383            result = HashUtilities.hashCode(result, this.x);
    384            result = HashUtilities.hashCode(result, this.y);
    385            result = HashUtilities.hashCode(result, this.maxWidth);
    386            result = HashUtilities.hashCode(result, this.maxHeight);
    387            result = HashUtilities.hashCode(result, this.title);
    388            return result;
    389        }
    390
    391        /**
    392         * Returns a clone of the annotation.
    393         *
    394         * @return A clone.
    395         *
    396         * @throws CloneNotSupportedException  if the annotation can't be cloned.
    397         */
    398        public Object clone() throws CloneNotSupportedException {
    399            return super.clone();
    400        }
    401
    402    }