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