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 * TitleEntity.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.HashUtils;
046import org.jfree.chart.title.Title;
047import org.jfree.chart.util.Args;
048import org.jfree.chart.util.SerialUtils;
049
050/**
051 * A class that captures information about a Title of a chart.
052 */
053public class TitleEntity extends ChartEntity {
054
055    /** For serialization. */
056    private static final long serialVersionUID = -4445994133561919083L;
057            //same as for ChartEntity!
058
059    /** The Title for the entity. */
060    private Title title;
061
062    /**
063     * Creates a new chart entity.
064     *
065     * @param area  the area ({@code null} not permitted).
066     * @param title  the title ({@code null} not permitted).
067     */
068    public TitleEntity(Shape area, Title title) {
069        // defer argument checks...
070        this(area, title, null);
071    }
072
073    /**
074     * Creates a new chart entity.
075     *
076     * @param area  the area ({@code null} not permitted).
077     * @param title  the title ({@code null} not permitted).
078     * @param toolTipText  the tool tip text ({@code null} permitted).
079     */
080    public TitleEntity(Shape area, Title title, String toolTipText) {
081        // defer argument checks...
082        this(area, title, toolTipText, null);
083    }
084
085    /**
086     * Creates a new entity.
087     *
088     * @param area  the area ({@code null} not permitted).
089     * @param title  the title ({@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 TitleEntity(Shape area, Title title, String toolTipText,
095            String urlText) {
096        super(area, toolTipText, urlText);
097        Args.nullNotPermitted(title, "title");
098        this.title = title;
099    }
100
101    /**
102     * Returns the title that occupies the entity area.
103     *
104     * @return The title (never {@code null}).
105     */
106    public Title getTitle() {
107        return this.title;
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("TitleEntity: ");
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 TitleEntity)) {
137            return false;
138        }
139        TitleEntity that = (TitleEntity) obj;
140
141        // fix the "equals not symmetric" problem
142        if (!that.canEqual(this)) {
143            return false;
144        }
145        if (!getArea().equals(that.getArea())) {
146            return false;
147        }
148        if (!Objects.equals(getToolTipText(), that.getToolTipText())) {
149            return false;
150        }
151        if (!Objects.equals(getURLText(), that.getURLText())) {
152            return false;
153        }
154        if (!(Objects.equals(this.title, that.title))) {
155            return false;
156        }
157        return super.equals(obj);
158    }
159
160    /**
161     * Ensures symmetry between super/subclass implementations of equals. For
162     * more detail, see http://jqno.nl/equalsverifier/manual/inheritance.
163     *
164     * @param other Object
165     * 
166     * @return true ONLY if the parameter is THIS class type
167     */
168    @Override
169    public boolean canEqual(Object other) {
170        // Solves Problem: equals not symmetric
171        return (other instanceof TitleEntity);
172    }
173
174    /**
175     * Returns a hash code for this instance.
176     *
177     * @return A hash code.
178     */
179    @Override
180    public int hashCode() {
181        int result = super.hashCode(); // equals calls superclass function, so hashCode must also
182        result = HashUtils.hashCode(result, getToolTipText());
183        result = HashUtils.hashCode(result, getURLText());
184        return result;
185    }
186
187    /**
188     * Returns a clone of the entity.
189     *
190     * @return A clone.
191     *
192     * @throws CloneNotSupportedException if there is a problem cloning the
193     *         entity.
194     */
195    @Override
196    public Object clone() throws CloneNotSupportedException {
197        return super.clone();
198    }
199
200    /**
201     * Provides serialization support.
202     *
203     * @param stream  the output stream.
204     *
205     * @throws IOException  if there is an I/O error.
206     */
207    private void writeObject(ObjectOutputStream stream) throws IOException {
208        stream.defaultWriteObject();
209        SerialUtils.writeShape(getArea(), stream);
210     }
211
212    /**
213     * Provides serialization support.
214     *
215     * @param stream  the input stream.
216     *
217     * @throws IOException  if there is an I/O error.
218     * @throws ClassNotFoundException  if there is a classpath problem.
219     */
220    private void readObject(ObjectInputStream stream)
221            throws IOException, ClassNotFoundException {
222        stream.defaultReadObject();
223        setArea(SerialUtils.readShape(stream));
224    }
225
226}