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

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import ilog.views.IlvConstantModeManagerFrame;
import ilog.views.IlvDefaultManagerFrame;
import ilog.views.IlvGrapher;
import ilog.views.IlvGraphic;
import ilog.views.IlvManager;
import ilog.views.IlvManagerView;
import ilog.views.IlvRect;
import ilog.views.accelerator.IlvIdentityAccelerator;
import ilog.views.accelerator.IlvZoomInAccelerator;
import ilog.views.accelerator.IlvZoomOutAccelerator;
import ilog.views.graphic.IlvRectangle;
import ilog.views.interactor.IlvSelectInteractor;
//import ilog.views.util.IlvProductUtil;
import ilog.views.swing.IlvJScrollManagerView;

/**
 * This sample shows how to create sub managers or sub graphers 
 * and how to use <code>IlvDefaultManagerFrame</code> or 
 * <code>IlvConstantModeManagerFrame</code>.
 * @proofread
 */
public class SubGraphs extends JFrame
{
  IlvManager manager;
  IlvManagerView mgrview;

  public SubGraphs()
  {
    // Creates the top manager:
    manager = new IlvManager();

    // Create a sub manager.
    IlvManager subManager = new  IlvManager();

    IlvGraphic obj;
    obj = new IlvRectangle(new IlvRect(210,30,50,50), false, true);
    subManager.addObject(obj, false);
    obj = new IlvRectangle(new IlvRect(300,120,50,50), false, true);
    subManager.addObject(obj, false);

    // Use a constant mode frame:
    IlvDefaultManagerFrame frame = new IlvConstantModeManagerFrame();
    frame.setBackground(Color.yellow);
    frame.setForeground(Color.BLUE);
    frame.setRightMargin(60);
    frame.setTitle("This manager is in constant mode");
    frame.setShowingTitle(true);
    subManager.setFrame(frame);

    // Add the sub manager to the top one:
    manager.addObject(subManager, false);

    // Create a sub grapher:
    IlvGrapher subGrapher = new  IlvGrapher() ;

    obj = new IlvRectangle(new IlvRect(10,30,50,50), false, true);
    subGrapher.addObject(obj, false);
    obj = new IlvRectangle(new IlvRect(100,120,50,50), false, true);
    subGrapher.addObject(obj, false);

    // Define a default frame:
    frame = new IlvDefaultManagerFrame();
    frame.setBackground(Color.pink);
    frame.setForeground(Color.BLUE);
    frame.setRightMargin(40);
    frame.setTitle("This manager is in default mode");
    frame.setShowingTitle(true);
    subGrapher.setFrame(frame);

    // Add the sub grapher to the top manager:
    manager.addObject(subGrapher, false);
    
    // Create a sub manager.
    subManager = new  IlvManager();

    obj = new IlvRectangle(new IlvRect(210,200,50,50), false, true);
    subManager.addObject(obj, false);
    obj = new IlvRectangle(new IlvRect(300,270,50,50), false, true);
    subManager.addObject(obj, false);

    // Use a constant mode frame:
    frame = new IlvConstantModeManagerFrame();
    frame.setBackground(Color.CYAN);
    frame.setForeground(Color.ORANGE);
    frame.setRightMargin(60);
    frame.setTitle("This manager is in constant mode. It has a long title and might be multiple lines!");
    frame.setShowingTitle(true);
    int justification = IlvDefaultManagerFrame.CENTER|IlvDefaultManagerFrame.BOTTOM|IlvDefaultManagerFrame.WRAPPED;
    frame.setTitleJustification(justification);
    subManager.setFrame(frame);

    // Add the sub manager to the top one:
    manager.addObject(subManager, false);

    obj = new IlvRectangle(new IlvRect(10,200,50,50), false, true);
    manager.addObject(obj, false);

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

    // Install View Interactor
    IlvSelectInteractor inter = new IlvSelectInteractor();
    inter.setOpaqueMove(true);
    inter.setOpaqueResize(true);
    mgrview.setInteractor(inter);

    // Install Zoom Accelerators
    manager.addAccelerator(new IlvIdentityAccelerator(KeyEvent.KEY_PRESSED, KeyEvent.VK_I, KeyEvent.CTRL_MASK));
    manager.addAccelerator(new IlvZoomOutAccelerator(KeyEvent.KEY_PRESSED, KeyEvent.VK_U, KeyEvent.CTRL_MASK));
    manager.addAccelerator(new IlvZoomInAccelerator(KeyEvent.KEY_PRESSED, KeyEvent.VK_Z, KeyEvent.CTRL_MASK));

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

  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 SubGraphs();
          frame.setTitle("JViews sub manager and sub grapher sample");
          frame.setSize(450, 450);
          frame.setVisible(true);
          frame.addWindowListener(new ExitOnWindowClosing());
        }
      });
  }

  static class ExitOnWindowClosing extends WindowAdapter
  {
    public ExitOnWindowClosing()
    {}

    Override
    public void windowClosing(WindowEvent e) {
      System.exit(0);
    }
  }
}