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 * LegendItemEntity.java
029 * ---------------------
030 * (C) Copyright 2003-2008, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 05-Jun-2003 : Version 1 (DG);
038 * 20-May-2004 : Added equals() method and implemented Cloneable and
039 *               Serializable (DG);
040 * ------------- JFREECHART 1.0.x ---------------------------------------------
041 * 18-May-2007 : Added dataset and seriesKey fields (DG);
042 *
043 */
044
045package org.jfree.chart.entity;
046
047import java.awt.Shape;
048import java.io.Serializable;
049
050import org.jfree.data.general.Dataset;
051import org.jfree.util.ObjectUtilities;
052
053/**
054 * An entity that represents an item within a legend.
055 */
056public class LegendItemEntity extends ChartEntity
057                              implements Cloneable, Serializable {
058
059    /** For serialization. */
060    private static final long serialVersionUID = -7435683933545666702L;
061
062    /**
063     * The dataset.
064     *
065     * @since 1.0.6
066     */
067    private Dataset dataset;
068
069    /**
070     * The series key.
071     *
072     * @since 1.0.6
073     */
074    private Comparable seriesKey;
075
076    /** The series index. */
077    private int seriesIndex;
078
079    /**
080     * Creates a legend item entity.
081     *
082     * @param area  the area.
083     */
084    public LegendItemEntity(Shape area) {
085        super(area);
086    }
087
088    /**
089     * Returns a reference to the dataset that this legend item is derived
090     * from.
091     *
092     * @return The dataset.
093     *
094     * @since 1.0.6
095     *
096     * @see #setDataset(Dataset)
097     */
098    public Dataset getDataset() {
099        return this.dataset;
100    }
101
102    /**
103     * Sets a reference to the dataset that this legend item is derived from.
104     *
105     * @param dataset  the dataset.
106     *
107     * @since 1.0.6
108     */
109    public void setDataset(Dataset dataset) {
110        this.dataset = dataset;
111    }
112
113    /**
114     * Returns the series key that identifies the legend item.
115     *
116     * @return The series key.
117     *
118     * @since 1.0.6
119     *
120     * @see #setSeriesKey(Comparable)
121     */
122    public Comparable getSeriesKey() {
123        return this.seriesKey;
124    }
125
126    /**
127     * Sets the key for the series.
128     *
129     * @param key  the key.
130     *
131     * @since 1.0.6
132     *
133     * @see #getSeriesKey()
134     */
135    public void setSeriesKey(Comparable key) {
136        this.seriesKey = key;
137    }
138
139    /**
140     * Returns the series index.
141     *
142     * @return The series index.
143     *
144     * @see #setSeriesIndex(int)
145     *
146     * @deprecated As of 1.0.6, use the {@link #getSeriesKey()} method.
147     */
148    public int getSeriesIndex() {
149        return this.seriesIndex;
150    }
151
152    /**
153     * Sets the series index.
154     *
155     * @param index  the series index.
156     *
157     * @see #getSeriesIndex()
158     *
159     * @deprecated As of 1.0.6, use the {@link #setSeriesKey(Comparable)}
160     *         method.
161     */
162    public void setSeriesIndex(int index) {
163        this.seriesIndex = index;
164    }
165
166    /**
167     * Tests this object for equality with an arbitrary object.
168     *
169     * @param obj  the object (<code>null</code> permitted).
170     *
171     * @return A boolean.
172     */
173    @Override
174    public boolean equals(Object obj) {
175        if (obj == this) {
176            return true;
177        }
178        if (!(obj instanceof LegendItemEntity)) {
179            return false;
180        }
181        LegendItemEntity that = (LegendItemEntity) obj;
182        if (!ObjectUtilities.equal(this.seriesKey, that.seriesKey)) {
183            return false;
184        }
185        if (this.seriesIndex != that.seriesIndex) {
186            return false;
187        }
188        if (!ObjectUtilities.equal(this.dataset, that.dataset)) {
189            return false;
190        }
191        return super.equals(obj);
192    }
193
194    /**
195     * Returns a clone of the entity.
196     *
197     * @return A clone.
198     *
199     * @throws CloneNotSupportedException if there is a problem cloning the
200     *         object.
201     */
202    @Override
203    public Object clone() throws CloneNotSupportedException {
204        return super.clone();
205    }
206
207    /**
208     * Returns a string representing this object (useful for debugging
209     * purposes).
210     *
211     * @return A string (never <code>null</code>).
212     */
213    @Override
214    public String toString() {
215        return "LegendItemEntity: seriesKey=" + this.seriesKey
216                + ", dataset=" + this.dataset;
217    }
218
219}