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