001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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;
049import org.jfree.chart.util.ObjectUtils;
050
051import org.jfree.data.general.Dataset;
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     * Tests this object for equality with an arbitrary object.
141     *
142     * @param obj  the object ({@code null} permitted).
143     *
144     * @return A boolean.
145     */
146    @Override
147    public boolean equals(Object obj) {
148        if (obj == this) {
149            return true;
150        }
151        if (!(obj instanceof LegendItemEntity)) {
152            return false;
153        }
154        LegendItemEntity that = (LegendItemEntity) obj;
155        if (!ObjectUtils.equal(this.seriesKey, that.seriesKey)) {
156            return false;
157        }
158        if (this.seriesIndex != that.seriesIndex) {
159            return false;
160        }
161        if (!ObjectUtils.equal(this.dataset, that.dataset)) {
162            return false;
163        }
164        return super.equals(obj);
165    }
166
167    /**
168     * Returns a clone of the entity.
169     *
170     * @return A clone.
171     *
172     * @throws CloneNotSupportedException if there is a problem cloning the
173     *         object.
174     */
175    @Override
176    public Object clone() throws CloneNotSupportedException {
177        return super.clone();
178    }
179
180    /**
181     * Returns a string representing this object (useful for debugging
182     * purposes).
183     *
184     * @return A string (never {@code null}).
185     */
186    @Override
187    public String toString() {
188        return "LegendItemEntity: seriesKey=" + this.seriesKey
189                + ", dataset=" + this.dataset;
190    }
191
192}