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