1 65 66 package org.jfree.chart.entity; 67 68 import java.awt.Shape ; 69 import java.awt.geom.PathIterator ; 70 import java.awt.geom.Rectangle2D ; 71 import java.io.IOException ; 72 import java.io.ObjectInputStream ; 73 import java.io.ObjectOutputStream ; 74 import java.io.Serializable ; 75 76 import org.jfree.chart.imagemap.ToolTipTagFragmentGenerator; 77 import org.jfree.chart.imagemap.URLTagFragmentGenerator; 78 import org.jfree.io.SerialUtilities; 79 import org.jfree.util.ObjectUtilities; 80 import org.jfree.util.PublicCloneable; 81 82 86 public class ChartEntity implements Cloneable , PublicCloneable, Serializable { 87 88 89 private static final long serialVersionUID = -4445994133561919083L; 90 91 92 private transient Shape area; 93 94 95 private String toolTipText; 96 97 98 private String urlText; 99 100 105 public ChartEntity(Shape area) { 106 this(area, null); 108 } 109 110 116 public ChartEntity(Shape area, String toolTipText) { 117 this(area, toolTipText, null); 119 } 120 121 129 public ChartEntity(Shape area, String toolTipText, String urlText) { 130 if (area == null) { 131 throw new IllegalArgumentException ("Null 'area' argument."); 132 } 133 this.area = area; 134 this.toolTipText = toolTipText; 135 this.urlText = urlText; 136 } 137 138 143 public Shape getArea() { 144 return this.area; 145 } 146 147 156 public void setArea(Shape area) { 157 if (area == null) { 158 throw new IllegalArgumentException ("Null 'area' argument."); 159 } 160 this.area = area; 161 } 162 163 168 public String getToolTipText() { 169 return this.toolTipText; 170 } 171 172 177 public void setToolTipText(String text) { 178 this.toolTipText = text; 179 } 180 181 186 public String getURLText() { 187 return this.urlText; 188 } 189 190 195 public void setURLText(String text) { 196 this.urlText = text; 197 } 198 199 205 public String getShapeType() { 206 if (this.area instanceof Rectangle2D ) { 207 return "rect"; 208 } 209 else { 210 return "poly"; 211 } 212 } 213 214 219 public String getShapeCoords() { 220 if (this.area instanceof Rectangle2D ) { 221 return getRectCoords((Rectangle2D ) this.area); 222 } 223 else { 224 return getPolyCoords(this.area); 225 } 226 } 227 228 236 private String getRectCoords(Rectangle2D rectangle) { 237 if (rectangle == null) { 238 throw new IllegalArgumentException ("Null 'rectangle' argument."); 239 } 240 int x1 = (int) rectangle.getX(); 241 int y1 = (int) rectangle.getY(); 242 int x2 = x1 + (int) rectangle.getWidth(); 243 int y2 = y1 + (int) rectangle.getHeight(); 244 if (x2 == x1) { 246 x2++; 247 } 248 if (y2 == y1) { 249 y2++; 250 } 251 return x1 + "," + y1 + "," + x2 + "," + y2; 253 } 254 255 263 private String getPolyCoords(Shape shape) { 264 if (shape == null) { 265 throw new IllegalArgumentException ("Null 'shape' argument."); 266 } 267 StringBuffer result = new StringBuffer (); 268 boolean first = true; 269 float[] coords = new float[6]; 270 PathIterator pi = shape.getPathIterator(null, 1.0); 271 while (!pi.isDone()) { 272 pi.currentSegment(coords); 273 if (first) { 274 first = false; 275 result.append((int) coords[0]); 276 result.append(",").append((int) coords[1]); 277 } 278 else { 279 result.append(","); 280 result.append((int) coords[0]); 281 result.append(","); 282 result.append((int) coords[1]); 283 } 284 pi.next(); 285 } 286 return result.toString(); 287 } 288 289 298 public String getImageMapAreaTag( 299 ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, 300 URLTagFragmentGenerator urlTagFragmentGenerator) { 301 302 StringBuffer tag = new StringBuffer (); 303 boolean hasURL 304 = (this.urlText == null ? false : !this.urlText.equals("")); 305 boolean hasToolTip 306 = (this.toolTipText == null ? false : !this.toolTipText.equals("")); 307 if (hasURL || hasToolTip) { 308 tag.append( 309 "<area shape=\"" + getShapeType() + "\"" + " coords=\"" 310 + getShapeCoords() + "\"" 311 ); 312 if (hasToolTip) { 313 tag.append(toolTipTagFragmentGenerator.generateToolTipFragment( 314 this.toolTipText 315 )); 316 } 317 if (hasURL) { 318 tag.append( 319 urlTagFragmentGenerator.generateURLFragment(this.urlText) 320 ); 321 } 322 if (!hasToolTip) { 325 tag.append(" alt=\"\""); 326 } 327 tag.append("/>"); 328 } 329 return tag.toString(); 330 } 331 332 338 public String toString() { 339 StringBuffer buf = new StringBuffer ("ChartEntity: "); 340 buf.append("tooltip = "); 341 buf.append(this.toolTipText); 342 return buf.toString(); 343 } 344 345 352 public boolean equals(Object obj) { 353 if (obj == this) { 354 return true; 355 } 356 if (obj instanceof ChartEntity) { 357 ChartEntity that = (ChartEntity) obj; 358 if (!this.area.equals(that.area)) { 359 return false; 360 } 361 if (!ObjectUtilities.equal(this.toolTipText, that.toolTipText)) { 362 return false; 363 } 364 if (!ObjectUtilities.equal(this.urlText, that.urlText)) { 365 return false; 366 } 367 return true; 368 } 369 return false; 370 } 371 372 380 public Object clone() throws CloneNotSupportedException { 381 return super.clone(); 382 } 383 384 391 private void writeObject(ObjectOutputStream stream) throws IOException { 392 stream.defaultWriteObject(); 393 SerialUtilities.writeShape(this.area, stream); 394 } 395 396 404 private void readObject(ObjectInputStream stream) 405 throws IOException , ClassNotFoundException { 406 stream.defaultReadObject(); 407 this.area = SerialUtilities.readShape(stream); 408 } 409 410 } 411 | Popular Tags |