Source for org.jfree.chart.urls.TimeSeriesURLGenerator

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2006, 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
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ---------------------------
  28:  * TimeSeriesURLGenerator.java
  29:  * ---------------------------
  30:  * (C) Copyright 2002-2006, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributors:     David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: TimeSeriesURLGenerator.java,v 1.5.2.2 2006/07/06 09:42:12 mungady Exp $
  36:  *
  37:  * Changes:
  38:  * --------
  39:  * 29-Aug-2002 : Initial version (RA);
  40:  * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41:  * 23-Mar-2003 : Implemented Serializable (DG);
  42:  * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 
  43:  *               getYValue() (DG);
  44:  * 13-Jan-2005 : Modified for XHTML 1.0 compliance (DG);
  45:  * ------------- JFREECHART 1.0.0 ---------------------------------------------
  46:  * 06-Jul-2006 : Swap call to dataset's getX() --> getXValue() (DG);
  47:  * 
  48:  */
  49: 
  50: package org.jfree.chart.urls;
  51: 
  52: import java.io.Serializable;
  53: import java.text.DateFormat;
  54: import java.util.Date;
  55: 
  56: import org.jfree.data.xy.XYDataset;
  57: 
  58: /**
  59:  * A URL generator.
  60:  */
  61: public class TimeSeriesURLGenerator implements XYURLGenerator, Serializable {
  62: 
  63:     /** For serialization. */
  64:     private static final long serialVersionUID = -9122773175671182445L;    
  65:     
  66:     /** A formatter for the date. */
  67:     private DateFormat dateFormat = DateFormat.getInstance();
  68:     
  69:     /** Prefix to the URL */
  70:     private String prefix = "index.html";
  71: 
  72:     /** Name to use to identify the series */
  73:     private String seriesParameterName = "series";
  74: 
  75:     /** Name to use to identify the item */
  76:     private String itemParameterName = "item";
  77: 
  78:     /**
  79:      * Blank constructor
  80:      */
  81:     public TimeSeriesURLGenerator() {
  82:         super();
  83:     }
  84: 
  85:     /**
  86:      * Construct TimeSeriesURLGenerator overriding defaults
  87:      *
  88:      * @param dDateFormat  a formatter for the date.
  89:      * @param sPrefix  the prefix of the URL.
  90:      * @param sSeriesParameterName  the name of the series parameter in the URL.
  91:      * @param sItemParameterName  the name of the item parameter in the URL.
  92:      */
  93:     public TimeSeriesURLGenerator(DateFormat dDateFormat, String sPrefix,
  94:                                   String sSeriesParameterName, 
  95:                                   String sItemParameterName) {
  96: 
  97:         this.dateFormat = dDateFormat;
  98:         this.prefix = sPrefix;
  99:         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 = (long) dataset.getXValue(series, item);
 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: }