/*
 * 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.IlvGrapher;
import ilog.views.IlvGraphic;
import ilog.views.IlvHandlesSelection;
import ilog.views.IlvManagerView;
import ilog.views.IlvTransformer;
import ilog.views.IlvPoint;

import ilog.views.interactor.IlvSelectInteractor;
import ilog.views.interactor.IlvZoomViewInteractor;

import ilog.views.event.ManagerSelectionListener;
import ilog.views.event.ManagerSelectionChangedEvent;

import ilog.views.swing.IlvJScrollManagerView;
import ilog.views.swing.IlvJManagerViewControlBar;

import ilog.views.graphlayout.IlvGraphLayout;
import ilog.views.graphlayout.IlvGraphLayoutReport;
import ilog.views.graphlayout.IlvGraphLayoutException;
import ilog.views.graphlayout.IlvGraphModel;
import ilog.views.graphlayout.IlvGrapherAdapter;
import ilog.views.graphlayout.GraphLayoutEvent;
import ilog.views.graphlayout.GraphLayoutEventListener;
import ilog.views.graphlayout.random.IlvRandomLayout;
import ilog.views.graphlayout.uniformlengthedges.IlvUniformLengthEdgesLayout;

import ilog.views.util.IlvProductUtil;
import ilog.views.util.IlvResourceUtil;
import ilog.views.util.IlvLocaleUtil;

import ilog.views.util.swing.IlvSwingUtil;
import ilog.views.util.swing.IlvJSpinnerDecimalNumberField;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;

import javax.swing.*;

/**
 * This is a very simple applet/application that uses the
 * <code>IlvGrapher</code> to perform a uniform length edges layout.
 * It shows how to use uniform length edges layout in applications that are
 * not based on CSS styling.
 */
