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     * StandardCategoryURLGenerator.java
029     * ---------------------------------
030     * (C) Copyright 2002-2007, by Richard Atkinson and Contributors.
031     *
032     * Original Author:  Richard Atkinson;
033     * Contributors:     David Gilbert (for Object Refinery Limited);
034     *                   Cleland Early;
035     *
036     * Changes:
037     * --------
038     * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
039     * 29-Aug-2002 : Reversed seriesParameterName and itemParameterName in 
040     *               constructor.  Never should have been the other way round.  
041     *               Also updated JavaDoc (RA);
042     * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
043     * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
044     * 23-Mar-2003 : Implemented Serializable (DG);
045     * 13-Aug-2003 : Implemented Cloneable (DG);
046     * 23-Dec-2003 : Added fix for bug 861282 (DG);
047     * 21-May-2004 : Added URL encoding - see patch 947854 (DG);
048     * 13-Jan-2004 : Fixed for compliance with XHTML 1.0 (DG);
049     * ------------- JFREECHART 1.0.x ---------------------------------------------
050     * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
051     * 17-Apr-2007 : Use new URLUtilities class to encode URLs (DG);
052     *
053     */
054    
055    package org.jfree.chart.urls;
056    
057    import java.io.Serializable;
058    
059    import org.jfree.data.category.CategoryDataset;
060    import org.jfree.util.ObjectUtilities;
061    
062    /**
063     * A URL generator that can be assigned to a 
064     * {@link org.jfree.chart.renderer.category.CategoryItemRenderer}.
065     */
066    public class StandardCategoryURLGenerator implements CategoryURLGenerator, 
067                                                         Cloneable, Serializable {
068    
069        /** For serialization. */
070        private static final long serialVersionUID = 2276668053074881909L;
071        
072        /** Prefix to the URL */
073        private String prefix = "index.html";
074    
075        /** Series parameter name to go in each URL */
076        private String seriesParameterName = "series";
077    
078        /** Category parameter name to go in each URL */
079        private String categoryParameterName = "category";
080    
081        /**
082         * Creates a new generator with default settings.
083         */
084        public StandardCategoryURLGenerator() {
085            super();
086        }
087    
088        /**
089         * Constructor that overrides default prefix to the URL.
090         *
091         * @param prefix  the prefix to the URL (<code>null</code> not permitted).
092         */
093        public StandardCategoryURLGenerator(String prefix) {
094            if (prefix == null) {
095                throw new IllegalArgumentException("Null 'prefix' argument.");   
096            }
097            this.prefix = prefix;
098        }
099    
100        /**
101         * Constructor that overrides all the defaults.
102         *
103         * @param prefix  the prefix to the URL (<code>null</code> not permitted).
104         * @param seriesParameterName  the name of the series parameter to go in 
105         *                             each URL (<code>null</code> not permitted).
106         * @param categoryParameterName  the name of the category parameter to go in
107         *                               each URL (<code>null</code> not permitted).
108         */
109        public StandardCategoryURLGenerator(String prefix,
110                                            String seriesParameterName,
111                                            String categoryParameterName) {
112    
113            if (prefix == null) {
114                throw new IllegalArgumentException("Null 'prefix' argument.");   
115            }
116            if (seriesParameterName == null) {
117                throw new IllegalArgumentException(
118                    "Null 'seriesParameterName' argument."
119                );   
120            }
121            if (categoryParameterName == null) {
122                throw new IllegalArgumentException(
123                    "Null 'categoryParameterName' argument."
124                );   
125            }
126            this.prefix = prefix;
127            this.seriesParameterName = seriesParameterName;
128            this.categoryParameterName = categoryParameterName;
129    
130        }
131    
132        /**
133         * Generates a URL for a particular item within a series.
134         *
135         * @param dataset  the dataset.
136         * @param series  the series index (zero-based).
137         * @param category  the category index (zero-based).
138         *
139         * @return The generated URL.
140         */
141        public String generateURL(CategoryDataset dataset, int series, 
142                                  int category) {
143            String url = this.prefix;
144            Comparable seriesKey = dataset.getRowKey(series);
145            Comparable categoryKey = dataset.getColumnKey(category);
146            boolean firstParameter = url.indexOf("?") == -1;
147            url += firstParameter ? "?" : "&amp;";
148            url += this.seriesParameterName + "=" + URLUtilities.encode(
149                    seriesKey.toString(), "UTF-8");
150            url += "&amp;" + this.categoryParameterName + "=" 
151                    + URLUtilities.encode(categoryKey.toString(), "UTF-8");
152            return url;
153        }
154    
155        /**
156         * Returns an independent copy of the URL generator.
157         * 
158         * @return A clone.
159         * 
160         * @throws CloneNotSupportedException not thrown by this class, but 
161         *         subclasses (if any) might.
162         */
163        public Object clone() throws CloneNotSupportedException {
164        
165            // all attributes are immutable, so we can just return the super.clone()
166            return super.clone();
167            
168        }
169        
170        /**
171         * Tests the generator for equality with an arbitrary object.
172         *
173         * @param obj  the object (<code>null</code> permitted).
174         *
175         * @return A boolean.
176         */
177        public boolean equals(Object obj) {
178            if (obj == this) {
179                return true;
180            }
181            if (!(obj instanceof StandardCategoryURLGenerator)) {
182                return false;
183            }
184            StandardCategoryURLGenerator that = (StandardCategoryURLGenerator) obj;
185            if (!ObjectUtilities.equal(this.prefix, that.prefix)) {
186                return false;
187            }
188    
189            if (!ObjectUtilities.equal(this.seriesParameterName, 
190                    that.seriesParameterName)) {
191                return false;
192            }
193            if (!ObjectUtilities.equal(this.categoryParameterName, 
194                    that.categoryParameterName)) {
195                return false;
196            }
197            return true;
198        }
199    
200        /**
201         * Returns a hash code.
202         * 
203         * @return A hash code.
204         */
205        public int hashCode() {
206            int result;
207            result = (this.prefix != null ? this.prefix.hashCode() : 0);
208            result = 29 * result 
209                + (this.seriesParameterName != null 
210                        ? this.seriesParameterName.hashCode() : 0);
211            result = 29 * result 
212                + (this.categoryParameterName != null 
213                        ? this.categoryParameterName.hashCode() : 0);
214            return result;
215        }
216        
217    }