001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ------------------- 028 * CompositeTitle.java 029 * ------------------- 030 * (C) Copyright 2005-2013, by David Gilbert and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Eric Penfold (patch 2006826); 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 * 02-Jul-2008 : Applied patch 2006826 by Eric Penfold, to enable chart 043 * entities to be generated (DG); 044 * 09-Jul-2008 : Added backgroundPaint field (DG); 045 * 03-Jul-2013 : Use ParamChecks (DG); 046 * 047 */ 048 049package org.jfree.chart.title; 050 051import java.awt.Graphics2D; 052import java.awt.Paint; 053import java.awt.geom.Rectangle2D; 054import java.io.IOException; 055import java.io.ObjectInputStream; 056import java.io.ObjectOutputStream; 057import java.io.Serializable; 058 059import org.jfree.chart.block.BlockContainer; 060import org.jfree.chart.block.BorderArrangement; 061import org.jfree.chart.block.RectangleConstraint; 062import org.jfree.chart.event.TitleChangeEvent; 063import org.jfree.chart.util.ParamChecks; 064import org.jfree.io.SerialUtilities; 065import org.jfree.ui.Size2D; 066import org.jfree.util.PaintUtilities; 067 068/** 069 * A title that contains multiple titles within a {@link BlockContainer}. 070 */ 071public class CompositeTitle extends Title implements Cloneable, Serializable { 072 073 /** For serialization. */ 074 private static final long serialVersionUID = -6770854036232562290L; 075 076 /** 077 * The background paint. 078 * 079 * @since 1.0.11 080 */ 081 private transient Paint backgroundPaint; 082 083 /** A container for the individual titles. */ 084 private BlockContainer container; 085 086 /** 087 * Creates a new composite title with a default border arrangement. 088 */ 089 public CompositeTitle() { 090 this(new BlockContainer(new BorderArrangement())); 091 } 092 093 /** 094 * Creates a new title using the specified container. 095 * 096 * @param container the container (<code>null</code> not permitted). 097 */ 098 public CompositeTitle(BlockContainer container) { 099 ParamChecks.nullNotPermitted(container, "container"); 100 this.container = container; 101 this.backgroundPaint = null; 102 } 103 104 /** 105 * Returns the background paint. 106 * 107 * @return The paint (possibly <code>null</code>). 108 * 109 * @since 1.0.11 110 */ 111 public Paint getBackgroundPaint() { 112 return this.backgroundPaint; 113 } 114 115 /** 116 * Sets the background paint and sends a {@link TitleChangeEvent} to all 117 * registered listeners. If you set this attribute to <code>null</code>, 118 * no background is painted (which makes the title background transparent). 119 * 120 * @param paint the background paint (<code>null</code> permitted). 121 * 122 * @since 1.0.11 123 */ 124 public void setBackgroundPaint(Paint paint) { 125 this.backgroundPaint = paint; 126 notifyListeners(new TitleChangeEvent(this)); 127 } 128 129 /** 130 * Returns the container holding the titles. 131 * 132 * @return The title container (never <code>null</code>). 133 */ 134 public BlockContainer getContainer() { 135 return this.container; 136 } 137 138 /** 139 * Sets the title container. 140 * 141 * @param container the container (<code>null</code> not permitted). 142 */ 143 public void setTitleContainer(BlockContainer container) { 144 ParamChecks.nullNotPermitted(container, "container"); 145 this.container = container; 146 } 147 148 /** 149 * Arranges the contents of the block, within the given constraints, and 150 * returns the block size. 151 * 152 * @param g2 the graphics device. 153 * @param constraint the constraint (<code>null</code> not permitted). 154 * 155 * @return The block size (in Java2D units, never <code>null</code>). 156 */ 157 @Override 158 public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) { 159 RectangleConstraint contentConstraint = toContentConstraint(constraint); 160 Size2D contentSize = this.container.arrange(g2, contentConstraint); 161 return new Size2D(calculateTotalWidth(contentSize.getWidth()), 162 calculateTotalHeight(contentSize.getHeight())); 163 } 164 165 /** 166 * Draws the title on a Java 2D graphics device (such as the screen or a 167 * printer). 168 * 169 * @param g2 the graphics device. 170 * @param area the area allocated for the title. 171 */ 172 @Override 173 public void draw(Graphics2D g2, Rectangle2D area) { 174 draw(g2, area, null); 175 } 176 177 /** 178 * Draws the block within the specified area. 179 * 180 * @param g2 the graphics device. 181 * @param area the area. 182 * @param params ignored (<code>null</code> permitted). 183 * 184 * @return Always <code>null</code>. 185 */ 186 @Override 187 public Object draw(Graphics2D g2, Rectangle2D area, Object params) { 188 area = trimMargin(area); 189 drawBorder(g2, area); 190 area = trimBorder(area); 191 if (this.backgroundPaint != null) { 192 g2.setPaint(this.backgroundPaint); 193 g2.fill(area); 194 } 195 area = trimPadding(area); 196 return this.container.draw(g2, area, params); 197 } 198 199 /** 200 * Tests this title for equality with an arbitrary object. 201 * 202 * @param obj the object (<code>null</code> permitted). 203 * 204 * @return A boolean. 205 */ 206 @Override 207 public boolean equals(Object obj) { 208 if (obj == this) { 209 return true; 210 } 211 if (!(obj instanceof CompositeTitle)) { 212 return false; 213 } 214 CompositeTitle that = (CompositeTitle) obj; 215 if (!this.container.equals(that.container)) { 216 return false; 217 } 218 if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) { 219 return false; 220 } 221 return super.equals(obj); 222 } 223 224 /** 225 * Provides serialization support. 226 * 227 * @param stream the output stream. 228 * 229 * @throws IOException if there is an I/O error. 230 */ 231 private void writeObject(ObjectOutputStream stream) throws IOException { 232 stream.defaultWriteObject(); 233 SerialUtilities.writePaint(this.backgroundPaint, stream); 234 } 235 236 /** 237 * Provides serialization support. 238 * 239 * @param stream the input stream. 240 * 241 * @throws IOException if there is an I/O error. 242 * @throws ClassNotFoundException if there is a classpath problem. 243 */ 244 private void readObject(ObjectInputStream stream) 245 throws IOException, ClassNotFoundException { 246 stream.defaultReadObject(); 247 this.backgroundPaint = SerialUtilities.readPaint(stream); 248 } 249 250}