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 * TitleEntity.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.HashUtils; 045import org.jfree.chart.title.Title; 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 Title of a chart. 052 * 053 * @since 1.0.13 054 */ 055public class TitleEntity extends ChartEntity { 056 057 /** For serialization. */ 058 private static final long serialVersionUID = -4445994133561919083L; 059 //same as for ChartEntity! 060 061 /** The Title for the entity. */ 062 private Title title; 063 064 /** 065 * Creates a new chart entity. 066 * 067 * @param area the area ({@code null} not permitted). 068 * @param title the title ({@code null} not permitted). 069 */ 070 public TitleEntity(Shape area, Title title) { 071 // defer argument checks... 072 this(area, title, null); 073 } 074 075 /** 076 * Creates a new chart entity. 077 * 078 * @param area the area ({@code null} not permitted). 079 * @param title the title ({@code null} not permitted). 080 * @param toolTipText the tool tip text ({@code null} permitted). 081 */ 082 public TitleEntity(Shape area, Title title, String toolTipText) { 083 // defer argument checks... 084 this(area, title, toolTipText, null); 085 } 086 087 /** 088 * Creates a new entity. 089 * 090 * @param area the area ({@code null} not permitted). 091 * @param title the title ({@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 TitleEntity(Shape area, Title title, String toolTipText, 097 String urlText) { 098 super(area, toolTipText, urlText); 099 Args.nullNotPermitted(title, "title"); 100 this.title = title; 101 } 102 103 /** 104 * Returns the title that occupies the entity area. 105 * 106 * @return The title (never {@code null}). 107 */ 108 public Title getTitle() { 109 return this.title; 110 } 111 112 /** 113 * Returns a string representation of the chart entity, useful for 114 * debugging. 115 * 116 * @return A string. 117 */ 118 @Override 119 public String toString() { 120 StringBuilder sb = new StringBuilder("TitleEntity: "); 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 TitleEntity)) { 139 return false; 140 } 141 TitleEntity that = (TitleEntity) 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.title.equals(that.title))) { 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 = 41; 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}