001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * JFreeChartEntity.java
029 * --------------------
030 * (C) Copyright 2009-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  Peter Kolb;
033 * Contributor(s):   Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
034 *
035 */
036
037package org.jfree.chart.entity;
038
039import java.awt.Shape;
040import java.io.IOException;
041import java.io.ObjectInputStream;
042import java.io.ObjectOutputStream;
043import java.util.Objects;
044
045import org.jfree.chart.JFreeChart;
046import org.jfree.chart.util.Args;
047import org.jfree.chart.util.SerialUtils;
048
049/**
050 * A class that captures information about an entire chart.
051 */
052public class JFreeChartEntity extends ChartEntity {
053
054    /** For serialization. */
055    private static final long serialVersionUID = -4445994133561919083L;
056            //same as for ChartEntity!
057
058    /** The chart. */
059    private JFreeChart chart;
060
061    /**
062     * Creates a new chart entity.
063     *
064     * @param area  the area ({@code null} not permitted).
065     * @param chart  the chart ({@code null} not permitted).
066     */
067    public JFreeChartEntity(Shape area, JFreeChart chart) {
068        // defer argument checks...
069        this(area, chart, null);
070    }
071
072    /**
073     * Creates a new chart entity.
074     *
075     * @param area  the area ({@code null} not permitted).
076     * @param chart  the chart ({@code null} not permitted).
077     * @param toolTipText  the tool tip text ({@code null} permitted).
078     */
079    public JFreeChartEntity(Shape area, JFreeChart chart, String toolTipText) {
080        // defer argument checks...
081        this(area, chart, toolTipText, null);
082    }
083
084    /**
085     * Creates a new chart entity.
086     *
087     * @param area  the area ({@code null} not permitted).
088     * @param chart  the chart ({@code null} not permitted).
089     * @param toolTipText  the tool tip text ({@code null} permitted).
090     * @param urlText  the URL text for HTML image maps ({@code null}
091     *                 permitted).
092     */
093    public JFreeChartEntity(Shape area, JFreeChart chart, String toolTipText,
094            String urlText) {
095        super(area, toolTipText, urlText);
096        Args.nullNotPermitted(chart, "chart");
097        this.chart = chart;
098    }
099
100    /**
101     * Returns the chart that occupies the entity area.
102     *
103     * @return The chart (never {@code null}).
104     */
105    public JFreeChart getChart() {
106        return this.chart;
107    }
108
109    /**
110     * Returns a string representation of the chart entity, useful for
111     * debugging.
112     *
113     * @return A string.
114     */
115    @Override
116    public String toString() {
117        StringBuilder sb = new StringBuilder("JFreeChartEntity: ");
118        sb.append("tooltip = ");
119        sb.append(getToolTipText());
120        return sb.toString();
121    }
122
123    /**
124     * Tests the entity for equality with an arbitrary object.
125     *
126     * @param obj  the object to test against ({@code null} permitted).
127     *
128     * @return A boolean.
129     */
130    @Override
131    public boolean equals(Object obj) {
132        if (obj == this) {
133            return true;
134        }
135        if (!(obj instanceof JFreeChartEntity)) {
136            return false;
137        }
138        JFreeChartEntity that = (JFreeChartEntity) obj;
139
140        // fix the "equals not symmetric" problem
141        if (!that.canEqual(this)) {
142            return false;
143        }
144        if (!(Objects.equals(this.chart, that.chart))) {
145            return false;
146        }
147        return super.equals(obj);
148    }
149
150    /**
151     * Ensures symmetry between super/subclass implementations of equals. For
152     * more detail, see http://jqno.nl/equalsverifier/manual/inheritance.
153     *
154     * @param other Object
155     * 
156     * @return true ONLY if the parameter is THIS class type
157     */
158    @Override
159    public boolean canEqual(Object other) {
160        // Solves Problem: equals not symmetric
161        return (other instanceof JFreeChartEntity);
162    }
163
164    @Override
165    public int hashCode() {
166        int hash = super.hashCode(); // equals calls superclass function, so hashCode must also
167        hash = 59 * hash + Objects.hashCode(this.chart);
168        return hash;
169    }
170
171    /**
172     * Returns a clone of the entity.
173     *
174     * @return A clone.
175     *
176     * @throws CloneNotSupportedException if there is a problem cloning the
177     *         entity.
178     */
179    @Override
180    public Object clone() throws CloneNotSupportedException {
181        return super.clone();
182    }
183
184    /**
185     * Provides serialization support.
186     *
187     * @param stream  the output stream.
188     *
189     * @throws IOException  if there is an I/O error.
190     */
191    private void writeObject(ObjectOutputStream stream) throws IOException {
192        stream.defaultWriteObject();
193        SerialUtils.writeShape(getArea(), stream);
194     }
195
196    /**
197     * Provides serialization support.
198     *
199     * @param stream  the input stream.
200     *
201     * @throws IOException  if there is an I/O error.
202     * @throws ClassNotFoundException  if there is a classpath problem.
203     */
204    private void readObject(ObjectInputStream stream)
205        throws IOException, ClassNotFoundException {
206        stream.defaultReadObject();
207        setArea(SerialUtils.readShape(stream));
208    }
209
210}