/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © 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 Rogue Wave JViews Diagrammer Framework
import ilog.views.diagrammer.*;

import ilog.views.util.IlvProductUtil;

// the Java AWT package
import java.awt.*;

// The Java Net package
import java.net.*;

// the Java Swing package
import javax.swing.*;
 
/**
 * A very simple example for the use of a Layout Algorithm.
 * It shows how to use graph layout directly with a diagram component.
 * It loads a CSS style sheet that contains all layout parameters.
 * The layout is fully specified in this style sheet.
 */
public class Sample1
{
  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 Rogue Wave JViews Diagrammer Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(
        IlvProductUtil.JViews_Diagrammer_Deployment);

    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        
        // Create the diagrammer component
        final 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 style sheet
        String cssFileName = "data/Sample.css";

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

        // Load the style sheet.
        // Since layout is fully specified in the style sheet, loading the style
        // sheet performs the layout. 
        try {
          diagrammer.setStyleSheet(new URL("file:" + cssFileName));
        } catch (Exception e) {
          System.out.println("could not read " + cssFileName);
          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
        frame.getContentPane().add(diagrammer);

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

        // Example of performing layout explicitly.
        // Not needed here, since layout was already done when loading the style
        // file, but just to show this piece of code:
        // The diagram component is a swing component, therefore any changes to
        // the diagram component  after it is visible must be done in the event thread.
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            performLayout(diagrammer);
          }
        });
        
      }
    });
  }

  /**
   * Example of performing node, label and link layout on a diagram component.
   */
  static void performLayout(IlvDiagrammer diagrammer)
  {
    // Layout is automatically applied whenever the style sheet changes.
    // If layout should be performed explicitly, this can be done by the
    // following sample code.
    if (diagrammer.isNodeLayoutAvailable() ||
        diagrammer.isLinkLayoutAvailable() ||
        diagrammer.isLabelLayoutAvailable()) {
      // If node layout is enabled, perform the layout of all nodes
      diagrammer.layoutAllNodes();
      // If link layout is enabled, perform the layout of links
      diagrammer.layoutLinks();
      // If label layout is enabled, perform the layout of labels
      diagrammer.layoutLabels();
      // Fit the view to show the entire graph
      diagrammer.fitToContents();
    }
  }
}