1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  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. * CustomXYURLGenerator.java
  28. * -------------------------
  29. * (C) Copyright 2002-2005, by Richard Atkinson and Contributors.
  30. *
  31. * Original Author: Richard Atkinson;
  32. * Contributors: David Gilbert (for Object Refinery Limited);
  33. *
  34. * $Id: CustomXYURLGenerator.java,v 1.4 2005/03/09 13:45:32 mungady Exp $
  35. *
  36. * Changes:
  37. * --------
  38. * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
  39. * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  40. * 23-Mar-2003 : Implemented Serializable (DG);
  41. * 20-Jan-2005 : Minor Javadoc update (DG);
  42. *
  43. */
  44. package org.jfree.chart.urls;
  45. import java.io.Serializable;
  46. import java.util.ArrayList;
  47. import java.util.List;
  48. import org.jfree.data.xy.XYDataset;
  49. /**
  50. * A custom URL generator.
  51. *
  52. * @author Richard Atkinson
  53. */
  54. public class CustomXYURLGenerator implements XYURLGenerator, Serializable {
  55. /** Storage for the URLs. */
  56. private ArrayList urlSeries = new ArrayList();
  57. /**
  58. * Default constructor.
  59. */
  60. public CustomXYURLGenerator() {
  61. super();
  62. }
  63. /**
  64. * Returns the number of URL lists stored by the renderer.
  65. *
  66. * @return The list count.
  67. */
  68. public int getListCount() {
  69. return this.urlSeries.size();
  70. }
  71. /**
  72. * Returns the number of URLs in a given list.
  73. *
  74. * @param list the list index (zero based).
  75. *
  76. * @return The URL count.
  77. */
  78. public int getURLCount(int list) {
  79. int result = 0;
  80. List urls = (List) this.urlSeries.get(list);
  81. if (urls != null) {
  82. result = urls.size();
  83. }
  84. return result;
  85. }
  86. /**
  87. * Returns the URL for an item.
  88. *
  89. * @param series the series index.
  90. * @param item the item index.
  91. *
  92. * @return The URL (possibly <code>null</code>).
  93. */
  94. public String getURL(int series, int item) {
  95. String result = null;
  96. if (series < getListCount()) {
  97. List urls = (List) this.urlSeries.get(series);
  98. if (urls != null) {
  99. if (item < urls.size()) {
  100. result = (String) urls.get(item);
  101. }
  102. }
  103. }
  104. return result;
  105. }
  106. /**
  107. * Generates a URL.
  108. *
  109. * @param dataset the dataset.
  110. * @param series the series (zero-based index).
  111. * @param item the item (zero-based index).
  112. *
  113. * @return A string containing the URL (possibly <code>null</code>).
  114. */
  115. public String generateURL(XYDataset dataset, int series, int item) {
  116. return getURL(series, item);
  117. }
  118. /**
  119. * Adds a list of URLs.
  120. *
  121. * @param urls the list of URLs.
  122. */
  123. public void addURLSeries(List urls) {
  124. this.urlSeries.add(urls);
  125. }
  126. /**
  127. * Tests if this object is equal to another.
  128. *
  129. * @param o the other object.
  130. *
  131. * @return A boolean.
  132. */
  133. public boolean equals(Object o) {
  134. if (o == null) {
  135. return false;
  136. }
  137. if (o == this) {
  138. return true;
  139. }
  140. if (!(o instanceof CustomXYURLGenerator)) {
  141. return false;
  142. }
  143. CustomXYURLGenerator generator = (CustomXYURLGenerator) o;
  144. int listCount = getListCount();
  145. if (listCount != generator.getListCount()) {
  146. return false;
  147. }
  148. for (int series = 0; series < listCount; series++) {
  149. int urlCount = getURLCount(series);
  150. if (urlCount != generator.getURLCount(series)) {
  151. return false;
  152. }
  153. for (int item = 0; item < urlCount; item++) {
  154. String u1 = getURL(series, item);
  155. String u2 = generator.getURL(series, item);
  156. if (u1 != null) {
  157. if (!u1.equals(u2)) {
  158. return false;
  159. }
  160. }
  161. else {
  162. if (u2 != null) {
  163. return false;
  164. }
  165. }
  166. }
  167. }
  168. return true;
  169. }
  170. }