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 * CategoryTextAnnotation.java
029 * ---------------------------
030 * (C) Copyright 2003-2013, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   Peter Kolb (patch 2809117);
034 *
035 * Changes:
036 * --------
037 * 02-Apr-2003 : Version 1 (DG);
038 * 02-Jul-2003 : Added new text alignment and rotation options (DG);
039 * 04-Jul-2003 : Added a category anchor option (DG);
040 * 19-Aug-2003 : Added equals() method and implemented Cloneable (DG);
041 * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
042 * 30-Sep-2004 : Moved drawRotatedString() from RefineryUtilities
043 *               --> TextUtilities (DG);
044 * ------------- JFREECHART 1.0.x -------------------------------------------
045 * 06-Mar-2007 : Implemented hashCode() (DG);
046 * 23-Apr-2008 : Implemented PublicCloneable (DG);
047 * 24-Jun-2009 : Fire change events (see patch 2809117 by PK) (DG);
048 * 02-Jul-2013 : Use ParamChecks (DG);
049 *
050 */
051
052package org.jfree.chart.annotations;
053
054import java.awt.Graphics2D;
055import java.awt.geom.Rectangle2D;
056import java.io.Serializable;
057
058import org.jfree.chart.axis.CategoryAnchor;
059import org.jfree.chart.axis.CategoryAxis;
060import org.jfree.chart.axis.ValueAxis;
061import org.jfree.chart.event.AnnotationChangeEvent;
062import org.jfree.chart.plot.CategoryPlot;
063import org.jfree.chart.plot.Plot;
064import org.jfree.chart.plot.PlotOrientation;
065import org.jfree.chart.util.ParamChecks;
066import org.jfree.data.category.CategoryDataset;
067import org.jfree.text.TextUtilities;
068import org.jfree.ui.RectangleEdge;
069import org.jfree.util.PublicCloneable;
070
071/**
072 * A text annotation that can be placed on a {@link CategoryPlot}.
073 */
074public class CategoryTextAnnotation extends TextAnnotation
075        implements CategoryAnnotation, Cloneable, PublicCloneable,
076                   Serializable {
077
078    /** For serialization. */
079    private static final long serialVersionUID = 3333360090781320147L;
080
081    /** The category. */
082    private Comparable category;
083
084    /** The category anchor (START, MIDDLE, or END). */
085    private CategoryAnchor categoryAnchor;
086
087    /** The value. */
088    private double value;
089
090    /**
091     * Creates a new annotation to be displayed at the given location.
092     *
093     * @param text  the text (<code>null</code> not permitted).
094     * @param category  the category (<code>null</code> not permitted).
095     * @param value  the value.
096     */
097    public CategoryTextAnnotation(String text, Comparable category,
098                                  double value) {
099        super(text);
100        ParamChecks.nullNotPermitted(category, "category");
101        this.category = category;
102        this.value = value;
103        this.categoryAnchor = CategoryAnchor.MIDDLE;
104    }
105
106    /**
107     * Returns the category.
108     *
109     * @return The category (never <code>null</code>).
110     *
111     * @see #setCategory(Comparable)
112     */
113    public Comparable getCategory() {
114        return this.category;
115    }
116
117    /**
118     * Sets the category that the annotation attaches to and sends an
119     * {@link AnnotationChangeEvent} to all registered listeners.
120     *
121     * @param category  the category (<code>null</code> not permitted).
122     *
123     * @see #getCategory()
124     */
125    public void setCategory(Comparable category) {
126        ParamChecks.nullNotPermitted(category, "category");
127        this.category = category;
128        fireAnnotationChanged();
129    }
130
131    /**
132     * Returns the category anchor point.
133     *
134     * @return The category anchor point.
135     *
136     * @see #setCategoryAnchor(CategoryAnchor)
137     */
138    public CategoryAnchor getCategoryAnchor() {
139        return this.categoryAnchor;
140    }
141
142    /**
143     * Sets the category anchor point and sends an
144     * {@link AnnotationChangeEvent} to all registered listeners.
145     *
146     * @param anchor  the anchor point (<code>null</code> not permitted).
147     *
148     * @see #getCategoryAnchor()
149     */
150    public void setCategoryAnchor(CategoryAnchor anchor) {
151        ParamChecks.nullNotPermitted(anchor, "anchor");
152        this.categoryAnchor = anchor;
153        fireAnnotationChanged();
154    }
155
156    /**
157     * Returns the value that the annotation attaches to.
158     *
159     * @return The value.
160     *
161     * @see #setValue(double)
162     */
163    public double getValue() {
164        return this.value;
165    }
166
167    /**
168     * Sets the value and sends an
169     * {@link AnnotationChangeEvent} to all registered listeners.
170     *
171     * @param value  the value.
172     *
173     * @see #getValue()
174     */
175    public void setValue(double value) {
176        this.value = value;
177        fireAnnotationChanged();
178    }
179
180    /**
181     * Draws the annotation.
182     *
183     * @param g2  the graphics device.
184     * @param plot  the plot.
185     * @param dataArea  the data area.
186     * @param domainAxis  the domain axis.
187     * @param rangeAxis  the range axis.
188     */
189    @Override
190    public void draw(Graphics2D g2, CategoryPlot plot, Rectangle2D dataArea,
191            CategoryAxis domainAxis, ValueAxis rangeAxis) {
192
193        CategoryDataset dataset = plot.getDataset();
194        int catIndex = dataset.getColumnIndex(this.category);
195        int catCount = dataset.getColumnCount();
196
197        float anchorX = 0.0f;
198        float anchorY = 0.0f;
199        PlotOrientation orientation = plot.getOrientation();
200        RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(
201                plot.getDomainAxisLocation(), orientation);
202        RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(
203                plot.getRangeAxisLocation(), orientation);
204
205        if (orientation == PlotOrientation.HORIZONTAL) {
206            anchorY = (float) domainAxis.getCategoryJava2DCoordinate(
207                    this.categoryAnchor, catIndex, catCount, dataArea,
208                    domainEdge);
209            anchorX = (float) rangeAxis.valueToJava2D(this.value, dataArea,
210                    rangeEdge);
211        }
212        else if (orientation == PlotOrientation.VERTICAL) {
213            anchorX = (float) domainAxis.getCategoryJava2DCoordinate(
214                    this.categoryAnchor, catIndex, catCount, dataArea,
215                    domainEdge);
216            anchorY = (float) rangeAxis.valueToJava2D(this.value, dataArea,
217                    rangeEdge);
218        }
219        g2.setFont(getFont());
220        g2.setPaint(getPaint());
221        TextUtilities.drawRotatedString(getText(), g2, anchorX, anchorY,
222                getTextAnchor(), getRotationAngle(), getRotationAnchor());
223
224    }
225
226    /**
227     * Tests this object for equality with another.
228     *
229     * @param obj  the object (<code>null</code> permitted).
230     *
231     * @return <code>true</code> or <code>false</code>.
232     */
233    @Override
234    public boolean equals(Object obj) {
235        if (obj == this) {
236            return true;
237        }
238        if (!(obj instanceof CategoryTextAnnotation)) {
239            return false;
240        }
241        CategoryTextAnnotation that = (CategoryTextAnnotation) obj;
242        if (!super.equals(obj)) {
243            return false;
244        }
245        if (!this.category.equals(that.getCategory())) {
246            return false;
247        }
248        if (!this.categoryAnchor.equals(that.getCategoryAnchor())) {
249            return false;
250        }
251        if (this.value != that.getValue()) {
252            return false;
253        }
254        return true;
255    }
256
257    /**
258     * Returns a hash code for this instance.
259     *
260     * @return A hash code.
261     */
262    @Override
263    public int hashCode() {
264        int result = super.hashCode();
265        result = 37 * result + this.category.hashCode();
266        result = 37 * result + this.categoryAnchor.hashCode();
267        long temp = Double.doubleToLongBits(this.value);
268        result = 37 * result + (int) (temp ^ (temp >>> 32));
269        return result;
270    }
271
272    /**
273     * Returns a clone of the annotation.
274     *
275     * @return A clone.
276     *
277     * @throws CloneNotSupportedException  this class will not throw this
278     *         exception, but subclasses (if any) might.
279     */
280    @Override
281    public Object clone() throws CloneNotSupportedException {
282        return super.clone();
283    }
284
285}