/*
 * 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 Diagrammer 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.diagrammer.IlvDiagrammer;
// the Perforce JViews SDM Utilities
import ilog.views.sdm.util.IlvSDMMutableStyleSheet;
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 modify layout
 * parameters in a way that is compatible with CSS style sheets: it uses the
 * mutable style sheet.
 */
public class Sample2 {
  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();

        // Create the mutable style sheet for temporary layout parameter
        // settings
        IlvSDMMutableStyleSheet styleSheet = new IlvSDMMutableStyleSheet(diagrammer.getEngine(), true, false);

        // Change diagram 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 main style sheet
        try {
          diagrammer.setStyleSheet(new URL("file:" + cssFileName));
        } catch (Exception e) {
          System.out.println("could not read " + cssFileName);
          return;
        }

        // Cascade the main style sheet with the mutable style sheet.
        // The mutable style sheet holds the temporary style changes that are
        // not statically stored in the main style sheet.
        try {
          diagrammer.getEngine().setStyleSheets(1, styleSheet.toString());
        } catch (Exception e) {
          System.out.println("could not load the style sheet");
        }

        // Load the sample data file
        try {
          diagrammer.setDataFile(new URL("file:" + xmlFileName));
        } catch (Exception e) {
          System.out.println("could not read " + xmlFileName);
          return;
        }

        // Change some layout parameters in the mutable style sheet
        styleSheet.setAdjusting(true);
        try {
          // enable graph layout
          styleSheet.setDeclaration("GraphLayout", "enabled", "true");
          // use Tree Layout
          styleSheet.setDeclaration("GraphLayout", "graphLayout", "Tree");
          // use flow direction towards top (just for demo purpose temporarily)
          styleSheet.setDeclaration("GraphLayout", "flowDirection", "Top");
          // use orthogonal link style
          styleSheet.setDeclaration("GraphLayout", "globalLinkStyle", "ORTHOGONAL_STYLE");
        } finally {
          // This completes the adjusting session: it validates the new
          // declarations and performs layout as neceesary
          styleSheet.setAdjusting(false);
        }

        // If layout parameters need to be changed later, this can easily be
        // achieved. Whenever the declaration of the style sheet changes
        // outside an adjustment session, or whenever an adjustment session
        // ends,
        // layout is automatically applied. For instance, the next statement,
        // calls layout again:

        styleSheet.setDeclaration("GraphLayout", "flowDirection", "Bottom");

        // 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);
        // just to see the entire graph at the end of this small sample
        diagrammer.fitToContents();
      }

    });
  }
}