Source for org.jfree.chart.title.CompositeTitle

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2005, 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:  * CompositeTitle.java
  29:  * -------------------
  30:  * (C) Copyright 2005, by David Gilbert and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: CompositeTitle.java,v 1.14.2.1 2005/10/25 20:58:34 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 19-Nov-2004 : Version 1 (DG);
  40:  * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
  41:  * 04-Feb-2005 : Implemented MAXIMUM_WIDTH in calculateSize (DG);
  42:  * 20-Apr-2005 : Added new draw() method (DG);
  43:  * 03-May-2005 : Implemented equals() method (DG);
  44:  *
  45:  */
  46: 
  47: package org.jfree.chart.title;
  48: 
  49: import java.awt.Graphics2D;
  50: import java.awt.geom.Rectangle2D;
  51: import java.io.Serializable;
  52: 
  53: import org.jfree.chart.block.BlockContainer;
  54: import org.jfree.chart.block.BorderArrangement;
  55: import org.jfree.chart.block.RectangleConstraint;
  56: import org.jfree.ui.Size2D;
  57: 
  58: /**
  59:  * A title that contains multiple titles within a {@link BlockContainer}.
  60:  */
  61: public class CompositeTitle extends Title implements Cloneable, Serializable {
  62:     
  63:     /** For serialization. */
  64:     private static final long serialVersionUID = -6770854036232562290L;
  65:     
  66:     /** A container for the individual titles. */
  67:     private BlockContainer container;
  68:     
  69:     /**
  70:      * Creates a new composite title with a default border arrangement.
  71:      */
  72:     public CompositeTitle() {
  73:         this(new BlockContainer(new BorderArrangement()));   
  74:     }
  75:     
  76:     /**
  77:      * Creates a new title using the specified container. 
  78:      * 
  79:      * @param container  the container (<code>null</code> not permitted).
  80:      */
  81:     public CompositeTitle(BlockContainer container) {
  82:         if (container == null) {
  83:             throw new IllegalArgumentException("Null 'container' argument.");
  84:         }
  85:         this.container = container;
  86:     }
  87:     
  88:     /**
  89:      * Returns the container holding the titles.
  90:      * 
  91:      * @return The title container (never <code>null</code>).
  92:      */
  93:     public BlockContainer getContainer() {
  94:         return this.container;
  95:     }
  96:     
  97:     /**
  98:      * Sets the title container.
  99:      * 
 100:      * @param container  the container (<code>null</code> not permitted).
 101:      */
 102:     public void setTitleContainer(BlockContainer container) {
 103:         if (container == null) {
 104:             throw new IllegalArgumentException("Null 'container' argument.");
 105:         }
 106:         this.container = container;    
 107:     }
 108:     
 109:     /**
 110:      * Arranges the contents of the block, within the given constraints, and 
 111:      * returns the block size.
 112:      * 
 113:      * @param g2  the graphics device.
 114:      * @param constraint  the constraint (<code>null</code> not permitted).
 115:      * 
 116:      * @return The block size (in Java2D units, never <code>null</code>).
 117:      */
 118:     public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
 119:         RectangleConstraint contentConstraint = toContentConstraint(constraint);
 120:         Size2D contentSize = this.container.arrange(g2, contentConstraint);
 121:         return new Size2D(
 122:             calculateTotalWidth(contentSize.getWidth()), 
 123:             calculateTotalHeight(contentSize.getHeight())
 124:         );
 125:     }
 126:     
 127:     /**
 128:      * Draws the title on a Java 2D graphics device (such as the screen or a 
 129:      * printer).
 130:      *
 131:      * @param g2  the graphics device.
 132:      * @param area  the area allocated for the title.
 133:      */
 134:     public void draw(Graphics2D g2, Rectangle2D area) {
 135:         area = trimMargin(area);
 136:         drawBorder(g2, area);
 137:         area = trimBorder(area);
 138:         area = trimPadding(area);
 139:         this.container.draw(g2, area);
 140:     }
 141:     
 142:     /**
 143:      * Draws the block within the specified area.
 144:      * 
 145:      * @param g2  the graphics device.
 146:      * @param area  the area.
 147:      * @param params  ignored (<code>null</code> permitted).
 148:      * 
 149:      * @return Always <code>null</code>.
 150:      */
 151:     public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
 152:         draw(g2, area);
 153:         return null;
 154:     }
 155:     
 156:     /**
 157:      * Tests this title for equality with an arbitrary object.
 158:      * 
 159:      * @param obj  the object (<code>null</code> permitted).
 160:      * 
 161:      * @return A boolean.
 162:      */
 163:     public boolean equals(Object obj) {
 164:         if (obj == this) {
 165:             return true;   
 166:         }
 167:         if (!(obj instanceof CompositeTitle)) {
 168:             return false;   
 169:         }
 170:         if (!super.equals(obj)) {
 171:             return false;   
 172:         }
 173:         CompositeTitle that = (CompositeTitle) obj;
 174:         if (!this.container.equals(that.container)) {
 175:             return false;   
 176:         }
 177:         return true;
 178:     }
 179: 
 180: }