KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > urls > StandardCategoryURLGenerator


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ---------------------------------
27  * StandardCategoryURLGenerator.java
28  * ---------------------------------
29  * (C) Copyright 2002-2005, by Richard Atkinson and Contributors.
30  *
31  * Original Author: Richard Atkinson;
32  * Contributors: David Gilbert (for Object Refinery Limited);
33  * Cleland Early;
34  *
35  * $Id: StandardCategoryURLGenerator.java,v 1.6 2005/05/19 14:01:16 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
40  * 29-Aug-2002 : Reversed seriesParameterName and itemParameterName in
41  * constructor. Never should have been the other way round.
42  * Also updated JavaDoc (RA);
43  * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
44  * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
45  * 23-Mar-2003 : Implemented Serializable (DG);
46  * 13-Aug-2003 : Implemented Cloneable (DG);
47  * 23-Dec-2003 : Added fix for bug 861282 (DG);
48  * 21-May-2004 : Added URL encoding - see patch 947854 (DG);
49  * 13-Jan-2004 : Fixed for compliance with XHTML 1.0 (DG);
50  *
51  */

52
53 package org.jfree.chart.urls;
54
55 import java.io.Serializable JavaDoc;
56 import java.net.URLEncoder JavaDoc;
57
58 import org.jfree.data.category.CategoryDataset;
59 import org.jfree.util.ObjectUtilities;
60
61 /**
62  * A URL generator that can be assigned to a
63  * {@link org.jfree.chart.renderer.category.CategoryItemRenderer}.
64  *
65  * @author Richard Atkinson
66  */

67 public class StandardCategoryURLGenerator implements CategoryURLGenerator,
68                                                      Cloneable JavaDoc, Serializable JavaDoc {
69
70     /** For serialization. */
71     private static final long serialVersionUID = 2276668053074881909L;
72     
73     /** Prefix to the URL */
74     private String JavaDoc prefix = "index.html";
75
76     /** Series parameter name to go in each URL */
77     private String JavaDoc seriesParameterName = "series";
78
79     /** Category parameter name to go in each URL */
80     private String JavaDoc categoryParameterName = "category";
81
82     /**
83      * Creates a new generator with default settings.
84      */

85     public StandardCategoryURLGenerator() {
86         super();
87     }
88
89     /**
90      * Constructor that overrides default prefix to the URL.
91      *
92      * @param prefix the prefix to the URL (<code>null</code> not permitted).
93      */

94     public StandardCategoryURLGenerator(String JavaDoc prefix) {
95         if (prefix == null) {
96             throw new IllegalArgumentException JavaDoc("Null 'prefix' argument.");
97         }
98         this.prefix = prefix;
99     }
100
101     /**
102      * Constructor that overrides all the defaults.
103      *
104      * @param prefix the prefix to the URL (<code>null</code> not permitted).
105      * @param seriesParameterName the name of the series parameter to go in
106      * each URL (<code>null</code> not permitted).
107      * @param categoryParameterName the name of the category parameter to go in
108      * each URL (<code>null</code> not permitted).
109      */

110     public StandardCategoryURLGenerator(String JavaDoc prefix,
111                                         String JavaDoc seriesParameterName,
112                                         String JavaDoc categoryParameterName) {
113
114         if (prefix == null) {
115             throw new IllegalArgumentException JavaDoc("Null 'prefix' argument.");
116         }
117         if (seriesParameterName == null) {
118             throw new IllegalArgumentException JavaDoc(
119                 "Null 'seriesParameterName' argument."
120             );
121         }
122         if (categoryParameterName == null) {
123             throw new IllegalArgumentException JavaDoc(
124                 "Null 'categoryParameterName' argument."
125             );
126         }
127         this.prefix = prefix;
128         this.seriesParameterName = seriesParameterName;
129         this.categoryParameterName = categoryParameterName;
130
131     }
132
133     /**
134      * Generates a URL for a particular item within a series.
135      *
136      * @param dataset the dataset.
137      * @param series the series index (zero-based).
138      * @param category the category index (zero-based).
139      *
140      * @return The generated URL.
141      */

142     public String JavaDoc generateURL(CategoryDataset dataset, int series,
143                               int category) {
144         String JavaDoc url = this.prefix;
145         Comparable JavaDoc seriesKey = dataset.getRowKey(series);
146         Comparable JavaDoc categoryKey = dataset.getColumnKey(category);
147         boolean firstParameter = url.indexOf("?") == -1;
148         url += firstParameter ? "?" : "&amp;";
149 // try {
150
url += this.seriesParameterName + "="
151                 + URLEncoder.encode(seriesKey.toString());
152                 // + URLEncoder.encode(seriesKey.toString(), "UTF-8");
153
// Not supported in JDK 1.2.2
154
// }
155
// catch (UnsupportedEncodingException uee) {
156
// url += this.seriesParameterName + "=" + seriesKey.toString();
157
// }
158
// try {
159
url += "&amp;" + this.categoryParameterName + "="
160                 + URLEncoder.encode(categoryKey.toString());
161                 //+ URLEncoder.encode(categoryKey.toString(), "UTF-8");
162
// not supported in JDK 1.2.2
163
// }
164
// catch (UnsupportedEncodingException uee) {
165
// url += "&" + this.categoryParameterName + "="
166
// + categoryKey.toString();
167
// }
168
return url;
169     }
170
171     /**
172      * Returns an independent copy of the URL generator.
173      *
174      * @return A clone.
175      *
176      * @throws CloneNotSupportedException not thrown by this class, but
177      * subclasses (if any) might.
178      */

179     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
180     
181         // all attributes are immutable, so we can just return the super.clone()
182
return super.clone();
183         
184     }
185     
186     /**
187      * Tests the generator for equality with an arbitrary object.
188      *
189      * @param obj the object (<code>null</code> permitted).
190      *
191      * @return A boolean.
192      */

193     public boolean equals(Object JavaDoc obj) {
194         if (obj == this) {
195             return true;
196         }
197         if (!(obj instanceof StandardCategoryURLGenerator)) {
198             return false;
199         }
200         StandardCategoryURLGenerator that = (StandardCategoryURLGenerator) obj;
201         if (!ObjectUtilities.equal(this.prefix, that.prefix)) {
202             return false;
203         }
204
205         if (!ObjectUtilities.equal(this.seriesParameterName,
206                 that.seriesParameterName)) {
207             return false;
208         }
209         if (!ObjectUtilities.equal(this.categoryParameterName,
210                 that.categoryParameterName)) {
211             return false;
212         }
213         return true;
214     }
215
216     /**
217      * Returns a hash code.
218      *
219      * @return A hash code.
220      */

221     public int hashCode() {
222         int result;
223         result = (this.prefix != null ? this.prefix.hashCode() : 0);
224         result = 29 * result
225             + (this.seriesParameterName != null
226                     ? this.seriesParameterName.hashCode() : 0);
227         result = 29 * result
228             + (this.categoryParameterName != null
229                     ? this.categoryParameterName.hashCode() : 0);
230         return result;
231     }
232     
233 }
234
Popular Tags