    001    /* ======================================
    002     * JFreeChart : a free Java chart library
    003     * ======================================
    004     *
    005     * (C) Copyright 2000-2005, 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     * CustomPieURLGenerator.java
    029     * --------------------------
    030     * (C) Copyright 2004-2005, by David Basten and Contributors.
    031     *
    032     * Original Author:  David Basten;
    033     * Contributors:     -;
    034     *
    035     * $Id: CustomPieURLGenerator.java,v 1.3.2.1 2005/10/25 20:59:31 mungady Exp $
    036     *
    037     * Changes:
    038     * --------
    039     * 04-Feb-2004 : Version 1, contributed by David Basten based on
    040     *               CustomXYURLGenerator by Richard Atkinson (added to main source
    041     *               tree on 25-May-2004);
    042     *
    043     */
    044    package org.jfree.chart.urls;
    045
    046    import java.io.Serializable;
    047    import java.util.ArrayList;
    048    import java.util.HashMap;
    049    import java.util.Iterator;
    050    import java.util.Map;
    051    import java.util.Set;
    052
    053    import org.jfree.data.general.PieDataset;
    054    import org.jfree.util.PublicCloneable;
    055
    056    /**
    057     * A custom URL generator for pie charts.
    058     */
    059    public class CustomPieURLGenerator implements PieURLGenerator,
    060                                                  Cloneable,
    061                                                  PublicCloneable,
    062                                                  Serializable {
    063
    064        /** For serialization. */
    065        private static final long serialVersionUID = 7100607670144900503L;
    066
    067        /** Storage for the URLs. */
    068        private ArrayList urls;
    069
    070        /**
    071         * Default constructor.
    072         */
    073        public CustomPieURLGenerator() {
    074            this.urls = new ArrayList();
    075        }
    076
    077        /**
    078         * Generates a URL.
    079         *
    080         * @param dataset  the dataset.
    081         * @param key  the item key.
    082         * @param pieIndex  the pie index (ignored).
    083         *
    084         * @return A string containing the generated URL.
    085         */
    086        public String generateURL(PieDataset dataset, Comparable key,
    087                                  int pieIndex) {
    088            return getURL(key, pieIndex);
    089        }
    090
    091        /**
    092         * Returns the number of URL lists stored by the renderer.
    093         *
    094         * @return The list count.
    095         */
    096        public int getListCount() {
    097            return this.urls.size();
    098        }
    099
    100        /**
    101         * Returns the number of URLs in a given list.
    102         *
    103         * @param list  the list index (zero based).
    104         *
    105         * @return The URL count.
    106         */
    107        public int getURLCount(int list) {
    108
    109            int result = 0;
    110            Map urlMap = (Map) this.urls.get(list);
    111            if (urlMap != null) {
    112                result = urlMap.size();
    113            }
    114            return result;
    115        }
    116
    117        /**
    118         * Returns the URL for an item.
    119         *
    120         * @param key  the key.
    121         * @param pieItem  the item index.
    122         *
    123         * @return The URL.
    124         */
    125        public String getURL(Comparable key, int pieItem) {
    126
    127            String result = null;
    128
    129            if (pieItem < getListCount()) {
    130                Map urlMap = (Map) this.urls.get(pieItem);
    131                if (urlMap != null) {
    132                    result = (String) urlMap.get(key);
    133                }
    134            }
    135
    136            return result;
    137        }
    138
    139        /**
    140         * Adds a map of URLs.
    141         *
    142         * @param urlMap  the URLs.
    143         */
    144        public void addURLs(Map urlMap) {
    145            this.urls.add(urlMap);
    146        }
    147
    148        /**
    149         * Tests if this object is equal to another.
    150         *
    151         * @param o  the other object.
    152         *
    153         * @return A boolean.
    154         */
    155        public boolean equals(Object o) {
    156
    157            if (o == this) {
    158                return true;
    159            }
    160
    161            if (o instanceof CustomPieURLGenerator) {
    162                CustomPieURLGenerator generator = (CustomPieURLGenerator) o;
    163                if (getListCount() != generator.getListCount()) {
    164                    return false;
    165                }
    166                Set keySet;
    167                for (int pieItem = 0; pieItem < getListCount(); pieItem++) {
    168                    if (getURLCount(pieItem) != generator.getURLCount(pieItem)) {
    169                        return false;
    170                    }
    171                    keySet = ((HashMap) this.urls.get(pieItem)).keySet();
    172                    String key;
    173                    for (Iterator i = keySet.iterator(); i.hasNext();) {
    174                    key = (String) i.next();
    175                        if (!getURL(key, pieItem).equals(
    176                                generator.getURL(key, pieItem))) {
    177                            return false;
    178                        }
    179                    }
    180                }
    181                return true;
    182            }
    183            return false;
    184        }
    185
    186        /**
    187         * Returns a clone of the generator.
    188         *
    189         * @return A clone.
    190         *
    191         * @throws CloneNotSupportedException if cloning is not supported.
    192         */
    193        public Object clone() throws CloneNotSupportedException {
    194            CustomPieURLGenerator urlGen = new CustomPieURLGenerator();
    195            Map map;
    196            Map newMap;
    197            String key;
    198
    199            for (Iterator i = this.urls.iterator(); i.hasNext();) {
    200                map = (Map) i.next();
    201
    202                newMap = new HashMap();
    203                for (Iterator j = map.keySet().iterator(); j.hasNext();) {
    204                    key = (String) j.next();
    205                    newMap.put(key, map.get(key));
    206                }
    207
    208                urlGen.addURLs(newMap);
    209                newMap = null;
    210            }
    211
    212            return urlGen;
    213        }
    214
    215    }