/*
 * 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.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.net.URL;

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

import gui.GradientPanel;
import gui.GradientPanel2;
import gui.LayoutShelf;
import gui.StackLayout;
import ilog.views.util.IlvProductUtil;



/**
 * This is the main class of the demo. It creates the GUI and loads all needed images and data (graph and control panels).
 *
 */
public class GraphLayout extends JRootPane {

  {
    // 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);
  }

  private static final int WIDTH = 1000;
  private static final int HEIGHT = 800;

  // The frame of the application
  private static JFrame frame;

  Insets noinsets = new Insets(0, 0, 0, 0);

  private JPanel view = null;

  private URL getResource(String path) {
    return getClass().getResource(path);
  }

  public void init(Container contentPane) throws Exception {

    // creates a diagrammer to load a graph+tree layout, and crates the control panel and its
    // associated controller
    GraphLayoutDiagrammer treeDiagrammer =
        new GraphLayoutDiagrammer(getResource("/displays/tree.idpr"));
    GraphLayoutControlPanel treeDiagrammerController = new GraphLayoutControlPanel(
        getResource("/displays/tree.idbd"), treeDiagrammer, new TreeController());

    // creates a diagrammer to load a graph+radialtree layout, and crates the control panel and its
    // associated controller
    GraphLayoutDiagrammer radialtreeDiagrammer =
        new GraphLayoutDiagrammer(getResource("/displays/radialtree.idpr"));
    GraphLayoutControlPanel radialtreeDiagrammerController = new GraphLayoutControlPanel(
        getResource("/displays/radialtree.idbd"), radialtreeDiagrammer, new TreeController());

    // creates a diagrammer to load a graph+tree tipover layout, and crates the control panel and
    // its associated controller
    GraphLayoutDiagrammer treetipoverDiagrammer =
        new GraphLayoutDiagrammer(getResource("/displays/treetipover.idpr"));
    GraphLayoutControlPanel treetipoveDiagrammerController = new GraphLayoutControlPanel(
        getResource("/displays/treetipover.idbd"), treetipoverDiagrammer, new TreeController());

    // creates a diagrammer to load a graph+grid layout, and crates the control panel and its
    // associated controller
    GraphLayoutDiagrammer gridDiagrammer =
        new GraphLayoutDiagrammer(getResource("/displays/grid.idpr"));
    GraphLayoutControlPanel gridDiagrammerController = new GraphLayoutControlPanel(
        getResource("/displays/grid.idbd"), gridDiagrammer, new GridController());

    // creates a diagrammer to load a graph+link layout, and crates the control panel and its
    // associated controller
    GraphLayoutDiagrammer linklayoutDiagrammer =
        new GraphLayoutDiagrammer(getResource("/displays/linklayout.idpr"));
    GraphLayoutControlPanel linklayoutDiagrammerController = new GraphLayoutControlPanel(
        getResource("/displays/linklayout.idbd"), linklayoutDiagrammer, new LinkLayoutController());

    // creates a diagrammer to load a graph+link layout, and crates the control panel and its
    // associated controller
    GraphLayoutDiagrammer nestedlayoutDiagrammer =
        new GraphLayoutDiagrammer(getResource("/displays/nestedlayout.idpr"));
    GraphLayoutControlPanel nestedlayoutDiagrammerController =
        new GraphLayoutControlPanel(getResource("/displays/nestedlayout.idbd"),
            nestedlayoutDiagrammer, new NestedLayoutController());

    // creates a diagrammer to load a graph+hierarchical layout, and creates
    // the control panel and its associated controller
    GraphLayoutDiagrammer hierarchicalLayoutDiagrammer =
        new GraphLayoutDiagrammer(getResource("/displays/hierarchical2.idpr"));
    GraphLayoutControlPanel hierarchicalLayoutDiagrammerController =
        new GraphLayoutControlPanel(getResource("/displays/hierarchical2.idbd"),
            hierarchicalLayoutDiagrammer, new HierarchicalController());


    // data structure to represent layout on the "shelf" and containing the diagrammer and control
    // panel to display
    // when a layout is choosen.
    final Object[][] data = {{"/images/ilog.gif", " ", null},
        {"/images/tree1.gif", "Tree", treeDiagrammer, treeDiagrammerController},
        {"/images/tree2.gif", "Radial Tree", radialtreeDiagrammer, radialtreeDiagrammerController},
        {"/images/tree3.gif", "Tree Tip-over", treetipoverDiagrammer,
            treetipoveDiagrammerController},
        {"/images/grid1.gif", "Grid", gridDiagrammer, gridDiagrammerController},
        {"/images/linklayout.gif", "Link", linklayoutDiagrammer, linklayoutDiagrammerController},
        {"/images/nested1.gif", "Nested", nestedlayoutDiagrammer, nestedlayoutDiagrammerController},
        {"/images/hierarchical2.gif", "Hierarchical", hierarchicalLayoutDiagrammer,
            hierarchicalLayoutDiagrammerController}};


    LayoutShelf shelf = new LayoutShelf(data) {

      // do the job when a new layout is choosen.
      Override
      public void avatarChanged(int index) {
        if (view == null) {
          return;
        }

        view.removeAll();
        if (index > 0 && (data[index][2] instanceof GraphLayoutDiagrammer)) {
          Object[] d = data[7];
          // displays the graph
          GraphLayoutDiagrammer diagrammer = (GraphLayoutDiagrammer) data[index][2];
          if (diagrammer != null) {
            view.add(diagrammer, BorderLayout.CENTER);
            diagrammer.fitToContents();
          }

          // displays the control panel
          GraphLayoutControlPanel controller = (GraphLayoutControlPanel) data[index][3];
          if (controller != null) {
            view.add(controller, BorderLayout.WEST);
          }
        }

        view.updateUI();
      }
    };

    JPanel mainMenu = new JPanel();
    mainMenu.setLayout(new StackLayout());
    mainMenu.add(new GradientPanel(), StackLayout.BOTTOM);
    mainMenu.add(shelf, StackLayout.TOP);

    view = new GradientPanel2(new BorderLayout());

    contentPane.setLayout(new GridBagLayout());
    contentPane.setBackground(Color.black);
    contentPane.add(mainMenu, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.CENTER,
        GridBagConstraints.HORIZONTAL, noinsets, 0, 0));
    contentPane.add(view, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER,
        GridBagConstraints.BOTH, noinsets, 0, 0));

  }

  public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
      Override
      public void run() {
        // Create an instance of the application, and show it.
        try {
          frame = new JFrame("Graph Layout");
          GraphLayout app = new GraphLayout();
          app.init(frame.getContentPane());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLocation(50, 50);
          frame.pack();
          frame.setSize(WIDTH, HEIGHT);
          frame.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace(System.err);
        }
      }
    });

  }


}