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

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.URL;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import ilog.views.IlvDirection;
import ilog.views.IlvGrapher;
import ilog.views.IlvHandlesSelection;
import ilog.views.IlvManagerView;
import ilog.views.IlvTransformer;
import ilog.views.animation.IlvManagerAnimator;
import ilog.views.graphlayout.IlvGraphLayout;
import ilog.views.graphlayout.IlvGraphLayoutException;
import ilog.views.graphlayout.IlvGraphLayoutReport;
import ilog.views.graphlayout.IlvGrapherAdapter;
import ilog.views.graphlayout.random.IlvRandomLayout;
import ilog.views.graphlayout.tree.IlvTreeLayout;
import ilog.views.swing.IlvJScrollManagerView;
import ilog.views.util.IlvProductUtil;
import ilog.views.util.swing.IlvSwingUtil;

/**
 * The main class MultiThreadedAnimationApp. This shows how to use the
 * animation framework in a multithreaded version. The app allows you to
 * select various layouts. The position changes between the layouts are animated
 * smoothly. The animation rate (how many animation steps are drawn to show the
 * movement) and the animation delay (the interval between animation steps) can
 * be entered to control the behavior of the app.
 */
public class MultiThreadedAnimationApp extends JRootPane {
  {
    // 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);
  }

  /** The grapher */
  IlvGrapher grapher = new IlvGrapher();

  /** The view of the grapher */
  IlvManagerView mgrview = new IlvManagerView(grapher, null);

  /** The animator */
  IlvManagerAnimator mgranim = new IlvManagerAnimator(grapher);

  /** An instance of the Tree Layout algorithm */
  IlvTreeLayout treeLayout = new IlvTreeLayout();

  /** An instance of the Random Layout algorithm */
  IlvRandomLayout randomLayout = new IlvRandomLayout();

  /** A text field to display messages */
  JTextField msgLine = new JTextField();

  /** Text fields for the animation parameters */
  JTextField animationRate = new JTextField("" + 40 + "  ", 4);
  JTextField accelerationRate = new JTextField("" + 20 + "  ", 4);
  JTextField animationDelay = new JTextField("" + 20 + "  ", 4);

  /** The chooser for the layout style */
  JComboBox<String> layoutChooser = new JComboBox<String>();

  /** Whether the app is busy during animation */
  boolean busy = true;

