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

import ilog.views.IlvManager;
import ilog.views.IlvManagerView;
import ilog.views.IlvRect;
import ilog.views.interactor.IlvManagerMagViewInteractor;
import ilog.views.maps.IlvLinearUnit;
import ilog.views.maps.IlvScaleVisibilityFilter;
import ilog.views.maps.beans.IlvScaleBar;
import ilog.views.swing.IlvJManagerViewControlBar;
import ilog.views.tiling.IlvTiledLayer;
import ilog.views.util.IlvProductUtil;

/**
 * This class implements a simple application that show how
 * IlvScaleVisibilityFileter can be used to filter LOD layers.
 */
public class ScaleLodViewer {
  // Variables to manage layers
  IlvManager manager;
  IlvManagerView managerView;
  IlvTiledLayer lowResolutionLodLayer;
  IlvTiledLayer highResolutionLodLayer;

  /**
   * Creates a new ScaleLodViewer
   */
  public ScaleLodViewer() {
    // Create main window
    JFrame mainFrame = new JFrame("LOD demonstration");
    mainFrame.addWindowListener(new WindowAdapter() {
      Override
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    mainFrame.getContentPane().setLayout(new BorderLayout());

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

    controlBar.add(new IlvScaleBar());

    // Create manager and layer
    manager = new IlvManager();

    // Create layers
    highResolutionLodLayer = new IlvTiledLayer(new IlvRect(0, 0, 100, 100));
    highResolutionLodLayer.setTileLoader(new ScaleLodLoader("H", 1));
    highResolutionLodLayer
        .addVisibilityFilter(new IlvScaleVisibilityFilter(1.0F / 10000.0F, 1.0F, IlvLinearUnit.METER));
    lowResolutionLodLayer = new IlvTiledLayer(new IlvRect(0, 0, 500, 500));
    lowResolutionLodLayer.setTileLoader(new ScaleLodLoader("L", 5));
    lowResolutionLodLayer.addVisibilityFilter(new IlvScaleVisibilityFilter(0.0F, 1.0F / 10000.0F, IlvLinearUnit.METER));
    manager.addLayer(highResolutionLodLayer, 0);
    manager.addLayer(lowResolutionLodLayer, 1);

    // Create manager view
    managerView = new IlvManagerView(manager);
    managerView.setKeepingAspectRatio(true);
    managerView.setBackground(Color.white);

    mainFrame.getContentPane().add("Center", managerView);
    controlBar.setView(managerView);

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

    // Show a debug view for hi- res tiles
    Frame hiResDebugFrame = new Frame("Hi-Res debug view");
    IlvManagerView hiResDebugView = new IlvManagerView(manager);
    hiResDebugView.setKeepingAspectRatio(true);
    hiResDebugView.setBackground(Color.white);
    highResolutionLodLayer.setDebugView(hiResDebugView);
    hiResDebugFrame.setLayout(new BorderLayout());
    hiResDebugFrame.add("Center", hiResDebugView);
    hiResDebugView.setVisible(1, false);

    IlvManagerMagViewInteractor interactor = new IlvManagerMagViewInteractor(managerView, true);
    interactor.setAutoTranslating(true);
    interactor.setAutoZooming(true);
    interactor.setBoundedByView(true);
    hiResDebugView.setInteractor(interactor);

    hiResDebugFrame.setBounds(500, 0, 300, 300);
    hiResDebugFrame.setVisible(true);

    // Show a debug view for low res tiles
    JFrame lowResDebugFrame = new JFrame("Low-Res debug view");
    IlvManagerView lowResDebugView = new IlvManagerView(manager);
    lowResDebugView.setKeepingAspectRatio(true);
    lowResDebugView.setBackground(Color.white);
    lowResolutionLodLayer.setDebugView(lowResDebugView);
    lowResDebugFrame.getContentPane().setLayout(new BorderLayout());
    lowResDebugFrame.getContentPane().add("Center", lowResDebugView);
    lowResDebugView.setVisible(0, false);

    interactor = new IlvManagerMagViewInteractor(managerView, true);
    interactor.setAutoTranslating(true);
    interactor.setAutoZooming(true);
    interactor.setBoundedByView(true);
    lowResDebugView.setInteractor(interactor);

    lowResDebugFrame.setBounds(500, 300, 300, 300);
    lowResDebugFrame.setVisible(true);

    // Show the main frame
    mainFrame.setSize(500, 500);
    mainFrame.setVisible(true);
  }

  /**
   * Creates a new instance of a ScaleLodViewer and launch it
   */
  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 ScaleLodViewer();
      }
    });
  }
}