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

import ilog.views.IlvGraphic;
import ilog.views.IlvManager;
import ilog.views.maps.IlvCoordinateSystemProperty;
import ilog.views.maps.IlvFeatureAttributeProperty;
import ilog.views.maps.IlvFeatureRenderer;
import ilog.views.maps.IlvMapFeature;
import ilog.views.maps.IlvMapFeatureIterator;
import ilog.views.maps.IlvMapRenderException;
import ilog.views.maps.format.IlvMapFormatException;
import ilog.views.maps.format.midmif.IlvMIDMIFReader;
import ilog.views.maps.srs.coordsys.IlvCoordinateSystem;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformation;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformationException;
import ilog.views.util.IlvProductUtil;

/**
 * Example of how to use the MIDMIF reader package.
 */
public class MIDMIFReader {
  IlvManager manager = new IlvManager();

  /**
   * Load a MIDMIF file by providing the MIF file name and the MID file name.
   */
  public void loadMIDMIF(String mif, String mid)
      throws IlvMapFormatException, IOException, IlvMapRenderException, IlvCoordinateTransformationException {

    // Create the MIDMIF reader.
    IlvMapFeatureIterator reader = new IlvMIDMIFReader(mif, mid);
    // Retreive the default renderer.
    IlvFeatureRenderer renderer = reader.getDefaultFeatureRenderer();
    // Initialize the coordinate transformation.
    IlvCoordinateSystem managerCS = IlvCoordinateSystemProperty.GetCoordinateSystem(manager);
    // The MIDMIF file can contain coordinate system information.
    IlvCoordinateSystem fileCS = reader.getCoordinateSystem();
    if ((managerCS == null) && (fileCS != null))
      manager.setNamedProperty(new IlvCoordinateSystemProperty(fileCS));
    // Create the transformation accordingly.
    IlvCoordinateTransformation tr = IlvCoordinateTransformation.CreateTransformation(fileCS, managerCS);
    // Retreive first feature.
    IlvMapFeature f = reader.getNextFeature();
    int layersCount = manager.getLayersCount();
    manager.setContentsAdjusting(true);
    while (f != null) {
      // Create graphic object.
      IlvGraphic g = renderer.makeGraphic(f, tr);
      if (g != null) {
        // Add it to the manager.
        manager.addObject(g, layersCount, false);
        // Retreive attributes.
        IlvFeatureAttributeProperty ap = f.getAttributes();
        if (ap != null) {
          // Copy the attributes into the graphic if any.
          g.setNamedProperty(ap.copy());
        }
      }
      // Loop on all features.
      f = reader.getNextFeature();
    }
  }

  public static void main(final String a[]) {
    // 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() {
        MIDMIFReader reader = new MIDMIFReader();
        try {
          reader.loadMIDMIF(a[0], a[1]);
        } catch (IlvMapFormatException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        } catch (IlvMapRenderException e) {
          e.printStackTrace();
        } catch (IlvCoordinateTransformationException e) {
          e.printStackTrace();
        }
      }
    });
  }

}