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