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