/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © 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 demo.charts;

import java.text.DecimalFormat;

import javax.faces.event.ValueChangeEvent;

import ilog.views.chart.IlvChart;
import ilog.views.chart.data.IlvDataSet;
import ilog.views.chart.data.IlvDataSetPoint;
import ilog.views.chart.data.IlvDataSetProperty;
import ilog.views.chart.faces.dhtml.interactor.IlvChartObjectSelectedFinder;
import ilog.views.util.servlet.model.IlvMenu;

/**
 * Server bean used in the sample.
 */
public class DemoBean {
  private String dataX;
  private String dataY;
  private IlvChartObjectSelectedFinder finder = new IlvChartObjectSelectedFinder() {
    Override
    public IlvDataSetPoint pickObject(IlvChart chart, String[] params) {
      System.out.println("Demo trace: Object picked");// trace //$NON-NLS-1$
      return super.pickObject(chart, params);
    }
  };
  IlvDataSetPoint oldPoint = null;

  /**
   * Server action called when a point is selected on the chart
   * 
   * @param event
   */
  public void pointSelected(ValueChangeEvent event) {
    IlvDataSetPoint point = (IlvDataSetPoint) event.getNewValue();
    if (point != null) {
      DecimalFormat format = new DecimalFormat("###.##"); //$NON-NLS-1$
      IlvDataSet dset = point.getDataSet();
      dataX = point.getXData() + " (" + format.format(dset.getXData(point.getIndex())) + ")";//$NON-NLS-1$ //$NON-NLS-2$
      dataY = point.getYData() + " (" + format.format(dset.getYData(point.getIndex())) + ")";//$NON-NLS-1$ //$NON-NLS-2$
      if (oldPoint != null)
        IlvDataSetProperty.removePseudoClass(oldPoint.getDataSet(), "selected");//$NON-NLS-1$
      IlvDataSetProperty.addPseudoClass(point.getDataSet(), "selected");//$NON-NLS-1$
      oldPoint = point;
    }
  }

  /**
   * Bean method to return the picked point X coordinate
   * 
   * @return the X coordinate
   */
  public String getDataX() {
    return dataX;
  }

  /**
   * Bean method to set the picked point X coordinate
   * 
   * @param dataX
   *          the X coordinate
   */
  public void setDataX(String dataX) {
    this.dataX = dataX;
  }

  /**
   * Bean method to return the picked point Y coordinate
   * 
   * @return the Y coordinate
   */
  public String getDataY() {
    return dataY;
  }

  /**
   * Bean method to set the picked point Y coordinate
   * 
   * @param dataY
   *          the Y coordinate
   */
  public void setDataY(String dataY) {
    this.dataY = dataY;
  }

  /**
   * Bean method to return the object finder.
   * 
   * @return the object finder
   */
  public IlvChartObjectSelectedFinder getFinder() {
    return finder;
  }

  /**
   * Bean method to set an object finder.
   * 
   * @param finder
   */
  public void setFinder(IlvChartObjectSelectedFinder finder) {
    this.finder = finder;
  }

  IlvMenu staticMenu = null;

  /**
   * Bean method to return a static menu
   * 
   * @return the static menu
   */
  public IlvMenu getMenu() {
    if (staticMenu == null) {
      staticMenu = new ChartMenuFactory().createMenu(null, null, "staticMenu"); //$NON-NLS-1$
    }
    return staticMenu;
  }
}