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