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