/*
 * 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.
 */

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;

import ilog.views.chart.IlvChart;
import ilog.views.chart.data.xml.IlvXMLDataSource;
import ilog.views.util.IlvProductUtil;

/**
 * A simple example that illustrates the <i>Styling</i> Chapter of the Charts
 * User's Manual.
 */
public class ChartCSSExample extends JFrame {

  private static final String XML_FILE = "resources/simple.xml";
  private static final String CSS_FILE = "resources/colored.css";

  protected IlvChart chart;
  protected IlvXMLDataSource xmlDS = new IlvXMLDataSource();

  /** Creates a new <code>ChartCSSExample</code>. */
  public ChartCSSExample() {
    super("Styling");
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.chart = createChart();
    getContentPane().add(chart);
    final JToggleButton button = new JToggleButton("Switch Style Sheet");
    button.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        if (button.isSelected())
          setStyleSheets(new String[] { CSS_FILE });
        else {
          setStyleSheets(xmlDS.getStyleSheets());
        }
      }
    });
    getContentPane().add(button, BorderLayout.NORTH);
  }

  /** Creates the chart. */
  protected IlvChart createChart() {
    IlvChart chart = new IlvChart();
    try {
      xmlDS.setFilename(XML_FILE);
    } catch (Exception x) {
      x.printStackTrace();
    }
    chart.setDataSource(xmlDS);
    return chart;
  }

  private void setStyleSheets(String[] styleSheets) {
    try {
      chart.setStyleSheets(styleSheets);
    } catch (ilog.views.util.styling.IlvStylingException x) {
      System.err.println("Cannot load style sheets: " + x.getMessage());
      x.printStackTrace();
    }
  }

  public static void main(String[] args) {
    // This sample uses JViews Charts features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Perforce JViews Charts Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(IlvProductUtil.JViews_Charts_Deployment);

    SwingUtilities.invokeLater(new Runnable() {
      Override
      public void run() {
        ChartCSSExample frame = new ChartCSSExample();
        frame.setSize(400, 400);
        frame.setVisible(true);
      }
    });
  }
}