001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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.HashUtilities;
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.util.ParamChecks;
067import org.jfree.chart.util.XYCoordinateType;
068import org.jfree.data.Range;
069import org.jfree.ui.RectangleAnchor;
070import org.jfree.ui.RectangleEdge;
071import org.jfree.ui.Size2D;
072import org.jfree.util.ObjectUtilities;
073import org.jfree.util.PublicCloneable;
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</code> 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</code> not permitted).
129     * @param anchor  the title anchor (<code>null</code> not permitted).
130     */
131    public XYTitleAnnotation(double x, double y, Title title,
132            RectangleAnchor anchor) {
133        super();
134        ParamChecks.nullNotPermitted(title, "title");
135        ParamChecks.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</code>).
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 = RectangleAnchor.coordinates(titleRect,
304                this.anchor);
305        xx = xx - (float) anchorPoint.getX();
306        yy = yy - (float) anchorPoint.getY();
307        titleRect.setRect(xx, yy, titleRect.getWidth(), titleRect.getHeight());
308        BlockParams p = new BlockParams();
309        if (info != null) {
310            if (info.getOwner().getEntityCollection() != null) {
311                p.setGenerateEntities(true);
312            }
313        }
314        Object result = this.title.draw(g2, titleRect, p);
315        if (info != null) {
316            if (result instanceof EntityBlockResult) {
317                EntityBlockResult ebr = (EntityBlockResult) result;
318                info.getOwner().getEntityCollection().addAll(
319                        ebr.getEntityCollection());
320            }
321            String toolTip = getToolTipText();
322            String url = getURL();
323            if (toolTip != null || url != null) {
324                addEntity(info, new Rectangle2D.Float(xx, yy,
325                        (float) size.width, (float) size.height),
326                        rendererIndex, toolTip, url);
327            }
328        }
329    }
330
331    /**
332     * Tests this object for equality with an arbitrary object.
333     *
334     * @param obj  the object (<code>null</code> permitted).
335     *
336     * @return A boolean.
337     */
338    @Override
339    public boolean equals(Object obj) {
340        if (obj == this) {
341            return true;
342        }
343        if (!(obj instanceof XYTitleAnnotation)) {
344            return false;
345        }
346        XYTitleAnnotation that = (XYTitleAnnotation) obj;
347        if (this.coordinateType != that.coordinateType) {
348            return false;
349        }
350        if (this.x != that.x) {
351            return false;
352        }
353        if (this.y != that.y) {
354            return false;
355        }
356        if (this.maxWidth != that.maxWidth) {
357            return false;
358        }
359        if (this.maxHeight != that.maxHeight) {
360            return false;
361        }
362        if (!ObjectUtilities.equal(this.title, that.title)) {
363            return false;
364        }
365        if (!this.anchor.equals(that.anchor)) {
366            return false;
367        }
368        return super.equals(obj);
369    }
370
371    /**
372     * Returns a hash code for this object.
373     *
374     * @return A hash code.
375     */
376    @Override
377    public int hashCode() {
378        int result = 193;
379        result = HashUtilities.hashCode(result, this.anchor);
380        result = HashUtilities.hashCode(result, this.coordinateType);
381        result = HashUtilities.hashCode(result, this.x);
382        result = HashUtilities.hashCode(result, this.y);
383        result = HashUtilities.hashCode(result, this.maxWidth);
384        result = HashUtilities.hashCode(result, this.maxHeight);
385        result = HashUtilities.hashCode(result, this.title);
386        return result;
387    }
388
389    /**
390     * Returns a clone of the annotation.
391     *
392     * @return A clone.
393     *
394     * @throws CloneNotSupportedException  if the annotation can't be cloned.
395     */
396    @Override
397    public Object clone() throws CloneNotSupportedException {
398        return super.clone();
399    }
400
401}