/*
 * 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.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JFrame;

import ilog.views.IlvManager;
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 TestViewer {
  JFrame mainFrame;
  IlvJManagerViewControlBar controlBar;
  IlvManager manager;
  IlvManagerView view;

  public TestViewer() {
    mainFrame = new JFrame("Polylines");

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

    mainFrame.getContentPane().setLayout(new BorderLayout());

    manager = new IlvManager();
    view = new IlvManagerView(manager);
    view.setKeepingAspectRatio(true);
    mainFrame.getContentPane().add("Center", new IlvJManagerViewPanel(view));

    controlBar = new IlvJManagerViewControlBar();
    mainFrame.getContentPane().add("North", controlBar);

    controlBar.setView(view);

    mainFrame.setSize(500, 500);
    mainFrame.setVisible(true);
  }

  public void loadFile(String filename) throws IOException {
    IlvMapLoader loader = new MapLoader(manager);

    loader.load(filename);
    view.fitTransformerToContent();
    view.repaint();
  }

  public static void main(String[] argv) {
    // 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);

    final String fileName;
    if (argv.length >= 1) {
      fileName = argv[0];
    } else {
      fileName = "data/dcwbrowse.pol";
    }
    // Sun recommends that to put the entire GUI initialization into the
    // AWT thread
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      Override
      public void run() {
        TestViewer viewer = new TestViewer();
        try {
          viewer.loadFile(fileName);
        } catch (IOException e) {
          e.printStackTrace();
          System.err.println("Cannot read file");
          System.exit(-1);
        }
      }
    });
  }
}