/*
 * 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 ilog.views.gantt.IlvGanttChart;
import ilog.views.gantt.IlvHierarchyChart;
import ilog.views.gantt.IlvScheduleChart;
import ilog.views.gantt.action.IlvZoomToFitAction;
import ilog.views.util.IlvProductUtil;
import ilog.views.util.styling.IlvStylingException;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


/**
 * Load Project Example sample.
 */
public class LoadProject extends JPanel {

  public IlvHierarchyChart chart;

  private ProjectFileChooser fileChooser = new ProjectFileChooser();
  {
    // Set the initial directory of the fileChooser to a directory that contains
    // some .igpr files.
    fileChooser.setCurrentDirectory(new File("../../../bin/designer/data/examples/"));
  }

  // The zoom-to-fit action.
  private IlvZoomToFitAction zoomToFitAction;

  public LoadProject(JFrame frame) {
    final JFrame parent = frame;

    // Prepare the JPanel's appearance
    setLayout(new BorderLayout());

    JPanel panel = new JPanel();

    // A button to perform loading a project file for Gantt.
    Button buttonGantt = new Button("Designer File for Gantt...");
    buttonGantt.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        File file = fileChooser.showOpenDialog(parent);
        if (file != null) {
          // Get the URL of the file
          URL url = null;
          try {
            url = file.toURI().toURL();
          } catch (MalformedURLException e1) {
          }

          // Load the file
          if (chart != null) {
            remove(chart);
          }

          IlvGanttChart ganttChart = new IlvGanttChart();
          try {
            ganttChart.setProject(url);
          } catch (IOException e1) {
            JOptionPane.showMessageDialog(parent,
                                          e1,
                                          "Error",
                                          JOptionPane.ERROR_MESSAGE);
          } catch (IlvStylingException e1) {
            JOptionPane.showMessageDialog(parent,
                                          e1,
                                          "Error",
                                          JOptionPane.ERROR_MESSAGE);
          }
          chart = ganttChart;
          zoomToFitAction = new IlvZoomToFitAction(chart, "", null, null, "", "");
          add(BorderLayout.CENTER, chart);
          chart.revalidate();
          // Make sure the full chart is visible.
          zoomToFit();
          chart.expandAllRows();
        }
      }
    });
    panel.add(buttonGantt);

    // A button to perform loading a project file for Schedule.
    Button buttonSchedule = new Button("Designer File for Schedule...");
    buttonSchedule.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        File file = fileChooser.showOpenDialog(parent);
        if (file != null) {
          // Get the URL of the file
          URL url = null;
          try {
            url = file.toURI().toURL();
          } catch (MalformedURLException e1) {
          }

          // Load the file
          if (chart != null) {
            remove(chart);
          }

          IlvScheduleChart scheduleChart = new IlvScheduleChart();
          try {
            scheduleChart.setProject(url);
          } catch (IOException e1) {
            JOptionPane.showMessageDialog(parent,
                                          e1,
                                          "Error",
                                          JOptionPane.ERROR_MESSAGE);
          } catch (IlvStylingException e1) {
            JOptionPane.showMessageDialog(parent,
                                          e1,
                                          "Error",
                                          JOptionPane.ERROR_MESSAGE);
          }
          chart = scheduleChart;
          zoomToFitAction = new IlvZoomToFitAction(chart, "", null, null, "", "");
          add(BorderLayout.CENTER, chart);
          chart.revalidate();
          // Make sure the full chart is visible.
          zoomToFit();
          chart.expandAllRows();
        }
      }
    });
    panel.add(buttonSchedule);

    add(BorderLayout.SOUTH, panel);
  }

  /**
   * Zooms the chart to fit the data model's time interval.
   */
  protected void zoomToFit() {
    zoomToFitAction.perform();
  }

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

    SwingUtilities.invokeLater(new Runnable() {
      Override
      public void run() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(10, 400, 700, 400);
        LoadProject example = new LoadProject(frame);
        frame.getContentPane().add(example);
        frame.setVisible(true);
      }
    });
  }


}