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 License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * --------------------------
  27. * CustomPieURLGenerator.java
  28. * --------------------------
  29. * (C) Copyright 2004-2005, by David Basten and Contributors.
  30. *
  31. * Original Author: David Basten;
  32. * Contributors: -;
  33. *
  34. * $Id: CustomPieURLGenerator.java,v 1.2 2005/03/09 13:45:32 mungady Exp $
  35. *
  36. * Changes:
  37. * --------
  38. * 04-Feb-2004 : Version 1, contributed by David Basten based on
  39. * CustomXYURLGenerator by Richard Atkinson (added to main source
  40. * tree on 25-May-2004);
  41. *
  42. */
  43. package org.jfree.chart.urls;
  44. import java.io.Serializable;
  45. import java.util.ArrayList;
  46. import java.util.HashMap;
  47. import java.util.Iterator;
  48. import java.util.Map;
  49. import java.util.Set;
  50. import org.jfree.data.general.PieDataset;
  51. import org.jfree.util.PublicCloneable;
  52. /**
  53. * A custom URL generator for pie charts.
  54. */
  55. public class CustomPieURLGenerator implements PieURLGenerator,
  56. Cloneable,
  57. PublicCloneable,
  58. Serializable {
  59. /** Storage for the URLs. */
  60. private ArrayList urls;
  61. /**
  62. * Default constructor.
  63. */
  64. public CustomPieURLGenerator() {
  65. this.urls = new ArrayList();
  66. }
  67. /**
  68. * Generates a URL.
  69. *
  70. * @param dataset the dataset.
  71. * @param key the item key.
  72. * @param pieIndex the pie index (ignored).
  73. *
  74. * @return A string containing the generated URL.
  75. */
  76. public String generateURL(PieDataset dataset, Comparable key,
  77. int pieIndex) {
  78. return getURL(key, pieIndex);
  79. }
  80. /**
  81. * Returns the number of URL lists stored by the renderer.
  82. *
  83. * @return The list count.
  84. */
  85. public int getListCount() {
  86. return this.urls.size();
  87. }
  88. /**
  89. * Returns the number of URLs in a given list.
  90. *
  91. * @param list the list index (zero based).
  92. *
  93. * @return The URL count.
  94. */
  95. public int getURLCount(int list) {
  96. int result = 0;
  97. Map urlMap = (Map) this.urls.get(list);
  98. if (urlMap != null) {
  99. result = urlMap.size();
  100. }
  101. return result;
  102. }
  103. /**
  104. * Returns the URL for an item.
  105. *
  106. * @param key the key.
  107. * @param pieItem the item index.
  108. *
  109. * @return The URL.
  110. */
  111. public String getURL(Comparable key, int pieItem) {
  112. String result = null;
  113. if (pieItem < getListCount()) {
  114. Map urlMap = (Map) this.urls.get(pieItem);
  115. if (urlMap != null) {
  116. result = (String) urlMap.get(key);
  117. }
  118. }
  119. return result;
  120. }
  121. /**
  122. * Adds a map of URLs.
  123. *
  124. * @param urlMap the URLs.
  125. */
  126. public void addURLs(Map urlMap) {
  127. this.urls.add(urlMap);
  128. }
  129. /**
  130. * Tests if this object is equal to another.
  131. *
  132. * @param o the other object.
  133. *
  134. * @return A boolean.
  135. */
  136. public boolean equals(Object o) {
  137. if (o == this) {
  138. return true;
  139. }
  140. if (o instanceof CustomPieURLGenerator) {
  141. CustomPieURLGenerator generator = (CustomPieURLGenerator) o;
  142. if (getListCount() != generator.getListCount()) {
  143. return false;
  144. }
  145. Set keySet;
  146. for (int pieItem = 0; pieItem < getListCount(); pieItem++) {
  147. if (getURLCount(pieItem) != generator.getURLCount(pieItem)) {
  148. return false;
  149. }
  150. keySet = ((HashMap) this.urls.get(pieItem)).keySet();
  151. String key;
  152. for (Iterator i = keySet.iterator(); i.hasNext();) {
  153. key = (String) i.next();
  154. if (!getURL(key, pieItem).equals(
  155. generator.getURL(key, pieItem))) {
  156. return false;
  157. }
  158. }
  159. }
  160. return true;
  161. }
  162. return false;
  163. }
  164. /**
  165. * Returns a clone of the generator.
  166. *
  167. * @return A clone.
  168. *
  169. * @throws CloneNotSupportedException if cloning is not supported.
  170. */
  171. public Object clone() throws CloneNotSupportedException {
  172. CustomPieURLGenerator urlGen = new CustomPieURLGenerator();
  173. Map map;
  174. Map newMap;
  175. String key;
  176. for (Iterator i = this.urls.iterator(); i.hasNext();) {
  177. map = (Map) i.next();
  178. newMap = new HashMap();
  179. for (Iterator j = map.keySet().iterator(); j.hasNext();) {
  180. key = (String) j.next();
  181. newMap.put(key, map.get(key));
  182. }
  183. urlGen.addURLs(newMap);
  184. newMap = null;
  185. }
  186. return urlGen;
  187. }
  188. }