/*
 * 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.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.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.filechooser.FileFilter;

import ilog.views.IlvManager;
import ilog.views.IlvManagerLayer;
import ilog.views.IlvManagerView;
import ilog.views.maps.format.IlvMapLoader;
import ilog.views.swing.IlvJManagerViewControlBar;
import ilog.views.swing.IlvJManagerViewPanel;
import ilog.views.util.IlvProductUtil;

public class UseMapLoader {
  IlvManager manager = new IlvManager();
  IlvManagerView view = null;
  JFileChooser loadFileDialog = null;
  JFileChooser saveFileDialog = null;
  IlvMapLoader mapLoader = null;
  JFrame frame = null;

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

    // initializing the map loader
    mapLoader = new IlvMapLoader(manager);

    // 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) {
        loadMapFile();
      }
    });
    fileMenu.add(loadItem);

    JMenuItem saveItem = new JMenuItem("Export as ivl");
    saveItem.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        save();
      }
    });
    fileMenu.add(saveItem);

    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 the file dialogs
    loadFileDialog = new JFileChooser();
    loadFileDialog.setDialogTitle("Select a Map file");
    loadFileDialog.setFileFilter(new FileFilter() {
      Override
      public boolean accept(File f) {
        // do not try to read ivl files.
        return !(f.getName().endsWith(".ivl"));
      }

      Override
      public String getDescription() {
        return "Map files";
      }

    });
    saveFileDialog = new JFileChooser();
    saveFileDialog.setDialogTitle("Export");
    // 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 loadMapFile() {
    loadFileDialog.setCurrentDirectory(new File("./data"));
    if (loadFileDialog.showOpenDialog(frame) != JFileChooser.APPROVE_OPTION)
      return;
    File file = loadFileDialog.getSelectedFile();
    String fileName = file.getPath();
    // adding a layer at the end of the manager
    manager.addLayer(-1);
    int last = manager.getLayersCount();
    IlvManagerLayer layer = manager.getManagerLayer(last - 1);
    // specifying to the map loader the layer in which the
    // graphic objects will be added
    mapLoader.setInsertionLayer(layer);
    try {
      // loading the Map File
      mapLoader.load(fileName);
    } catch (IOException e) {
      Toolkit.getDefaultToolkit().beep();
      new ilog.views.maps.beans.IlvExceptionMessage(e, "Could not load data");
      manager.removeLayer(layer.getIndex(), true);
    }

    view.fitTransformerToContent();
    view.repaint();
  }

  /**
   * Saves the content of the manager in the ivl format
   */
  public void save() {
    if (saveFileDialog.showSaveDialog(frame) != JFileChooser.APPROVE_OPTION)
      return;
    File file = saveFileDialog.getSelectedFile();
    String fileName = file.getPath();
    try {
      manager.write(fileName);
    } catch (IOException e) {
      Toolkit.getDefaultToolkit().beep();
      new ilog.views.maps.beans.IlvExceptionMessage(e, "Could not export data");
    }
  }

  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 UseMapLoader();
      }
    });
  }
}