/*
 * 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.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.io.File;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;

import ilog.views.chart.IlvChart;
import ilog.views.chart.IlvChartInteractor;
import ilog.views.chart.interactor.IlvChart3DViewInteractor;
import ilog.views.util.IlvProductUtil;

/**
 * A class that loads a Perforce JViews Charts Designer project file into a
 * chart. It also customizes the chart by adding a 3D interactor.
 */
public class ProjectBased extends JFrame {
  private IlvChart chart;

  public ProjectBased() {
    super("Simple Application based on a Designer Project File");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    // Creates the chart instance on which project file will be applied.
    JButton button = new JButton("Open Project File");
    button.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent evt) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.addChoosableFileFilter(new FileFilter() {
          Override
          public boolean accept(File f) {
            return f.isDirectory() || f.getName().endsWith(".icpr");
          }

          Override
          public String getDescription() {
            return "Perforce JViews Charts Designer project file";
          }
        });
        int ret = fileChooser.showOpenDialog(ProjectBased.this);
        if (ret == JFileChooser.APPROVE_OPTION) {
          File file = fileChooser.getSelectedFile();
          try {
            URL project = file.toURI().toURL();
            // Apply the project file to the chart
            chart.setProject(project);
          } catch (Exception e) {
            e.printStackTrace();
          }
          chart.set3D(true);
          chart.setInteractors(new IlvChartInteractor[] {
              new IlvChart3DViewInteractor(InputEvent.BUTTON1_DOWN_MASK, InputEvent.BUTTON1_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK) });
        }
      }
    });
    getContentPane().add(button, BorderLayout.NORTH);
    chart = new IlvChart();
    getContentPane().add(chart, BorderLayout.CENTER);

    setSize(500, 400);
  }

  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() {
        ProjectBased frame = new ProjectBased();
        frame.setVisible(true);
      }
    });
  }
}