001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2005, 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-2005, 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.1 2005/10/25 20:59:31 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     *
048     */
049     
050    package org.jfree.chart.urls;
051    
052    import java.io.Serializable;
053    
054    import org.jfree.data.general.PieDataset;
055    
056    /**
057     * A URL generator for pie charts.
058     *
059     * @author Richard Atkinson
060     */
061    public class StandardPieURLGenerator implements PieURLGenerator, Serializable {
062    
063        /** For serialization. */
064        private static final long serialVersionUID = 1626966402065883419L;
065        
066        /** The prefix. */
067        private String prefix = "index.html";
068    
069        /** The category parameter name. */
070        private String categoryParameterName = "category";
071        
072        /** The pie index parameter name. */
073        private String indexParameterName = "pieIndex";
074    
075        /**
076         * Default constructor.
077         */
078        public StandardPieURLGenerator() {
079            super();
080        }
081    
082        /**
083         * Creates a new generator.
084         *
085         * @param prefix  the prefix.
086         */
087        public StandardPieURLGenerator(String prefix) {
088            this.prefix = prefix;
089        }
090    
091        /**
092         * Creates a new generator.
093         *
094         * @param prefix  the prefix.
095         * @param categoryParameterName  the category parameter name.
096         */
097        public StandardPieURLGenerator(String prefix, 
098                                       String categoryParameterName) {
099            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 += "&" + this.categoryParameterName + "=" + key.toString();
133            }
134            else {
135                url += "?" + this.categoryParameterName + "=" + key.toString();
136            }
137            if (this.indexParameterName != null) {
138                url += "&" + 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    }