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     * XYDrawableAnnotation.java
029     * -------------------------
030     * (C) Copyright 2003, 2004, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * $Id: XYDrawableAnnotation.java,v 1.6.2.1 2005/10/25 16:51:15 mungady Exp $
036     *
037     * Changes:
038     * --------
039     * 21-May-2003 : Version 1 (DG);
040     * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
041     * 30-Sep-2004 : Added support for tool tips and URLs (DG);
042     *
043     */
044    
045    package org.jfree.chart.annotations;
046    
047    import java.awt.Graphics2D;
048    import java.awt.geom.Rectangle2D;
049    import java.io.Serializable;
050    
051    import org.jfree.chart.axis.ValueAxis;
052    import org.jfree.chart.plot.Plot;
053    import org.jfree.chart.plot.PlotOrientation;
054    import org.jfree.chart.plot.PlotRenderingInfo;
055    import org.jfree.chart.plot.XYPlot;
056    import org.jfree.ui.Drawable;
057    import org.jfree.ui.RectangleEdge;
058    import org.jfree.util.ObjectUtilities;
059    import org.jfree.util.PublicCloneable;
060    
061    /**
062     * A general annotation that can be placed on 
063     * an {@link org.jfree.chart.plot.XYPlot}.
064     */
065    public class XYDrawableAnnotation extends AbstractXYAnnotation
066                                      implements Cloneable, PublicCloneable, 
067                                                 Serializable {
068    
069        /** For serialization. */
070        private static final long serialVersionUID = -6540812859722691020L;
071        
072        /** The x-coordinate. */
073        private double x;
074    
075        /** The y-coordinate. */
076        private double y;
077    
078        /** The width. */
079        private double width;
080    
081        /** The height. */
082        private double height;
083    
084        /** The drawable object. */
085        private Drawable drawable;
086    
087        /**
088         * Creates a new annotation to be displayed within the given area.
089         *
090         * @param x  the x-coordinate for the area.
091         * @param y  the y-coordinate for the area.
092         * @param width  the width of the area.
093         * @param height  the height of the area.
094         * @param drawable  the drawable object (<code>null</code> not permitted).
095         */
096        public XYDrawableAnnotation(double x, double y, double width, double height,
097                                    Drawable drawable) {
098    
099            if (drawable == null) {
100                throw new IllegalArgumentException("Null 'drawable' argument.");
101            }
102            this.x = x;
103            this.y = y;
104            this.width = width;
105            this.height = height;
106            this.drawable = drawable;
107    
108        }
109    
110        /**
111         * Draws the annotation.
112         *
113         * @param g2  the graphics device.
114         * @param plot  the plot.
115         * @param dataArea  the data area.
116         * @param domainAxis  the domain axis.
117         * @param rangeAxis  the range axis.
118         * @param rendererIndex  the renderer index.
119         * @param info  if supplied, this info object will be populated with
120         *              entity information.
121         */
122        public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
123                         ValueAxis domainAxis, ValueAxis rangeAxis, 
124                         int rendererIndex,
125                         PlotRenderingInfo info) {
126    
127            PlotOrientation orientation = plot.getOrientation();
128            RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
129                plot.getDomainAxisLocation(), orientation
130            );
131            RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
132                plot.getRangeAxisLocation(), orientation
133            );
134            float j2DX = (float) domainAxis.valueToJava2D(
135                this.x, dataArea, domainEdge
136            );
137            float j2DY = (float) rangeAxis.valueToJava2D(
138                this.y, dataArea, rangeEdge
139            );
140            Rectangle2D area = new Rectangle2D.Double(
141                j2DX - this.width / 2.0, j2DY - this.height / 2.0,
142                this.width, this.height
143            );
144            this.drawable.draw(g2, area);
145            String toolTip = getToolTipText();
146            String url = getURL();
147            if (toolTip != null || url != null) {
148                addEntity(info, area, rendererIndex, toolTip, url);
149            }
150            
151        }
152    
153        /**
154         * Tests this annotation for equality with an arbitrary object.
155         * 
156         * @param obj  the object to test against.
157         * 
158         * @return <code>true</code> or <code>false</code>.
159         */
160        public boolean equals(Object obj) {
161            
162            if (obj == this) { // simple case
163                return true;
164            }      
165            // now try to reject equality...
166            if (!super.equals(obj)) {
167                return false;
168            }
169            if (!(obj instanceof XYDrawableAnnotation)) {
170                return false;
171            }
172            XYDrawableAnnotation that = (XYDrawableAnnotation) obj;
173            if (this.x != that.x) {
174                return false;
175            }
176            if (this.y != that.y) {
177                return false;
178            }
179            if (this.width != that.width) {
180                return false;
181            }
182            if (this.height != that.height) {
183                return false;
184            }
185            if (!ObjectUtilities.equal(this.drawable, that.drawable)) {
186                return false;
187            }
188            // seem to be the same... 
189            return true;
190            
191        }
192        
193        /**
194         * Returns a hash code.
195         * 
196         * @return A hash code.
197         */
198        public int hashCode() {
199            int result;
200            long temp;
201            temp = Double.doubleToLongBits(this.x);
202            result = (int) (temp ^ (temp >>> 32));
203            temp = Double.doubleToLongBits(this.y);
204            result = 29 * result + (int) (temp ^ (temp >>> 32));
205            temp = Double.doubleToLongBits(this.width);
206            result = 29 * result + (int) (temp ^ (temp >>> 32));
207            temp = Double.doubleToLongBits(this.height);
208            result = 29 * result + (int) (temp ^ (temp >>> 32));
209            return result;
210        }
211        
212        /**
213         * Returns a clone of the annotation.
214         * 
215         * @return A clone.
216         * 
217         * @throws CloneNotSupportedException  if the annotation can't be cloned.
218         */
219        public Object clone() throws CloneNotSupportedException {
220            return super.clone();
221        }
222    
223    }