/**
   * Schedules a series with the MASON simulation. You specify a value provider to give series data
   * each timestep. This value provider can be null, in which case no data will ever be updated --
   * you'd have to update it on your own as you saw fit.
   */
  public static Stoppable scheduleSeries(
      final GUIState state,
      final BarChartSeriesAttributes attributes,
      final ProvidesCollection valueProvider) {
    return state.scheduleRepeatingImmediatelyAfter(
        new Steppable() {
          double last = state.state.schedule.BEFORE_SIMULATION;

          public void step(SimState state) {
            double x = state.schedule.getTime();
            if (x > last && x >= state.schedule.EPOCH && x < state.schedule.AFTER_SIMULATION) {
              last = x;
              if (valueProvider != null) {
                final Collection vals = valueProvider.provide();

                // JFreeChart isn't synchronized.  So we have to update it from the Swing Event
                // Thread
                if (vals != null)
                  SwingUtilities.invokeLater(
                      new Runnable() {
                        public void run() {
                          attributes.setElements(new ArrayList(vals));
                        }
                      });
              }
              // this will get pushed on the swing queue late
              attributes.getGenerator().updateChartLater(state.schedule.getSteps());
            }
          }
        });
  }
  ChartGenerator createNewChart(final GUIState simulation) {
    generator = createNewGenerator();
    globalAttributes = new GlobalAttributes();
    generator.addGlobalAttribute(globalAttributes); // it'll be added last

    // set up the simulation -- need a new name other than guiObjects: and it should be
    // a HashMap rather than a Bag.
    if (simulation.guiObjects == null) simulation.guiObjects = new Bag();
    simulation.guiObjects.add(generator);
    final JFrame f = generator.createFrame(simulation);
    WindowListener wl =
        new WindowListener() {
          public void windowActivated(WindowEvent e) {}

          public void windowClosed(WindowEvent e) {}

          public void windowClosing(WindowEvent e) {
            generator.quit();
          }

          public void windowDeactivated(WindowEvent e) {}

          public void windowDeiconified(WindowEvent e) {}

          public void windowIconified(WindowEvent e) {}

          public void windowOpened(WindowEvent e) {}
        };
    f.addWindowListener(wl);
    f.setVisible(true);

    return generator;
  }
  /**
   * Schedules a series with the MASON simulation. You specify a value provider to give series data
   * each timestep. This value provider can be null, in which case no data will ever be updated --
   * you'd have to update it on your own as you saw fit.
   */
  public static Stoppable scheduleSeries(
      final GUIState state, final TimeSeriesAttributes attributes, final Valuable valueProvider) {
    return state.scheduleRepeatingImmediatelyAfter(
        new Steppable() {
          final XYSeries series = attributes.getSeries();
          double last = state.state.schedule.BEFORE_SIMULATION;

          public void step(SimState state) {
            final double x = state.schedule.getTime();
            if (x > last && x >= state.schedule.EPOCH && x < state.schedule.AFTER_SIMULATION) {
              last = x;
              final double value =
                  (valueProvider == null) ? Double.NaN : valueProvider.doubleValue();

              // JFreeChart isn't synchronized.  So we have to update it from the Swing Event Thread
              SwingUtilities.invokeLater(
                  new Runnable() {
                    public void run() {
                      attributes.possiblyCull();
                      series.add(x, value, true);
                    }
                  });
              // this will get pushed on the swing queue late
              attributes.getGenerator().updateChartLater(state.schedule.getSteps());
            }
          }
        });
  }