001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2007, 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     * CategoryItemEntity.java
029     * -----------------------
030     * (C) Copyright 2002-2007, 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     * $Id: CategoryItemEntity.java,v 1.5.2.2 2007/05/18 10:28:19 mungady Exp $
037     *
038     * Changes:
039     * --------
040     * 23-May-2002 : Version 1 (DG);
041     * 12-Jun-2002 : Added Javadoc comments (DG);
042     * 26-Jun-2002 : Added getImageMapAreaTag() method (DG);
043     * 05-Aug-2002 : Added new constructor to populate URLText
044     *               Moved getImageMapAreaTag() to ChartEntity (superclass) (RA);
045     * 03-Oct-2002 : Fixed errors reported by Checkstyle (DG);
046     * 30-Jul-2003 : Added CategoryDataset reference (CZ);
047     * 20-May-2004 : Added equals() and clone() methods, and implemented 
048     *               Serializable (DG);
049     * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
050     * ------------- JFREECHART 1.0.x ---------------------------------------------
051     * 18-May-2007 : Updated to use row and column keys to identify item (DG);
052     *
053     */
054    
055    package org.jfree.chart.entity;
056    
057    import java.awt.Shape;
058    import java.io.Serializable;
059    
060    import org.jfree.data.category.CategoryDataset;
061    import org.jfree.util.ObjectUtilities;
062    
063    /**
064     * A chart entity that represents one item within a category plot.
065     */
066    public 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            if (dataset == null) {
133                throw new IllegalArgumentException("Null 'dataset' argument.");
134            }
135            this.dataset = dataset;
136            this.series = series;
137            this.category = category;
138            this.categoryIndex = categoryIndex;
139            this.rowKey = dataset.getRowKey(series);
140            this.columnKey = dataset.getColumnKey(categoryIndex);
141        }
142        
143        /**
144         * Creates a new entity instance for an item in the specified dataset.
145         * 
146         * @param area  the 'hotspot' area (<code>null</code> not permitted).
147         * @param toolTipText  the tool tip text.
148         * @param urlText  the URL text.
149         * @param dataset  the dataset (<code>null</code> not permitted).
150         * @param rowKey  the row key (<code>null</code> not permitted).
151         * @param columnKey  the column key (<code>null</code> not permitted).
152         * 
153         * @since 1.0.6
154         */
155        public CategoryItemEntity(Shape area, String toolTipText, String urlText,
156                CategoryDataset dataset, Comparable rowKey, Comparable columnKey) {
157            super(area, toolTipText, urlText);
158            if (dataset == null) {
159                throw new IllegalArgumentException("Null 'dataset' argument.");
160            }
161            this.dataset = dataset;
162            this.rowKey = rowKey;
163            this.columnKey = columnKey;
164            
165            // populate the deprecated fields
166            this.series = dataset.getRowIndex(rowKey);
167            this.category = columnKey;
168            this.categoryIndex = dataset.getColumnIndex(columnKey);
169        }
170    
171        /**
172         * Returns the dataset this entity refers to.  This can be used to 
173         * differentiate between items in a chart that displays more than one
174         * dataset.
175         *
176         * @return The dataset (never <code>null</code>).
177         * 
178         * @see #setDataset(CategoryDataset)
179         */
180        public CategoryDataset getDataset() {
181            return this.dataset; 
182        }
183    
184        /**
185         * Sets the dataset this entity refers to.
186         *
187         * @param dataset  the dataset (<code>null</code> not permitted).
188         * 
189         * @see #getDataset()
190         */
191        public void setDataset(CategoryDataset dataset) {
192            if (dataset == null) {
193                throw new IllegalArgumentException("Null 'dataset' argument.");
194            }
195            this.dataset = dataset;
196        }
197        
198        /**
199         * Returns the row key.
200         * 
201         * @return The row key (never <code>null</code>).
202         * 
203         * @since 1.0.6
204         * 
205         * @see #setRowKey(Comparable)
206         */
207        public Comparable getRowKey() {
208            return this.rowKey;
209        }
210        
211        /**
212         * Sets the row key.
213         * 
214         * @param rowKey  the row key (<code>null</code> not permitted).
215         * 
216         * @since 1.0.6
217         * 
218         * @see #getRowKey()
219         */
220        public void setRowKey(Comparable rowKey) {
221            this.rowKey = rowKey;
222            // update the deprecated field
223            this.series = this.dataset.getRowIndex(rowKey);
224        }
225    
226        /**
227         * Returns the column key.
228         * 
229         * @return The column key (never <code>null</code>).
230         * 
231         * @since 1.0.6
232         * 
233         * @see #setColumnKey(Comparable)
234         */
235        public Comparable getColumnKey() {
236            return this.columnKey;
237        }
238        
239        /**
240         * Sets the column key.
241         * 
242         * @param columnKey  the column key (<code>null</code> not permitted).
243         * 
244         * @since 1.0.6
245         * 
246         * @see #getColumnKey()
247         */
248        public void setColumnKey(Comparable columnKey) {
249            this.columnKey = columnKey;
250            // update the deprecated fields
251            this.category = columnKey;
252            this.categoryIndex = this.dataset.getColumnIndex(columnKey);
253        }
254    
255        /**
256         * Returns the series index.
257         *
258         * @return The series index.
259         * 
260         * @see #setSeries(int)
261         * 
262         * @deprecated As of 1.0.6, you can derive this information from the 
263         *         {@link #getRowKey()} method.
264         */
265        public int getSeries() {
266            return this.series;
267        }
268    
269        /**
270         * Sets the series index.
271         *
272         * @param series  the series index (zero-based).
273         * 
274         * @see #getSeries()
275         * 
276         * @deprecated As of 1.0.6, you should use {@link #setRowKey(Comparable)} 
277         *         to designate the series.
278         */
279        public void setSeries(int series) {
280            this.series = series;
281        }
282    
283        /**
284         * Returns the category.
285         *
286         * @return The category (possibly <code>null</code>).
287         * 
288         * @see #setCategory(Object)
289         * 
290         * @deprecated The return type for this method should be 
291         *         <code>Comparable</code>, so it has been deprecated as of 
292         *         version 1.0.6 and replaced by {@link #getColumnKey()}.
293         */
294        public Object getCategory() {
295            return this.category;
296        }
297    
298        /**
299         * Sets the category.
300         *
301         * @param category  the category (<code>null</code> permitted).
302         * 
303         * @see #getCategory()
304         * 
305         * @deprecated As of version 1.0.6, use {@link #setColumnKey(Comparable)}.
306         */
307        public void setCategory(Object category) {
308            this.category = category;
309        }
310    
311        /**
312         * Returns the category index.
313         *
314         * @return The index.
315         * 
316         * @see #setCategoryIndex(int)
317         * 
318         * @deprecated As of 1.0.6, you can derive this information from the 
319         *         {@link #getColumnKey()} method.
320         */
321        public int getCategoryIndex() {
322            return this.categoryIndex;
323        }
324    
325        /**
326         * Sets the category index.
327         *
328         * @param index  the category index.
329         * 
330         * @see #getCategoryIndex()
331         * 
332         * @deprecated As of 1.0.6, use {@link #setColumnKey(Comparable)} to 
333         *         designate the category.
334         */
335        public void setCategoryIndex(int index) {
336            this.categoryIndex = index;
337        }
338    
339        /**
340         * Returns a string representing this object (useful for debugging 
341         * purposes).
342         *
343         * @return A string (never <code>null</code>).
344         */
345        public String toString() {
346            return "CategoryItemEntity: rowKey=" + this.rowKey 
347                   + ", columnKey=" + this.columnKey + ", dataset=" + this.dataset;
348        }
349    
350        /**
351         * Tests the entity for equality with an arbitrary object.
352         * 
353         * @param obj  the object (<code>null</code> permitted).
354         * 
355         * @return A boolean.
356         */
357        public boolean equals(Object obj) {
358            if (obj == this) {
359                return true;      
360            }
361            if (!(obj instanceof CategoryItemEntity)) {
362                return false;
363            }
364            CategoryItemEntity that = (CategoryItemEntity) obj;
365            if (!this.rowKey.equals(that.rowKey)) {
366                return false;
367            }
368            if (!this.columnKey.equals(that.columnKey)) {
369                return false;
370            }
371            if (!ObjectUtilities.equal(this.dataset, that.dataset)) {
372                return false;
373            }
374            
375            // check the deprecated fields
376            if (this.categoryIndex != that.categoryIndex) {
377                return false;   
378            }
379            if (this.series != that.series) {
380                return false;   
381            }
382            if (!ObjectUtilities.equal(this.category, that.category)) {
383                return false;   
384            }
385            return super.equals(obj);
386        }
387    
388    }