/*
 * 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.Color;
import java.awt.FileDialog;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import ilog.views.IlvManager;
import ilog.views.IlvManagerView;
import ilog.views.io.IlvReadFileException;
import ilog.views.maps.IlvMapInputStream;
import ilog.views.swing.IlvJManagerViewControlBar;
import ilog.views.swing.IlvJManagerViewPanel;
import ilog.views.util.IlvProductUtil;

/**
 * Example of how to create an application that load a map expressed in a
 */
public class LoadIvlMap {
  private IlvManager manager = new IlvManager();
  private IlvManagerView view = null;
  private FileDialog fileDialog = null;

  public LoadIvlMap() {
    JFrame frame = new JFrame("Loading a JViews  Map");
    frame.getContentPane().setLayout(new BorderLayout());

    // creating a view of the manager and adding it in the frame
    view = new IlvManagerView(manager);
    view.setKeepingAspectRatio(true);
    view.setBackground(Color.lightGray);
    frame.getContentPane().add("Center", new IlvJManagerViewPanel(view));

    // adding a control bar
    IlvJManagerViewControlBar controlBar = new IlvJManagerViewControlBar();
    controlBar.setView(view);
    frame.getContentPane().add("North", controlBar);

    // adding a menu bar
    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);

    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);
    JMenuItem loadItem = new JMenuItem("load");
    loadItem.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        loadJViewsFile();
      }
    });
    fileMenu.add(loadItem);

    JMenuItem quitItem = new JMenuItem("Quit");
    quitItem.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });
    fileMenu.add(quitItem);

    frame.addWindowListener(new WindowAdapter() {
      Override
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    // create a file dialog
    fileDialog = new FileDialog(frame, "Select a JViews file", FileDialog.LOAD);

    // setting the frame size
    frame.setSize(300, 300);
    frame.setLocation(new Point(30, 30));
    frame.setVisible(true);

  }

  /**
   * This function pops a file selector and loads the requested JViews file
   */
  public void loadJViewsFile() {
    fileDialog.setDirectory("./data");
    fileDialog.setTitle("Select an IVL file");
    fileDialog.setVisible(true);
    String fileName = fileDialog.getFile();
    // only continue if the selected file is an ivl file.
    if (fileName == null) {
      return;
    }
    if (!fileName.endsWith(".ivl")) {
      Toolkit.getDefaultToolkit().beep();
      System.out.println("No JViews file");
      return;
    }
    fileName = fileDialog.getDirectory() + fileName;
    // loading the JViews File
    try {
      // clean the IlvManager without redisplay.
      manager.deleteAll(false);

      // Read the file.
      IlvMapInputStream mapInput = new IlvMapInputStream(fileName);
      // Load the map
      mapInput.read(manager);
      // fit to view all the map.
      view.fitTransformerToContent();
      // repaint.
      view.repaint();
    } catch (IlvReadFileException e) {
      Toolkit.getDefaultToolkit().beep();
      System.out.println("Error in the JViews file");
      e.printStackTrace();
      return;
    } catch (IOException e) {
      Toolkit.getDefaultToolkit().beep();
      System.out.println("IO error");
      e.printStackTrace();
    }
  }

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

    // Sun recommends that to put the entire GUI initialization into the
    // AWT thread
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      Override
      public void run() {
        new LoadIvlMap();
      }
    });
  }
}