001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it 
010 * under the terms of the GNU Lesser General Public License as published by 
011 * the Free Software Foundation; either version 2.1 of the License, or 
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but 
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022 * USA.  
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025 * in the United States and other countries.]
026 *
027 * -------------------
028 * CompositeTitle.java
029 * -------------------
030 * (C) Copyright 2005, 2007, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 19-Nov-2004 : Version 1 (DG);
038 * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
039 * 04-Feb-2005 : Implemented MAXIMUM_WIDTH in calculateSize (DG);
040 * 20-Apr-2005 : Added new draw() method (DG);
041 * 03-May-2005 : Implemented equals() method (DG);
042 *
043 */
044
045package org.jfree.chart.title;
046
047import java.awt.Graphics2D;
048import java.awt.geom.Rectangle2D;
049import java.io.Serializable;
050
051import org.jfree.chart.block.BlockContainer;
052import org.jfree.chart.block.BorderArrangement;
053import org.jfree.chart.block.RectangleConstraint;
054import org.jfree.ui.Size2D;
055
056/**
057 * A title that contains multiple titles within a {@link BlockContainer}.
058 */
059public class CompositeTitle extends Title implements Cloneable, Serializable {
060    
061    /** For serialization. */
062    private static final long serialVersionUID = -6770854036232562290L;
063    
064    /** A container for the individual titles. */
065    private BlockContainer container;
066    
067    /**
068     * Creates a new composite title with a default border arrangement.
069     */
070    public CompositeTitle() {
071        this(new BlockContainer(new BorderArrangement()));   
072    }
073    
074    /**
075     * Creates a new title using the specified container. 
076     * 
077     * @param container  the container (<code>null</code> not permitted).
078     */
079    public CompositeTitle(BlockContainer container) {
080        if (container == null) {
081            throw new IllegalArgumentException("Null 'container' argument.");
082        }
083        this.container = container;
084    }
085    
086    /**
087     * Returns the container holding the titles.
088     * 
089     * @return The title container (never <code>null</code>).
090     */
091    public BlockContainer getContainer() {
092        return this.container;
093    }
094    
095    /**
096     * Sets the title container.
097     * 
098     * @param container  the container (<code>null</code> not permitted).
099     */
100    public void setTitleContainer(BlockContainer container) {
101        if (container == null) {
102            throw new IllegalArgumentException("Null 'container' argument.");
103        }
104        this.container = container;    
105    }
106    
107    /**
108     * Arranges the contents of the block, within the given constraints, and 
109     * returns the block size.
110     * 
111     * @param g2  the graphics device.
112     * @param constraint  the constraint (<code>null</code> not permitted).
113     * 
114     * @return The block size (in Java2D units, never <code>null</code>).
115     */
116    public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
117        RectangleConstraint contentConstraint = toContentConstraint(constraint);
118        Size2D contentSize = this.container.arrange(g2, contentConstraint);
119        return new Size2D(
120            calculateTotalWidth(contentSize.getWidth()), 
121            calculateTotalHeight(contentSize.getHeight())
122        );
123    }
124    
125    /**
126     * Draws the title on a Java 2D graphics device (such as the screen or a 
127     * printer).
128     *
129     * @param g2  the graphics device.
130     * @param area  the area allocated for the title.
131     */
132    public void draw(Graphics2D g2, Rectangle2D area) {
133        area = trimMargin(area);
134        drawBorder(g2, area);
135        area = trimBorder(area);
136        area = trimPadding(area);
137        this.container.draw(g2, area);
138    }
139    
140    /**
141     * Draws the block within the specified area.
142     * 
143     * @param g2  the graphics device.
144     * @param area  the area.
145     * @param params  ignored (<code>null</code> permitted).
146     * 
147     * @return Always <code>null</code>.
148     */
149    public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
150        draw(g2, area);
151        return null;
152    }
153    
154    /**
155     * Tests this title for equality with an arbitrary object.
156     * 
157     * @param obj  the object (<code>null</code> permitted).
158     * 
159     * @return A boolean.
160     */
161    public boolean equals(Object obj) {
162        if (obj == this) {
163            return true;   
164        }
165        if (!(obj instanceof CompositeTitle)) {
166            return false;   
167        }
168        if (!super.equals(obj)) {
169            return false;   
170        }
171        CompositeTitle that = (CompositeTitle) obj;
172        if (!this.container.equals(that.container)) {
173            return false;   
174        }
175        return true;
176    }
177
178}