001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * LegendItemBlockContainer.java
029 * -----------------------------
030 * (C) Copyright 2006-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.title;
038
039import java.awt.Graphics2D;
040import java.awt.Shape;
041import java.awt.geom.Rectangle2D;
042
043import org.jfree.chart.block.Arrangement;
044import org.jfree.chart.block.BlockContainer;
045import org.jfree.chart.block.BlockResult;
046import org.jfree.chart.block.EntityBlockParams;
047import org.jfree.chart.block.EntityBlockResult;
048import org.jfree.chart.entity.EntityCollection;
049import org.jfree.chart.entity.LegendItemEntity;
050import org.jfree.chart.entity.StandardEntityCollection;
051import org.jfree.data.general.Dataset;
052
053/**
054 * A container that holds all the pieces of a single legend item.
055 */
056public class LegendItemBlockContainer extends BlockContainer {
057
058    /**
059     * The dataset.
060     */
061    private Dataset dataset;
062
063    /**
064     * The series key.
065     */
066    private Comparable seriesKey;
067
068    /** The dataset index. */
069    private int datasetIndex;
070
071    /** The series index. */
072    private int series;
073
074    /** The tool tip text (can be {@code null}). */
075    private String toolTipText;
076
077    /** The URL text (can be {@code null}). */
078    private String urlText;
079
080    /**
081     * Creates a new legend item block.
082     *
083     * @param arrangement  the arrangement.
084     * @param dataset  the dataset.
085     * @param seriesKey  the series key.
086     */
087    public LegendItemBlockContainer(Arrangement arrangement, Dataset dataset,
088            Comparable seriesKey) {
089        super(arrangement);
090        this.dataset = dataset;
091        this.seriesKey = seriesKey;
092    }
093
094    /**
095     * Returns a reference to the dataset for the associated legend item.
096     *
097     * @return A dataset reference.
098     */
099    public Dataset getDataset() {
100        return this.dataset;
101    }
102
103    /**
104     * Returns the series key.
105     *
106     * @return The series key.
107     */
108    public Comparable getSeriesKey() {
109        return this.seriesKey;
110    }
111
112    /**
113     * Returns the series index.
114     *
115     * @return The series index.
116     */
117    public int getSeriesIndex() {
118        return this.series;
119    }
120
121    /**
122     * Returns the tool tip text.
123     *
124     * @return The tool tip text (possibly {@code null}).
125     */
126    public String getToolTipText() {
127        return this.toolTipText;
128    }
129
130    /**
131     * Sets the tool tip text.
132     *
133     * @param text  the text ({@code null} permitted).
134     */
135    public void setToolTipText(String text) {
136        this.toolTipText = text;
137    }
138
139    /**
140     * Returns the URL text.
141     *
142     * @return The URL text (possibly {@code null}).
143     */
144    public String getURLText() {
145        return this.urlText;
146    }
147
148    /**
149     * Sets the URL text.
150     *
151     * @param text  the text ({@code null} permitted).
152     */
153    public void setURLText(String text) {
154        this.urlText = text;
155    }
156
157    /**
158     * Draws the block within the specified area.
159     *
160     * @param g2  the graphics device.
161     * @param area  the area.
162     * @param params  passed on to blocks within the container
163     *                ({@code null} permitted).
164     *
165     * @return An instance of {@link EntityBlockResult}, or {@code null}.
166     */
167    @Override
168    public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
169        // draw the block without collecting entities
170        super.draw(g2, area, null);
171        EntityBlockParams ebp;
172        BlockResult r = new BlockResult();
173        if (params instanceof EntityBlockParams) {
174            ebp = (EntityBlockParams) params;
175            if (ebp.getGenerateEntities()) {
176                EntityCollection ec = new StandardEntityCollection();
177                LegendItemEntity entity = new LegendItemEntity(
178                        (Shape) area.clone());
179                entity.setSeriesKey(this.seriesKey);
180                entity.setDataset(this.dataset);
181                entity.setToolTipText(getToolTipText());
182                entity.setURLText(getURLText());
183                ec.add(entity);
184                r.setEntityCollection(ec);
185            }
186        }
187        return r;
188    }
189}