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

import ilog.views.IlvGraphic;
import ilog.views.IlvManager;
import ilog.views.maps.IlvFeatureRenderer;
import ilog.views.maps.IlvMapFeature;
import ilog.views.maps.IlvMapRenderException;
import ilog.views.maps.format.IlvMapFormatException;
import ilog.views.maps.format.cadrg.IlvCADRGCoverage;
import ilog.views.maps.format.cadrg.IlvCADRGFrameReader;
import ilog.views.maps.format.cadrg.IlvCADRGLayer;
import ilog.views.maps.format.cadrg.IlvCADRGTocReader;
import ilog.views.maps.rendering.IlvDefaultImageRenderer;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformation;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformationException;
import ilog.views.util.IlvProductUtil;

/**
 * Examples of how to use the CADRG reader package.
 */
public class CADRGReader {
  IlvManager manager = new IlvManager();

  /**
   * Reads a single CADRG frame.
   */
  public void readSingleFrame(String frameFileName)
      throws IlvMapFormatException, IOException, IlvMapRenderException, IlvCoordinateTransformationException {

    // Instantiate a reader.
    IlvCADRGFrameReader freader = new IlvCADRGFrameReader(frameFileName, false);
    // Retreive the default renderer.
    IlvFeatureRenderer renderer = freader.getDefaultFeatureRenderer();
    ((IlvDefaultImageRenderer) renderer).getImageRenderingStyle().setHighQualityRendering(true);
    // Create a dummy transformation. CADRG files cannot be reprojected.
    IlvCoordinateTransformation tr = IlvCoordinateTransformation.CreateTransformation(null, null);
    // Retreive the first map feature.
    IlvMapFeature feature = freader.getNextFeature();
    while (feature != null) {
      // Create corresponding graphic object.
      IlvGraphic graphic = renderer.makeGraphic(feature, tr);
      // Adds it to a manager.
      manager.addObject(graphic, false);
      // Loop on features.
      feature = freader.getNextFeature();
    }
  }

  /**
   * Create a IlvCADRGLayer for each coverage. Each layer being a load on demand
   * layer.
   */
  public void readFromToc(String aDotToc) throws FileNotFoundException, IlvMapFormatException, IOException {

    // Create table of content reader.
    IlvCADRGTocReader tocReader = new IlvCADRGTocReader(aDotToc);
    // Retreive coverages.
    IlvCADRGCoverage coverages[] = tocReader.getCoverages();
    // Create a layer for each coverage, add it to the manager.
    for (int i = 0; i < coverages.length; i++) {
      IlvCADRGLayer layer = new IlvCADRGLayer(coverages[i]);
      manager.addLayer(layer, -1);
    }
  }

  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() {
        CADRGReader reader = new CADRGReader();
        try {
          reader.readFromToc(a[0]);
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IlvMapFormatException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    });
  }
}