/*
 * 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;
import java.awt.SystemColor;
// The Java Net package
import java.net.URL;

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

import ilog.views.IlvDirection;
// the Perforce JViews Diagrammer Framework
import ilog.views.diagrammer.IlvDiagrammer;
// 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 a diagram component. It shows how to bypass the
 * style sheets when changing layout parameters: It calls the layout parameter
 * API on the graph layout instance directly. Layout parameter modifications on
 * the graph layout instance are automatically undone when the style sheet
 * changes or is reloaded, therefore this mechanism is not well suited for
 * permanent layout parameter settings.
 */
public class Sample3 {
  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() {
        // Create the diagram component
        IlvDiagrammer diagrammer = new IlvDiagrammer();

        // Change diagrammer parameters.
        diagrammer.setSelectMode(false);
        diagrammer.setScrollable(false);
        diagrammer.setEditingAllowed(true);
        diagrammer.getView().setBackground(Color.white);
        diagrammer.getView().setForeground(SystemColor.windowText);

        // The name of the XML file containing the model data
        String xmlFileName = "data/Sample.xml";

        // The name of the CSS file containing the main style sheet
        String cssFileName = "data/Sample.css";

        // Load the style sheet
        try {
          diagrammer.setStyleSheet(new URL("file:" + cssFileName));
        } catch (Exception e) {
          System.out.println("could not read " + cssFileName);
          return;
        }

        // Load the sample data file
        try {
          diagrammer.setDataFile(new URL("file:" + xmlFileName));
        } catch (Exception e) {
          System.out.println("could not read " + xmlFileName);
          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(diagrammer);

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

        // The style sheet specifies Tree layout, therefore the current layout
        // instance has type IlvTreeLayout
        IlvTreeLayout layout = (IlvTreeLayout) diagrammer.getEngine().getNodeLayoutRenderer().getGraphLayout();

        // Change some layout parameters on the layout instance directly.
        layout.setFlowDirection(IlvDirection.Bottom);
        layout.setGlobalLinkStyle(IlvTreeLayout.ORTHOGONAL_STYLE);

        // Changing the layout parameters in this way does not perform a layout
        // automatically. Therefore layout is called explicitly.
        // Luckily we are already in the swing thread, because it needs to be
        // done inside SwingUtilities.invokeLater.
        if (diagrammer.isNodeLayoutAvailable()) {
          // Perform the layout of all nodes
          diagrammer.layoutAllNodes();
          // Fit the view to show the entire graph
          diagrammer.fitToContents();
        }
      }
    });
  }
}