Source for org.jfree.chart.urls.CustomXYURLGenerator

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2008, 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:  * CustomXYURLGenerator.java
  29:  * -------------------------
  30:  * (C) Copyright 2002-2008, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributors:     David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * Changes:
  36:  * --------
  37:  * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
  38:  * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  39:  * 23-Mar-2003 : Implemented Serializable (DG);
  40:  * 20-Jan-2005 : Minor Javadoc update (DG);
  41:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  42:  * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
  43:  * 11-Apr-2008 : Implemented Cloneable, otherwise charts using this URL
  44:  *               generator will fail to clone (DG);
  45:  *
  46:  */
  47: 
  48: package org.jfree.chart.urls;
  49: 
  50: import java.io.Serializable;
  51: import java.util.ArrayList;
  52: import java.util.List;
  53: 
  54: import org.jfree.data.xy.XYDataset;
  55: import org.jfree.util.PublicCloneable;
  56: 
  57: /**
  58:  * A custom URL generator.
  59:  */
  60: public class CustomXYURLGenerator implements XYURLGenerator, Cloneable,
  61:         PublicCloneable, Serializable {
  62: 
  63:     /** For serialization. */
  64:     private static final long serialVersionUID = -8565933356596551832L;
  65: 
  66:     /** Storage for the URLs. */
  67:     private ArrayList urlSeries = new ArrayList();
  68: 
  69:     /**
  70:      * Default constructor.
  71:      */
  72:     public CustomXYURLGenerator() {
  73:         super();
  74:     }
  75: 
  76:     /**
  77:      * Returns the number of URL lists stored by the renderer.
  78:      *
  79:      * @return The list count.
  80:      */
  81:     public int getListCount() {
  82:         return this.urlSeries.size();
  83:     }
  84: 
  85:     /**
  86:      * Returns the number of URLs in a given list.
  87:      *
  88:      * @param list  the list index (zero based).
  89:      *
  90:      * @return The URL count.
  91:      */
  92:     public int getURLCount(int list) {
  93:         int result = 0;
  94:         List urls = (List) this.urlSeries.get(list);
  95:         if (urls != null) {
  96:             result = urls.size();
  97:         }
  98:         return result;
  99:     }
 100: 
 101:     /**
 102:      * Returns the URL for an item.
 103:      *
 104:      * @param series  the series index.
 105:      * @param item  the item index.
 106:      *
 107:      * @return The URL (possibly <code>null</code>).
 108:      */
 109:     public String getURL(int series, int item) {
 110:         String result = null;
 111:         if (series < getListCount()) {
 112:             List urls = (List) this.urlSeries.get(series);
 113:             if (urls != null) {
 114:                 if (item < urls.size()) {
 115:                     result = (String) urls.get(item);
 116:                 }
 117:             }
 118:         }
 119:         return result;
 120:     }
 121: 
 122:     /**
 123:      * Generates a URL.
 124:      *
 125:      * @param dataset  the dataset.
 126:      * @param series  the series (zero-based index).
 127:      * @param item  the item (zero-based index).
 128:      *
 129:      * @return A string containing the URL (possibly <code>null</code>).
 130:      */
 131:     public String generateURL(XYDataset dataset, int series, int item) {
 132:         return getURL(series, item);
 133:     }
 134: 
 135:     /**
 136:      * Adds a list of URLs.
 137:      *
 138:      * @param urls  the list of URLs (<code>null</code> permitted, the list
 139:      *     is copied).
 140:      */
 141:     public void addURLSeries(List urls) {
 142:         List listToAdd = null;
 143:         if (urls != null) {
 144:             listToAdd = new java.util.ArrayList(urls);
 145:         }
 146:         this.urlSeries.add(listToAdd);
 147:     }
 148: 
 149:     /**
 150:      * Tests this generator for equality with an arbitrary object.
 151:      *
 152:      * @param obj  the object (<code>null</code> permitted).
 153:      *
 154:      * @return A boolean.
 155:      */
 156:     public boolean equals(Object obj) {
 157:         if (obj == this) {
 158:             return true;
 159:         }
 160:         if (!(obj instanceof CustomXYURLGenerator)) {
 161:             return false;
 162:         }
 163:         CustomXYURLGenerator that = (CustomXYURLGenerator) obj;
 164:         int listCount = getListCount();
 165:         if (listCount != that.getListCount()) {
 166:             return false;
 167:         }
 168: 
 169:         for (int series = 0; series < listCount; series++) {
 170:             int urlCount = getURLCount(series);
 171:             if (urlCount != that.getURLCount(series)) {
 172:                 return false;
 173:             }
 174: 
 175:             for (int item = 0; item < urlCount; item++) {
 176:                 String u1 = getURL(series, item);
 177:                 String u2 = that.getURL(series, item);
 178:                 if (u1 != null) {
 179:                     if (!u1.equals(u2)) {
 180:                         return false;
 181:                     }
 182:                 }
 183:                 else {
 184:                     if (u2 != null) {
 185:                         return false;
 186:                     }
 187:                 }
 188:             }
 189:         }
 190:         return true;
 191: 
 192:     }
 193: 
 194:     /**
 195:      * Returns a new generator that is a copy of, and independent from, this
 196:      * generator.
 197:      *
 198:      * @return A clone.
 199:      */
 200:     public Object clone() throws CloneNotSupportedException {
 201:         CustomXYURLGenerator clone = (CustomXYURLGenerator) super.clone();
 202:         clone.urlSeries = new java.util.ArrayList(this.urlSeries);
 203:         return clone;
 204:     }
 205: 
 206: }