/*
 * 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.Component;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import ilog.views.IlvManager;
import ilog.views.IlvManagerView;
import ilog.views.maps.IlvCoordinateSystemProperty;
import ilog.views.maps.IlvMapLayerTreeProperty;
import ilog.views.maps.IlvMapScaleLimiter;
import ilog.views.maps.beans.IlvExceptionMessage;
import ilog.views.maps.beans.IlvJMapsManagerViewControlBar;
import ilog.views.maps.beans.IlvLayerTreePanel;
import ilog.views.maps.beans.IlvMapLayer;
import ilog.views.maps.beans.IlvMapLayerTreeModel;
import ilog.views.maps.datasource.IlvMIDMIFDataSource;
import ilog.views.maps.graphic.IlvMapSelectionFactory;
import ilog.views.maps.graphic.style.IlvMapStyle;
import ilog.views.maps.graphic.style.IlvPointStyle;
import ilog.views.maps.graphic.style.IlvPolylineStyle;
import ilog.views.maps.srs.coordsys.IlvGeographicCoordinateSystem;
import ilog.views.util.IlvProductUtil;

/**
 * The MID/MIF demo. This demo show how to load a MD/MIF file using the API. The
 * API to load MID/MIF files mailnly consist on simply instanciating a
 * IlvMIDMIFDataSource class.
 */
SuppressWarnings("serial")
public class MidMifDemo extends JRootPane {

  {
    // 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);
  }

  IlvManagerView view;
  IlvManager manager;

  /**
   * Construct a new MidMifDemo.
   * 
   * @throws Exception
   */
  public MidMifDemo() throws Exception {
    // Make sure the swing construction is called in Swing event thread.
    ilog.views.util.swing.IlvSwingUtil.invokeAndWait(new Runnable() {
      Override
      public void run() {
        setupUI();
        try {
          loadData();
        } catch (Exception e) {
          new IlvExceptionMessage(e, null);
        }
      }
    });
  }

  /**
   * Load data using an <code>IlvMIDMIFDataSource</code>.
   * 
   * @throws Exception
   */
  void loadData() throws Exception {
    // find the URL of the data in the jar.
    URL url = getClass().getResource("EgeanSea.mif"); //$NON-NLS-1$
    // instanciate a IlvMIDMIFDataSource.
    IlvMIDMIFDataSource source = new IlvMIDMIFDataSource(url);
    source.setManager(manager);
    // rename top layer.
    IlvMapLayer layer = source.getInsertionLayer();
    layer.setName("Egean Sea");//$NON-NLS-1$
    // add it in the map layer tree
    IlvMapLayerTreeModel ltm = IlvMapLayerTreeProperty.GetMapLayerTreeModel(manager);
    ltm.addChild(null, layer);
    // start the data source: file is read and layers are created.
    source.start();
    // configure resulting layers (example).
    IlvMapLayer[] layers = ltm.getChildren(layer);

    for (int i = 0; i < layers.length; i++) {
      IlvMapLayer l = layers[i];
      IlvMapStyle s = l.getStyle();

      if (s instanceof IlvPointStyle) {
        IlvPointStyle ps = (IlvPointStyle) s;
        ps.setSize(2);
        ps.setForeground(Color.red);
      } else if (s instanceof IlvPolylineStyle) {
        ((IlvPolylineStyle) s).setDecoration(null);
      }
    }
    // fit the map.
    view.fitTransformerToContent();
  }

  /**
   * Construct the user interface.
   */
  void setupUI() {
    view = new IlvManagerView();
    view.setKeepingAspectRatio(true);
    view.setSize(600, 300);
    manager = view.getManager();
    manager.setSelectionFactory(new IlvMapSelectionFactory());
    // JV-2819: geo reference the view
    view.getManager().setNamedProperty(new IlvCoordinateSystemProperty(IlvGeographicCoordinateSystem.WGS84));
    // limit the zoom to correct scales.
    IlvMapScaleLimiter limiter = new IlvMapScaleLimiter((float) (1 / 1E4), (float) (1 / 1E9));
    limiter.setView(view);
    IlvLayerTreePanel layerPanel = new IlvLayerTreePanel();
    layerPanel.setView(view);
    Component left = layerPanel;
    Component right = new JScrollPane(view);
    JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
    pane.setDividerLocation(200);
    getContentPane().add(pane, BorderLayout.CENTER);

    IlvJMapsManagerViewControlBar bar = new IlvJMapsManagerViewControlBar();
    bar.setView(view);
    getContentPane().add(bar, BorderLayout.NORTH);
  }

  /**
   * @param args
   *          : not used
   */
  public static void main(String[] args) {
    // Sun recommends that to put the entire GUI initialization into the
    // AWT thread
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      Override
      public void run() {
        JFrame frame = new JFrame("New MidMif sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MidMifDemo demo;
        try {
          demo = new MidMifDemo();
        } catch (Exception e) {
          e.printStackTrace();
          return;
        }
        frame.getContentPane().add(demo);
        frame.pack();
        frame.setVisible(true);
      }
    });
  }

}