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