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

import ilog.views.IlvManager;
import ilog.views.IlvManagerView;
import ilog.views.IlvRect;
import ilog.views.awt.IlvManagerViewControlBar;
import ilog.views.interactor.IlvManagerMagViewInteractor;
import ilog.views.tiling.IlvTiledLayer;
import ilog.views.tiling.TileEvent;
import ilog.views.tiling.TileListener;
import ilog.views.util.IlvProductUtil;

/**
 * This class implements a simple application that shows how LOD tiling works
 */
public class LodViewer {
  // Variables to manage lod layers
  IlvManager manager;
  IlvManagerView managerView;
  IlvTiledLayer lodLayer;
  LodLoader lodLoader;

  /**
   * Constructs a new LodViewer
   */
  public LodViewer() {
    // Create main window
    Frame mainFrame = new Frame("LOD demonstration");
    mainFrame.setLayout(new BorderLayout());

    // Add a control bar
    IlvManagerViewControlBar controlBar = new IlvManagerViewControlBar();
    mainFrame.add("North", controlBar);

    // Create manager and layer
    manager = new IlvManager();
    lodLayer = new IlvTiledLayer(new IlvRect(0, 0, 100, 100));
    lodLayer.setTileLoader(new LodLoader("LOD"));
    manager.addLayer(lodLayer, 0);
    lodLayer.getTileController().addTileListener(new TileListener() {
      Override
      public void tileChanged(TileEvent e) {
        // System.err.print(e.getType());
      }
    });

    // Create manager view
    managerView = new IlvManagerView(manager);
    managerView.setKeepingAspectRatio(true);
    managerView.setBackground(Color.white);
    mainFrame.add("Center", managerView);
    controlBar.setView(managerView);

    // Center on tile (0,0) when window opens
    mainFrame.addWindowListener(new WindowAdapter() {
      Override
      public void windowOpened(WindowEvent e) {
        lodLayer.fitTransformerToTile(managerView, 0, 0);
      }
    });

    // Show a debug view
    Frame debugFrame = new Frame("Debug view");
    IlvManagerView debugView = new IlvManagerView(manager);
    debugView.setKeepingAspectRatio(true);
    debugView.setBackground(Color.white);
    lodLayer.setDebugView(debugView);
    debugFrame.setLayout(new BorderLayout());
    debugFrame.add("Center", debugView);

    // set a MagViewInteractor on debug view for easier usage
    IlvManagerMagViewInteractor interactor = new IlvManagerMagViewInteractor(managerView, true);
    interactor.setAutoTranslating(true);
    interactor.setAutoZooming(true);
    interactor.setBoundedByView(true);
    debugView.setInteractor(interactor);
    debugFrame.setBounds(500, 0, 300, 300);
    debugFrame.setVisible(true);

    // Show the frame
    mainFrame.setSize(500, 500);
    mainFrame.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);

    // Sun recommends that to put the entire GUI initialization into the
    // AWT thread
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      Override
      public void run() {
        new LodViewer();
      }
    });
  }
}