Source for org.jfree.chart.urls.StandardCategoryURLGenerator

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, 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:  * StandardCategoryURLGenerator.java
  29:  * ---------------------------------
  30:  * (C) Copyright 2002-2007, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributors:     David Gilbert (for Object Refinery Limited);
  34:  *                   Cleland Early;
  35:  *
  36:  * Changes:
  37:  * --------
  38:  * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
  39:  * 29-Aug-2002 : Reversed seriesParameterName and itemParameterName in 
  40:  *               constructor.  Never should have been the other way round.  
  41:  *               Also updated JavaDoc (RA);
  42:  * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  43:  * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
  44:  * 23-Mar-2003 : Implemented Serializable (DG);
  45:  * 13-Aug-2003 : Implemented Cloneable (DG);
  46:  * 23-Dec-2003 : Added fix for bug 861282 (DG);
  47:  * 21-May-2004 : Added URL encoding - see patch 947854 (DG);
  48:  * 13-Jan-2004 : Fixed for compliance with XHTML 1.0 (DG);
  49:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  50:  * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
  51:  * 17-Apr-2007 : Use new URLUtilities class to encode URLs (DG);
  52:  *
  53:  */
  54: 
  55: package org.jfree.chart.urls;
  56: 
  57: import java.io.Serializable;
  58: 
  59: import org.jfree.data.category.CategoryDataset;
  60: import org.jfree.util.ObjectUtilities;
  61: 
  62: /**
  63:  * A URL generator that can be assigned to a 
  64:  * {@link org.jfree.chart.renderer.category.CategoryItemRenderer}.
  65:  */
  66: public class StandardCategoryURLGenerator implements CategoryURLGenerator, 
  67:                                                      Cloneable, Serializable {
  68: 
  69:     /** For serialization. */
  70:     private static final long serialVersionUID = 2276668053074881909L;
  71:     
  72:     /** Prefix to the URL */
  73:     private String prefix = "index.html";
  74: 
  75:     /** Series parameter name to go in each URL */
  76:     private String seriesParameterName = "series";
  77: 
  78:     /** Category parameter name to go in each URL */
  79:     private String categoryParameterName = "category";
  80: 
  81:     /**
  82:      * Creates a new generator with default settings.
  83:      */
  84:     public StandardCategoryURLGenerator() {
  85:         super();
  86:     }
  87: 
  88:     /**
  89:      * Constructor that overrides default prefix to the URL.
  90:      *
  91:      * @param prefix  the prefix to the URL (<code>null</code> not permitted).
  92:      */
  93:     public StandardCategoryURLGenerator(String prefix) {
  94:         if (prefix == null) {
  95:             throw new IllegalArgumentException("Null 'prefix' argument.");   
  96:         }
  97:         this.prefix = prefix;
  98:     }
  99: 
 100:     /**
 101:      * Constructor that overrides all the defaults.
 102:      *
 103:      * @param prefix  the prefix to the URL (<code>null</code> not permitted).
 104:      * @param seriesParameterName  the name of the series parameter to go in 
 105:      *                             each URL (<code>null</code> not permitted).
 106:      * @param categoryParameterName  the name of the category parameter to go in
 107:      *                               each URL (<code>null</code> not permitted).
 108:      */
 109:     public StandardCategoryURLGenerator(String prefix,
 110:                                         String seriesParameterName,
 111:                                         String categoryParameterName) {
 112: 
 113:         if (prefix == null) {
 114:             throw new IllegalArgumentException("Null 'prefix' argument.");   
 115:         }
 116:         if (seriesParameterName == null) {
 117:             throw new IllegalArgumentException(
 118:                 "Null 'seriesParameterName' argument."
 119:             );   
 120:         }
 121:         if (categoryParameterName == null) {
 122:             throw new IllegalArgumentException(
 123:                 "Null 'categoryParameterName' argument."
 124:             );   
 125:         }
 126:         this.prefix = prefix;
 127:         this.seriesParameterName = seriesParameterName;
 128:         this.categoryParameterName = categoryParameterName;
 129: 
 130:     }
 131: 
 132:     /**
 133:      * Generates a URL for a particular item within a series.
 134:      *
 135:      * @param dataset  the dataset.
 136:      * @param series  the series index (zero-based).
 137:      * @param category  the category index (zero-based).
 138:      *
 139:      * @return The generated URL.
 140:      */
 141:     public String generateURL(CategoryDataset dataset, int series, 
 142:                               int category) {
 143:         String url = this.prefix;
 144:         Comparable seriesKey = dataset.getRowKey(series);
 145:         Comparable categoryKey = dataset.getColumnKey(category);
 146:         boolean firstParameter = url.indexOf("?") == -1;
 147:         url += firstParameter ? "?" : "&amp;";
 148:         url += this.seriesParameterName + "=" + URLUtilities.encode(
 149:                 seriesKey.toString(), "UTF-8");
 150:         url += "&amp;" + this.categoryParameterName + "=" 
 151:                 + URLUtilities.encode(categoryKey.toString(), "UTF-8");
 152:         return url;
 153:     }
 154: 
 155:     /**
 156:      * Returns an independent copy of the URL generator.
 157:      * 
 158:      * @return A clone.
 159:      * 
 160:      * @throws CloneNotSupportedException not thrown by this class, but 
 161:      *         subclasses (if any) might.
 162:      */
 163:     public Object clone() throws CloneNotSupportedException {
 164:     
 165:         // all attributes are immutable, so we can just return the super.clone()
 166:         return super.clone();
 167:         
 168:     }
 169:     
 170:     /**
 171:      * Tests the generator for equality with an arbitrary object.
 172:      *
 173:      * @param obj  the object (<code>null</code> permitted).
 174:      *
 175:      * @return A boolean.
 176:      */
 177:     public boolean equals(Object obj) {
 178:         if (obj == this) {
 179:             return true;
 180:         }
 181:         if (!(obj instanceof StandardCategoryURLGenerator)) {
 182:             return false;
 183:         }
 184:         StandardCategoryURLGenerator that = (StandardCategoryURLGenerator) obj;
 185:         if (!ObjectUtilities.equal(this.prefix, that.prefix)) {
 186:             return false;
 187:         }
 188: 
 189:         if (!ObjectUtilities.equal(this.seriesParameterName, 
 190:                 that.seriesParameterName)) {
 191:             return false;
 192:         }
 193:         if (!ObjectUtilities.equal(this.categoryParameterName, 
 194:                 that.categoryParameterName)) {
 195:             return false;
 196:         }
 197:         return true;
 198:     }
 199: 
 200:     /**
 201:      * Returns a hash code.
 202:      * 
 203:      * @return A hash code.
 204:      */
 205:     public int hashCode() {
 206:         int result;
 207:         result = (this.prefix != null ? this.prefix.hashCode() : 0);
 208:         result = 29 * result 
 209:             + (this.seriesParameterName != null 
 210:                     ? this.seriesParameterName.hashCode() : 0);
 211:         result = 29 * result 
 212:             + (this.categoryParameterName != null 
 213:                     ? this.categoryParameterName.hashCode() : 0);
 214:         return result;
 215:     }
 216:     
 217: }