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 * PlotEntity.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;
043
044import org.jfree.chart.plot.Plot;
045import org.jfree.chart.HashUtils;
046import org.jfree.chart.util.Args;
047import org.jfree.chart.util.SerialUtils;
048
049/**
050 * A class that captures information about a plot.
051 */
052public class PlotEntity extends ChartEntity {
053
054    /** For serialization. */
055    private static final long serialVersionUID = -4445994133561919083L;
056            //same as for ChartEntity!
057
058    /** The plot. */
059    private Plot plot;
060
061    /**
062     * Creates a new plot entity.
063     *
064     * @param area  the area ({@code null} not permitted).
065     * @param plot  the plot ({@code null} not permitted).
066     */
067    public PlotEntity(Shape area, Plot plot) {
068        // defer argument checks...
069        this(area, plot, null);
070    }
071
072    /**
073     * Creates a new plot entity.
074     *
075     * @param area  the area ({@code null} not permitted).
076     * @param plot  the plot ({@code null} not permitted).
077     * @param toolTipText  the tool tip text ({@code null} permitted).
078     */
079    public PlotEntity(Shape area, Plot plot, String toolTipText) {
080        // defer argument checks...
081        this(area, plot, toolTipText, null);
082    }
083
084    /**
085     * Creates a new plot entity.
086     *
087     * @param area  the area ({@code null} not permitted).
088     * @param plot  the plot ({@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 PlotEntity(Shape area, Plot plot, String toolTipText,
094            String urlText) {
095        super(area, toolTipText, urlText);
096        Args.nullNotPermitted(plot, "plot");
097        this.plot = plot;
098    }
099
100    /**
101     * Returns the plot that occupies the entity area.
102     *
103     * @return The plot (never {@code null}).
104     */
105    public Plot getPlot() {
106        return this.plot;
107    }
108
109    /**
110     * Returns a string representation of the plot entity, useful for
111     * debugging.
112     *
113     * @return A string.
114     */
115    @Override
116    public String toString() {
117        StringBuilder sb = new StringBuilder("PlotEntity: ");
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 PlotEntity)) {
136            return false;
137        }
138        PlotEntity that = (PlotEntity) obj;
139
140        // fix the "equals not symmetric" problem
141        if (!that.canEqual(this)) {
142            return false;
143        }
144        if (!(this.plot.equals(that.plot))) {
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 PlotEntity);
162    }
163
164    /**
165     * Returns a hash code for this instance.
166     *
167     * @return A hash code.
168     */
169    @Override
170    public int hashCode() {
171        int result = super.hashCode(); // equals calls superclass function, so hashCode must also
172        result = HashUtils.hashCode(result, getToolTipText());
173        result = HashUtils.hashCode(result, getURLText());
174        return result;
175    }
176
177    /**
178     * Returns a clone of the entity.
179     *
180     * @return A clone.
181     *
182     * @throws CloneNotSupportedException if there is a problem cloning the
183     *         entity.
184     */
185    @Override
186    public Object clone() throws CloneNotSupportedException {
187        return super.clone();
188    }
189
190    /**
191     * Provides serialization support.
192     *
193     * @param stream  the output stream.
194     *
195     * @throws IOException  if there is an I/O error.
196     */
197    private void writeObject(ObjectOutputStream stream) throws IOException {
198        stream.defaultWriteObject();
199        SerialUtils.writeShape(getArea(), stream);
200     }
201
202    /**
203     * Provides serialization support.
204     *
205     * @param stream  the input stream.
206     *
207     * @throws IOException  if there is an I/O error.
208     * @throws ClassNotFoundException  if there is a classpath problem.
209     */
210    private void readObject(ObjectInputStream stream)
211            throws IOException, ClassNotFoundException {
212        stream.defaultReadObject();
213        setArea(SerialUtils.readShape(stream));
214    }
215
216}