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-2005, 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-2005, 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.1 2005/10/25 20:59:31 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:  *
  48:  */
  49:  
  50: package org.jfree.chart.urls;
  51: 
  52: import java.io.Serializable;
  53: 
  54: import org.jfree.data.general.PieDataset;
  55: 
  56: /**
  57:  * A URL generator for pie charts.
  58:  *
  59:  * @author Richard Atkinson
  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:         super();
  80:     }
  81: 
  82:     /**
  83:      * Creates a new generator.
  84:      *
  85:      * @param prefix  the prefix.
  86:      */
  87:     public StandardPieURLGenerator(String prefix) {
  88:         this.prefix = prefix;
  89:     }
  90: 
  91:     /**
  92:      * Creates a new generator.
  93:      *
  94:      * @param prefix  the prefix.
  95:      * @param categoryParameterName  the category parameter name.
  96:      */
  97:     public StandardPieURLGenerator(String prefix, 
  98:                                    String categoryParameterName) {
  99:         this.prefix = prefix;
 100:         this.categoryParameterName = categoryParameterName;
 101:     }
 102: 
 103:     /**
 104:      * Creates a new generator.
 105:      *
 106:      * @param prefix  the prefix.
 107:      * @param categoryParameterName  the category parameter name.
 108:      * @param indexParameterName  the index parameter name 
 109:      *                            (<code>null</code> permitted).
 110:      */
 111:     public StandardPieURLGenerator(String prefix, 
 112:                                    String categoryParameterName, 
 113:                                    String indexParameterName) {
 114:         this.prefix = prefix;
 115:         this.categoryParameterName = categoryParameterName;
 116:         this.indexParameterName = indexParameterName;
 117:     }
 118: 
 119:     /**
 120:      * Generates a URL.
 121:      *
 122:      * @param data  the dataset.
 123:      * @param key  the item key.
 124:      * @param pieIndex  the pie index (ignored).
 125:      *
 126:      * @return A string containing the generated URL.
 127:      */
 128:     public String generateURL(PieDataset data, Comparable key, int pieIndex) {
 129: 
 130:         String url = this.prefix;
 131:         if (url.indexOf("?") > -1) {
 132:             url += "&amp;" + this.categoryParameterName + "=" + key.toString();
 133:         }
 134:         else {
 135:             url += "?" + this.categoryParameterName + "=" + key.toString();
 136:         }
 137:         if (this.indexParameterName != null) {
 138:             url += "&amp;" + this.indexParameterName + "=" 
 139:                    + String.valueOf(pieIndex);
 140:         }
 141:         return url;
 142: 
 143:     }
 144: 
 145:     /**
 146:      * Tests if this object is equal to another.
 147:      *
 148:      * @param obj  the object (<code>null</code> permitted).
 149:      *
 150:      * @return A boolean.
 151:      */
 152:     public boolean equals(Object obj) {
 153: 
 154:         if (obj == null) {
 155:             return false;
 156:         }
 157:         if (obj == this) {
 158:             return true;
 159:         }
 160: 
 161:         if ((obj instanceof StandardPieURLGenerator) == false) {
 162:             return false;
 163:         }
 164: 
 165:         StandardPieURLGenerator generator = (StandardPieURLGenerator) obj;
 166:         return (
 167:             this.categoryParameterName.equals(generator.categoryParameterName))
 168:             && (this.prefix.equals(generator.prefix)
 169:         );
 170: 
 171:     }
 172: }