1 80 81 package org.jfree.chart.title; 82 83 import java.awt.Color ; 84 import java.awt.Font ; 85 import java.awt.Graphics2D ; 86 import java.awt.Paint ; 87 import java.awt.geom.Rectangle2D ; 88 import java.io.IOException ; 89 import java.io.ObjectInputStream ; 90 import java.io.ObjectOutputStream ; 91 import java.io.Serializable ; 92 93 import org.jfree.chart.block.BlockResult; 94 import org.jfree.chart.block.EntityBlockParams; 95 import org.jfree.chart.block.LengthConstraintType; 96 import org.jfree.chart.block.RectangleConstraint; 97 import org.jfree.chart.entity.ChartEntity; 98 import org.jfree.chart.entity.EntityCollection; 99 import org.jfree.chart.entity.StandardEntityCollection; 100 import org.jfree.chart.event.TitleChangeEvent; 101 import org.jfree.data.Range; 102 import org.jfree.io.SerialUtilities; 103 import org.jfree.text.G2TextMeasurer; 104 import org.jfree.text.TextBlock; 105 import org.jfree.text.TextBlockAnchor; 106 import org.jfree.text.TextUtilities; 107 import org.jfree.ui.HorizontalAlignment; 108 import org.jfree.ui.RectangleEdge; 109 import org.jfree.ui.RectangleInsets; 110 import org.jfree.ui.Size2D; 111 import org.jfree.ui.VerticalAlignment; 112 import org.jfree.util.ObjectUtilities; 113 import org.jfree.util.PaintUtilities; 114 import org.jfree.util.PublicCloneable; 115 116 120 public class TextTitle extends Title 121 implements Serializable , Cloneable , PublicCloneable { 122 123 124 private static final long serialVersionUID = 8372008692127477443L; 125 126 127 public static final Font DEFAULT_FONT 128 = new Font ("SansSerif", Font.BOLD, 12); 129 130 131 public static final Paint DEFAULT_TEXT_PAINT = Color.black; 132 133 134 private String text; 135 136 137 private Font font; 138 139 140 private HorizontalAlignment textAlignment; 141 142 143 private transient Paint paint; 144 145 146 private transient Paint backgroundPaint; 147 148 149 private String toolTipText; 150 151 152 private String urlText; 153 154 155 private TextBlock content; 156 157 161 private boolean expandToFitSpace = false; 162 163 166 public TextTitle() { 167 this(""); 168 } 169 170 175 public TextTitle(String text) { 176 this(text, TextTitle.DEFAULT_FONT, TextTitle.DEFAULT_TEXT_PAINT, 177 Title.DEFAULT_POSITION, Title.DEFAULT_HORIZONTAL_ALIGNMENT, 178 Title.DEFAULT_VERTICAL_ALIGNMENT, Title.DEFAULT_PADDING); 179 } 180 181 187 public TextTitle(String text, Font font) { 188 this(text, font, TextTitle.DEFAULT_TEXT_PAINT, Title.DEFAULT_POSITION, 189 Title.DEFAULT_HORIZONTAL_ALIGNMENT, 190 Title.DEFAULT_VERTICAL_ALIGNMENT, Title.DEFAULT_PADDING); 191 } 192 193 206 public TextTitle(String text, Font font, Paint paint, 207 RectangleEdge position, 208 HorizontalAlignment horizontalAlignment, 209 VerticalAlignment verticalAlignment, 210 RectangleInsets padding) { 211 212 super(position, horizontalAlignment, verticalAlignment, padding); 213 214 if (text == null) { 215 throw new NullPointerException ("Null 'text' argument."); 216 } 217 if (font == null) { 218 throw new NullPointerException ("Null 'font' argument."); 219 } 220 if (paint == null) { 221 throw new NullPointerException ("Null 'paint' argument."); 222 } 223 this.text = text; 224 this.font = font; 225 this.paint = paint; 226 this.textAlignment = horizontalAlignment; 230 this.backgroundPaint = null; 231 this.content = null; 232 this.toolTipText = null; 233 this.urlText = null; 234 235 } 236 237 242 public String getText() { 243 return this.text; 244 } 245 246 252 public void setText(String text) { 253 if (text == null) { 254 throw new NullPointerException ("Null 'text' argument."); 255 } 256 if (!this.text.equals(text)) { 257 this.text = text; 258 notifyListeners(new TitleChangeEvent(this)); 259 } 260 } 261 262 270 public HorizontalAlignment getTextAlignment() { 271 return this.textAlignment; 272 } 273 274 279 public void setTextAlignment(HorizontalAlignment alignment) { 280 if (alignment == null) { 281 throw new IllegalArgumentException ("Null 'alignment' argument."); 282 } 283 this.textAlignment = alignment; 284 notifyListeners(new TitleChangeEvent(this)); 285 } 286 287 292 public Font getFont() { 293 return this.font; 294 } 295 296 302 public void setFont(Font font) { 303 if (font == null) { 304 throw new IllegalArgumentException ("Null 'font' argument."); 305 } 306 if (!this.font.equals(font)) { 307 this.font = font; 308 notifyListeners(new TitleChangeEvent(this)); 309 } 310 } 311 312 317 public Paint getPaint() { 318 return this.paint; 319 } 320 321 327 public void setPaint(Paint paint) { 328 if (paint == null) { 329 throw new IllegalArgumentException ("Null 'paint' argument."); 330 } 331 if (!this.paint.equals(paint)) { 332 this.paint = paint; 333 notifyListeners(new TitleChangeEvent(this)); 334 } 335 } 336 337 342 public Paint getBackgroundPaint() { 343 return this.backgroundPaint; 344 } 345 346 353 public void setBackgroundPaint(Paint paint) { 354 this.backgroundPaint = paint; 355 notifyListeners(new TitleChangeEvent(this)); 356 } 357 358 363 public String getToolTipText() { 364 return this.toolTipText; 365 } 366 367 373 public void setToolTipText(String text) { 374 this.toolTipText = text; 375 notifyListeners(new TitleChangeEvent(this)); 376 } 377 378 383 public String getURLText() { 384 return this.urlText; 385 } 386 387 393 public void setURLText(String text) { 394 this.urlText = text; 395 notifyListeners(new TitleChangeEvent(this)); 396 } 397 398 404 public boolean getExpandToFitSpace() { 405 return this.expandToFitSpace; 406 } 407 408 415 public void setExpandToFitSpace(boolean expand) { 416 this.expandToFitSpace = expand; 417 notifyListeners(new TitleChangeEvent(this)); 418 } 419 420 429 public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) { 430 RectangleConstraint cc = toContentConstraint(constraint); 431 LengthConstraintType w = cc.getWidthConstraintType(); 432 LengthConstraintType h = cc.getHeightConstraintType(); 433 Size2D contentSize = null; 434 if (w == LengthConstraintType.NONE) { 435 if (h == LengthConstraintType.NONE) { 436 throw new RuntimeException ("Not yet implemented."); 437 } 438 else if (h == LengthConstraintType.RANGE) { 439 throw new RuntimeException ("Not yet implemented."); 440 } 441 else if (h == LengthConstraintType.FIXED) { 442 throw new RuntimeException ("Not yet implemented."); 443 } 444 } 445 else if (w == LengthConstraintType.RANGE) { 446 if (h == LengthConstraintType.NONE) { 447 throw new RuntimeException ("Not yet implemented."); 448 } 449 else if (h == LengthConstraintType.RANGE) { 450 contentSize = arrangeRR(g2, cc.getWidthRange(), 451 cc.getHeightRange()); 452 } 453 else if (h == LengthConstraintType.FIXED) { 454 throw new RuntimeException ("Not yet implemented."); 455 } 456 } 457 else if (w == LengthConstraintType.FIXED) { 458 if (h == LengthConstraintType.NONE) { 459 throw new RuntimeException ("Not yet implemented."); 460 } 461 else if (h == LengthConstraintType.RANGE) { 462 throw new RuntimeException ("Not yet implemented."); 463 } 464 else if (h == LengthConstraintType.FIXED) { 465 throw new RuntimeException ("Not yet implemented."); 466 } 467 } 468 return new Size2D(calculateTotalWidth(contentSize.getWidth()), 469 calculateTotalHeight(contentSize.getHeight())); 470 } 471 472 483 protected Size2D arrangeRR(Graphics2D g2, Range widthRange, 484 Range heightRange) { 485 RectangleEdge position = getPosition(); 486 if (position == RectangleEdge.TOP || position == RectangleEdge.BOTTOM) { 487 float maxWidth = (float) widthRange.getUpperBound(); 488 g2.setFont(this.font); 489 this.content = TextUtilities.createTextBlock(this.text, this.font, 490 this.paint, maxWidth, new G2TextMeasurer(g2)); 491 this.content.setLineAlignment(this.textAlignment); 492 Size2D contentSize = this.content.calculateDimensions(g2); 493 if (this.expandToFitSpace) { 494 return new Size2D(maxWidth, contentSize.getHeight()); 495 } 496 else { 497 return contentSize; 498 } 499 } 500 else if (position == RectangleEdge.LEFT || position 501 == RectangleEdge.RIGHT) { 502 float maxWidth = (float) heightRange.getUpperBound(); 503 g2.setFont(this.font); 504 this.content = TextUtilities.createTextBlock(this.text, this.font, 505 this.paint, maxWidth, new G2TextMeasurer(g2)); 506 this.content.setLineAlignment(this.textAlignment); 507 Size2D contentSize = this.content.calculateDimensions(g2); 508 509 if (this.expandToFitSpace) { 511 return new Size2D(contentSize.getHeight(), maxWidth); 512 } 513 else { 514 return new Size2D(contentSize.height, contentSize.width); 515 } 516 } 517 else { 518 throw new RuntimeException ("Unrecognised exception."); 519 } 520 } 521 522 529 public void draw(Graphics2D g2, Rectangle2D area) { 530 draw(g2, area, null); 531 } 532 533 545 public Object draw(Graphics2D g2, Rectangle2D area, Object params) { 546 if (this.content == null) { 547 return null; 548 } 549 area = trimMargin(area); 550 drawBorder(g2, area); 551 if (this.text.equals("")) { 552 return null; 553 } 554 ChartEntity entity = null; 555 if (params instanceof EntityBlockParams) { 556 EntityBlockParams p = (EntityBlockParams) params; 557 if (p.getGenerateEntities()) { 558 entity = new ChartEntity(area, this.toolTipText, this.urlText); 559 } 560 } 561 area = trimBorder(area); 562 if (this.backgroundPaint != null) { 563 g2.setPaint(this.backgroundPaint); 564 g2.fill(area); 565 } 566 area = trimPadding(area); 567 RectangleEdge position = getPosition(); 568 if (position == RectangleEdge.TOP || position == RectangleEdge.BOTTOM) { 569 drawHorizontal(g2, area); 570 } 571 else if (position == RectangleEdge.LEFT 572 || position == RectangleEdge.RIGHT) { 573 drawVertical(g2, area); 574 } 575 BlockResult result = new BlockResult(); 576 if (entity != null) { 577 StandardEntityCollection sec = new StandardEntityCollection(); 578 sec.add(entity); 579 result.setEntityCollection(sec); 580 } 581 return result; 582 } 583 584 592 protected void drawHorizontal(Graphics2D g2, Rectangle2D area) { 593 Rectangle2D titleArea = (Rectangle2D ) area.clone(); 594 g2.setFont(this.font); 595 g2.setPaint(this.paint); 596 TextBlockAnchor anchor = null; 597 float x = 0.0f; 598 HorizontalAlignment horizontalAlignment = getHorizontalAlignment(); 599 if (horizontalAlignment == HorizontalAlignment.LEFT) { 600 x = (float) titleArea.getX(); 601 anchor = TextBlockAnchor.TOP_LEFT; 602 } 603 else if (horizontalAlignment == HorizontalAlignment.RIGHT) { 604 x = (float) titleArea.getMaxX(); 605 anchor = TextBlockAnchor.TOP_RIGHT; 606 } 607 else if (horizontalAlignment == HorizontalAlignment.CENTER) { 608 x = (float) titleArea.getCenterX(); 609 anchor = TextBlockAnchor.TOP_CENTER; 610 } 611 float y = 0.0f; 612 RectangleEdge position = getPosition(); 613 if (position == RectangleEdge.TOP) { 614 y = (float) titleArea.getY(); 615 } 616 else if (position == RectangleEdge.BOTTOM) { 617 y = (float) titleArea.getMaxY(); 618 if (horizontalAlignment == HorizontalAlignment.LEFT) { 619 anchor = TextBlockAnchor.BOTTOM_LEFT; 620 } 621 else if (horizontalAlignment == HorizontalAlignment.CENTER) { 622 anchor = TextBlockAnchor.BOTTOM_CENTER; 623 } 624 else if (horizontalAlignment == HorizontalAlignment.RIGHT) { 625 anchor = TextBlockAnchor.BOTTOM_RIGHT; 626 } 627 } 628 this.content.draw(g2, x, y, anchor); 629 } 630 631 639 protected void drawVertical(Graphics2D g2, Rectangle2D area) { 640 Rectangle2D titleArea = (Rectangle2D ) area.clone(); 641 g2.setFont(this.font); 642 g2.setPaint(this.paint); 643 TextBlockAnchor anchor = null; 644 float y = 0.0f; 645 VerticalAlignment verticalAlignment = getVerticalAlignment(); 646 if (verticalAlignment == VerticalAlignment.TOP) { 647 y = (float) titleArea.getY(); 648 anchor = TextBlockAnchor.TOP_RIGHT; 649 } 650 else if (verticalAlignment == VerticalAlignment.BOTTOM) { 651 y = (float) titleArea.getMaxY(); 652 anchor = TextBlockAnchor.TOP_LEFT; 653 } 654 else if (verticalAlignment == VerticalAlignment.CENTER) { 655 y = (float) titleArea.getCenterY(); 656 anchor = TextBlockAnchor.TOP_CENTER; 657 } 658 float x = 0.0f; 659 RectangleEdge position = getPosition(); 660 if (position == RectangleEdge.LEFT) { 661 x = (float) titleArea.getX(); 662 } 663 else if (position == RectangleEdge.RIGHT) { 664 x = (float) titleArea.getMaxX(); 665 if (verticalAlignment == VerticalAlignment.TOP) { 666 anchor = TextBlockAnchor.BOTTOM_RIGHT; 667 } 668 else if (verticalAlignment == VerticalAlignment.CENTER) { 669 anchor = TextBlockAnchor.BOTTOM_CENTER; 670 } 671 else if (verticalAlignment == VerticalAlignment.BOTTOM) { 672 anchor = TextBlockAnchor.BOTTOM_LEFT; 673 } 674 } 675 this.content.draw(g2, x, y, anchor, x, y, -Math.PI / 2.0); 676 } 677 678 685 public boolean equals(Object obj) { 686 if (obj == this) { 687 return true; 688 } 689 if (!(obj instanceof TextTitle)) { 690 return false; 691 } 692 if (!super.equals(obj)) { 693 return false; 694 } 695 TextTitle that = (TextTitle) obj; 696 if (!ObjectUtilities.equal(this.text, that.text)) { 697 return false; 698 } 699 if (!ObjectUtilities.equal(this.font, that.font)) { 700 return false; 701 } 702 if (!PaintUtilities.equal(this.paint, that.paint)) { 703 return false; 704 } 705 if (this.textAlignment != that.textAlignment) { 706 return false; 707 } 708 if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) { 709 return false; 710 } 711 return true; 712 } 713 714 719 public int hashCode() { 720 int result = super.hashCode(); 721 result = 29 * result + (this.text != null ? this.text.hashCode() : 0); 722 result = 29 * result + (this.font != null ? this.font.hashCode() : 0); 723 result = 29 * result + (this.paint != null ? this.paint.hashCode() : 0); 724 result = 29 * result + (this.backgroundPaint != null 725 ? this.backgroundPaint.hashCode() : 0); 726 return result; 727 } 728 729 736 public Object clone() throws CloneNotSupportedException { 737 return super.clone(); 738 } 739 740 747 private void writeObject(ObjectOutputStream stream) throws IOException { 748 stream.defaultWriteObject(); 749 SerialUtilities.writePaint(this.paint, stream); 750 SerialUtilities.writePaint(this.backgroundPaint, stream); 751 } 752 753 761 private void readObject(ObjectInputStream stream) 762 throws IOException , ClassNotFoundException 763 { 764 stream.defaultReadObject(); 765 this.paint = SerialUtilities.readPaint(stream); 766 this.backgroundPaint = SerialUtilities.readPaint(stream); 767 } 768 769 } 770 771 | Popular Tags |