KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > urls > CustomPieURLGenerator


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------------------
28  * CustomPieURLGenerator.java
29  * --------------------------
30  * (C) Copyright 2004-2005, by David Basten and Contributors.
31  *
32  * Original Author: David Basten;
33  * Contributors: -;
34  *
35  * $Id: CustomPieURLGenerator.java,v 1.3.2.1 2005/10/25 20:59:31 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 04-Feb-2004 : Version 1, contributed by David Basten based on
40  * CustomXYURLGenerator by Richard Atkinson (added to main source
41  * tree on 25-May-2004);
42  *
43  */

44 package org.jfree.chart.urls;
45
46 import java.io.Serializable JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.Set JavaDoc;
52
53 import org.jfree.data.general.PieDataset;
54 import org.jfree.util.PublicCloneable;
55
56 /**
57  * A custom URL generator for pie charts.
58  */

59 public class CustomPieURLGenerator implements PieURLGenerator,
60                                               Cloneable JavaDoc,
61                                               PublicCloneable,
62                                               Serializable JavaDoc {
63
64     /** For serialization. */
65     private static final long serialVersionUID = 7100607670144900503L;
66
67     /** Storage for the URLs. */
68     private ArrayList JavaDoc urls;
69
70     /**
71      * Default constructor.
72      */

73     public CustomPieURLGenerator() {
74         this.urls = new ArrayList JavaDoc();
75     }
76
77     /**
78      * Generates a URL.
79      *
80      * @param dataset the dataset.
81      * @param key the item key.
82      * @param pieIndex the pie index (ignored).
83      *
84      * @return A string containing the generated URL.
85      */

86     public String JavaDoc generateURL(PieDataset dataset, Comparable JavaDoc key,
87                               int pieIndex) {
88         return getURL(key, pieIndex);
89     }
90
91     /**
92      * Returns the number of URL lists stored by the renderer.
93      *
94      * @return The list count.
95      */

96     public int getListCount() {
97         return this.urls.size();
98     }
99     
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 JavaDoc urlMap = (Map JavaDoc) 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 JavaDoc getURL(Comparable JavaDoc key, int pieItem) {
126
127         String JavaDoc result = null;
128         
129         if (pieItem < getListCount()) {
130             Map JavaDoc urlMap = (Map JavaDoc) this.urls.get(pieItem);
131             if (urlMap != null) {
132                 result = (String JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc keySet;
167             for (int pieItem = 0; pieItem < getListCount(); pieItem++) {
168                 if (getURLCount(pieItem) != generator.getURLCount(pieItem)) {
169                     return false;
170                 }
171                 keySet = ((HashMap JavaDoc) this.urls.get(pieItem)).keySet();
172                 String JavaDoc key;
173                 for (Iterator JavaDoc i = keySet.iterator(); i.hasNext();) {
174                 key = (String JavaDoc) 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 JavaDoc clone() throws CloneNotSupportedException JavaDoc {
194         CustomPieURLGenerator urlGen = new CustomPieURLGenerator();
195         Map JavaDoc map;
196         Map JavaDoc newMap;
197         String JavaDoc key;
198
199         for (Iterator JavaDoc i = this.urls.iterator(); i.hasNext();) {
200             map = (Map JavaDoc) i.next();
201
202             newMap = new HashMap JavaDoc();
203             for (Iterator JavaDoc j = map.keySet().iterator(); j.hasNext();) {
204                 key = (String JavaDoc) 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 }
216
Popular Tags