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