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