001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006     *
007     * Project Info:  http://www.jfree.org/jfreechart/index.html
008     *
009     * This library is free software; you can redistribute it and/or modify it 
010     * under the terms of the GNU Lesser General Public License as published by 
011     * the Free Software Foundation; either version 2.1 of the License, or 
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but 
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022     * USA.  
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     *
027     * ----------------------------
028     * StandardPieURLGenerator.java
029     * ----------------------------
030     * (C) Copyright 2002-2006, by Richard Atkinson and Contributors.
031     *
032     * Original Author:  Richard Atkinson;
033     * Contributors:     David Gilbert (for Object Refinery Limited);
034     *
035     * $Id: StandardPieURLGenerator.java,v 1.4.2.3 2007/04/17 16:05:27 mungady Exp $
036     *
037     * Changes:
038     * --------
039     * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
040     * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041     * 07-Mar-2003 : Modified to use KeyedValuesDataset and added pieIndex 
042     *               parameter (DG);
043     * 21-Mar-2003 : Implemented Serializable (DG);
044     * 24-Apr-2003 : Switched around PieDataset and KeyedValuesDataset (DG);
045     * 31-Mar-2004 : Added an optional 'pieIndex' parameter (DG);
046     * 13-Jan-2005 : Fixed for compliance with XHTML 1.0 (DG);
047     * ------------- JFREECHART 1.0.x ---------------------------------------------
048     * 24-Nov-2006 : Fixed equals() method and added argument checks (DG);
049     * 17-Apr-2007 : Encode section key in generateURL() (DG);
050     *
051     */
052     
053    package org.jfree.chart.urls;
054    
055    import java.io.Serializable;
056    
057    import org.jfree.data.general.PieDataset;
058    import org.jfree.util.ObjectUtilities;
059    
060    /**
061     * A URL generator for pie charts.  Instances of this class are immutable.
062     */
063    public class StandardPieURLGenerator implements PieURLGenerator, Serializable {
064    
065        /** For serialization. */
066        private static final long serialVersionUID = 1626966402065883419L;
067        
068        /** The prefix. */
069        private String prefix = "index.html";
070    
071        /** The category parameter name. */
072        private String categoryParameterName = "category";
073        
074        /** The pie index parameter name. */
075        private String indexParameterName = "pieIndex";
076    
077        /**
078         * Default constructor.
079         */
080        public StandardPieURLGenerator() {
081            this("index.html");
082        }
083    
084        /**
085         * Creates a new generator.
086         *
087         * @param prefix  the prefix (<code>null</code> not permitted).
088         */
089        public StandardPieURLGenerator(String prefix) {
090            this(prefix, "category");
091        }
092    
093        /**
094         * Creates a new generator.
095         *
096         * @param prefix  the prefix (<code>null</code> not permitted).
097         * @param categoryParameterName  the category parameter name 
098         *     (<code>null</code> not permitted).
099         */
100        public StandardPieURLGenerator(String prefix, 
101                                       String categoryParameterName) {
102            this(prefix, categoryParameterName, "pieIndex");
103        }
104    
105        /**
106         * Creates a new generator.
107         *
108         * @param prefix  the prefix (<code>null</code> not permitted).
109         * @param categoryParameterName  the category parameter name 
110         *     (<code>null</code> not permitted).
111         * @param indexParameterName  the index parameter name (<code>null</code> 
112         *     permitted).
113         */
114        public StandardPieURLGenerator(String prefix, 
115                                       String categoryParameterName, 
116                                       String indexParameterName) {
117            if (prefix == null) {
118                throw new IllegalArgumentException("Null 'prefix' argument.");
119            }
120            if (categoryParameterName == null) {
121                throw new IllegalArgumentException(
122                        "Null 'categoryParameterName' argument.");
123            }
124            this.prefix = prefix;
125            this.categoryParameterName = categoryParameterName;
126            this.indexParameterName = indexParameterName;
127        }
128    
129        /**
130         * Generates a URL.
131         *
132         * @param dataset  the dataset (ignored).
133         * @param key  the item key (<code>null</code> not permitted).
134         * @param pieIndex  the pie index.
135         *
136         * @return A string containing the generated URL.
137         */
138        public String generateURL(PieDataset dataset, Comparable key, int pieIndex) {
139            String url = this.prefix;
140            if (url.indexOf("?") > -1) {
141                url += "&amp;" + this.categoryParameterName + "=" 
142                        + URLUtilities.encode(key.toString(), "UTF-8");
143            }
144            else {
145                url += "?" + this.categoryParameterName + "=" 
146                        + URLUtilities.encode(key.toString(), "UTF-8");
147            }
148            if (this.indexParameterName != null) {
149                url += "&amp;" + this.indexParameterName + "=" 
150                       + String.valueOf(pieIndex);
151            }
152            return url;
153        }
154    
155        /**
156         * Tests if this object is equal to another.
157         *
158         * @param obj  the object (<code>null</code> permitted).
159         *
160         * @return A boolean.
161         */
162        public boolean equals(Object obj) {
163            if (obj == this) {
164                return true;
165            }
166            if (!(obj instanceof StandardPieURLGenerator)) {
167                return false;
168            }
169            StandardPieURLGenerator that = (StandardPieURLGenerator) obj;
170            if (!this.prefix.equals(that.prefix)) {
171                return false;
172            }
173            if (!this.categoryParameterName.equals(that.categoryParameterName)) {
174                return false;
175            }
176            if (!ObjectUtilities.equal(this.indexParameterName, 
177                    that.indexParameterName)) {
178                return false;
179            }
180            return true;
181        }
182    }