/*
 * 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.
 */

// the Perforce JViews Graphic Framework
// the Java AWT package
import java.awt.Color;

// the Java Swing package
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import ilog.views.IlvDirection;
import ilog.views.IlvGrapher;
import ilog.views.IlvManagerView;
// the Perforce JViews Graph Layout Framework
import ilog.views.graphlayout.IlvGraphLayoutException;
import ilog.views.graphlayout.IlvGraphLayoutReport;
// the Perforce JViews Tree Layout
import ilog.views.graphlayout.tree.IlvTreeLayout;
import ilog.views.util.IlvProductUtil;

/**
 * A very simple example for the use of a Layout Algorithm. It shows how to use
 * graph layout directly with the graphics framework. It illustrates the usage
 * of graph layout outside the diagram component. It shows how to apply a Tree
 * Layout directly to a grapher and how to set layout parameters by code.
 */
public class Sample4 {
  public static void main(String[] arg) {
    // 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);

    SwingUtilities.invokeLater(new Runnable() {
      Override
      public void run() {
        // Declare a handle for the layout instance
        IlvTreeLayout layout = new IlvTreeLayout();

        // Create the grapher instance
        IlvGrapher grapher = new IlvGrapher();

        // Create the manager view instance
        IlvManagerView view = new IlvManagerView(grapher);

        // Change view parameters.
        view.setBackground(Color.white);
        view.setKeepingAspectRatio(true);

        // The name of the IVL file containing the graph data
        String fileName = "data/Sample.ivl";

        // Fill the grapher with nodes and links from a JViews IVL file.
        // Alternatively, the nodes and links could be created programmatically.
        try {
          grapher.read(fileName);
        } catch (Exception e) {
          System.out.println("could not read " + fileName);
          return;
        }

        // A Swing Frame to display
        JFrame frame = new JFrame("Layout Sample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Put the manager view inside the Swing Frame and show it.
        frame.getContentPane().add(view);

        frame.setSize(600, 600);
        frame.setVisible(true);

        // Perform the layout. Should always be done in the AWT thread
        performLayout(grapher, view, layout);
      }
    });

  }

  /**
   * Perform the layout.
   */
  private static void performLayout(IlvGrapher grapher, IlvManagerView view, IlvTreeLayout layout) {
    // Attach the grapher to the layout instance
    layout.attach(grapher);

    // Change some layout parameters
    layout.setFlowDirection(IlvDirection.Bottom);
    layout.setGlobalLinkStyle(IlvTreeLayout.ORTHOGONAL_STYLE);

    try {
      // Perform the layout and get the layout report
      IlvGraphLayoutReport layoutReport = layout.performLayout();

      int code = layoutReport.getCode();

      // Print information from the layout report (optional)
      System.out.println("Done in " + layoutReport.getLayoutTime() + " millisec., return code = " + code + " ("
          + layoutReport.codeToString(code) + ")");
    }

    // Catch the exceptions
    catch (IlvGraphLayoutException e) {
      System.out.println(e.getMessage());
    }

    finally {
      // Fit the view to show the entire graph
      view.fitTransformerToContent();
      // Redraw the grapher
      grapher.reDraw();
    }

    // Detach the grapher from the layout instance
    layout.detach();

  }
}