1 57 58 package org.jfree.chart.title; 59 60 import java.awt.Color ; 61 import java.awt.Font ; 62 import java.awt.Graphics2D ; 63 import java.awt.Paint ; 64 import java.awt.geom.Rectangle2D ; 65 import java.io.IOException ; 66 import java.io.ObjectInputStream ; 67 import java.io.ObjectOutputStream ; 68 import java.io.Serializable ; 69 70 import org.jfree.chart.LegendItem; 71 import org.jfree.chart.LegendItemCollection; 72 import org.jfree.chart.LegendItemSource; 73 import org.jfree.chart.block.Arrangement; 74 import org.jfree.chart.block.Block; 75 import org.jfree.chart.block.BlockContainer; 76 import org.jfree.chart.block.BorderArrangement; 77 import org.jfree.chart.block.CenterArrangement; 78 import org.jfree.chart.block.ColumnArrangement; 79 import org.jfree.chart.block.FlowArrangement; 80 import org.jfree.chart.block.LabelBlock; 81 import org.jfree.chart.block.RectangleConstraint; 82 import org.jfree.chart.event.TitleChangeEvent; 83 import org.jfree.io.SerialUtilities; 84 import org.jfree.ui.RectangleAnchor; 85 import org.jfree.ui.RectangleEdge; 86 import org.jfree.ui.RectangleInsets; 87 import org.jfree.ui.Size2D; 88 import org.jfree.util.PaintUtilities; 89 import org.jfree.util.PublicCloneable; 90 91 98 public class LegendTitle extends Title 99 implements Cloneable , PublicCloneable, Serializable { 100 101 102 private static final long serialVersionUID = 2644010518533854633L; 103 104 105 public static final Font DEFAULT_ITEM_FONT 106 = new Font ("SansSerif", Font.PLAIN, 12); 107 108 109 public static final Paint DEFAULT_ITEM_PAINT = Color.black; 110 111 112 private LegendItemSource[] sources; 113 114 115 private transient Paint backgroundPaint; 116 117 118 private RectangleEdge legendItemGraphicEdge; 119 120 121 private RectangleAnchor legendItemGraphicAnchor; 122 123 124 private RectangleAnchor legendItemGraphicLocation; 125 126 127 private RectangleInsets legendItemGraphicPadding; 128 129 130 private Font itemFont; 131 132 133 private transient Paint itemPaint; 134 135 136 private RectangleInsets itemLabelPadding; 137 138 141 private BlockContainer items; 142 143 private Arrangement hLayout; 144 145 private Arrangement vLayout; 146 147 151 private BlockContainer wrapper; 152 153 158 public LegendTitle(LegendItemSource source) { 159 this(source, new FlowArrangement(), new ColumnArrangement()); 160 } 161 162 171 public LegendTitle(LegendItemSource source, 172 Arrangement hLayout, Arrangement vLayout) { 173 this.sources = new LegendItemSource[] {source}; 174 this.items = new BlockContainer(hLayout); 175 this.hLayout = hLayout; 176 this.vLayout = vLayout; 177 this.backgroundPaint = null; 178 this.legendItemGraphicEdge = RectangleEdge.LEFT; 179 this.legendItemGraphicAnchor = RectangleAnchor.CENTER; 180 this.legendItemGraphicLocation = RectangleAnchor.CENTER; 181 this.legendItemGraphicPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0); 182 this.itemFont = DEFAULT_ITEM_FONT; 183 this.itemPaint = DEFAULT_ITEM_PAINT; 184 this.itemLabelPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0); 185 } 186 187 192 public LegendItemSource[] getSources() { 193 return this.sources; 194 } 195 196 202 public void setSources(LegendItemSource[] sources) { 203 if (sources == null) { 204 throw new IllegalArgumentException ("Null 'sources' argument."); 205 } 206 this.sources = sources; 207 notifyListeners(new TitleChangeEvent(this)); 208 } 209 210 215 public Paint getBackgroundPaint() { 216 return this.backgroundPaint; 217 } 218 219 225 public void setBackgroundPaint(Paint paint) { 226 this.backgroundPaint = paint; 227 notifyListeners(new TitleChangeEvent(this)); 228 } 229 230 235 public RectangleEdge getLegendItemGraphicEdge() { 236 return this.legendItemGraphicEdge; 237 } 238 239 244 public void setLegendItemGraphicEdge(RectangleEdge edge) { 245 if (edge == null) { 246 throw new IllegalArgumentException ("Null 'edge' argument."); 247 } 248 this.legendItemGraphicEdge = edge; 249 notifyListeners(new TitleChangeEvent(this)); 250 } 251 252 257 public RectangleAnchor getLegendItemGraphicAnchor() { 258 return this.legendItemGraphicAnchor; 259 } 260 261 266 public void setLegendItemGraphicAnchor(RectangleAnchor anchor) { 267 if (anchor == null) { 268 throw new IllegalArgumentException ("Null 'anchor' point."); 269 } 270 this.legendItemGraphicAnchor = anchor; 271 } 272 273 278 public RectangleAnchor getLegendItemGraphicLocation() { 279 return this.legendItemGraphicLocation; 280 } 281 282 287 public void setLegendItemGraphicLocation(RectangleAnchor anchor) { 288 this.legendItemGraphicLocation = anchor; 289 } 290 291 296 public RectangleInsets getLegendItemGraphicPadding() { 297 return this.legendItemGraphicPadding; 298 } 299 300 306 public void setLegendItemGraphicPadding(RectangleInsets padding) { 307 if (padding == null) { 308 throw new IllegalArgumentException ("Null 'padding' argument."); 309 } 310 this.legendItemGraphicPadding = padding; 311 notifyListeners(new TitleChangeEvent(this)); 312 } 313 314 319 public Font getItemFont() { 320 return this.itemFont; 321 } 322 323 329 public void setItemFont(Font font) { 330 if (font == null) { 331 throw new IllegalArgumentException ("Null 'font' argument."); 332 } 333 this.itemFont = font; 334 notifyListeners(new TitleChangeEvent(this)); 335 } 336 337 342 public Paint getItemPaint() { 343 return this.itemPaint; 344 } 345 346 351 public void setItemPaint(Paint paint) { 352 if (paint == null) { 353 throw new IllegalArgumentException ("Null 'paint' argument."); 354 } 355 this.itemPaint = paint; 356 notifyListeners(new TitleChangeEvent(this)); 357 } 358 359 364 public RectangleInsets getItemLabelPadding() { 365 return this.itemLabelPadding; 366 } 367 368 373 public void setItemLabelPadding(RectangleInsets padding) { 374 if (padding == null) { 375 throw new IllegalArgumentException ("Null 'padding' argument."); 376 } 377 this.itemLabelPadding = padding; 378 notifyListeners(new TitleChangeEvent(this)); 379 } 380 381 384 protected void fetchLegendItems() { 385 this.items.clear(); 386 RectangleEdge p = getPosition(); 387 if (RectangleEdge.isTopOrBottom(p)) { 388 this.items.setArrangement(this.hLayout); 389 } 390 else { 391 this.items.setArrangement(this.vLayout); 392 } 393 for (int s = 0; s < this.sources.length; s++) { 394 LegendItemCollection legendItems = this.sources[s].getLegendItems(); 395 if (legendItems != null) { 396 for (int i = 0; i < legendItems.getItemCount(); i++) { 397 LegendItem item = legendItems.get(i); 398 Block block = createLegendItemBlock(item); 399 this.items.add(block); 400 } 401 } 402 } 403 } 404 405 412 protected Block createLegendItemBlock(LegendItem item) { 413 BlockContainer result = null; 414 LegendGraphic lg = new LegendGraphic(item.getShape(), 415 item.getFillPaint()); 416 lg.setShapeFilled(item.isShapeFilled()); 417 lg.setLine(item.getLine()); 418 lg.setLineStroke(item.getLineStroke()); 419 lg.setLinePaint(item.getLinePaint()); 420 lg.setLineVisible(item.isLineVisible()); 421 lg.setShapeVisible(item.isShapeVisible()); 422 lg.setShapeOutlineVisible(item.isShapeOutlineVisible()); 423 lg.setOutlinePaint(item.getOutlinePaint()); 424 lg.setOutlineStroke(item.getOutlineStroke()); 425 lg.setPadding(this.legendItemGraphicPadding); 426 427 LegendItemBlockContainer legendItem = new LegendItemBlockContainer( 428 new BorderArrangement(), item.getDatasetIndex(), 429 item.getSeriesIndex()); 430 lg.setShapeAnchor(getLegendItemGraphicAnchor()); 431 lg.setShapeLocation(getLegendItemGraphicLocation()); 432 legendItem.add(lg, this.legendItemGraphicEdge); 433 LabelBlock labelBlock = new LabelBlock(item.getLabel(), this.itemFont, 434 this.itemPaint); 435 labelBlock.setPadding(this.itemLabelPadding); 436 legendItem.add(labelBlock); 437 legendItem.setToolTipText(item.getToolTipText()); 438 legendItem.setURLText(item.getURLText()); 439 440 result = new BlockContainer(new CenterArrangement()); 441 result.add(legendItem); 442 443 return result; 444 } 445 446 451 public BlockContainer getItemContainer() { 452 return this.items; 453 } 454 455 464 public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) { 465 Size2D result = new Size2D(); 466 fetchLegendItems(); 467 if (this.items.isEmpty()) { 468 return result; 469 } 470 BlockContainer container = this.wrapper; 471 if (container == null) { 472 container = this.items; 473 } 474 RectangleConstraint c = toContentConstraint(constraint); 475 Size2D size = container.arrange(g2, c); 476 result.height = calculateTotalHeight(size.height); 477 result.width = calculateTotalWidth(size.width); 478 return result; 479 } 480 481 488 public void draw(Graphics2D g2, Rectangle2D area) { 489 draw(g2, area, null); 490 } 491 492 502 public Object draw(Graphics2D g2, Rectangle2D area, Object params) { 503 Rectangle2D target = (Rectangle2D ) area.clone(); 504 target = trimMargin(target); 505 if (this.backgroundPaint != null) { 506 g2.setPaint(this.backgroundPaint); 507 g2.fill(target); 508 } 509 getBorder().draw(g2, target); 510 getBorder().getInsets().trim(target); 511 BlockContainer container = this.wrapper; 512 if (container == null) { 513 container = this.items; 514 } 515 target = trimPadding(target); 516 return container.draw(g2, target, params); 517 } 518 519 524 public void setWrapper(BlockContainer wrapper) { 525 this.wrapper = wrapper; 526 } 527 528 535 public boolean equals(Object obj) { 536 if (obj == this) { 537 return true; 538 } 539 if (!(obj instanceof LegendTitle)) { 540 return false; 541 } 542 if (!super.equals(obj)) { 543 return false; 544 } 545 LegendTitle that = (LegendTitle) obj; 546 if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) { 547 return false; 548 } 549 if (this.legendItemGraphicEdge != that.legendItemGraphicEdge) { 550 return false; 551 } 552 if (this.legendItemGraphicAnchor != that.legendItemGraphicAnchor) { 553 return false; 554 } 555 if (this.legendItemGraphicLocation != that.legendItemGraphicLocation) { 556 return false; 557 } 558 if (!this.itemFont.equals(that.itemFont)) { 559 return false; 560 } 561 if (!this.itemPaint.equals(that.itemPaint)) { 562 return false; 563 } 564 if (!this.hLayout.equals(that.hLayout)) { 565 return false; 566 } 567 if (!this.vLayout.equals(that.vLayout)) { 568 return false; 569 } 570 return true; 571 } 572 573 580 private void writeObject(ObjectOutputStream stream) throws IOException { 581 stream.defaultWriteObject(); 582 SerialUtilities.writePaint(this.backgroundPaint, stream); 583 SerialUtilities.writePaint(this.itemPaint, stream); 584 } 585 586 594 private void readObject(ObjectInputStream stream) 595 throws IOException , ClassNotFoundException { 596 stream.defaultReadObject(); 597 this.backgroundPaint = SerialUtilities.readPaint(stream); 598 this.itemPaint = SerialUtilities.readPaint(stream); 599 } 600 601 } 602 | Popular Tags |