/*
 * 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 javax.swing.JFrame;

import ilog.views.IlvManager;
import ilog.views.IlvManagerView;
import ilog.views.maps.IlvFeatureRenderer;
import ilog.views.maps.IlvMapFeatureIterator;
import ilog.views.maps.format.IlvMapLoader;
import ilog.views.maps.rendering.IlvIntervalColorModel;
import ilog.views.swing.IlvJManagerViewControlBar;
import ilog.views.swing.IlvJManagerViewPanel;
import ilog.views.util.IlvProductUtil;

/**
 * A viewer to show how to use customized renderers. A ColorLineRenderer is used
 * to display altitude lines (file HYLINE.SHP, using attribute HYLNVAL as an
 * altitude value). A MarkerTextRenderer is used to display populated places
 * (file PPPOINT.SHP, using attribute PPPTNAME as a label).
 */
public class Viewer {
  /**
   * Variables
   */
  IlvManagerView view; // the view

  /**
   * Constructs a new Viewer
   */
  public Viewer(String base) {
    // Create and init main window
    JFrame mainWindow = new JFrame("Renderer examples");
    mainWindow.getContentPane().setLayout(new java.awt.BorderLayout());
    // Create tool bar
    IlvJManagerViewControlBar controlBar = new IlvJManagerViewControlBar();
    mainWindow.getContentPane().add("North", controlBar);
    // Create manager
    IlvManager manager = new IlvManager();
    // Create and initialize view
    view = new IlvManagerView(manager);
    IlvJManagerViewPanel pan = new IlvJManagerViewPanel(view);
    mainWindow.getContentPane().add("Center", pan);
    controlBar.setView(view);
    // Initialize a map loader
    IlvMapLoader loader = new IlvMapLoader(manager);
    // we do want attributes
    loader.setAttachingAttributes(true);
    // get a feature iterator to read features HYLINE.SHP
    IlvMapFeatureIterator featureIterator = null;
    try {
      featureIterator = loader.makeFeatureIterator(base + "/HYLINE.SHP");
    } catch (IOException e) {
      e.printStackTrace();
      System.err.println("Cannot get a feature iterator");
    }
    // load the features with a color line renderer
    if (featureIterator != null) {
      IlvFeatureRenderer renderer = new ColorLineRenderer(IlvIntervalColorModel.MakeElevationColorModel(), "HYLNVAL");
      try {
        int nb = loader.load(featureIterator, renderer);
        System.out.println("Loaded " + nb + " features");
      } catch (IOException e) {
        System.err.println("Error while loading data");
      }
    }
    // get a feature iterator to read features from PPPOINT.SHP
    featureIterator = null;
    try {
      featureIterator = loader.makeFeatureIterator(base + "/PPPOINT.SHP");
    } catch (IOException e) {
      e.printStackTrace();
      System.err.println("Cannot get a feature iterator");
    }
    // load the features with a marker text renderer
    if (featureIterator != null) {
      IlvFeatureRenderer renderer = new MarkerTextRenderer("PPPTNAME");
      try {
        int nb = loader.load(featureIterator, renderer);
        System.out.println("Loaded " + nb + " features");
      } catch (IOException e) {
        System.err.println("Error while loading data");
      }
    }
    // Make the main window visible
    view.setSize(400, 400);
    view.fitTransformerToContent();
    mainWindow.pack();
    mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainWindow.setVisible(true);
  }

  /**
   * Main method
   */
  public static 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);
    final String baseName;
    if (args.length > 0) {
      baseName = args[0];
    } else {
      baseName = "data";
    }
    // Sun recommends that to put the entire GUI initialization into the
    // AWT thread
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      Override
      public void run() {
        new Viewer(baseName);
      }
    });
  }
}