Source for org.jfree.chart.urls.StandardPieURLGenerator

   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:  * StandardPieURLGenerator.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:  *
  35:  * Changes:
  36:  * --------
  37:  * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
  38:  * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  39:  * 07-Mar-2003 : Modified to use KeyedValuesDataset and added pieIndex 
  40:  *               parameter (DG);
  41:  * 21-Mar-2003 : Implemented Serializable (DG);
  42:  * 24-Apr-2003 : Switched around PieDataset and KeyedValuesDataset (DG);
  43:  * 31-Mar-2004 : Added an optional 'pieIndex' parameter (DG);
  44:  * 13-Jan-2005 : Fixed for compliance with XHTML 1.0 (DG);
  45:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  46:  * 24-Nov-2006 : Fixed equals() method and added argument checks (DG);
  47:  * 17-Apr-2007 : Encode section key in generateURL() (DG);
  48:  *
  49:  */
  50:  
  51: package org.jfree.chart.urls;
  52: 
  53: import java.io.Serializable;
  54: 
  55: import org.jfree.data.general.PieDataset;
  56: import org.jfree.util.ObjectUtilities;
  57: 
  58: /**
  59:  * A URL generator for pie charts.  Instances of this class are immutable.
  60:  */
  61: public class StandardPieURLGenerator implements PieURLGenerator, Serializable {
  62: 
  63:     /** For serialization. */
  64:     private static final long serialVersionUID = 1626966402065883419L;
  65:     
  66:     /** The prefix. */
  67:     private String prefix = "index.html";
  68: 
  69:     /** The category parameter name. */
  70:     private String categoryParameterName = "category";
  71:     
  72:     /** The pie index parameter name. */
  73:     private String indexParameterName = "pieIndex";
  74: 
  75:     /**
  76:      * Default constructor.
  77:      */
  78:     public StandardPieURLGenerator() {
  79:         this("index.html");
  80:     }
  81: 
  82:     /**
  83:      * Creates a new generator.
  84:      *
  85:      * @param prefix  the prefix (<code>null</code> not permitted).
  86:      */
  87:     public StandardPieURLGenerator(String prefix) {
  88:         this(prefix, "category");
  89:     }
  90: 
  91:     /**
  92:      * Creates a new generator.
  93:      *
  94:      * @param prefix  the prefix (<code>null</code> not permitted).
  95:      * @param categoryParameterName  the category parameter name 
  96:      *     (<code>null</code> not permitted).
  97:      */
  98:     public StandardPieURLGenerator(String prefix, 
  99:                                    String categoryParameterName) {
 100:         this(prefix, categoryParameterName, "pieIndex");
 101:     }
 102: 
 103:     /**
 104:      * Creates a new generator.
 105:      *
 106:      * @param prefix  the prefix (<code>null</code> not permitted).
 107:      * @param categoryParameterName  the category parameter name 
 108:      *     (<code>null</code> not permitted).
 109:      * @param indexParameterName  the index parameter name (<code>null</code> 
 110:      *     permitted).
 111:      */
 112:     public StandardPieURLGenerator(String prefix, 
 113:                                    String categoryParameterName, 
 114:                                    String indexParameterName) {
 115:         if (prefix == null) {
 116:             throw new IllegalArgumentException("Null 'prefix' argument.");
 117:         }
 118:         if (categoryParameterName == null) {
 119:             throw new IllegalArgumentException(
 120:                     "Null 'categoryParameterName' argument.");
 121:         }
 122:         this.prefix = prefix;
 123:         this.categoryParameterName = categoryParameterName;
 124:         this.indexParameterName = indexParameterName;
 125:     }
 126: 
 127:     /**
 128:      * Generates a URL.
 129:      *
 130:      * @param dataset  the dataset (ignored).
 131:      * @param key  the item key (<code>null</code> not permitted).
 132:      * @param pieIndex  the pie index.
 133:      *
 134:      * @return A string containing the generated URL.
 135:      */
 136:     public String generateURL(PieDataset dataset, Comparable key, 
 137:             int pieIndex) {
 138:         String url = this.prefix;
 139:         if (url.indexOf("?") > -1) {
 140:             url += "&amp;" + this.categoryParameterName + "=" 
 141:                     + URLUtilities.encode(key.toString(), "UTF-8");
 142:         }
 143:         else {
 144:             url += "?" + this.categoryParameterName + "=" 
 145:                     + URLUtilities.encode(key.toString(), "UTF-8");
 146:         }
 147:         if (this.indexParameterName != null) {
 148:             url += "&amp;" + this.indexParameterName + "=" 
 149:                    + String.valueOf(pieIndex);
 150:         }
 151:         return url;
 152:     }
 153: 
 154:     /**
 155:      * Tests if this object is equal to another.
 156:      *
 157:      * @param obj  the object (<code>null</code> permitted).
 158:      *
 159:      * @return A boolean.
 160:      */
 161:     public boolean equals(Object obj) {
 162:         if (obj == this) {
 163:             return true;
 164:         }
 165:         if (!(obj instanceof StandardPieURLGenerator)) {
 166:             return false;
 167:         }
 168:         StandardPieURLGenerator that = (StandardPieURLGenerator) obj;
 169:         if (!this.prefix.equals(that.prefix)) {
 170:             return false;
 171:         }
 172:         if (!this.categoryParameterName.equals(that.categoryParameterName)) {
 173:             return false;
 174:         }
 175:         if (!ObjectUtilities.equal(this.indexParameterName, 
 176:                 that.indexParameterName)) {
 177:             return false;
 178:         }
 179:         return true;
 180:     }
 181: }