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 * TimeSeriesURLGenerator.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: TimeSeriesURLGenerator.java,v 1.5.2.1 2005/10/25 20:59:31 mungady Exp $ 036 * 037 * Changes: 038 * -------- 039 * 29-Aug-2002 : Initial version (RA); 040 * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG); 041 * 23-Mar-2003 : Implemented Serializable (DG); 042 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 043 * getYValue() (DG); 044 * 13-Jan-2005 : Modified for XHTML 1.0 compliance (DG); 045 * 046 */ 047 048 package org.jfree.chart.urls; 049 050 import java.io.Serializable; 051 import java.text.DateFormat; 052 import java.util.Date; 053 054 import org.jfree.data.xy.XYDataset; 055 056 /** 057 * A URL generator. 058 * 059 * @author Richard Atkinson 060 */ 061 public class TimeSeriesURLGenerator implements XYURLGenerator, Serializable { 062 063 /** For serialization. */ 064 private static final long serialVersionUID = -9122773175671182445L; 065 066 /** A formatter for the date. */ 067 private DateFormat dateFormat = DateFormat.getInstance(); 068 069 /** Prefix to the URL */ 070 private String prefix = "index.html"; 071 072 /** Name to use to identify the series */ 073 private String seriesParameterName = "series"; 074 075 /** Name to use to identify the item */ 076 private String itemParameterName = "item"; 077 078 /** 079 * Blank constructor 080 */ 081 public TimeSeriesURLGenerator() { 082 super(); 083 } 084 085 /** 086 * Construct TimeSeriesURLGenerator overriding defaults 087 * 088 * @param dDateFormat a formatter for the date. 089 * @param sPrefix the prefix of the URL. 090 * @param sSeriesParameterName the name of the series parameter in the URL. 091 * @param sItemParameterName the name of the item parameter in the URL. 092 */ 093 public TimeSeriesURLGenerator(DateFormat dDateFormat, String sPrefix, 094 String sSeriesParameterName, 095 String sItemParameterName) { 096 097 this.dateFormat = dDateFormat; 098 this.prefix = sPrefix; 099 this.seriesParameterName = sSeriesParameterName; 100 this.itemParameterName = sItemParameterName; 101 102 } 103 104 /** 105 * Generates a URL for a particular item within a series. 106 * 107 * @param dataset the dataset. 108 * @param series the series number (zero-based index). 109 * @param item the item number (zero-based index). 110 * 111 * @return The generated URL. 112 */ 113 public String generateURL(XYDataset dataset, int series, int item) { 114 String result = this.prefix; 115 boolean firstParameter = result.indexOf("?") == -1; 116 Comparable seriesKey = dataset.getSeriesKey(series); 117 if (seriesKey != null) { 118 result += firstParameter ? "?" : "&"; 119 result += this.seriesParameterName + "=" + seriesKey.toString(); 120 firstParameter = false; 121 } 122 123 long x = dataset.getX(series, item).longValue(); 124 String xValue = this.dateFormat.format(new Date(x)); 125 result += firstParameter ? "?" : "&"; 126 result += this.itemParameterName + "=" + xValue; 127 128 return result; 129 } 130 131 132 }