  /**
   * Initializes the app.
   */
  public void init() {
    showMessage("animation sample initialization ...");

    // Set the default values of the animator
    mgranim.setAnimationRate(40);
    mgranim.setAnimationAccelerationRate(20);

    // attach the grapher to the layout instances
    IlvGrapherAdapter adapter = new IlvGrapherAdapter(grapher);
    treeLayout.attach(adapter);
    randomLayout.attach(adapter);

    // use manager coordinates
    treeLayout.setCoordinatesMode(IlvGraphLayout.MANAGER_COORDINATES);
    randomLayout.setCoordinatesMode(IlvGraphLayout.MANAGER_COORDINATES);

    // create the scroll manager view
    IlvJScrollManagerView scrollManView = new IlvJScrollManagerView(mgrview);

    // Some settings on the manager view and on the scroll manager view.
    // Keep antialiasing off, because if it's on, drawing is slower.
    mgrview.setAntialiasing(false);
    mgrview.setKeepingAspectRatio(true);
    mgrview.setBackground(Color.white);
    mgrview.setForeground(SystemColor.windowText);
    Color xc = SystemColor.windowText;
    xc = new Color(255 - xc.getRed(), 255 - xc.getGreen(), 255 - xc.getBlue());
    mgrview.setDefaultXORColor(xc);
    mgrview.setDefaultGhostColor(SystemColor.windowText);

    // Settings parameters for selection handles
    IlvHandlesSelection.defaultHandleColor = Color.black;
    IlvHandlesSelection.defaultHandleBackgroundColor = Color.white;
    IlvHandlesSelection.defaultHandleShape = IlvHandlesSelection.SQUARE_SHAPE;

    // set the layout manager
    getContentPane().setLayout(new BorderLayout(0, 0));

    // a panel at the top
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT));

    // layout style selection
    JComboBox<String> chooser = new JComboBox<String>();
    panel.add(new JLabel("Select layout:"));
    panel.add(chooser);
    chooser.addItem("random");
    chooser.addItem("tree");
    chooser.addItem("org chart");
    chooser.addItem("tip over");
    chooser.addItem("radial");
    chooser.setSelectedIndex(0);
    chooser.setToolTipText("Select a style and start layout");
    chooser.addItemListener(new ItemListener() {
      Override
      public void itemStateChanged(ItemEvent event) {
        if (event.getStateChange() == ItemEvent.DESELECTED)
          return;
        if (busy) {
          showMessage("Input ignored ... animation running ...");
          return;
        }
        transferAnimationParameter();
        performLayout((String) event.getItem());
      }
    });

    panel.add(new JLabel("  "));
    panel.add(new JLabel("Animation rate:"));
    panel.add(animationRate);
    panel.add(new JLabel("  "));
    panel.add(new JLabel("Acceleration rate:"));
    panel.add(accelerationRate);
    panel.add(new JLabel("  "));
    panel.add(new JLabel("Animation delay:"));
    panel.add(animationDelay);

    animationRate.setToolTipText("Set the animation frame rate");
    animationRate.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        // transfer parameters from the text fields to the manager animator
        transferAnimationParameter();
      }
    });

    accelerationRate.setToolTipText("Set the animation acceleration rate");
    accelerationRate.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        // transfer parameters from the text fields to the manager animator
        transferAnimationParameter();
      }
    });

    animationDelay.setToolTipText("Set the delay between animation frames");
    animationDelay.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        // transfer parameters from the text fields to the manager animator
        transferAnimationParameter();
      }
    });

    // the message line at the bottom of the frame
    msgLine.setEditable(false);

    // fit all together
    getContentPane().add("Center", scrollManView);
    getContentPane().add("North", panel);
    getContentPane().add("South", msgLine);

    // load a sample grapher
    try {
      grapher.read("data" + System.getProperty("file.separator") + "Sample.ivl");
      grapher.reDraw();
    } catch (Exception e) {
      // e.printStackTrace();
      showMessage(e.getMessage());
    }

    busy = false;
  }

  /**
   * Allows you to run the demo as a standalone application.
   */
  public static void main(String[] arg) {
    MultiThreadedAnimationApp app = new MultiThreadedAnimationApp();
    app.init();

    JFrame frame = new JFrame("Animated Layout Demo (Multi Threaded)");
    // EXIT_ON_CLOSE is not defined in jdk1.2, hence this way:
    frame.setDefaultCloseOperation(3 /* EXIT_ON_CLOSE, >= jdk1.2.2 */);
    frame.setSize(750, 600);
    frame.getContentPane().add(app);
    frame.setVisible(true);
  }

  /**
   * Displays a message.
   */
  void showMessage(String message) {
    // the message is displayed in the message line
    msgLine.setText(message);
    msgLine.paintImmediately(0, 0, msgLine.getWidth(), msgLine.getHeight());
  }

  /**
   * Transfers the animation parameters from the text fields of the app to
   * the manager animator.
   */
  void transferAnimationParameter() {
    int value;
    try {
      // validate the animation rate
      value = Integer.valueOf(animationRate.getText().trim()).intValue();
      if (value < 1)
        value = 1;
      animationRate.setText("" + value);
      mgranim.setAnimationRate(value);
      // validate the animation acceleration rate
      value = Integer.valueOf(accelerationRate.getText().trim()).intValue();
      if (value < 0)
        value = 0;
      if (value > mgranim.getAnimationRate() / 2)
        value = mgranim.getAnimationRate() / 2;
      accelerationRate.setText("" + value);
      mgranim.setAnimationAccelerationRate(value);
      // validate the animation delay
      value = Integer.valueOf(animationDelay.getText().trim()).intValue();
      if (value < 10)
        value = 10;
      animationDelay.setText("" + value);
      mgranim.setAnimationDelay(value);
    } catch (Exception ex) {
      showMessage("Illegal animation rate or delay: " + ex.getMessage());
    }
  }

  /**
   * Prepares and performs the graph layout. The input string indicates what
   * kind of layout should be performed.
   */
  void performLayout(String layoutType) {
    if (layoutType.equals("random")) {
      layout(randomLayout, grapher);
    } else if (layoutType.equals("tree")) {
      treeLayout.setLayoutMode(IlvTreeLayout.FREE);
      treeLayout.setGlobalAlignment(IlvTreeLayout.CENTER);
      treeLayout.setFlowDirection(IlvDirection.Bottom);
      treeLayout.setGlobalLinkStyle(IlvTreeLayout.STRAIGHT_LINE_STYLE);
      layout(treeLayout, grapher);
    } else if (layoutType.equals("org chart")) {
      treeLayout.setLayoutMode(IlvTreeLayout.FREE);
      treeLayout.setGlobalAlignment(IlvTreeLayout.CENTER);
      treeLayout.setFlowDirection(IlvDirection.Right);
      treeLayout.setGlobalLinkStyle(IlvTreeLayout.ORTHOGONAL_STYLE);
      layout(treeLayout, grapher);
    } else if (layoutType.equals("tip over")) {
      treeLayout.setLayoutMode(IlvTreeLayout.FREE);
      treeLayout.setGlobalAlignment(IlvTreeLayout.TIP_OVER);
      treeLayout.setFlowDirection(IlvDirection.Bottom);
      treeLayout.setGlobalLinkStyle(IlvTreeLayout.ORTHOGONAL_STYLE);
      layout(treeLayout, grapher);
    } else if (layoutType.equals("radial")) {
      treeLayout.setLayoutMode(IlvTreeLayout.RADIAL);
      treeLayout.setGlobalAlignment(IlvTreeLayout.CENTER);
      treeLayout.setFlowDirection(IlvDirection.Bottom);
      treeLayout.setGlobalLinkStyle(IlvTreeLayout.STRAIGHT_LINE_STYLE);
      treeLayout.setAspectRatio(1.0);
      layout(treeLayout, grapher);
    }
  }

  /**
   * Start layout.
   */
  synchronized void layout(IlvGraphLayout layout, IlvGrapher grapher) {
    // this starts layout and animation in a separate thread
    mgranim.start(new GraphLayoutPerformer(this, layout, grapher));
  }
}

