package net.sf.jasperreports.charts.design;

import java.util.ArrayList;
import java.util.List;
import net.sf.jasperreports.charts.JRTimeSeries;
import net.sf.jasperreports.charts.JRTimeSeriesDataset;
import net.sf.jasperreports.engine.JRChartDataset;
import net.sf.jasperreports.engine.JRExpressionCollector;
import net.sf.jasperreports.engine.design.JRDesignChartDataset;
import net.sf.jasperreports.engine.design.JRVerifier;

public class JRDesignTimeSeriesDataset extends JRDesignChartDataset implements JRTimeSeriesDataset {
  private static final long serialVersionUID = 10200L;
  
  public static final String PROPERTY_TIME_PERIOD = "timePeriod";
  
  public static final String PROPERTY_TIME_SERIES = "timeSeries";
  
  private List timeSeriesList = new ArrayList();
  
  private Class timePeriod = null;
  
  public JRDesignTimeSeriesDataset(JRChartDataset dataset) {
    super(dataset);
  }
  
  public JRTimeSeries[] getSeries() {
    JRTimeSeries[] timeSeriesArray = new JRTimeSeries[this.timeSeriesList.size()];
    this.timeSeriesList.toArray((Object[])timeSeriesArray);
    return timeSeriesArray;
  }
  
  public List getSeriesList() {
    return this.timeSeriesList;
  }
  
  public void addTimeSeries(JRTimeSeries timeSeries) {
    this.timeSeriesList.add(timeSeries);
    getEventSupport().fireCollectionElementAddedEvent("timeSeries", timeSeries, this.timeSeriesList.size() - 1);
  }
  
  public JRTimeSeries removeTimeSeries(JRTimeSeries timeSeries) {
    if (timeSeries != null) {
      int idx = this.timeSeriesList.indexOf(timeSeries);
      if (idx >= 0) {
        this.timeSeriesList.remove(idx);
        getEventSupport().fireCollectionElementRemovedEvent("timeSeries", timeSeries, idx);
      } 
    } 
    return timeSeries;
  }
  
  public Class getTimePeriod() {
    return this.timePeriod;
  }
  
  public void setTimePeriod(Class timePeriod) {
    Object old = this.timePeriod;
    this.timePeriod = timePeriod;
    getEventSupport().firePropertyChange("timePeriod", old, this.timePeriod);
  }
  
  public byte getDatasetType() {
    return 6;
  }
  
  public void collectExpressions(JRExpressionCollector collector) {
    collector.collect(this);
  }
  
  public void validate(JRVerifier verifier) {
    verifier.verify(this);
  }
  
  public Object clone() {
    JRDesignTimeSeriesDataset clone = (JRDesignTimeSeriesDataset)super.clone();
    if (this.timeSeriesList != null) {
      clone.timeSeriesList = new ArrayList(this.timeSeriesList.size());
      for (int i = 0; i < this.timeSeriesList.size(); i++)
        clone.timeSeriesList.add(((JRTimeSeries)this.timeSeriesList.get(i)).clone()); 
    } 
    return clone;
  }
}
