    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     * LegendItemBlockContainer.java
    029     * -----------------------------
    030     * (C) Copyright 2006, 2007, 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
    043    package org.jfree.chart.title;
    044
    045    import java.awt.Graphics2D;
    046    import java.awt.Shape;
    047    import java.awt.geom.Rectangle2D;
    048
    049    import org.jfree.chart.block.Arrangement;
    050    import org.jfree.chart.block.BlockContainer;
    051    import org.jfree.chart.block.BlockResult;
    052    import org.jfree.chart.block.EntityBlockParams;
    053    import org.jfree.chart.block.EntityBlockResult;
    054    import org.jfree.chart.entity.EntityCollection;
    055    import org.jfree.chart.entity.LegendItemEntity;
    056    import org.jfree.chart.entity.StandardEntityCollection;
    057    import 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     */
    064    public 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</code>). */
    087        private String toolTipText;
    088
    089        /** The URL text (can be <code>null</code>). */
    090        private String urlText;
    091
    092        /**
    093         * Creates a new legend item block.
    094         *
    095         * @param arrangement  the arrangement.
    096         * @param datasetIndex  the dataset index.
    097         * @param series  the series index.
    098         *
    099         * @deprecated As of 1.0.6, use the other constructor.
    100         */
    101        public LegendItemBlockContainer(Arrangement arrangement, int datasetIndex,
    102                int series) {
    103            super(arrangement);
    104            this.datasetIndex = datasetIndex;
    105            this.series = series;
    106        }
    107
    108        /**
    109         * Creates a new legend item block.
    110         *
    111         * @param arrangement  the arrangement.
    112         * @param dataset  the dataset.
    113         * @param seriesKey  the series key.
    114         *
    115         * @since 1.0.6
    116         */
    117        public LegendItemBlockContainer(Arrangement arrangement, Dataset dataset,
    118                Comparable seriesKey) {
    119            super(arrangement);
    120            this.dataset = dataset;
    121            this.seriesKey = seriesKey;
    122        }
    123
    124        /**
    125         * Returns a reference to the dataset for the associated legend item.
    126         *
    127         * @return A dataset reference.
    128         *
    129         * @since 1.0.6
    130         */
    131        public Dataset getDataset() {
    132            return this.dataset;
    133        }
    134
    135        /**
    136         * Returns the series key.
    137         *
    138         * @return The series key.
    139         *
    140         * @since 1.0.6
    141         */
    142        public Comparable getSeriesKey() {
    143            return this.seriesKey;
    144        }
    145
    146        /**
    147         * Returns the dataset index.
    148         *
    149         * @return The dataset index.
    150         *
    151         * @deprecated As of 1.0.6, use the {@link #getDataset()} method.
    152         */
    153        public int getDatasetIndex() {
    154            return this.datasetIndex;
    155        }
    156
    157        /**
    158         * Returns the series index.
    159         *
    160         * @return The series index.
    161         */
    162        public int getSeriesIndex() {
    163            return this.series;
    164        }
    165
    166        /**
    167         * Returns the tool tip text.
    168         *
    169         * @return The tool tip text (possibly <code>null</code>).
    170         *
    171         * @since 1.0.3
    172         */
    173        public String getToolTipText() {
    174            return this.toolTipText;
    175        }
    176
    177        /**
    178         * Sets the tool tip text.
    179         *
    180         * @param text  the text (<code>null</code> permitted).
    181         *
    182         * @since 1.0.3
    183         */
    184        public void setToolTipText(String text) {
    185            this.toolTipText = text;
    186        }
    187
    188        /**
    189         * Returns the URL text.
    190         *
    191         * @return The URL text (possibly <code>null</code>).
    192         *
    193         * @since 1.0.3
    194         */
    195        public String getURLText() {
    196            return this.urlText;
    197        }
    198
    199        /**
    200         * Sets the URL text.
    201         *
    202         * @param text  the text (<code>null</code> permitted).
    203         *
    204         * @since 1.0.3
    205         */
    206        public void setURLText(String text) {
    207            this.urlText = text;
    208        }
    209
    210        /**
    211         * Draws the block within the specified area.
    212         *
    213         * @param g2  the graphics device.
    214         * @param area  the area.
    215         * @param params  passed on to blocks within the container
    216         *                (<code>null</code> permitted).
    217         *
    218         * @return An instance of {@link EntityBlockResult}, or <code>null</code>.
    219         */
    220        public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
    221            // draw the block without collecting entities
    222            super.draw(g2, area, null);
    223            EntityBlockParams ebp = null;
    224            BlockResult r = new BlockResult();
    225            if (params instanceof EntityBlockParams) {
    226                ebp = (EntityBlockParams) params;
    227                if (ebp.getGenerateEntities()) {
    228                    EntityCollection ec = new StandardEntityCollection();
    229                    LegendItemEntity entity = new LegendItemEntity(
    230                            (Shape) area.clone());
    231                    entity.setSeriesIndex(this.series);
    232                    entity.setSeriesKey(this.seriesKey);
    233                    entity.setDataset(this.dataset);
    234                    entity.setToolTipText(getToolTipText());
    235                    entity.setURLText(getURLText());
    236                    ec.add(entity);
    237                    r.setEntityCollection(ec);
    238                }
    239            }
    240            return r;
    241        }
    242    }