// ----------------------------------------------------------------------------
// The GraphLayoutPerformer, a Runnable that performs the layout.

class GraphLayoutPerformer implements Runnable {
  MultiThreadedAnimationApp app;
  IlvGraphLayout layout;
  IlvGrapher grapher;

  /**
   * Create a new layout performer.
   */
  GraphLayoutPerformer(MultiThreadedAnimationApp app, IlvGraphLayout layout, IlvGrapher grapher) {
    this.app = app;
    this.layout = layout;
    this.grapher = grapher;
  }

  /**
   * Perform a layout and the animation.
   */
  Override
  public void run() {
    // disable the GUI
    app.layoutChooser.setEnabled(false);
    app.animationRate.setEnabled(false);
    app.accelerationRate.setEnabled(false);
    app.animationDelay.setEnabled(false);

    // and mark the app as busy to prevent further input
    app.busy = true;

    // record the start state for the animation
    app.mgranim.recordState();

    // show a start message
    showMessage("layout started ... wait ...");

    try {
      // to avoid influence by the transformer, set transformer to null before
      // layout
      app.mgrview.setTransformer(new IlvTransformer());

      // perform the layout and get the layout report
      IlvGraphLayoutReport layoutReport = layout.performLayout(false, false);

      // fit all in view
      app.mgrview.fitTransformerToContent();

      // print the code from the layout report
      showMessage(layoutReport.codeToString(layoutReport.getCode()) + " ... animation started ...");

      // animate the transition from the recorded state to the current state
      app.mgranim.animate();

      // final message
      showMessage("animation done");
    } catch (IlvGraphLayoutException e) {
      // e.printStackTrace();
      showMessage(e.getMessage());
    } finally {
      // mark the app as not busy anymore
      app.busy = false;

      // enable the GUI
      app.layoutChooser.setEnabled(true);
      app.animationRate.setEnabled(true);
      app.accelerationRate.setEnabled(true);
      app.animationDelay.setEnabled(true);
    }
  }

  /**
   * Shows a message.
   */
  void showMessage(String msg) {
    // Showing the message must be done on the AWT event thread which handles
    // the GUI, but the GraphLayoutPerformer thread is not the AWT event thread.
    // Hence, go over invokeLater.
    final MultiThreadedAnimationApp a = app;
    final String message = msg;
    Runnable doShowMessage = new Runnable() {
      Override
      public void run() {
        a.showMessage(message);
      }
    };
    SwingUtilities.invokeLater(doShowMessage);
  }
}