1 47 48 package org.jfree.chart.block; 49 50 import java.awt.Graphics2D ; 51 import java.awt.geom.Rectangle2D ; 52 import java.io.IOException ; 53 import java.io.ObjectInputStream ; 54 import java.io.ObjectOutputStream ; 55 import java.io.Serializable ; 56 57 import org.jfree.data.Range; 58 import org.jfree.io.SerialUtilities; 59 import org.jfree.ui.RectangleInsets; 60 import org.jfree.ui.Size2D; 61 62 66 public class AbstractBlock implements Serializable { 67 68 69 private static final long serialVersionUID = 7689852412141274563L; 70 71 72 private String id; 73 74 75 private RectangleInsets margin; 76 77 78 private BlockBorder border; 79 80 81 private RectangleInsets padding; 82 83 87 private double width; 88 89 93 private double height; 94 95 98 private transient Rectangle2D bounds; 99 100 103 protected AbstractBlock() { 104 this.id = null; 105 this.width = 0.0; 106 this.height = 0.0; 107 this.bounds = new Rectangle2D.Float (); 108 this.margin = RectangleInsets.ZERO_INSETS; 109 this.border = BlockBorder.NONE; 110 this.padding = RectangleInsets.ZERO_INSETS; 111 } 112 113 118 public String getID() { 119 return this.id; 120 } 121 122 127 public void setID(String id) { 128 this.id = id; 129 } 130 131 138 public double getWidth() { 139 return this.width; 140 } 141 142 147 public void setWidth(double width) { 148 this.width = width; 149 } 150 151 158 public double getHeight() { 159 return this.height; 160 } 161 162 167 public void setHeight(double height) { 168 this.height = height; 169 } 170 171 176 public RectangleInsets getMargin() { 177 return this.margin; 178 } 179 180 186 public void setMargin(RectangleInsets margin) { 187 if (margin == null) { 188 throw new IllegalArgumentException ("Null 'margin' argument."); 189 } 190 this.margin = margin; 191 } 192 193 201 public void setMargin(double top, double left, double bottom, 202 double right) { 203 setMargin(new RectangleInsets(top, left, bottom, right)); 204 } 205 206 211 public BlockBorder getBorder() { 212 return this.border; 213 } 214 215 221 public void setBorder(BlockBorder border) { 222 if (border == null) { 223 throw new IllegalArgumentException ("Null 'border' argument."); 224 } 225 this.border = border; 226 } 227 228 236 public void setBorder(double top, double left, double bottom, 237 double right) { 238 setBorder(new BlockBorder(top, left, bottom, right)); 239 } 240 241 246 public RectangleInsets getPadding() { 247 return this.padding; 248 } 249 250 256 public void setPadding(RectangleInsets padding) { 257 if (padding == null) { 258 throw new IllegalArgumentException ("Null 'padding' argument."); 259 } 260 this.padding = padding; 261 } 262 263 268 public double getContentXOffset() { 269 return this.margin.getLeft() + this.border.getInsets().getLeft() 270 + this.padding.getLeft(); 271 } 272 273 278 public double getContentYOffset() { 279 return this.margin.getTop() + this.border.getInsets().getTop() 280 + this.padding.getTop(); 281 } 282 283 291 public void setPadding(double top, double left, double bottom, 292 double right) { 293 setPadding(new RectangleInsets(top, left, bottom, right)); 294 } 295 296 304 public Size2D arrange(Graphics2D g2) { 305 return arrange(g2, RectangleConstraint.NONE); 306 } 307 308 317 public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) { 318 Size2D base = new Size2D(getWidth(), getHeight()); 319 return constraint.calculateConstrainedSize(base); 320 } 321 322 327 public Rectangle2D getBounds() { 328 return this.bounds; 329 } 330 331 336 public void setBounds(Rectangle2D bounds) { 337 if (bounds == null) { 338 throw new IllegalArgumentException ("Null 'bounds' argument."); 339 } 340 this.bounds = bounds; 341 } 342 343 352 protected double trimToContentWidth(double fixedWidth) { 353 double result = this.margin.trimWidth(fixedWidth); 354 result = this.border.getInsets().trimWidth(result); 355 result = this.padding.trimWidth(result); 356 return Math.max(result, 0.0); 357 } 358 359 368 protected double trimToContentHeight(double fixedHeight) { 369 double result = this.margin.trimHeight(fixedHeight); 370 result = this.border.getInsets().trimHeight(result); 371 result = this.padding.trimHeight(result); 372 return Math.max(result, 0.0); 373 } 374 375 383 protected RectangleConstraint toContentConstraint(RectangleConstraint c) { 384 if (c == null) { 385 throw new IllegalArgumentException ("Null 'c' argument."); 386 } 387 if (c.equals(RectangleConstraint.NONE)) { 388 return c; 389 } 390 double w = c.getWidth(); 391 Range wr = c.getWidthRange(); 392 double h = c.getHeight(); 393 Range hr = c.getHeightRange(); 394 double ww = trimToContentWidth(w); 395 double hh = trimToContentHeight(h); 396 Range wwr = trimToContentWidth(wr); 397 Range hhr = trimToContentHeight(hr); 398 return new RectangleConstraint( 399 ww, wwr, c.getWidthConstraintType(), 400 hh, hhr, c.getHeightConstraintType() 401 ); 402 } 403 404 private Range trimToContentWidth(Range r) { 405 if (r == null) { 406 return null; 407 } 408 double lowerBound = 0.0; 409 double upperBound = Double.POSITIVE_INFINITY; 410 if (r.getLowerBound() > 0.0) { 411 lowerBound = trimToContentWidth(r.getLowerBound()); 412 } 413 if (r.getUpperBound() < Double.POSITIVE_INFINITY) { 414 upperBound = trimToContentWidth(r.getUpperBound()); 415 } 416 return new Range(lowerBound, upperBound); 417 } 418 419 private Range trimToContentHeight(Range r) { 420 if (r == null) { 421 return null; 422 } 423 double lowerBound = 0.0; 424 double upperBound = Double.POSITIVE_INFINITY; 425 if (r.getLowerBound() > 0.0) { 426 lowerBound = trimToContentHeight(r.getLowerBound()); 427 } 428 if (r.getUpperBound() < Double.POSITIVE_INFINITY) { 429 upperBound = trimToContentHeight(r.getUpperBound()); 430 } 431 return new Range(lowerBound, upperBound); 432 } 433 434 441 protected double calculateTotalWidth(double contentWidth) { 442 double result = contentWidth; 443 result = this.padding.extendWidth(result); 444 result = this.border.getInsets().extendWidth(result); 445 result = this.margin.extendWidth(result); 446 return result; 447 } 448 449 456 protected double calculateTotalHeight(double contentHeight) { 457 double result = contentHeight; 458 result = this.padding.extendHeight(result); 459 result = this.border.getInsets().extendHeight(result); 460 result = this.margin.extendHeight(result); 461 return result; 462 } 463 464 472 protected Rectangle2D trimMargin(Rectangle2D area) { 473 this.margin.trim(area); 475 return area; 476 } 477 478 486 protected Rectangle2D trimBorder(Rectangle2D area) { 487 this.border.getInsets().trim(area); 489 return area; 490 } 491 492 500 protected Rectangle2D trimPadding(Rectangle2D area) { 501 this.padding.trim(area); 503 return area; 504 } 505 506 512 protected void drawBorder(Graphics2D g2, Rectangle2D area) { 513 this.border.draw(g2, area); 514 } 515 516 523 public boolean equals(Object obj) { 524 if (obj == this) { 525 return true; 526 } 527 if (!(obj instanceof AbstractBlock)) { 528 return false; 529 } 530 AbstractBlock that = (AbstractBlock) obj; 531 if (!this.border.equals(that.border)) { 532 return false; 533 } 534 if (!this.bounds.equals(that.bounds)) { 535 return false; 536 } 537 if (!this.margin.equals(that.margin)) { 538 return false; 539 } 540 if (!this.padding.equals(that.padding)) { 541 return false; 542 } 543 if (this.height != that.height) { 544 return false; 545 } 546 if (this.width != that.width) { 547 return false; 548 } 549 return true; 550 } 551 552 559 private void writeObject(ObjectOutputStream stream) throws IOException { 560 stream.defaultWriteObject(); 561 SerialUtilities.writeShape(this.bounds, stream); 562 } 563 564 572 private void readObject(ObjectInputStream stream) 573 throws IOException , ClassNotFoundException { 574 stream.defaultReadObject(); 575 this.bounds = (Rectangle2D ) SerialUtilities.readShape(stream); 576 } 577 578 } 579 | Popular Tags |