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