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. * TimeSeriesURLGenerator.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. *
  34. * $Id: TimeSeriesURLGenerator.java,v 1.3 2005/03/09 13:45:32 mungady Exp $
  35. *
  36. * Changes:
  37. * --------
  38. * 29-Aug-2002 : Initial version (RA);
  39. * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  40. * 23-Mar-2003 : Implemented Serializable (DG);
  41. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
  42. * getYValue() (DG);
  43. * 13-Jan-2005 : Modified for XHTML 1.0 compliance (DG);
  44. *
  45. */
  46. package org.jfree.chart.urls;
  47. import java.io.Serializable;
  48. import java.text.DateFormat;
  49. import java.util.Date;
  50. import org.jfree.data.xy.XYDataset;
  51. /**
  52. * A URL generator.
  53. *
  54. * @author Richard Atkinson
  55. */
  56. public class TimeSeriesURLGenerator implements XYURLGenerator, Serializable {
  57. /** A formatter for the date. */
  58. private DateFormat dateFormat = DateFormat.getInstance();
  59. /** Prefix to the URL */
  60. private String prefix = "index.html";
  61. /** Name to use to identify the series */
  62. private String seriesParameterName = "series";
  63. /** Name to use to identify the item */
  64. private String itemParameterName = "item";
  65. /**
  66. * Blank constructor
  67. */
  68. public TimeSeriesURLGenerator() {
  69. super();
  70. }
  71. /**
  72. * Construct TimeSeriesURLGenerator overriding defaults
  73. *
  74. * @param dDateFormat a formatter for the date.
  75. * @param sPrefix the prefix of the URL.
  76. * @param sSeriesParameterName the name of the series parameter in the URL.
  77. * @param sItemParameterName the name of the item parameter in the URL.
  78. */
  79. public TimeSeriesURLGenerator(DateFormat dDateFormat, String sPrefix,
  80. String sSeriesParameterName,
  81. String sItemParameterName) {
  82. this.dateFormat = dDateFormat;
  83. this.prefix = sPrefix;
  84. this.seriesParameterName = sSeriesParameterName;
  85. this.itemParameterName = sItemParameterName;
  86. }
  87. /**
  88. * Generates a URL for a particular item within a series.
  89. *
  90. * @param dataset the dataset.
  91. * @param series the series number (zero-based index).
  92. * @param item the item number (zero-based index).
  93. *
  94. * @return The generated URL.
  95. */
  96. public String generateURL(XYDataset dataset, int series, int item) {
  97. String result = this.prefix;
  98. boolean firstParameter = result.indexOf("?") == -1;
  99. String seriesName = dataset.getSeriesName(series);
  100. if (seriesName != null) {
  101. result += firstParameter ? "?" : "&";
  102. result += this.seriesParameterName + "=" + seriesName;
  103. firstParameter = false;
  104. }
  105. long x = dataset.getX(series, item).longValue();
  106. String xValue = this.dateFormat.format(new Date(x));
  107. result += firstParameter ? "?" : "&";
  108. result += this.itemParameterName + "=" + xValue;
  109. return result;
  110. }
  111. }