/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2017 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */
package realtime;

import java.awt.Color;

import ilog.views.chart.IlvStyle;
import ilog.views.chart.data.IlvCyclicDataSet;
import ilog.views.chart.graphic.IlvDataIndicator;

class MonitorDataSet extends IlvCyclicDataSet {
  private static final IlvStyle CURSOR_STYLE = IlvStyle.createStroked(Color.white);

  private int counter = 0;
  private boolean sweepMode = false;
  private int bufferSize;
  private IlvDataIndicator cursor;

  /**
   * Creates a new <code>MonitorDataSet</code>.
   */
  public MonitorDataSet(String name, int bufferSize) {
    super(name, bufferSize, ROTATE_MODE, false);
    this.bufferSize = bufferSize;
    cursor = new IlvDataIndicator(-1, 0, null);
    cursor.setDrawOrder(2);
    cursor.setStyle(CURSOR_STYLE);
    cursor.setVisible(false);
  }

  /**
   * Returns the indicator used by the sweep mode.
   */
  public final IlvDataIndicator getCursor() {
    return cursor;
  }

  /**
   * Indicates whether the sweep mode is on.
   */
  public final boolean isSweepMode() {
    return sweepMode;
  }

  /**
   * Toggles the sweep mode.
   */
  public void setSweepMode(boolean sweepMode) {
    this.sweepMode = sweepMode;
    setData(null, new double[0], 0);
    counter = 0;
    cursor.setVisible(sweepMode);
    if (getDataCount() > 0)
      cursor.setValue(getXData(counter % bufferSize));
    else
      cursor.setValue(0);
  }

  /**
   * Returns the number of points added to this data set.
   */
  public final int getCounter() {
    return counter;
  }

  /**
   * Processes a new data value.
   */
  public void processData(double v) {
    if (isSweepMode() && getDataCount() == bufferSize) {
      setData(counter % bufferSize, 0, v);
    } else {
      this.addData(0, v);
    }
    cursor.setValue(getXData(counter % bufferSize));
    ++counter;
  }
}