Source for org.jfree.chart.urls.CustomCategoryURLGenerator

   1: /* ======================================
   2:  * JFreeChart : a free Java chart library
   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:  * CustomCategoryURLGenerator.java
  29:  * -------------------------------
  30:  * (C) Copyright 2008, by Diego Pierangeli and Contributors.
  31:  *
  32:  * Original Author:  Diego Pierangeli;
  33:  * Contributors:     David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * Changes:
  36:  * --------
  37:  * 23-Apr-2008 : Version 1, contributed by Diego Pierangeli based on
  38:  *               CustomXYURLGenerator by Richard Atkinson, with some
  39:  *               modifications by David Gilbert(DG);
  40:  *
  41:  */
  42: package org.jfree.chart.urls;
  43: 
  44: import java.io.Serializable;
  45: import java.util.ArrayList;
  46: import java.util.List;
  47: 
  48: import org.jfree.data.category.CategoryDataset;
  49: import org.jfree.util.PublicCloneable;
  50: 
  51: /**
  52:  * A custom URL generator.
  53:  */
  54: public class CustomCategoryURLGenerator implements CategoryURLGenerator,
  55:         Cloneable, PublicCloneable, Serializable {
  56: 
  57:     /** Storage for the URLs. */
  58:     private ArrayList urlSeries = new ArrayList();
  59: 
  60:     /**
  61:      * Default constructor.
  62:      */
  63:     public CustomCategoryURLGenerator() {
  64:         super();
  65:     }
  66: 
  67:     /**
  68:      * Returns the number of URL lists stored by the renderer.
  69:      *
  70:      * @return The list count.
  71:      */
  72:     public int getListCount() {
  73:         return this.urlSeries.size();
  74:     }
  75: 
  76:     /**
  77:      * Returns the number of URLs in a given list.
  78:      *
  79:      * @param list  the list index (zero based).
  80:      *
  81:      * @return The URL count.
  82:      */
  83:     public int getURLCount(int list) {
  84:         int result = 0;
  85:         List urls = (List) this.urlSeries.get(list);
  86:         if (urls != null) {
  87:             result = urls.size();
  88:         }
  89:         return result;
  90:     }
  91: 
  92:     /**
  93:      * Returns the URL for an item.
  94:      *
  95:      * @param series  the series index.
  96:      * @param item  the item index.
  97:      *
  98:      * @return The URL (possibly <code>null</code>).
  99:      */
 100:     public String getURL(int series, int item) {
 101:         String result = null;
 102:         if (series < getListCount()) {
 103:             List urls = (List) this.urlSeries.get(series);
 104:             if (urls != null) {
 105:                 if (item < urls.size()) {
 106:                     result = (String) urls.get(item);
 107:                 }
 108:             }
 109:         }
 110:         return result;
 111:     }
 112: 
 113:     /**
 114:      * Generates a URL.
 115:      *
 116:      * @param dataset  the dataset (ignored in this implementation).
 117:      * @param series  the series (zero-based index).
 118:      * @param item  the item (zero-based index).
 119:      *
 120:      * @return A string containing the URL (possibly <code>null</code>).
 121:      */
 122:     public String generateURL(CategoryDataset dataset, int series, int item) {
 123:         return getURL(series, item);
 124:     }
 125: 
 126:     /**
 127:      * Adds a list of URLs.
 128:      *
 129:      * @param urls  the list of URLs (<code>null</code> permitted).
 130:      */
 131:     public void addURLSeries(List urls) {
 132:         List listToAdd = null;
 133:         if (urls != null) {
 134:             listToAdd = new java.util.ArrayList(urls);
 135:         }
 136:         this.urlSeries.add(listToAdd);
 137:     }
 138: 
 139:     /**
 140:      * Tests if this object is equal to another.
 141:      *
 142:      * @param obj  the other object.
 143:      *
 144:      * @return A boolean.
 145:      */
 146:     public boolean equals(Object obj) {
 147:         if (obj == this) {
 148:             return true;
 149:         }
 150:         if (!(obj instanceof CustomCategoryURLGenerator)) {
 151:             return false;
 152:         }
 153:         CustomCategoryURLGenerator generator = (CustomCategoryURLGenerator) obj;
 154:         int listCount = getListCount();
 155:         if (listCount != generator.getListCount()) {
 156:             return false;
 157:         }
 158: 
 159:         for (int series = 0; series < listCount; series++) {
 160:             int urlCount = getURLCount(series);
 161:             if (urlCount != generator.getURLCount(series)) {
 162:                 return false;
 163:             }
 164: 
 165:             for (int item = 0; item < urlCount; item++) {
 166:                 String u1 = getURL(series, item);
 167:                 String u2 = generator.getURL(series, item);
 168:                 if (u1 != null) {
 169:                     if (!u1.equals(u2)) {
 170:                         return false;
 171:                     }
 172:                 } else {
 173:                     if (u2 != null) {
 174:                         return false;
 175:                     }
 176:                 }
 177:             }
 178:         }
 179:         return true;
 180:     }
 181: 
 182:     /**
 183:      * Returns a new generator that is a copy of, and independent from, this
 184:      * generator.
 185:      *
 186:      * @return A clone.
 187:      */
 188:     public Object clone() throws CloneNotSupportedException {
 189:         CustomCategoryURLGenerator clone
 190:                 = (CustomCategoryURLGenerator) super.clone();
 191:         clone.urlSeries = new java.util.ArrayList(this.urlSeries);
 192:         return clone;
 193:     }
 194: 
 195: }