/*
 * 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.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.MessageFormat;
import java.util.Locale;

import javax.swing.JButton;
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.graphlayout.GraphLayoutEvent;
import ilog.views.graphlayout.GraphLayoutEventListener;
import ilog.views.graphlayout.IlvGraphLayout;
import ilog.views.graphlayout.IlvGraphLayoutException;
import ilog.views.graphlayout.IlvGraphLayoutReport;
import ilog.views.graphlayout.IlvGrapherAdapter;
import ilog.views.graphlayout.hierarchical.IlvHierarchicalLayout;
import ilog.views.graphlayout.random.IlvRandomLayout;
import ilog.views.interactor.IlvSelectInteractor;
import ilog.views.interactor.IlvZoomViewInteractor;
import ilog.views.swing.IlvJManagerViewControlBar;
import ilog.views.swing.IlvJScrollManagerView;
import ilog.views.util.IlvLocaleUtil;
import ilog.views.util.IlvProductUtil;
import ilog.views.util.IlvResourceUtil;
import ilog.views.util.swing.IlvJSpinnerDecimalNumberField;
import ilog.views.util.swing.IlvSwingUtil;

/**
 * This is a very simple application that uses the
 * <code>IlvGrapher</code> to perform a hierarchical layout. It shows how to use
 * hierarchical layout in applications that are not based on CSS styling.
 */
