001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2021, 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 * ChartEntity.java 029 * ---------------- 030 * (C) Copyright 2002-2021, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Richard Atkinson; 034 * Xavier Poinsard; 035 * Robert Fuller; 036 */ 037 038package org.jfree.chart.entity; 039 040import java.awt.Shape; 041import java.awt.geom.PathIterator; 042import java.awt.geom.Rectangle2D; 043import java.io.IOException; 044import java.io.ObjectInputStream; 045import java.io.ObjectOutputStream; 046import java.io.Serializable; 047import java.util.Objects; 048 049import org.jfree.chart.HashUtils; 050import org.jfree.chart.imagemap.ToolTipTagFragmentGenerator; 051import org.jfree.chart.imagemap.URLTagFragmentGenerator; 052import org.jfree.chart.util.Args; 053import org.jfree.chart.util.PublicCloneable; 054import org.jfree.chart.util.SerialUtils; 055 056/** 057 * A class that captures information about some component of a chart (a bar, 058 * line etc). 059 */ 060public class ChartEntity implements Cloneable, PublicCloneable, Serializable { 061 062 /** For serialization. */ 063 private static final long serialVersionUID = -4445994133561919083L; 064 065 /** The area occupied by the entity (in Java 2D space). */ 066 private transient Shape area; 067 068 /** The tool tip text for the entity. */ 069 private String toolTipText; 070 071 /** The URL text for the entity. */ 072 private String urlText; 073 074 /** 075 * Creates a new chart entity. 076 * 077 * @param area the area ({@code null} not permitted). 078 */ 079 public ChartEntity(Shape area) { 080 // defer argument checks... 081 this(area, null); 082 } 083 084 /** 085 * Creates a new chart entity. 086 * 087 * @param area the area ({@code null} not permitted). 088 * @param toolTipText the tool tip text ({@code null} permitted). 089 */ 090 public ChartEntity(Shape area, String toolTipText) { 091 // defer argument checks... 092 this(area, toolTipText, null); 093 } 094 095 /** 096 * Creates a new entity. 097 * 098 * @param area the area ({@code null} not permitted). 099 * @param toolTipText the tool tip text ({@code null} permitted). 100 * @param urlText the URL text for HTML image maps ({@code null} 101 * permitted). 102 */ 103 public ChartEntity(Shape area, String toolTipText, String urlText) { 104 Args.nullNotPermitted(area, "area"); 105 this.area = area; 106 this.toolTipText = toolTipText; 107 this.urlText = urlText; 108 } 109 110 /** 111 * Returns the area occupied by the entity (in Java 2D space). 112 * 113 * @return The area (never {@code null}). 114 */ 115 public Shape getArea() { 116 return this.area; 117 } 118 119 /** 120 * Sets the area for the entity. 121 * <P> 122 * This class conveys information about chart entities back to a client. 123 * Setting this area doesn't change the entity (which has already been 124 * drawn). 125 * 126 * @param area the area ({@code null} not permitted). 127 */ 128 public void setArea(Shape area) { 129 Args.nullNotPermitted(area, "area"); 130 this.area = area; 131 } 132 133 /** 134 * Returns the tool tip text for the entity. Be aware that this text 135 * may have been generated from user supplied data, so for security 136 * reasons some form of filtering should be applied before incorporating 137 * this text into any HTML output. 138 * 139 * @return The tool tip text (possibly {@code null}). 140 */ 141 public String getToolTipText() { 142 return this.toolTipText; 143 } 144 145 /** 146 * Sets the tool tip text. 147 * 148 * @param text the text ({@code null} permitted). 149 */ 150 public void setToolTipText(String text) { 151 this.toolTipText = text; 152 } 153 154 /** 155 * Returns the URL text for the entity. Be aware that this text 156 * may have been generated from user supplied data, so some form of 157 * filtering should be applied before this "URL" is used in any output. 158 * 159 * @return The URL text (possibly {@code null}). 160 */ 161 public String getURLText() { 162 return this.urlText; 163 } 164 165 /** 166 * Sets the URL text. 167 * 168 * @param text the text ({@code null} permitted). 169 */ 170 public void setURLText(String text) { 171 this.urlText = text; 172 } 173 174 /** 175 * Returns a string describing the entity area. This string is intended 176 * for use in an AREA tag when generating an image map. 177 * 178 * @return The shape type (never {@code null}). 179 */ 180 public String getShapeType() { 181 if (this.area instanceof Rectangle2D) { 182 return "rect"; 183 } 184 else { 185 return "poly"; 186 } 187 } 188 189 /** 190 * Returns the shape coordinates as a string. 191 * 192 * @return The shape coordinates (never {@code null}). 193 */ 194 public String getShapeCoords() { 195 if (this.area instanceof Rectangle2D) { 196 return getRectCoords((Rectangle2D) this.area); 197 } 198 else { 199 return getPolyCoords(this.area); 200 } 201 } 202 203 /** 204 * Returns a string containing the coordinates (x1, y1, x2, y2) for a given 205 * rectangle. This string is intended for use in an image map. 206 * 207 * @param rectangle the rectangle ({@code null} not permitted). 208 * 209 * @return Upper left and lower right corner of a rectangle. 210 */ 211 private String getRectCoords(Rectangle2D rectangle) { 212 Args.nullNotPermitted(rectangle, "rectangle"); 213 int x1 = (int) rectangle.getX(); 214 int y1 = (int) rectangle.getY(); 215 int x2 = x1 + (int) rectangle.getWidth(); 216 int y2 = y1 + (int) rectangle.getHeight(); 217 // fix by rfuller 218 if (x2 == x1) { 219 x2++; 220 } 221 if (y2 == y1) { 222 y2++; 223 } 224 // end fix by rfuller 225 return x1 + "," + y1 + "," + x2 + "," + y2; 226 } 227 228 /** 229 * Returns a string containing the coordinates for a given shape. This 230 * string is intended for use in an image map. 231 * 232 * @param shape the shape ({@code null} not permitted). 233 * 234 * @return The coordinates for a given shape as string. 235 */ 236 private String getPolyCoords(Shape shape) { 237 Args.nullNotPermitted(shape, "shape"); 238 StringBuilder result = new StringBuilder(); 239 boolean first = true; 240 float[] coords = new float[6]; 241 PathIterator pi = shape.getPathIterator(null, 1.0); 242 while (!pi.isDone()) { 243 pi.currentSegment(coords); 244 if (first) { 245 first = false; 246 result.append((int) coords[0]); 247 result.append(",").append((int) coords[1]); 248 } 249 else { 250 result.append(","); 251 result.append((int) coords[0]); 252 result.append(","); 253 result.append((int) coords[1]); 254 } 255 pi.next(); 256 } 257 return result.toString(); 258 } 259 260 /** 261 * Returns an HTML image map tag for this entity. The returned fragment 262 * should be {@code XHTML 1.0} compliant. 263 * 264 * @param toolTipTagFragmentGenerator a generator for the HTML fragment 265 * that will contain the tooltip text ({@code null} not permitted 266 * if this entity contains tooltip information). 267 * @param urlTagFragmentGenerator a generator for the HTML fragment that 268 * will contain the URL reference ({@code null} not permitted if 269 * this entity has a URL). 270 * 271 * @return The HTML tag. 272 */ 273 public String getImageMapAreaTag( 274 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, 275 URLTagFragmentGenerator urlTagFragmentGenerator) { 276 277 StringBuilder tag = new StringBuilder(); 278 boolean hasURL = (this.urlText == null ? false 279 : !this.urlText.equals("")); 280 boolean hasToolTip = (this.toolTipText == null ? false 281 : !this.toolTipText.equals("")); 282 if (hasURL || hasToolTip) { 283 tag.append("<area shape=\"").append(getShapeType()).append("\"") 284 .append(" coords=\"").append(getShapeCoords()).append("\""); 285 if (hasToolTip) { 286 tag.append(toolTipTagFragmentGenerator.generateToolTipFragment( 287 this.toolTipText)); 288 } 289 if (hasURL) { 290 tag.append(urlTagFragmentGenerator.generateURLFragment( 291 this.urlText)); 292 } 293 else { 294 tag.append(" nohref=\"nohref\""); 295 } 296 // if there is a tool tip, we expect it to generate the title and 297 // alt values, so we only add an empty alt if there is no tooltip 298 if (!hasToolTip) { 299 tag.append(" alt=\"\""); 300 } 301 tag.append("/>"); 302 } 303 return tag.toString(); 304 } 305 306 /** 307 * Returns a string representation of the chart entity, useful for 308 * debugging. 309 * 310 * @return A string. 311 */ 312 @Override 313 public String toString() { 314 StringBuilder sb = new StringBuilder("ChartEntity: "); 315 sb.append("tooltip = "); 316 sb.append(this.toolTipText); 317 return sb.toString(); 318 } 319 320 /** 321 * Tests the entity for equality with an arbitrary object. 322 * 323 * @param obj the object to test against ({@code null} permitted). 324 * 325 * @return A boolean. 326 */ 327 @Override 328 public boolean equals(Object obj) { 329 if (obj == this) { 330 return true; 331 } 332 if (!(obj instanceof ChartEntity)) { 333 return false; 334 } 335 ChartEntity that = (ChartEntity) obj; 336 if (!this.area.equals(that.area)) { 337 return false; 338 } 339 if (!Objects.equals(this.toolTipText, that.toolTipText)) { 340 return false; 341 } 342 if (!Objects.equals(this.urlText, that.urlText)) { 343 return false; 344 } 345 return true; 346 } 347 348 /** 349 * Returns a hash code for this instance. 350 * 351 * @return A hash code. 352 */ 353 @Override 354 public int hashCode() { 355 int result = 37; 356 result = HashUtils.hashCode(result, this.toolTipText); 357 result = HashUtils.hashCode(result, this.urlText); 358 return result; 359 } 360 361 /** 362 * Returns a clone of the entity. 363 * 364 * @return A clone. 365 * 366 * @throws CloneNotSupportedException if there is a problem cloning the 367 * entity. 368 */ 369 @Override 370 public Object clone() throws CloneNotSupportedException { 371 return super.clone(); 372 } 373 374 /** 375 * Provides serialization support. 376 * 377 * @param stream the output stream. 378 * 379 * @throws IOException if there is an I/O error. 380 */ 381 private void writeObject(ObjectOutputStream stream) throws IOException { 382 stream.defaultWriteObject(); 383 SerialUtils.writeShape(this.area, stream); 384 } 385 386 /** 387 * Provides serialization support. 388 * 389 * @param stream the input stream. 390 * 391 * @throws IOException if there is an I/O error. 392 * @throws ClassNotFoundException if there is a classpath problem. 393 */ 394 private void readObject(ObjectInputStream stream) 395 throws IOException, ClassNotFoundException { 396 stream.defaultReadObject(); 397 this.area = SerialUtils.readShape(stream); 398 } 399 400}