    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2008, 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-2008, by Richard Atkinson and Contributors.
    031     *
    032     * Original Author:  Richard Atkinson;
    033     * Contributors:     David Gilbert (for Object Refinery Limited);
    034     *
    035     * Changes:
    036     * --------
    037     * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
    038     * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    039     * 07-Mar-2003 : Modified to use KeyedValuesDataset and added pieIndex
    040     *               parameter (DG);
    041     * 21-Mar-2003 : Implemented Serializable (DG);
    042     * 24-Apr-2003 : Switched around PieDataset and KeyedValuesDataset (DG);
    043     * 31-Mar-2004 : Added an optional 'pieIndex' parameter (DG);
    044     * 13-Jan-2005 : Fixed for compliance with XHTML 1.0 (DG);
    045     * ------------- JFREECHART 1.0.x ---------------------------------------------
    046     * 24-Nov-2006 : Fixed equals() method and added argument checks (DG);
    047     * 17-Apr-2007 : Encode section key in generateURL() (DG);
    048     *
    049     */
    050
    051    package org.jfree.chart.urls;
    052
    053    import java.io.Serializable;
    054
    055    import org.jfree.data.general.PieDataset;
    056    import org.jfree.util.ObjectUtilities;
    057
    058    /**
    059     * A URL generator for pie charts.  Instances of this class are immutable.
    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            this("index.html");
    080        }
    081
    082        /**
    083         * Creates a new generator.
    084         *
    085         * @param prefix  the prefix (<code>null</code> not permitted).
    086         */
    087        public StandardPieURLGenerator(String prefix) {
    088            this(prefix, "category");
    089        }
    090
    091        /**
    092         * Creates a new generator.
    093         *
    094         * @param prefix  the prefix (<code>null</code> not permitted).
    095         * @param categoryParameterName  the category parameter name
    096         *     (<code>null</code> not permitted).
    097         */
    098        public StandardPieURLGenerator(String prefix,
    099                                       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    }