Source for org.jfree.chart.title.LegendTitle

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * ----------------
  28:  * LegendTitle.java
  29:  * ----------------
  30:  * (C) Copyright 2002-2006, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   Pierre-Marie Le Biot;
  34:  *
  35:  * $Id: LegendTitle.java,v 1.20.2.6 2006/07/20 16:21:59 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 25-Nov-2004 : First working version (DG);
  40:  * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
  41:  * 08-Feb-2005 : Updated for changes in RectangleConstraint class (DG);
  42:  * 11-Feb-2005 : Implemented PublicCloneable (DG);
  43:  * 23-Feb-2005 : Replaced chart reference with LegendItemSource (DG);
  44:  * 16-Mar-2005 : Added itemFont attribute (DG);
  45:  * 17-Mar-2005 : Fixed missing fillShape setting (DG);
  46:  * 20-Apr-2005 : Added new draw() method (DG);
  47:  * 03-May-2005 : Modified equals() method to ignore sources (DG);
  48:  * 13-May-2005 : Added settings for legend item label and graphic padding (DG);
  49:  * 09-Jun-2005 : Fixed serialization bug (DG);
  50:  * 01-Sep-2005 : Added itemPaint attribute (PMLB);
  51:  * ------------- JFREECHART 1.0.0 ---------------------------------------------
  52:  * 20-Jul-2006 : Use new LegendItemBlockContainer to restore support for
  53:  *               LegendItemEntities (DG);
  54:  * 
  55:  */
  56: 
  57: package org.jfree.chart.title;
  58: 
  59: import java.awt.Color;
  60: import java.awt.Font;
  61: import java.awt.Graphics2D;
  62: import java.awt.Paint;
  63: import java.awt.geom.Rectangle2D;
  64: import java.io.IOException;
  65: import java.io.ObjectInputStream;
  66: import java.io.ObjectOutputStream;
  67: import java.io.Serializable;
  68: 
  69: import org.jfree.chart.LegendItem;
  70: import org.jfree.chart.LegendItemCollection;
  71: import org.jfree.chart.LegendItemSource;
  72: import org.jfree.chart.block.Arrangement;
  73: import org.jfree.chart.block.Block;
  74: import org.jfree.chart.block.BlockContainer;
  75: import org.jfree.chart.block.BorderArrangement;
  76: import org.jfree.chart.block.CenterArrangement;
  77: import org.jfree.chart.block.ColumnArrangement;
  78: import org.jfree.chart.block.FlowArrangement;
  79: import org.jfree.chart.block.LabelBlock;
  80: import org.jfree.chart.block.RectangleConstraint;
  81: import org.jfree.chart.event.TitleChangeEvent;
  82: import org.jfree.io.SerialUtilities;
  83: import org.jfree.ui.RectangleAnchor;
  84: import org.jfree.ui.RectangleEdge;
  85: import org.jfree.ui.RectangleInsets;
  86: import org.jfree.ui.Size2D;
  87: import org.jfree.util.PaintUtilities;
  88: import org.jfree.util.PublicCloneable;
  89: 
  90: /**
  91:  * A chart title that displays a legend for the data in the chart.
  92:  * <P>
  93:  * The title can be populated with legend items manually, or you can assign a
  94:  * reference to the plot, in which case the legend items will be automatically
  95:  * created to match the dataset(s).
  96:  */
  97: public class LegendTitle extends Title 
  98:                          implements Cloneable, PublicCloneable, Serializable {
  99: 
 100:     /** For serialization. */
 101:     private static final long serialVersionUID = 2644010518533854633L;
 102:     
 103:     /** The default item font. */
 104:     public static final Font DEFAULT_ITEM_FONT 
 105:         = new Font("SansSerif", Font.PLAIN, 12);
 106: 
 107:     /** The default item paint. */
 108:     public static final Paint DEFAULT_ITEM_PAINT = Color.black;
 109: 
 110:     /** The sources for legend items. */
 111:     private LegendItemSource[] sources;
 112:     
 113:     /** The background paint (possibly <code>null</code>). */
 114:     private transient Paint backgroundPaint;
 115:     
 116:     /** The edge for the legend item graphic relative to the text. */
 117:     private RectangleEdge legendItemGraphicEdge;
 118:     
 119:     /** The anchor point for the legend item graphic. */
 120:     private RectangleAnchor legendItemGraphicAnchor;
 121:     
 122:     /** The legend item graphic location. */
 123:     private RectangleAnchor legendItemGraphicLocation;
 124:     
 125:     /** The padding for the legend item graphic. */
 126:     private RectangleInsets legendItemGraphicPadding;
 127: 
 128:     /** The item font. */
 129:     private Font itemFont;
 130:     
 131:     /** The item paint. */
 132:     private transient Paint itemPaint;
 133: 
 134:     /** The padding for the item labels. */
 135:     private RectangleInsets itemLabelPadding;
 136: 
 137:     /**
 138:      * A container that holds and displays the legend items.
 139:      */
 140:     private BlockContainer items;
 141:     
 142:     private Arrangement hLayout;
 143:     
 144:     private Arrangement vLayout;
 145:     
 146:     /** 
 147:      * An optional container for wrapping the legend items (allows for adding
 148:      * a title or other text to the legend). 
 149:      */
 150:     private BlockContainer wrapper;
 151: 
 152:     /**
 153:      * Constructs a new (empty) legend for the specified source.
 154:      * 
 155:      * @param source  the source.
 156:      */
 157:     public LegendTitle(LegendItemSource source) {
 158:         this(source, new FlowArrangement(), new ColumnArrangement());
 159:     }
 160:     
 161:     /**
 162:      * Creates a new legend title with the specified arrangement.
 163:      * 
 164:      * @param source  the source.
 165:      * @param hLayout  the horizontal item arrangement (<code>null</code> not
 166:      *                 permitted).
 167:      * @param vLayout  the vertical item arrangement (<code>null</code> not
 168:      *                 permitted).
 169:      */
 170:     public LegendTitle(LegendItemSource source, 
 171:                        Arrangement hLayout, Arrangement vLayout) {
 172:         this.sources = new LegendItemSource[] {source};
 173:         this.items = new BlockContainer(hLayout);
 174:         this.hLayout = hLayout;
 175:         this.vLayout = vLayout;
 176:         this.backgroundPaint = null;  
 177:         this.legendItemGraphicEdge = RectangleEdge.LEFT;
 178:         this.legendItemGraphicAnchor = RectangleAnchor.CENTER;
 179:         this.legendItemGraphicLocation = RectangleAnchor.CENTER;
 180:         this.legendItemGraphicPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0);
 181:         this.itemFont = DEFAULT_ITEM_FONT;
 182:         this.itemPaint = DEFAULT_ITEM_PAINT;
 183:         this.itemLabelPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0);
 184:     }
 185:     
 186:     /**
 187:      * Returns the legend item sources.
 188:      * 
 189:      * @return The sources.
 190:      */
 191:     public LegendItemSource[] getSources() {
 192:         return this.sources;   
 193:     }
 194:     
 195:     /**
 196:      * Sets the legend item sources and sends a {@link TitleChangeEvent} to
 197:      * all registered listeners.
 198:      * 
 199:      * @param sources  the sources (<code>null</code> not permitted).
 200:      */
 201:     public void setSources(LegendItemSource[] sources) {
 202:         if (sources == null) {
 203:             throw new IllegalArgumentException("Null 'sources' argument.");   
 204:         }
 205:         this.sources = sources;
 206:         notifyListeners(new TitleChangeEvent(this));
 207:     }
 208: 
 209:     /**
 210:      * Returns the background paint.
 211:      * 
 212:      * @return The background paint (possibly <code>null</code>).
 213:      */
 214:     public Paint getBackgroundPaint() {
 215:         return this.backgroundPaint;   
 216:     }
 217:     
 218:     /**
 219:      * Sets the background paint for the legend and sends a 
 220:      * {@link TitleChangeEvent} to all registered listeners.
 221:      * 
 222:      * @param paint  the paint (<code>null</code> permitted).
 223:      */
 224:     public void setBackgroundPaint(Paint paint) {
 225:         this.backgroundPaint = paint;   
 226:         notifyListeners(new TitleChangeEvent(this));
 227:     }
 228:     
 229:     /**
 230:      * Returns the location of the shape within each legend item. 
 231:      * 
 232:      * @return The location (never <code>null</code>).
 233:      */
 234:     public RectangleEdge getLegendItemGraphicEdge() {
 235:         return this.legendItemGraphicEdge;
 236:     }
 237:     
 238:     /**
 239:      * Sets the location of the shape within each legend item.
 240:      * 
 241:      * @param edge  the edge (<code>null</code> not permitted).
 242:      */
 243:     public void setLegendItemGraphicEdge(RectangleEdge edge) {
 244:         if (edge == null) {
 245:             throw new IllegalArgumentException("Null 'edge' argument.");
 246:         }
 247:         this.legendItemGraphicEdge = edge;
 248:         notifyListeners(new TitleChangeEvent(this));
 249:     }
 250:     
 251:     /**
 252:      * Returns the legend item graphic anchor.
 253:      * 
 254:      * @return The graphic anchor (never <code>null</code>).
 255:      */
 256:     public RectangleAnchor getLegendItemGraphicAnchor() {
 257:         return this.legendItemGraphicAnchor;
 258:     }
 259:     
 260:     /**
 261:      * Sets the anchor point used for the graphic in each legend item.
 262:      * 
 263:      * @param anchor  the anchor point (<code>null</code> not permitted).
 264:      */
 265:     public void setLegendItemGraphicAnchor(RectangleAnchor anchor) {
 266:         if (anchor == null) {
 267:             throw new IllegalArgumentException("Null 'anchor' point.");
 268:         }
 269:         this.legendItemGraphicAnchor = anchor;
 270:     }
 271:     
 272:     /**
 273:      * Returns the legend item graphic location.
 274:      * 
 275:      * @return The location (never <code>null</code>).
 276:      */
 277:     public RectangleAnchor getLegendItemGraphicLocation() {
 278:         return this.legendItemGraphicLocation;
 279:     }
 280:     
 281:     /**
 282:      * Sets the legend item graphic location.
 283:      * 
 284:      * @param anchor  the anchor (<code>null</code> not permitted).
 285:      */
 286:     public void setLegendItemGraphicLocation(RectangleAnchor anchor) {
 287:         this.legendItemGraphicLocation = anchor;
 288:     }
 289:     
 290:     /**
 291:      * Returns the padding that will be applied to each item graphic.
 292:      * 
 293:      * @return The padding (never <code>null</code>).
 294:      */
 295:     public RectangleInsets getLegendItemGraphicPadding() {
 296:         return this.legendItemGraphicPadding;    
 297:     }
 298:     
 299:     /**
 300:      * Sets the padding that will be applied to each item graphic in the 
 301:      * legend and sends a {@link TitleChangeEvent} to all registered listeners.
 302:      * 
 303:      * @param padding  the padding (<code>null</code> not permitted).
 304:      */
 305:     public void setLegendItemGraphicPadding(RectangleInsets padding) {
 306:         if (padding == null) {
 307:             throw new IllegalArgumentException("Null 'padding' argument.");   
 308:         }
 309:         this.legendItemGraphicPadding = padding;
 310:         notifyListeners(new TitleChangeEvent(this));
 311:     }
 312:     
 313:     /**
 314:      * Returns the item font.
 315:      * 
 316:      * @return The font (never <code>null</code>).
 317:      */
 318:     public Font getItemFont() {
 319:         return this.itemFont;   
 320:     }
 321:     
 322:     /**
 323:      * Sets the item font and sends a {@link TitleChangeEvent} to
 324:      * all registered listeners.
 325:      * 
 326:      * @param font  the font (<code>null</code> not permitted).
 327:      */
 328:     public void setItemFont(Font font) {
 329:         if (font == null) {
 330:             throw new IllegalArgumentException("Null 'font' argument.");   
 331:         }
 332:         this.itemFont = font;
 333:         notifyListeners(new TitleChangeEvent(this));
 334:     }
 335:     
 336:     /**
 337:      * Returns the item paint.
 338:      *
 339:      * @return The paint (never <code>null</code>).
 340:      */
 341:     public Paint getItemPaint() {
 342:         return this.itemPaint;   
 343:     }
 344:    
 345:     /**
 346:      * Sets the item paint.
 347:      *
 348:      * @param paint  the paint (<code>null</code> not permitted).
 349:      */
 350:     public void setItemPaint(Paint paint) {
 351:         if (paint == null) {
 352:             throw new IllegalArgumentException("Null 'paint' argument.");   
 353:         }
 354:         this.itemPaint = paint;
 355:         notifyListeners(new TitleChangeEvent(this));
 356:     }
 357:    
 358:     /**
 359:      * Returns the padding used for the items labels.
 360:      * 
 361:      * @return The padding (never <code>null</code>).
 362:      */
 363:     public RectangleInsets getItemLabelPadding() {
 364:         return this.itemLabelPadding;   
 365:     }
 366:     
 367:     /**
 368:      * Sets the padding used for the item labels in the legend.
 369:      * 
 370:      * @param padding  the padding (<code>null</code> not permitted).
 371:      */
 372:     public void setItemLabelPadding(RectangleInsets padding) {
 373:         if (padding == null) {
 374:             throw new IllegalArgumentException("Null 'padding' argument.");   
 375:         }
 376:         this.itemLabelPadding = padding;
 377:         notifyListeners(new TitleChangeEvent(this));
 378:     }
 379:     
 380:     /**
 381:      * Fetches the latest legend items.
 382:      */
 383:     protected void fetchLegendItems() {
 384:         this.items.clear();
 385:         RectangleEdge p = getPosition();
 386:         if (RectangleEdge.isTopOrBottom(p)) {
 387:             this.items.setArrangement(this.hLayout);   
 388:         }
 389:         else {
 390:             this.items.setArrangement(this.vLayout);   
 391:         }
 392:         for (int s = 0; s < this.sources.length; s++) {
 393:             LegendItemCollection legendItems = this.sources[s].getLegendItems();
 394:             if (legendItems != null) {
 395:                 for (int i = 0; i < legendItems.getItemCount(); i++) {
 396:                     LegendItem item = legendItems.get(i);
 397:                     Block block = createLegendItemBlock(item);
 398:                     this.items.add(block);
 399:                 }
 400:             }
 401:         }
 402:     }
 403:     
 404:     /**
 405:      * Creates a legend item block.
 406:      * 
 407:      * @param item  the legend item.
 408:      * 
 409:      * @return The block.
 410:      */
 411:     protected Block createLegendItemBlock(LegendItem item) {
 412:         BlockContainer result = null;
 413:         LegendGraphic lg = new LegendGraphic(item.getShape(), 
 414:                 item.getFillPaint());
 415:         lg.setShapeFilled(item.isShapeFilled());
 416:         lg.setLine(item.getLine());
 417:         lg.setLineStroke(item.getLineStroke());
 418:         lg.setLinePaint(item.getLinePaint());
 419:         lg.setLineVisible(item.isLineVisible());
 420:         lg.setShapeVisible(item.isShapeVisible());
 421:         lg.setShapeOutlineVisible(item.isShapeOutlineVisible());
 422:         lg.setOutlinePaint(item.getOutlinePaint());
 423:         lg.setOutlineStroke(item.getOutlineStroke());
 424:         lg.setPadding(this.legendItemGraphicPadding);
 425: 
 426:         LegendItemBlockContainer legendItem = new LegendItemBlockContainer(
 427:                 new BorderArrangement(), item.getDatasetIndex(), 
 428:                 item.getSeriesIndex());
 429:         lg.setShapeAnchor(getLegendItemGraphicAnchor());
 430:         lg.setShapeLocation(getLegendItemGraphicLocation());
 431:         legendItem.add(lg, this.legendItemGraphicEdge);
 432:         LabelBlock labelBlock = new LabelBlock(item.getLabel(), this.itemFont, 
 433:                 this.itemPaint);
 434:         labelBlock.setPadding(this.itemLabelPadding);
 435:         labelBlock.setToolTipText(item.getToolTipText());
 436:         legendItem.add(labelBlock);
 437:         
 438:         result = new BlockContainer(new CenterArrangement());
 439:         result.add(legendItem);
 440:         
 441:         return result;
 442:     }
 443:     
 444:     /**
 445:      * Returns the container that holds the legend items.
 446:      * 
 447:      * @return The container for the legend items.
 448:      */
 449:     public BlockContainer getItemContainer() {
 450:         return this.items;
 451:     }
 452: 
 453:     /**
 454:      * Arranges the contents of the block, within the given constraints, and 
 455:      * returns the block size.
 456:      * 
 457:      * @param g2  the graphics device.
 458:      * @param constraint  the constraint (<code>null</code> not permitted).
 459:      * 
 460:      * @return The block size (in Java2D units, never <code>null</code>).
 461:      */
 462:     public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
 463:         Size2D result = new Size2D();
 464:         fetchLegendItems();
 465:         if (this.items.isEmpty()) {
 466:             return result;   
 467:         }
 468:         BlockContainer container = this.wrapper;
 469:         if (container == null) {
 470:             container = this.items;
 471:         }
 472:         RectangleConstraint c = toContentConstraint(constraint);
 473:         Size2D size = container.arrange(g2, c);
 474:         result.height = calculateTotalHeight(size.height);
 475:         result.width = calculateTotalWidth(size.width);
 476:         return result;
 477:     }
 478: 
 479:     /**
 480:      * Draws the title on a Java 2D graphics device (such as the screen or a
 481:      * printer).
 482:      *
 483:      * @param g2  the graphics device.
 484:      * @param area  the available area for the title.
 485:      */
 486:     public void draw(Graphics2D g2, Rectangle2D area) {
 487:         draw(g2, area, null);
 488:     }
 489: 
 490:     /**
 491:      * Draws the block within the specified area.
 492:      * 
 493:      * @param g2  the graphics device.
 494:      * @param area  the area.
 495:      * @param params  ignored (<code>null</code> permitted).
 496:      * 
 497:      * @return An {@link org.jfree.chart.block.EntityBlockResult} or 
 498:      *         <code>null</code>.
 499:      */
 500:     public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
 501:         Rectangle2D target = (Rectangle2D) area.clone();
 502:         target = trimMargin(target);
 503:         if (this.backgroundPaint != null) {
 504:             g2.setPaint(this.backgroundPaint);
 505:             g2.fill(target);
 506:         }
 507:         getBorder().draw(g2, target);
 508:         getBorder().getInsets().trim(target);
 509:         BlockContainer container = this.wrapper;
 510:         if (container == null) {
 511:             container = this.items; 
 512:         }
 513:         target = trimPadding(target);
 514:         return container.draw(g2, target, params);   
 515:     }
 516: 
 517:     /**
 518:      * Sets the wrapper container for the legend.
 519:      * 
 520:      * @param wrapper  the wrapper container.
 521:      */
 522:     public void setWrapper(BlockContainer wrapper) {
 523:         this.wrapper = wrapper;
 524:     }
 525:     
 526:     /**
 527:      * Tests this title for equality with an arbitrary object.
 528:      * 
 529:      * @param obj  the object (<code>null</code> permitted).
 530:      * 
 531:      * @return A boolean.
 532:      */
 533:     public boolean equals(Object obj) {
 534:         if (obj == this) {
 535:             return true;   
 536:         }
 537:         if (!(obj instanceof LegendTitle)) {
 538:             return false;   
 539:         }
 540:         if (!super.equals(obj)) {
 541:             return false;   
 542:         }
 543:         LegendTitle that = (LegendTitle) obj;
 544:         if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) {
 545:             return false;   
 546:         }
 547:         if (this.legendItemGraphicEdge != that.legendItemGraphicEdge) {
 548:             return false;   
 549:         }
 550:         if (this.legendItemGraphicAnchor != that.legendItemGraphicAnchor) {
 551:             return false;   
 552:         }
 553:         if (this.legendItemGraphicLocation != that.legendItemGraphicLocation) {
 554:             return false;   
 555:         }
 556:         if (!this.itemFont.equals(that.itemFont)) {
 557:             return false;   
 558:         }
 559:         if (!this.itemPaint.equals(that.itemPaint)) {
 560:             return false;   
 561:         }
 562:         if (!this.hLayout.equals(that.hLayout)) {
 563:             return false;   
 564:         }
 565:         if (!this.vLayout.equals(that.vLayout)) {
 566:             return false;   
 567:         }
 568:         return true;
 569:     }
 570:     
 571:     /**
 572:      * Provides serialization support.
 573:      *
 574:      * @param stream  the output stream.
 575:      *
 576:      * @throws IOException  if there is an I/O error.
 577:      */
 578:     private void writeObject(ObjectOutputStream stream) throws IOException {
 579:         stream.defaultWriteObject();
 580:         SerialUtilities.writePaint(this.backgroundPaint, stream);
 581:         SerialUtilities.writePaint(this.itemPaint, stream);
 582:     }
 583: 
 584:     /**
 585:      * Provides serialization support.
 586:      *
 587:      * @param stream  the input stream.
 588:      *
 589:      * @throws IOException  if there is an I/O error.
 590:      * @throws ClassNotFoundException  if there is a classpath problem.
 591:      */
 592:     private void readObject(ObjectInputStream stream) 
 593:         throws IOException, ClassNotFoundException {
 594:         stream.defaultReadObject();
 595:         this.backgroundPaint = SerialUtilities.readPaint(stream);
 596:         this.itemPaint = SerialUtilities.readPaint(stream);
 597:     }
 598: 
 599: }