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 * CategoryItemEntity.java
029 * -----------------------
030 * (C) Copyright 2002-2013, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   Richard Atkinson;
034 *                   Christian W. Zuckschwerdt;
035 *
036 * Changes:
037 * --------
038 * 23-May-2002 : Version 1 (DG);
039 * 12-Jun-2002 : Added Javadoc comments (DG);
040 * 26-Jun-2002 : Added getImageMapAreaTag() method (DG);
041 * 05-Aug-2002 : Added new constructor to populate URLText
042 *               Moved getImageMapAreaTag() to ChartEntity (superclass) (RA);
043 * 03-Oct-2002 : Fixed errors reported by Checkstyle (DG);
044 * 30-Jul-2003 : Added CategoryDataset reference (CZ);
045 * 20-May-2004 : Added equals() and clone() methods, and implemented
046 *               Serializable (DG);
047 * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
048 * ------------- JFREECHART 1.0.x ---------------------------------------------
049 * 18-May-2007 : Updated to use row and column keys to identify item (DG);
050 * 02-Jul-2013 : Use ParamChecks (DG);
051 *
052 */
053
054package org.jfree.chart.entity;
055
056import java.awt.Shape;
057import java.io.Serializable;
058import org.jfree.chart.util.ParamChecks;
059
060import org.jfree.data.category.CategoryDataset;
061import org.jfree.util.ObjectUtilities;
062
063/**
064 * A chart entity that represents one item within a category plot.
065 */
066public class CategoryItemEntity extends ChartEntity
067        implements Cloneable, Serializable {
068
069    /** For serialization. */
070    private static final long serialVersionUID = -8657249457902337349L;
071
072    /** The dataset. */
073    private CategoryDataset dataset;
074
075    /**
076     * The series (zero-based index).
077     *
078     * @deprecated As of 1.0.6, this field is redundant as you can derive the
079     *         index from the <code>rowKey</code> field.
080     */
081    private int series;
082
083    /**
084     * The category.
085     *
086     * @deprecated As of 1.0.6, this field is deprecated in favour of the
087     *         <code>columnKey</code> field.
088     */
089    private Object category;
090
091    /**
092     * The category index.
093     *
094     * @deprecated As of 1.0.6, this field is redundant as you can derive the
095     *         index from the <code>columnKey</code> field.
096     */
097    private int categoryIndex;
098
099    /**
100     * The row key.
101     *
102     * @since 1.0.6
103     */
104    private Comparable rowKey;
105
106    /**
107     * The column key.
108     *
109     * @since 1.0.6
110     */
111    private Comparable columnKey;
112
113    /**
114     * Creates a new category item entity.
115     *
116     * @param area  the area (<code>null</code> not permitted).
117     * @param toolTipText  the tool tip text.
118     * @param urlText  the URL text for HTML image maps.
119     * @param dataset  the dataset.
120     * @param series  the series (zero-based index).
121     * @param category  the category.
122     * @param categoryIndex  the category index.
123     *
124     * @deprecated As of 1.0.6, use {@link #CategoryItemEntity(Shape, String,
125     *         String, CategoryDataset, Comparable, Comparable)}.
126     */
127    public CategoryItemEntity(Shape area, String toolTipText, String urlText,
128                              CategoryDataset dataset,
129                              int series, Object category, int categoryIndex) {
130
131        super(area, toolTipText, urlText);
132        ParamChecks.nullNotPermitted(dataset, "dataset");
133        this.dataset = dataset;
134        this.series = series;
135        this.category = category;
136        this.categoryIndex = categoryIndex;
137        this.rowKey = dataset.getRowKey(series);
138        this.columnKey = dataset.getColumnKey(categoryIndex);
139    }
140
141    /**
142     * Creates a new entity instance for an item in the specified dataset.
143     *
144     * @param area  the 'hotspot' area (<code>null</code> not permitted).
145     * @param toolTipText  the tool tip text.
146     * @param urlText  the URL text.
147     * @param dataset  the dataset (<code>null</code> not permitted).
148     * @param rowKey  the row key (<code>null</code> not permitted).
149     * @param columnKey  the column key (<code>null</code> not permitted).
150     *
151     * @since 1.0.6
152     */
153    public CategoryItemEntity(Shape area, String toolTipText, String urlText,
154            CategoryDataset dataset, Comparable rowKey, Comparable columnKey) {
155        super(area, toolTipText, urlText);
156        ParamChecks.nullNotPermitted(dataset, "dataset");
157        this.dataset = dataset;
158        this.rowKey = rowKey;
159        this.columnKey = columnKey;
160
161        // populate the deprecated fields
162        this.series = dataset.getRowIndex(rowKey);
163        this.category = columnKey;
164        this.categoryIndex = dataset.getColumnIndex(columnKey);
165    }
166
167    /**
168     * Returns the dataset this entity refers to.  This can be used to
169     * differentiate between items in a chart that displays more than one
170     * dataset.
171     *
172     * @return The dataset (never <code>null</code>).
173     *
174     * @see #setDataset(CategoryDataset)
175     */
176    public CategoryDataset getDataset() {
177        return this.dataset;
178    }
179
180    /**
181     * Sets the dataset this entity refers to.
182     *
183     * @param dataset  the dataset (<code>null</code> not permitted).
184     *
185     * @see #getDataset()
186     */
187    public void setDataset(CategoryDataset dataset) {
188        ParamChecks.nullNotPermitted(dataset, "dataset");
189        this.dataset = dataset;
190    }
191
192    /**
193     * Returns the row key.
194     *
195     * @return The row key (never <code>null</code>).
196     *
197     * @since 1.0.6
198     *
199     * @see #setRowKey(Comparable)
200     */
201    public Comparable getRowKey() {
202        return this.rowKey;
203    }
204
205    /**
206     * Sets the row key.
207     *
208     * @param rowKey  the row key (<code>null</code> not permitted).
209     *
210     * @since 1.0.6
211     *
212     * @see #getRowKey()
213     */
214    public void setRowKey(Comparable rowKey) {
215        this.rowKey = rowKey;
216        // update the deprecated field
217        this.series = this.dataset.getRowIndex(rowKey);
218    }
219
220    /**
221     * Returns the column key.
222     *
223     * @return The column key (never <code>null</code>).
224     *
225     * @since 1.0.6
226     *
227     * @see #setColumnKey(Comparable)
228     */
229    public Comparable getColumnKey() {
230        return this.columnKey;
231    }
232
233    /**
234     * Sets the column key.
235     *
236     * @param columnKey  the column key (<code>null</code> not permitted).
237     *
238     * @since 1.0.6
239     *
240     * @see #getColumnKey()
241     */
242    public void setColumnKey(Comparable columnKey) {
243        this.columnKey = columnKey;
244        // update the deprecated fields
245        this.category = columnKey;
246        this.categoryIndex = this.dataset.getColumnIndex(columnKey);
247    }
248
249    /**
250     * Returns the series index.
251     *
252     * @return The series index.
253     *
254     * @see #setSeries(int)
255     *
256     * @deprecated As of 1.0.6, you can derive this information from the
257     *         {@link #getRowKey()} method.
258     */
259    public int getSeries() {
260        return this.series;
261    }
262
263    /**
264     * Sets the series index.
265     *
266     * @param series  the series index (zero-based).
267     *
268     * @see #getSeries()
269     *
270     * @deprecated As of 1.0.6, you should use {@link #setRowKey(Comparable)}
271     *         to designate the series.
272     */
273    public void setSeries(int series) {
274        this.series = series;
275    }
276
277    /**
278     * Returns the category.
279     *
280     * @return The category (possibly <code>null</code>).
281     *
282     * @see #setCategory(Object)
283     *
284     * @deprecated The return type for this method should be
285     *         <code>Comparable</code>, so it has been deprecated as of
286     *         version 1.0.6 and replaced by {@link #getColumnKey()}.
287     */
288    public Object getCategory() {
289        return this.category;
290    }
291
292    /**
293     * Sets the category.
294     *
295     * @param category  the category (<code>null</code> permitted).
296     *
297     * @see #getCategory()
298     *
299     * @deprecated As of version 1.0.6, use {@link #setColumnKey(Comparable)}.
300     */
301    public void setCategory(Object category) {
302        this.category = category;
303    }
304
305    /**
306     * Returns the category index.
307     *
308     * @return The index.
309     *
310     * @see #setCategoryIndex(int)
311     *
312     * @deprecated As of 1.0.6, you can derive this information from the
313     *         {@link #getColumnKey()} method.
314     */
315    public int getCategoryIndex() {
316        return this.categoryIndex;
317    }
318
319    /**
320     * Sets the category index.
321     *
322     * @param index  the category index.
323     *
324     * @see #getCategoryIndex()
325     *
326     * @deprecated As of 1.0.6, use {@link #setColumnKey(Comparable)} to
327     *         designate the category.
328     */
329    public void setCategoryIndex(int index) {
330        this.categoryIndex = index;
331    }
332
333    /**
334     * Returns a string representing this object (useful for debugging
335     * purposes).
336     *
337     * @return A string (never <code>null</code>).
338     */
339    @Override
340    public String toString() {
341        return "CategoryItemEntity: rowKey=" + this.rowKey
342               + ", columnKey=" + this.columnKey + ", dataset=" + this.dataset;
343    }
344
345    /**
346     * Tests the entity for equality with an arbitrary object.
347     *
348     * @param obj  the object (<code>null</code> permitted).
349     *
350     * @return A boolean.
351     */
352    @Override
353    public boolean equals(Object obj) {
354        if (obj == this) {
355            return true;
356        }
357        if (!(obj instanceof CategoryItemEntity)) {
358            return false;
359        }
360        CategoryItemEntity that = (CategoryItemEntity) obj;
361        if (!this.rowKey.equals(that.rowKey)) {
362            return false;
363        }
364        if (!this.columnKey.equals(that.columnKey)) {
365            return false;
366        }
367        if (!ObjectUtilities.equal(this.dataset, that.dataset)) {
368            return false;
369        }
370
371        // check the deprecated fields
372        if (this.categoryIndex != that.categoryIndex) {
373            return false;
374        }
375        if (this.series != that.series) {
376            return false;
377        }
378        if (!ObjectUtilities.equal(this.category, that.category)) {
379            return false;
380        }
381        return super.equals(obj);
382    }
383
384}