001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2007, 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 * CustomXYURLGenerator.java 029 * ------------------------- 030 * (C) Copyright 2002-2007, by Richard Atkinson and Contributors. 031 * 032 * Original Author: Richard Atkinson; 033 * Contributors: David Gilbert (for Object Refinery Limited); 034 * 035 * Changes: 036 * -------- 037 * 05-Aug-2002 : Version 1, contributed by Richard Atkinson; 038 * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG); 039 * 23-Mar-2003 : Implemented Serializable (DG); 040 * 20-Jan-2005 : Minor Javadoc update (DG); 041 * ------------- JFREECHART 1.0.x --------------------------------------------- 042 * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG); 043 * 044 */ 045 046 package org.jfree.chart.urls; 047 048 import java.io.Serializable; 049 import java.util.ArrayList; 050 import java.util.List; 051 052 import org.jfree.data.xy.XYDataset; 053 054 /** 055 * A custom URL generator. 056 */ 057 public class CustomXYURLGenerator implements XYURLGenerator, Serializable { 058 059 /** For serialization. */ 060 private static final long serialVersionUID = -8565933356596551832L; 061 062 /** Storage for the URLs. */ 063 private ArrayList urlSeries = new ArrayList(); 064 065 /** 066 * Default constructor. 067 */ 068 public CustomXYURLGenerator() { 069 super(); 070 } 071 072 /** 073 * Returns the number of URL lists stored by the renderer. 074 * 075 * @return The list count. 076 */ 077 public int getListCount() { 078 return this.urlSeries.size(); 079 } 080 081 /** 082 * Returns the number of URLs in a given list. 083 * 084 * @param list the list index (zero based). 085 * 086 * @return The URL count. 087 */ 088 public int getURLCount(int list) { 089 int result = 0; 090 List urls = (List) this.urlSeries.get(list); 091 if (urls != null) { 092 result = urls.size(); 093 } 094 return result; 095 } 096 097 /** 098 * Returns the URL for an item. 099 * 100 * @param series the series index. 101 * @param item the item index. 102 * 103 * @return The URL (possibly <code>null</code>). 104 */ 105 public String getURL(int series, int item) { 106 String result = null; 107 if (series < getListCount()) { 108 List urls = (List) this.urlSeries.get(series); 109 if (urls != null) { 110 if (item < urls.size()) { 111 result = (String) urls.get(item); 112 } 113 } 114 } 115 return result; 116 } 117 118 /** 119 * Generates a URL. 120 * 121 * @param dataset the dataset. 122 * @param series the series (zero-based index). 123 * @param item the item (zero-based index). 124 * 125 * @return A string containing the URL (possibly <code>null</code>). 126 */ 127 public String generateURL(XYDataset dataset, int series, int item) { 128 return getURL(series, item); 129 } 130 131 /** 132 * Adds a list of URLs. 133 * 134 * @param urls the list of URLs. 135 */ 136 public void addURLSeries(List urls) { 137 this.urlSeries.add(urls); 138 } 139 140 /** 141 * Tests if this object is equal to another. 142 * 143 * @param o the other object. 144 * 145 * @return A boolean. 146 */ 147 public boolean equals(Object o) { 148 149 if (o == null) { 150 return false; 151 } 152 if (o == this) { 153 return true; 154 } 155 156 if (!(o instanceof CustomXYURLGenerator)) { 157 return false; 158 } 159 CustomXYURLGenerator generator = (CustomXYURLGenerator) o; 160 int listCount = getListCount(); 161 if (listCount != generator.getListCount()) { 162 return false; 163 } 164 165 for (int series = 0; series < listCount; series++) { 166 int urlCount = getURLCount(series); 167 if (urlCount != generator.getURLCount(series)) { 168 return false; 169 } 170 171 for (int item = 0; item < urlCount; item++) { 172 String u1 = getURL(series, item); 173 String u2 = generator.getURL(series, item); 174 if (u1 != null) { 175 if (!u1.equals(u2)) { 176 return false; 177 } 178 } 179 else { 180 if (u2 != null) { 181 return false; 182 } 183 } 184 } 185 } 186 return true; 187 188 } 189 190 }