public class HierarchicalLayoutApp extends JRootPane {
  static {
    // 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 grapher adapter for layout */
  IlvGrapherAdapter adapter = new IlvGrapherAdapter(grapher);

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

  /** An instance of the Hierarchical Layout algorithm */
  IlvHierarchicalLayout layout = new IlvHierarchicalLayout();

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

  /** The flow direction selector */
  JComboBox<String> flowDirectionSelector = new JComboBox<String>();

  /** The link style selector */
  JComboBox<String> linkStyleSelector = new JComboBox<String>();

  /** Text fields for the offset parameters */
  IlvJSpinnerDecimalNumberField horiNodeOffset = new IlvJSpinnerDecimalNumberField(0, 200, 40.0, true, 6, 5);
  IlvJSpinnerDecimalNumberField vertNodeOffset = new IlvJSpinnerDecimalNumberField(0, 200, 40.0, true, 6, 5);
  IlvJSpinnerDecimalNumberField horiLinkOffset = new IlvJSpinnerDecimalNumberField(0, 200, 15.0, true, 6, 5);
  IlvJSpinnerDecimalNumberField vertLinkOffset = new IlvJSpinnerDecimalNumberField(0, 200, 15.0, true, 6, 5);
  IlvJSpinnerDecimalNumberField horiNodeLinkOffset = new IlvJSpinnerDecimalNumberField(0, 200, 20.0, true, 6, 5);
  IlvJSpinnerDecimalNumberField vertNodeLinkOffset = new IlvJSpinnerDecimalNumberField(0, 200, 20.0, true, 6, 5);

  /**
   * Initializes the application.
   */
  public void init() {
    showMessage(getString("InitMessage"));

    // attach the grapher to the layout instances
    layout.attach(adapter);
    randomLayout.attach(adapter);

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

    // change the default flow direction
    layout.setFlowDirection(IlvDirection.Right);

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

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

    // 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
    JComboBox<String> fileSelector = new JComboBox<String>();
    fileSelector.addItem("small");
    fileSelector.addItem("medium");
    fileSelector.addItem("large");
    fileSelector.addItem("huge");
    fileSelector.addItem("ports1");
    fileSelector.addItem("ports2");
    fileSelector.setToolTipText(getString("fileChooser.Tooltip"));
    fileSelector.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent event) {
        SuppressWarnings("unchecked")
        JComboBox<String> comboBox = (JComboBox<String>) event.getSource();
        String fileName = (String) comboBox.getSelectedItem();
        loadGrapher(fileName);
      }
    });

    // create the randomize button
    JButton randomizeButton = new JButton(getString("RandomizeButton.Label"));
    randomizeButton.setToolTipText(getString("RandomizeButton.Tooltip"));
    randomizeButton.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent evt) {
        layout(randomLayout, grapher);
      }
    });

    // create the layout button
    JButton layoutButton = new JButton(getString("LayoutButton.Label"));
    layoutButton.setToolTipText(getString("LayoutButton.Tooltip"));
    layoutButton.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent evt) {
        // before performing layout, make sure that the values in the text
        // fields are transfered to the style sheet
        setOffset("horizontalNodeOffset", horiNodeOffset);
        setOffset("verticalNodeOffset", vertNodeOffset);
        setOffset("horizontalLinkOffset", horiLinkOffset);
        setOffset("verticalLinkOffset", vertLinkOffset);
        setOffset("horizontalNodeLinkOffset", horiNodeLinkOffset);
        setOffset("verticalNodeLinkOffset", vertNodeLinkOffset);
        // now the layout
        layout(layout, grapher);
      }
    });

    // create the panel for the layout buttons
    JPanel layoutButtonPanel = new JPanel();
    layoutButtonPanel.setLayout(new FlowLayout());
    layoutButtonPanel.add(randomizeButton);
    layoutButtonPanel.add(layoutButton);

    // create the top panel with the toolbar and the file selector
    JPanel topPanel = new JPanel();
    topPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
    topPanel.add(controlBar);
    topPanel.add(new JLabel(getString("fileChooser.Label")));
    topPanel.add(fileSelector);

    // create the bottom panel with the layout buttons and the message line
    JPanel bottomPanel = new JPanel();
    bottomPanel.setLayout(new BorderLayout());
    bottomPanel.add(layoutButtonPanel, BorderLayout.NORTH);
    bottomPanel.add(createParameterPanel(), BorderLayout.CENTER);
    bottomPanel.add(msgLine, BorderLayout.SOUTH);
    msgLine.setEditable(false);

    // put manager view, top panel and bottom panel together
    getContentPane().setLayout(new BorderLayout(0, 0));
    getContentPane().add(scrollManView, BorderLayout.CENTER);
    getContentPane().add(topPanel, BorderLayout.NORTH);
    getContentPane().add(bottomPanel, BorderLayout.SOUTH);

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

  /**
   * Creates the layout parameter panel.
   */
  private JPanel createParameterPanel() {
    JPanel panel = 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);
    panel.setLayout(gridbag);

    // link style parameter
    c.gridx = 0;
    c.gridy = 1;
    panel.add(new JLabel(getString("linkStyleSelector.Label")), c);
    c.gridx = 1;
    panel.add(linkStyleSelector, c);
    linkStyleSelector.addItem(getString("linkStyleSelector.PolyMode"));
    linkStyleSelector.addItem(getString("linkStyleSelector.OrthMode"));
    linkStyleSelector.addItem(getString("linkStyleSelector.StraightMode"));
    linkStyleSelector.setSelectedIndex(0);
    linkStyleSelector.setToolTipText(getString("linkStyleSelector.Tooltip"));
    linkStyleSelector.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent event) {
        SuppressWarnings("unchecked")
        JComboBox<String> comboBox = (JComboBox<String>) event.getSource();
        String style = (String) comboBox.getSelectedItem();
        setGlobalLinkStyle(style);
      }
    });

    // flow direction parameter
    c.gridx = 0;
    c.gridy = 2;
    panel.add(new JLabel(getString("flowDirectionSelector.Label")), c);
    c.gridx = 1;
    panel.add(flowDirectionSelector, c);
    flowDirectionSelector.addItem(getString("flowDirectionSelector.TopMode"));
    flowDirectionSelector.addItem(getString("flowDirectionSelector.BottomMode"));
    flowDirectionSelector.addItem(getString("flowDirectionSelector.LeftMode"));
    flowDirectionSelector.addItem(getString("flowDirectionSelector.RightMode"));
    flowDirectionSelector.setSelectedIndex(3);
    flowDirectionSelector.setToolTipText(getString("flowDirectionSelector.Tooltip"));
    flowDirectionSelector.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent event) {
        SuppressWarnings("unchecked")
        JComboBox<String> comboBox = (JComboBox<String>) event.getSource();
        String flow = (String) comboBox.getSelectedItem();
        setFlowDirection(flow);
      }
    });

    // hierarchical layout offset parameters
    c.gridx = 3;
    c.gridy = 0;
    panel.add(new JLabel(getString("offsetHorizontalHeader.Label")), c);
    c.gridy = 1;
    panel.add(horiNodeOffset, c);
    c.gridy = 2;
    panel.add(horiNodeLinkOffset, c);
    c.gridy = 3;
    panel.add(horiLinkOffset, c);

    c.gridx = 4;
    c.gridy = 0;
    c.insets = new Insets(0, 5, 0, 0);
    panel.add(new JLabel(getString("offsetVerticalHeader.Label")), c);
    c.gridy = 1;
    panel.add(vertNodeOffset, c);
    c.gridy = 2;
    panel.add(vertNodeLinkOffset, c);
    c.gridy = 3;
    panel.add(vertLinkOffset, c);

    c.gridx = 2;
    c.gridy = 0;
    c.insets = new Insets(0, 25, 0, 0);
    panel.add(new JLabel(getString("offsets.Label")), c);
    c.gridy = 1;
    panel.add(new JLabel(getString("nodeOffset.Label")), c);
    c.gridy = 2;
    panel.add(new JLabel(getString("nodeLinkOffset.Label")), c);
    c.gridy = 3;
    panel.add(new JLabel(getString("linkOffset.Label")), c);

    horiNodeOffset.setToolTipText(getString("nodeOffset.horiz.Tooltip"));
    horiNodeOffset.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        setOffset("horizontalNodeOffset", (IlvJSpinnerDecimalNumberField) e.getSource());
      }
    });
    horiNodeLinkOffset.setToolTipText(getString("nodeLinkOffset.horiz.Tooltip"));
    horiNodeLinkOffset.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        setOffset("horizontalNodeLinkOffset", (IlvJSpinnerDecimalNumberField) e.getSource());
      }
    });
    horiLinkOffset.setToolTipText(getString("linkOffset.horiz.Tooltip"));
    horiLinkOffset.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        setOffset("horizontalLinkOffset", (IlvJSpinnerDecimalNumberField) e.getSource());
      }
    });
    vertNodeOffset.setToolTipText(getString("nodeOffset.vert.Tooltip"));
    vertNodeOffset.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        setOffset("verticalNodeOffset", (IlvJSpinnerDecimalNumberField) e.getSource());
      }
    });
    vertNodeLinkOffset.setToolTipText(getString("nodeLinkOffset.vert.Tooltip"));
    vertNodeLinkOffset.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        setOffset("verticalNodeLinkOffset", (IlvJSpinnerDecimalNumberField) e.getSource());
      }
    });
    vertLinkOffset.setToolTipText(getString("linkOffset.vert.Tooltip"));
    vertLinkOffset.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        setOffset("verticalLinkOffset", (IlvJSpinnerDecimalNumberField) e.getSource());
      }
    });

    return panel;
  }

  /**
   * Sets the flow direction.
   */
  private void setFlowDirection(String flow) {
    if (flow.equals(getString("flowDirectionSelector.TopMode")))
      layout.setFlowDirection(IlvDirection.Top);
    else if (flow.equals(getString("flowDirectionSelector.BottomMode")))
      layout.setFlowDirection(IlvDirection.Bottom);
    else if (flow.equals(getString("flowDirectionSelector.LeftMode")))
      layout.setFlowDirection(IlvDirection.Left);
    else if (flow.equals(getString("flowDirectionSelector.RightMode")))
      layout.setFlowDirection(IlvDirection.Right);
  }

  /**
   * Sets the global link style.
   */
  private void setGlobalLinkStyle(String style) {
    if (style.equals(getString("linkStyleSelector.OrthMode")))
      layout.setGlobalLinkStyle(IlvHierarchicalLayout.ORTHOGONAL_STYLE);
    else if (style.equals(getString("linkStyleSelector.StraightMode")))
      layout.setGlobalLinkStyle(IlvHierarchicalLayout.STRAIGHT_LINE_STYLE);
    else if (style.equals(getString("linkStyleSelector.PolyMode")))
      layout.setGlobalLinkStyle(IlvHierarchicalLayout.POLYLINE_STYLE);
  }

  /**
   * Sets the sibling, branch or parent/child offset from a textfield.
   */
  private void setOffset(String property, IlvJSpinnerDecimalNumberField valueSelector) {
    double value = valueSelector.getDoubleValue();

    try {
      // set the new value of the layout property
      if (property.equals("horizontalNodeOffset"))
        layout.setHorizontalNodeOffset(value);
      else if (property.equals("verticalNodeOffset"))
        layout.setVerticalNodeOffset(value);
      else if (property.equals("horizontalLinkOffset"))
        layout.setHorizontalLinkOffset(value);
      else if (property.equals("verticalLinkOffset"))
        layout.setVerticalLinkOffset(value);
      else if (property.equals("horizontalNodeLinkOffset"))
        layout.setHorizontalNodeLinkOffset(value);
      else if (property.equals("verticalNodeLinkOffset"))
        layout.setVerticalNodeLinkOffset(value);

    } catch (Exception ex) {
      showMessage(ex.getMessage());
    }
  }

  /**
   * Performs the layout of the graph.
   */
  private void layout(IlvGraphLayout layout, IlvGrapher grapher) {
    // the layout report instance; this is an object in which
    // the layout algorithm stores information about its behavior
    IlvGraphLayoutReport layoutReport = null;

    showMessage(getString("LayoutStartMessage"));

    // initialize the iteration listener
    layoutListener.initialize();

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

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

  /**
   * 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) {
    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"));
      }
      adapter.loadParametersFromNamedProperties(layout);
      validateParameters();
      // the transformer may have been modified, so
      // we set the identity transformer
      mgrview.setTransformer(new IlvTransformer());
      grapher.reDraw();
      showMessage(getString("FileReadingDoneMessage"), fileNameBase);
    } catch (Exception e) {
      // e.printStackTrace();
      showMessage(e.getMessage());
    }
  }

  /**
   * Sets the offset fields and link style to the current values of layout.
   */
  void validateParameters() {
    layout.setSelfLinkFromPortSide(IlvHierarchicalLayout.SOUTH);
    layout.setSelfLinkToPortSide(IlvHierarchicalLayout.EAST);

    horiNodeOffset.setDoubleValue(layout.getHorizontalNodeOffset());
    vertNodeOffset.setDoubleValue(layout.getVerticalNodeOffset());
    horiLinkOffset.setDoubleValue(layout.getHorizontalLinkOffset());
    vertLinkOffset.setDoubleValue(layout.getVerticalLinkOffset());
    horiNodeLinkOffset.setDoubleValue(layout.getHorizontalNodeLinkOffset());
    vertNodeLinkOffset.setDoubleValue(layout.getVerticalNodeLinkOffset());

    if (linkStyleSelector != null) {
      if (layout.getGlobalLinkStyle() == IlvHierarchicalLayout.POLYLINE_STYLE)
        linkStyleSelector.setSelectedIndex(0);
      else if (layout.getGlobalLinkStyle() == IlvHierarchicalLayout.ORTHOGONAL_STYLE)
        linkStyleSelector.setSelectedIndex(1);
      else if (layout.getGlobalLinkStyle() == IlvHierarchicalLayout.STRAIGHT_LINE_STYLE)
        linkStyleSelector.setSelectedIndex(2);
    }

    if (flowDirectionSelector != null) {
      if (layout.getFlowDirection() == IlvDirection.Top)
        flowDirectionSelector.setSelectedIndex(0);
      else if (layout.getFlowDirection() == IlvDirection.Bottom)
        flowDirectionSelector.setSelectedIndex(1);
      else if (layout.getFlowDirection() == IlvDirection.Left)
        flowDirectionSelector.setSelectedIndex(2);
      else if (layout.getFlowDirection() == IlvDirection.Right)
        flowDirectionSelector.setSelectedIndex(3);
    }

    // just to be sure
    layout.setCoordinatesMode(IlvGraphLayout.MANAGER_COORDINATES);
    randomLayout.setCoordinatesMode(IlvGraphLayout.MANAGER_COORDINATES);
  }

  /**
   * 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) {
    showMessage(MessageFormat.format(msgformat, val));
  }

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

  /**
   * 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() {
      Override
      public void run() {
        HierarchicalLayoutApp app = new HierarchicalLayoutApp();
        app.init();

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

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

  /**
   * 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 the percentage completed by the layout
   * each time the method layoutStepPerformed is called, that is after each
   * iteration of the Hierarchical Layout algorithm.
   */
  class LayoutIterationListener implements GraphLayoutEventListener {
    private float oldPercentage;

    /**
     * This method is automatically called by the layout algorithm.
     */
    Override
    public void layoutStepPerformed(GraphLayoutEvent event) {
      float percentage = event.getLayoutReport().getPercentageComplete();
      if (percentage != oldPercentage) {
        showMessage(getString("percentage.Label"), Float.valueOf(percentage));
        oldPercentage = percentage;
      }
    }

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

}