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