org.jfree.chart.title.CompositeTitle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jfreechart Show documentation
Show all versions of jfreechart Show documentation
JFreeChart is a class library, written in Java, for generating charts.
Utilising the Java2D API, it supports a wide range of chart types including
bar charts, pie charts, line charts, XY-plots, time series plots, Sankey charts
and more.
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-present, by David Gilbert and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.]
*
* -------------------
* CompositeTitle.java
* -------------------
* (C) Copyright 2005-present, by David Gilbert and Contributors.
*
* Original Author: David Gilbert;
* Contributor(s): Eric Penfold (patch 2006826);
* Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
*
*/
package org.jfree.chart.title;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Objects;
import org.jfree.chart.HashUtils;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.RectangleConstraint;
import org.jfree.chart.event.TitleChangeEvent;
import org.jfree.chart.ui.Size2D;
import org.jfree.chart.util.PaintUtils;
import org.jfree.chart.util.Args;
import org.jfree.chart.util.SerialUtils;
/**
* A title that contains multiple titles within a {@link BlockContainer}.
*/
public class CompositeTitle extends Title implements Cloneable, Serializable {
/** For serialization. */
private static final long serialVersionUID = -6770854036232562290L;
/**
* The background paint.
*/
private transient Paint backgroundPaint;
/** A container for the individual titles. */
private BlockContainer container;
/**
* Creates a new composite title with a default border arrangement.
*/
public CompositeTitle() {
this(new BlockContainer(new BorderArrangement()));
}
/**
* Creates a new title using the specified container.
*
* @param container the container ({@code null} not permitted).
*/
public CompositeTitle(BlockContainer container) {
Args.nullNotPermitted(container, "container");
this.container = container;
this.backgroundPaint = null;
}
/**
* Returns the background paint.
*
* @return The paint (possibly {@code null}).
*/
public Paint getBackgroundPaint() {
return this.backgroundPaint;
}
/**
* Sets the background paint and sends a {@link TitleChangeEvent} to all
* registered listeners. If you set this attribute to {@code null},
* no background is painted (which makes the title background transparent).
*
* @param paint the background paint ({@code null} permitted).
*/
public void setBackgroundPaint(Paint paint) {
this.backgroundPaint = paint;
notifyListeners(new TitleChangeEvent(this));
}
/**
* Returns the container holding the titles.
*
* @return The title container (never {@code null}).
*/
public BlockContainer getContainer() {
return this.container;
}
/**
* Sets the title container.
*
* @param container the container ({@code null} not permitted).
*/
public void setTitleContainer(BlockContainer container) {
Args.nullNotPermitted(container, "container");
this.container = container;
}
/**
* Arranges the contents of the block, within the given constraints, and
* returns the block size.
*
* @param g2 the graphics device.
* @param constraint the constraint ({@code null} not permitted).
*
* @return The block size (in Java2D units, never {@code null}).
*/
@Override
public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
RectangleConstraint contentConstraint = toContentConstraint(constraint);
Size2D contentSize = this.container.arrange(g2, contentConstraint);
return new Size2D(calculateTotalWidth(contentSize.getWidth()),
calculateTotalHeight(contentSize.getHeight()));
}
/**
* Draws the title on a Java 2D graphics device (such as the screen or a
* printer).
*
* @param g2 the graphics device.
* @param area the area allocated for the title.
*/
@Override
public void draw(Graphics2D g2, Rectangle2D area) {
draw(g2, area, null);
}
/**
* Draws the block within the specified area.
*
* @param g2 the graphics device.
* @param area the area.
* @param params ignored ({@code null} permitted).
*
* @return Always {@code null}.
*/
@Override
public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
area = trimMargin(area);
drawBorder(g2, area);
area = trimBorder(area);
if (this.backgroundPaint != null) {
g2.setPaint(this.backgroundPaint);
g2.fill(area);
}
area = trimPadding(area);
return this.container.draw(g2, area, params);
}
/**
* Tests this title for equality with an arbitrary object.
*
* @param obj the object ({@code null} permitted).
*
* @return A boolean.
*/
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof CompositeTitle)) {
return false;
}
CompositeTitle that = (CompositeTitle) obj;
if (!Objects.equals(this.container, that.container)) {
return false;
}
if (!PaintUtils.equal(this.backgroundPaint, that.backgroundPaint)) {
return false;
}
if (!that.canEqual(this)) {
return false;
}
return super.equals(obj);
}
/**
* Ensures symmetry between super/subclass implementations of equals. For
* more detail, see http://jqno.nl/equalsverifier/manual/inheritance.
*
* @param other Object
*
* @return true ONLY if the parameter is THIS class type
*/
@Override
public boolean canEqual(Object other) {
// fix the "equals not symmetric" problem
return (other instanceof CompositeTitle);
}
@Override
public int hashCode() {
int hash = super.hashCode(); // equals calls superclass, hashCode must also
hash = 53 * hash + Objects.hashCode(this.container);
hash = 53 * hash + HashUtils.hashCodeForPaint(this.backgroundPaint);
return hash;
}
/**
* Provides serialization support.
*
* @param stream the output stream.
*
* @throws IOException if there is an I/O error.
*/
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
SerialUtils.writePaint(this.backgroundPaint, stream);
}
/**
* Provides serialization support.
*
* @param stream the input stream.
*
* @throws IOException if there is an I/O error.
* @throws ClassNotFoundException if there is a classpath problem.
*/
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
this.backgroundPaint = SerialUtils.readPaint(stream);
}
}