/*
 * 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.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import ilog.views.IlvGraphic;
import ilog.views.IlvGraphicEnumeration;
import ilog.views.IlvManager;
import ilog.views.IlvManagerView;
import ilog.views.IlvManagerViewInteractor;
import ilog.views.IlvRect;
import ilog.views.event.InteractorChangedEvent;
import ilog.views.event.InteractorListener;
//import ilog.views.util.IlvProductUtil;
import ilog.views.graphic.IlvIcon;
import ilog.views.interactor.IlvSelectInteractor;
import ilog.views.interactor.IlvUnZoomViewInteractor;
import ilog.views.interactor.IlvZoomViewInteractor;
import ilog.views.swing.IlvJScrollManagerView;

/**
 * A small JViews example.
 */
public class Sample4 extends JFrame
implements InteractorListener
{
  IlvManager manager;
  IlvManagerView mgrview;
  IlvSelectInteractor selectInteractor;
  IlvManagerViewInteractor zoomInteractor, unZoomInteractor;
  JButton selectButton, zoomButton, unZoomButton;

  public Sample4()
  {
    // Creates the manager object and reads an IVL file.
    manager = new IlvManager();
    try {
      manager.read("data/lou.ivl");
    } catch (Exception e) {}

    // Creates a view associated to the manager
    mgrview = new IlvManagerView(manager);
    mgrview.setAntialiasing(true);
    mgrview.setBackground(Color.white);

    // Creates the necessary swing components
    getContentPane().setLayout(new BorderLayout(0,0));
    getContentPane().add(new IlvJScrollManagerView(mgrview),
                         BorderLayout.CENTER);

    // Creates additional buttons
    createButtons();

    // Listens to modification of interactors
    mgrview.addInteractorListener(this);

    // Adds some graphic objects
    addObjects();
  }

  /**
   * Creates some swing buttons.
   */
  void createButtons()
  {
    JPanel buttons = new JPanel();

    // A button to install a select interactor
    selectButton = new JButton("Select");
    selectButton.setBackground(Color.gray);
    selectButton.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent evt) {
        if (selectInteractor == null)
          selectInteractor = new IlvSelectInteractor();
        if (mgrview.getInteractor() != selectInteractor)
          mgrview.setInteractor(selectInteractor);
      }
    });
    buttons.add(selectButton);

    // A button to install the unzoom interactor
    unZoomButton = new JButton("-");
    unZoomButton.setBackground(Color.gray);
    unZoomButton.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent evt) {
        if (unZoomInteractor == null)
          unZoomInteractor = new IlvUnZoomViewInteractor();
        if (mgrview.getInteractor() != unZoomInteractor)
          mgrview.setInteractor(unZoomInteractor);
      }
    });

    // a button to install the zoom interactor
    buttons.add(unZoomButton);
    zoomButton = new JButton("+");
    zoomButton.setBackground(Color.gray);
    zoomButton.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent evt) {
        if (zoomInteractor == null)
          zoomInteractor = new IlvZoomViewInteractor();
        if (mgrview.getInteractor() != zoomInteractor)
          mgrview.setInteractor(zoomInteractor);
      }
    });
    buttons.add(zoomButton);

    // a button to move graphic objects in a randomly
    JButton moveButton = new JButton("move");
    moveButton.setBackground(Color.gray);
    moveButton.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent event) {
        moveObjects();
      }
    });
    buttons.add(moveButton);
    getContentPane().add(buttons, BorderLayout.SOUTH);
  }

  /**
   * A method that gets called when the interactor of the
   * view (IlvManagerView) changes.
   * The method changes the color of the corresponding
   * interactor to red.
   */
  Override
  public void interactorChanged(InteractorChangedEvent event)
  {
    IlvManagerViewInteractor oldI = event.getOldValue();
    IlvManagerViewInteractor newI = event.getNewValue();
    if (oldI == selectInteractor)
      selectButton.setBackground(Color.gray);
    else if (oldI == zoomInteractor)
      zoomButton.setBackground(Color.gray);
    else if (oldI == unZoomInteractor)
      unZoomButton.setBackground(Color.gray);

    // there is no new interactor
    if (newI == null)
      return;
    if (newI == selectInteractor)
      selectButton.setBackground(Color.red);
    else if (newI == zoomInteractor)
      zoomButton.setBackground(Color.red);
    else if (newI == unZoomInteractor)
      unZoomButton.setBackground(Color.red);
  }

  /**
   * Adds some graphic objects to the manager.
   */
  public void addObjects()
  {
    manager.setSelectable(0, false);

    for (int i = 0 ; i < 10 ; i++) {
      IlvGraphic obj = new IlvIcon("data/image.gif", new IlvRect(0,0,37,38));
      manager.addObject(obj, 1, false);
    }

  }

  /**
   * Moves graphic objects randomly.
   */
  public void moveObjects()
  {
    IlvGraphic state, obj;
    IlvGraphicEnumeration objects, states;
    // traverse objects in layer number 1

    for (objects = manager.getObjects(1); objects.hasMoreElements();) {
      obj = objects.nextElement();
      // get an random state in layer 0
      states = manager.getObjects(0);
      int index = (int)(manager.getCardinal(0)*Math.random());
      state = states.nextElement();
      for (int i = 1 ; i < index; i++)
        state = states.nextElement();

      // place the graphic object in the middle of the state
      if (obj != null && state != null) {
        IlvRect bbox = state.boundingBox();
        manager.moveObject(obj, bbox.x+bbox.width/2,
                           bbox.y+bbox.height/2, true);
      }
    }
  }

  public static void main(String[] args)
  {
    // This sample uses JViews Diagrammer features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Perforce JViews Diagrammer Deployment license.
//    IlvProductUtil.DeploymentLicenseRequired(
//        IlvProductUtil.JViews_Diagrammer_Deployment);

    // Sun recommends that to put the entire GUI initialization into the
    // AWT thread
    SwingUtilities.invokeLater(
      new Runnable() {
        Override
        public void run() {
          JFrame frame = new Sample4();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(450,350);
          frame.setVisible(true);
        }
      });
  }
}