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

import ilog.views.*;
import ilog.views.animation.*;
import ilog.views.swing.*;
import ilog.views.graphlayout.*;
import ilog.views.graphlayout.random.*;
import ilog.views.graphlayout.tree.*;
import ilog.views.util.*;
import ilog.views.util.swing.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * The main class MultiThreadedAnimationApplet.
 * This shows how to use the animation framework in a multithreaded version.
 * The applet 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 applet.
 */
public class MultiThreadedAnimationApplet 
  extends JApplet
{
  {
    // 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);
  }

  /** 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 layoutChooser = new JComboBox();

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

  /** Whether the demo was started as an applet or as a standalone app */
  boolean isApplet = true;

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

    super.init();

    // 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 chooser = new JComboBox();
    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() {
      public void itemStateChanged(ItemEvent event) {
        if (event.getStateChange() == event.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() {
      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() {
      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() {
      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 {
      if (isApplet)
        grapher.read(new URL(getDocumentBase(), "data/Sample.ivl"));
      else
        grapher.read("data" + System.getProperty("file.separator") + "Sample.ivl");
      grapher.reDraw();
    } catch (Exception e) {
      //e.printStackTrace();
      showMessage(e.getMessage());
    }

    busy = false;
  }

  /**
   * Called when this applet is being reclaimed in order to destroy
   * any resources that it has allocated.
   */
  public void destroy()
  {
    super.destroy();

    // This method is intended to workaround memory management issues
    // in the Sun JRE. Please refer to the method documentation for more
    // details and a description of the known issues.
    IlvSwingUtil.cleanupApplet();
  }

  /**
   * Allows you to run the demo as a standalone application.
   */
  public static void main(String[] arg)
  {
    MultiThreadedAnimationApplet applet = new MultiThreadedAnimationApplet();
    applet.isApplet = false;
    applet.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(applet);
    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 applet
   * 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.0f);
      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
{
  MultiThreadedAnimationApplet applet;
  IlvGraphLayout layout;
  IlvGrapher grapher;

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

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

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

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

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

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

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

      // fit all in view
      applet.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
      applet.mgranim.animate();

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

      // enable the GUI
      applet.layoutChooser.setEnabled(true);
      applet.animationRate.setEnabled(true);
      applet.accelerationRate.setEnabled(true);
      applet.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 MultiThreadedAnimationApplet a = applet;
    final String message = msg;
    Runnable doShowMessage = new Runnable() {
      public void run() { a.showMessage(message); }
    };
    SwingUtilities.invokeLater(doShowMessage);
  }
}