/*
 * 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 JViews Graphic Framework
// the Java AWT package
import java.awt.Color;

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

import ilog.views.IlvGrapher;
import ilog.views.IlvLinkImage;
import ilog.views.IlvManagerView;
import ilog.views.IlvPoint;
import ilog.views.IlvRect;
import ilog.views.graphic.IlvReliefRectangle;
import ilog.views.graphic.IlvZoomableLabel;
// the JViews Label Layout Framework
import ilog.views.graphlayout.labellayout.IlvLabelLayoutReport;
// the JViews Annealing Label Layout
import ilog.views.graphlayout.labellayout.annealing.IlvAnnealingLabelLayout;
import ilog.views.graphlayout.labellayout.annealing.IlvAnnealingPolylineLabelDescriptor;
import ilog.views.util.IlvProductUtil;

/**
 * A very simple example for the use of a Label Layout Algorithm.
 */
public class Sample2 {
  public static final 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 JViews Diagrammer Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(IlvProductUtil.JViews_Diagrammer_Deployment);

    // Create the manager instance (subclass of IlvManager). Since we want
    // to place link labels, we use a grapher in this example.
    IlvGrapher grapher = new IlvGrapher();

    // Create the manager view instance
    IlvManagerView view = new IlvManagerView(grapher);
    view.setBackground(Color.white);

    // A Swing Frame to display
    JFrame frame = new JFrame("Label Layout Sample");
    // EXIT_ON_CLOSE is not defined in jdk1.2, hence this way:
    frame.setDefaultCloseOperation(3 /* EXIT_ON_CLOSE, >= jdk1.2.2 */);

    // Put the manager view inside an Swing Frame and show it
    frame.getContentPane().add(view);
    frame.setSize(400, 400);
    frame.setVisible(true);

    // Fill the grapher with nodes and links
    IlvReliefRectangle node1 = new IlvReliefRectangle(new IlvRect(0, 0, 50, 50));
    IlvReliefRectangle node2 = new IlvReliefRectangle(new IlvRect(200, 0, 50, 50));
    IlvReliefRectangle node3 = new IlvReliefRectangle(new IlvRect(0, 200, 50, 50));
    IlvReliefRectangle node4 = new IlvReliefRectangle(new IlvRect(200, 200, 50, 50));
    grapher.addNode(node1, false);
    grapher.addNode(node2, false);
    grapher.addNode(node3, false);
    grapher.addNode(node4, false);

    // set some nice colors
    setNodeColors(node1, node2, node3, node4);

    IlvLinkImage link1 = new IlvLinkImage(node1, node2, true);
    IlvLinkImage link2 = new IlvLinkImage(node1, node3, true);
    IlvLinkImage link3 = new IlvLinkImage(node2, node4, true);
    IlvLinkImage link4 = new IlvLinkImage(node4, node3, true);
    IlvLinkImage link5 = new IlvLinkImage(node1, node4, true);

    grapher.addLink(link1, false);
    grapher.addLink(link2, false);
    grapher.addLink(link3, false);
    grapher.addLink(link4, false);
    grapher.addLink(link5, false);

    // set some nice colors
    setLinkColors(link1, link2, link3, link4, link5);

    // Add labels. Labels are neither "nodes" nor "links", hence add them as
    // objects. Since we perform layout later, the initial position doesn't
    // play a role.

    IlvPoint p = new IlvPoint(0, 0);
    IlvZoomableLabel label1 = new IlvZoomableLabel(p, "Label1");
    IlvZoomableLabel label2 = new IlvZoomableLabel(p, "Label2");
    IlvZoomableLabel label3 = new IlvZoomableLabel(p, "Label3");
    IlvZoomableLabel label4 = new IlvZoomableLabel(p, "Label4");
    IlvZoomableLabel label5 = new IlvZoomableLabel(p, "Start Label");
    IlvZoomableLabel label6 = new IlvZoomableLabel(p, "End Label");

    grapher.addObject(label1, false);
    grapher.addObject(label2, false);
    grapher.addObject(label3, false);
    grapher.addObject(label4, false);
    grapher.addObject(label5, false);
    grapher.addObject(label6, false);

    // Declare a handle for the layout instance
    IlvAnnealingLabelLayout layout = new IlvAnnealingLabelLayout();

    // Declare a handle for the layout report
    IlvLabelLayoutReport layoutReport = null;

    // Attach the manager to the layout instance
    layout.attach(grapher);

    // For each label, set a label descriptor that specifies: label1 belongs
    // to link1, label2 belongs to link2, and so on. link5 has 2 labels.

    layout.setLabelDescriptor(label1,
        new IlvAnnealingPolylineLabelDescriptor(label1, link1, IlvAnnealingPolylineLabelDescriptor.CENTER));
    layout.setLabelDescriptor(label2,
        new IlvAnnealingPolylineLabelDescriptor(label2, link2, IlvAnnealingPolylineLabelDescriptor.CENTER));
    layout.setLabelDescriptor(label3,
        new IlvAnnealingPolylineLabelDescriptor(label3, link3, IlvAnnealingPolylineLabelDescriptor.CENTER));
    layout.setLabelDescriptor(label4,
        new IlvAnnealingPolylineLabelDescriptor(label4, link4, IlvAnnealingPolylineLabelDescriptor.CENTER));
    layout.setLabelDescriptor(label5,
        new IlvAnnealingPolylineLabelDescriptor(label5, link5, IlvAnnealingPolylineLabelDescriptor.START));
    layout.setLabelDescriptor(label6,
        new IlvAnnealingPolylineLabelDescriptor(label6, link5, IlvAnnealingPolylineLabelDescriptor.END));

    // Modify the layout parameters, if needed
    layout.setLabelOffset(10);
    layout.setObstacleOffset(5);

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

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

    // Fit the graph in the window
    view.fitTransformerToContent();
    // Redraw the grapher
    grapher.reDraw();

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

  /**
   * Auxiliary to set nice node colors and parameters.
   */
  private static void setNodeColors(IlvReliefRectangle n1, IlvReliefRectangle n2, IlvReliefRectangle n3,
      IlvReliefRectangle n4) {
    n1.setBackground(Color.cyan);
    n2.setBackground(Color.cyan);
    n3.setBackground(Color.cyan);
    n4.setBackground(Color.cyan);
    n1.setThickness(8);
    n2.setThickness(8);
    n3.setThickness(8);
    n4.setThickness(8);
  }

  /**
   * Auxiliary to set nice link colors and parameters.
   */
  private static void setLinkColors(IlvLinkImage l1, IlvLinkImage l2, IlvLinkImage l3, IlvLinkImage l4,
      IlvLinkImage l5) {
    l1.setForeground(Color.red);
    l2.setForeground(Color.red);
    l3.setForeground(Color.red);
    l4.setForeground(Color.red);
    l5.setForeground(Color.red);
    l1.setLineWidth(4);
    l2.setLineWidth(4);
    l3.setLineWidth(4);
    l4.setLineWidth(4);
    l5.setLineWidth(4);
  }
}