public class UniformLengthEdgesLayoutApplet 
  extends JApplet
{
  static {
    // This applet is designed to run only with default resource bundle
    // and various selected other resource bundles.
    // Setting the available resource suffixes avoids that the applet
    // tries to load resource bundles for other locales over the net,
    // even if the current locale of the browser is different.
    if (IlvResourceUtil.isInApplet())
      IlvResourceUtil.setAvailableResourceSuffixes("", "_ja");

    // Test code whether the demo works in RTL locales
    // Locale newLocale = new Locale("he");
    // IlvSwingUtil.setDefaultLocale(newLocale);
  }

  {
    // 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);
  }

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

  /** The view of the grapher */
  IlvManagerView mgrview = new IlvManagerView(grapher, null);
  
  /** An instance of the Uniform Length Edges algorithm */
  IlvUniformLengthEdgesLayout layout = new IlvUniformLengthEdgesLayout();
  
  /** An instance of the Random Layout algorithm */
  IlvRandomLayout randomLayout = new IlvRandomLayout();
  
  /** A graph layout event listener */
  LayoutIterationListener layoutListener = new LayoutIterationListener();
  
  /** A text field to display messages */
  JTextField msgLine = new JTextField();

  /** A text field for the preferred length */
  IlvJSpinnerDecimalNumberField preferredLength;

  /** the allowed layout time */
  private final long defaultAllowedTime = layout.getAllowedTime();
  
  private JComboBox layoutModeChooser;

  /**
   * Initializes the applet/application.
   */
  public void init()
  {
    showMessage(getString("InitMessage"));
    
    JPanel panel;
    super.init();
    
    // attach the grapher to the layout instances
    IlvGrapherAdapter adapter = new IlvGrapherAdapter(grapher);
    layout.attach(adapter);
    randomLayout.attach(adapter);

    // Enable the optional features
    layout.setRespectNodeSizes(true);
    layout.setConnectLinksToNodeCenters(true);
    layout.setMultiLinkMode(IlvUniformLengthEdgesLayout.NARROW_STRAIGHT_LINE_BUNDLE);
    layout.setSelfLinkMode(IlvUniformLengthEdgesLayout.NARROW_CONNECTED_SQUARE);
    layout.setSelfLinkRelativeAttachPosition(new IlvPoint(0.5f, 0.5f));
    layout.setSelfLinkSpacing(-1);
    
    // selection listener to specify selected nodes as fixed
    grapher.addManagerSelectionListener(new MySelectionListener());

    // install the layout iteration listener on the layout instance
    layout.addGraphLayoutEventListener(layoutListener);

    // use manager coordinates
    layout.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
    mgrview.setAntialiasing(true);
    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);
    mgrview.setZoomFactorRange(0.02, 10.0);
    mgrview.setWheelZoomingEnabled(true);
    scrollManView.setWheelScrollingEnabled(true);

    // 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));

    // fit so far all together
    getContentPane().add("Center", scrollManView);
    getContentPane().add("North", panel = new JPanel());
    panel.setLayout(new FlowLayout(FlowLayout.LEADING));

    // create the standard control bar
    IlvJManagerViewControlBar controlBar = new IlvJManagerViewControlBar();
    controlBar.setView(mgrview);
    panel.add(controlBar);

    // modify the interactors such that the demo looks better
    ((IlvSelectInteractor)controlBar.getSelectInteractor()).setOpaqueMove(true);
    ((IlvZoomViewInteractor)controlBar.getZoomViewInteractor()).setPermanent(true);

    // set the initial interactor
    mgrview.setInteractor(controlBar.getSelectInteractor());

    // create the graph selector
    panel.add(new JLabel(getString("fileChooser.Label")));    
    JComboBox chooser;
    panel.add(chooser = new JComboBox());
    chooser.addItem("small");
    chooser.addItem("medium1");   
    chooser.addItem("medium2");
    chooser.addItem("medium3");
    chooser.addItem("sierpinski5");
    chooser.addItem("grid30x30");
    chooser.addItem("grid40x40");
    
    chooser.setToolTipText(getString("fileChooser.Tooltip"));
    chooser.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
          JComboBox comboBox = (JComboBox)event.getSource();
          String fileName = (String)comboBox.getSelectedItem();
          loadGrapher(fileName);
          configureLayout();
        }
      });

    // create the panel on bottom
    JPanel bottomPanel = new JPanel();
    bottomPanel.setLayout(new BorderLayout());
    getContentPane().add("South", bottomPanel);
    
    // ----------------------------------------------------------------------
    // The layout buttons
    
    JButton b;
    JPanel layoutPanel = new JPanel();
    bottomPanel.add("North", layoutPanel);
    layoutPanel.add(b = new JButton(getString("RandomizeButton.Label")));
    b.setToolTipText(getString("RandomizeButton.Tooltip"));
    b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        layout(randomLayout, grapher);
      }});
    
    layoutPanel.add(b = new JButton(getString("LayoutButton.Label")));
    b.setToolTipText(getString("LayoutButton.Tooltip"));
    b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        try {
          // validate the edge length
          float value = (float)preferredLength.getDoubleValue();
          layout.setPreferredLinksLength(value);
        }
        catch (Exception ex) {
          showMessage(getString("IllegalPrefLinkLengthMessage"), ex.getMessage());
          return;
        }
        // now the layout
        layout(layout, grapher);
      }});

    // ----------------------------------------------------------------------
    // Preferred length

    layoutPanel = new JPanel();
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.LINE_END;
    c.insets = new Insets(1,0,1,0);
    layoutPanel.setLayout(gridbag);
    bottomPanel.add("Center", layoutPanel);

    c.gridx = 0;
    c.gridy = 0;
    layoutPanel.add(new JLabel(getString("preferredLength.Label")), c);
    preferredLength = 
      new IlvJSpinnerDecimalNumberField(0, 200,
                                        layout.getPreferredLinksLength(),
                                        true, 5, 5);
    c.gridx = 1;
    layoutPanel.add(preferredLength, c);
    preferredLength.setToolTipText(getString("preferredLength.Tooltip"));
    preferredLength.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          // get the value of the parameter from the textfield
          float value = (float)preferredLength.getDoubleValue();
          // set the new value of the parameter
          layout.setPreferredLinksLength(value);
          if (layout.isAutoLayout())
            layout(layout, grapher);
        }
        catch (Exception ex) {
          showMessage(getString("IllegalPrefLinkLengthMessage"), ex.getMessage());
        }
      }
    });

    // ----------------------------------------------------------------------
    // Respect node size
    
    c.gridx = 3;
    JCheckBox respectNodeSizeOption = 
      new JCheckBox(getString("respectNodeSizeCheckbox.Label"), layout.isRespectNodeSizes());

    layoutPanel.add(respectNodeSizeOption, c);

    respectNodeSizeOption.setToolTipText(getString("respectNodeSizeCheckbox.Tooltip"));
    respectNodeSizeOption.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) 
          layout.setRespectNodeSizes(true);
        else if (e.getStateChange() == ItemEvent.DESELECTED) 
          layout.setRespectNodeSizes(false);
        if (layout.isAutoLayout())
          layout(layout, grapher);
      }
    });
    
    // ----------------------------------------------------------------------
    // Autolayout
    
    final JCheckBox autolayoutOption = 
      new JCheckBox(getString("autoLayoutCheckbox.Label"), layout.isAutoLayout());
    autolayoutOption.setToolTipText(getString("autolayoutCheckBox.Tooltip"));
    autolayoutOption.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
          layout.setAutoLayout(true);
          layout.setPreserveFixedNodes(true);
          layout(layout, grapher);
        } else if (e.getStateChange() == ItemEvent.DESELECTED) {
          layout.setAutoLayout(false);
          layout.setPreserveFixedNodes(false);
        }
      }
    });
    
    // ----------------------------------------------------------------------
    // Layout mode

    layoutModeChooser = new JComboBox();
    layoutModeChooser.addItem(getString("layoutModeChooser.IncrMode"));
    layoutModeChooser.addItem(getString("layoutModeChooser.NonIncrMode"));
    layoutModeChooser.addItem(getString("layoutModeChooser.MultiLevelMode"));

    layoutModeChooser.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        JComboBox comboBox = (JComboBox)event.getSource();
        String item = (String)comboBox.getSelectedItem();
        boolean incremental = false;
        if (item.equals(getString("layoutModeChooser.IncrMode"))) {
          layout.setLayoutMode(IlvUniformLengthEdgesLayout.INCREMENTAL_MODE);
          incremental = true;
        } else if (item.equals(getString("layoutModeChooser.NonIncrMode"))) {
          layout.setLayoutMode(IlvUniformLengthEdgesLayout.NON_INCREMENTAL_MODE);
        } else if (item.equals(getString("layoutModeChooser.MultiLevelMode"))) {
          layout.setLayoutMode(IlvUniformLengthEdgesLayout.FAST_MULTILEVEL_MODE);
        }
        autolayoutOption.setEnabled(incremental);
        if (autolayoutOption.isSelected()) {
          layout.setAutoLayout(incremental);
          layout.setPreserveFixedNodes(incremental);
        }
      }});
    String layoutModeTooltip = getString("layoutModeChooser.Tooltip");
    layoutModeChooser.setToolTipText(layoutModeTooltip);
    
    c.gridx = 0;
    c.gridy = 1;
    layoutPanel.add(new JLabel(getString("layoutModeChooser.Label")), c);
    c.gridx = 1;
    layoutPanel.add(layoutModeChooser, c);
    c.gridx = 2;
    layoutPanel.add(new JLabel(" "), c);
    c.gridx = 3;
    layoutPanel.add(autolayoutOption, c);

    
    // ----------------------------------------------------------------------
    
    // add the message line to the bottom panel
    bottomPanel.add("South", msgLine);
    msgLine.setEditable(false);

    // set the component orientation according to the locale
    Locale loc = IlvSwingUtil.getDefaultLocale();
    ComponentOrientation co = ComponentOrientation.getOrientation(loc);
    getContentPane().applyComponentOrientation(co);

    // load a sample grapher
    loadGrapher("small");
  }

  /**
   * 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)
  {
    // Sun recommends that to put the entire GUI initialization into the
    // AWT thread
    SwingUtilities.invokeLater(
      new Runnable() {
        public void run() {
          UniformLengthEdgesLayoutApplet applet = new UniformLengthEdgesLayoutApplet();
          applet.init();

          JFrame frame = new JFrame(getString("Frame.Label"));
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(800, 800);
          frame.getContentPane().add(applet);
          frame.setVisible(true);    
        }
      });
  }

  /**
   * Load a sample IVL file.
   * @param fileNameBase The base of the filename, excluding the path prefix
   *                     and the extension suffix.
   */
  private void loadGrapher(String fileNameBase)
  {
    if ("small".equals(fileNameBase))
      layout.setConvergenceThreshold(0.5f);
    else
      layout.setConvergenceThreshold(1f);

    if (fileNameBase.startsWith("medium2")) {
      layout.setMultiLinkMode(IlvUniformLengthEdgesLayout.FREE_ONE_BEND_BUNDLE);
      layout.setMultiLinkOffset(20);
      layout.setMultiLinkMaxSpread(100);
      layout.setSelfLinkMode(IlvUniformLengthEdgesLayout.NARROW_CONNECTED_RECTANGULAR);
      layout.setSelfLinkSpacing(20);
    } else {
      layout.setMultiLinkMode(IlvUniformLengthEdgesLayout.NARROW_STRAIGHT_LINE_BUNDLE);
      layout.setMultiLinkOffset(10);
      layout.setMultiLinkMaxSpread(50);
      layout.setSelfLinkMode(IlvUniformLengthEdgesLayout.NARROW_CONNECTED_SQUARE);
      layout.setSelfLinkSpacing(-1);
    }
    
    try {
      showMessage(getString("FileReadingStartMessage"), fileNameBase);
      grapher.deleteAll(false);
      try {
        grapher.read(
          IlvSwingUtil.getRelativeURL(this, "data/" + fileNameBase + ".ivl"));
      } catch (Exception ex1) {
        // This case occurs only when we pack the entire application into
        // one jar including the data files, and start as application
        grapher.read(getClass().getResource("data/" + fileNameBase + ".ivl"));
      }
      // the transformer may have been modified, so
      // we set the identity transformer
      mgrview.setTransformer(new IlvTransformer());
      grapher.reDraw();
      IlvGraphModel model = new IlvGrapherAdapter(grapher);
      showMessage(getString("FileReadingDoneMessage"), fileNameBase,
                            new Integer(model.getNodesCount()),
                            new Integer(model.getLinksCount()));

      showMessage("Reading " + fileNameBase + ".ivl done; #nodes: " + 
                  model.getNodesCount() + " #links: " + model.getLinksCount());
    } catch (Exception e) {
      // e.printStackTrace();
      showMessage(e.getMessage());
    }
  }
  
  /**
   * Adjusts the graph layout parameters to fit the graph size.
   */
  private void configureLayout()
  {
    // Use the fast multilevel layout mode for large graphs. This is much faster.
    // For details, see the javadoc of IlvUniformLengthEdgesLayout.setLayoutMode.
    if (layout.getGraphModel().getNodesCount() > 200) {
      layout.setLayoutMode(IlvUniformLengthEdgesLayout.FAST_MULTILEVEL_MODE);
      layoutModeChooser.setSelectedItem(getString("layoutModeChooser.MultiLevelMode"));
    }
    
    // increase the allowed time for large graphs
    if (layout.getGraphModel().getNodesCount() > 100)
      layout.setAllowedTime(120000L);
    else
      layout.setAllowedTime(defaultAllowedTime);
  }

  /**
   * Performs the layout of the graph.
   */
  private void layout(IlvGraphLayout layout, IlvGrapher grapher) 
  {
    // the layout report instance; this is an object which
    // contains information about the particular conditions
    // in which the layout was performed
    IlvGraphLayoutReport layoutReport = null;
    
    showMessage(getString("LayoutStartMessage"));

    // initialize the iteration listener 
    // (this will simply empty the message string)
    layoutListener.initialize();

    try {
      // perform the layout and get the layout report
      layoutReport = layout.performLayout();
      
      // print the code from the layout report
      showMessage(getString("LayoutDoneMessage"),
                  layoutReport.codeToString(layoutReport.getCode(),
                                            IlvLocaleUtil.getCurrentULocale()));
    }
    catch (IlvGraphLayoutException e) {
      // e.printStackTrace();
      showMessage(e.getMessage());
    } finally {
      if (layoutReport != null &&
          layoutReport.getCode() != IlvGraphLayoutReport.NOT_NEEDED) {
        // show all
        mgrview.fitTransformerToContent();
        mgrview.repaint();
      }
    }
  }

  /**
   * 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());
  }

  void showMessage(String msgformat, Object val)
  {
    Object[] args = { val };
    showMessage(MessageFormat.format(msgformat, args));
  }

  void showMessage(String msgformat, Object val1, Object val2, Object val3)
  {
    Object[] args = { val1, val2, val3 };
    showMessage(MessageFormat.format(msgformat, args));
  }

  /**
   * Returns a string.
   */
  static String getString(String key)
  {
    return IlvResourceUtil.getString(key, UniformLengthEdgesLayoutApplet.class,
                                     IlvLocaleUtil.getCurrentLocale());
  }

  // -------------------------------------------------------------------------

  /**
   * A graph layout iteration listener. Implementing the interface
   * GraphLayoutEventListener gives you the possibility to receive
   * during the layout information about the behavior of the layout 
   * algorithm. This information is contained in the graph layout event.
   * 
   * In our case, we will simply print a dot each time the method
   * layoutStepPerformed is called, that is after each iteration of
   * the Bus Layout algorithm.
   */
  class LayoutIterationListener
    implements GraphLayoutEventListener
  {
    private String toShow = "";
    private static final int MAX_LENGTH = 50;

    /** 
     * This method is automatically called by the layout algorithm.
     */
    public void layoutStepPerformed(GraphLayoutEvent event)
    {
      // the status area has a limited width, so we are forced to
      // reinitialize the message string
      if (toShow.length() > MAX_LENGTH)
        toShow = "";

      toShow += ".";

      showMessage(toShow);
    }

    /**
     * Initialize the listener by reseting the toShow variable.
     * This method must be called before the layout is started.
     */
    void initialize()
    {
      toShow = "";
    }
  }

  // -------------------------------------------------------------------------

  /**
   * A listener that specifies the selected nodes as fixed for the layout.
   * The fixed nodes are taken into account only if the user selected the 
   * checkbox "Autolayout".
   */
  private class MySelectionListener
    implements ManagerSelectionListener
  {
    public void selectionChanged(ManagerSelectionChangedEvent event)
    {
      IlvGraphic obj = event.getGraphic();
      if (grapher.isNode(obj)) 
        layout.setFixed(obj, grapher.isSelected(obj));
    }
  }
}