/* <CODE>
 ****************************************************************
 * Copyright (C) 1997-2000 Micro Focus International Ltd.       *
 * All Rights Reserved.                                         *
 ****************************************************************
 *                                                              *
 * This COBOL program demonstrates how to read and update Java  *
 * Arrays from COBOL.                                           *
 *                                                              *
 *  Version History:                                            *
 *  @author            Micro Focus International Development    *
 *  @version           1.0, Created                             *
 ****************************************************************
 *</CODE>
 */
import java.awt.*;
import java.awt.event.*;

public class Frame1 extends Frame
{

  //Construct the frame
  MenuBar ApplicationMenu = new MenuBar();
  Menu menu1 = new Menu();
  Menu menu2 = new Menu();
  MenuItem menuItem2 = new MenuItem();
  MenuItem menuItem1 = new MenuItem();
  GridLayout gridLayout1 = new GridLayout();
  Menu menu3 = new Menu();

  Label label1 = new Label();
  Label label2 = new Label();
  Label nextterm = new Label();
  Label label3 = new Label();
  Label currentpi = new Label();
  Label label4 = new Label();

  calc_pi classicDemo = new calc_pi();
  Menu menu4 = new Menu();
  MenuItem menuItem3 = new MenuItem();
  MenuItem menuItem4 = new MenuItem();
  MenuItem menuItem5 = new MenuItem();
  MenuItem menuItem6 = new MenuItem();

  public Frame1()
  {
    try
    {
      jbInit();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
//Component initialization

  private void jbInit() throws Exception 
  {
    this.setSize(new Dimension(600, 200));
    this.setTitle("Classic PI Demo");
    ApplicationMenu.setHelpMenu(menu2);
    menu1.setLabel("File");
    menu2.setLabel("Help");
    menuItem2.setLabel("About");
    menuItem2.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        menuItem2_actionPerformed(e);
      }
    });
    menuItem1.setLabel("Exit");
    menu3.setLabel("Run");
    label1.setFont(new Font("Dialog", 0, 16));
    label1.setAlignment(2);
    label1.setText("Calculation");
    label2.setAlignment(1);
    label2.setText("Select \"Run\" menu to start demo");
    nextterm.setText("");
    label3.setAlignment(1);
    label3.setText("");
    currentpi.setText("");
    label4.setFont(new Font("Dialog", 0, 16));
    label4.setText(" of PI");
    menu4.setLabel("calculate pi");
    menuItem3.setLabel("quickly");
    menuItem4.setLabel("slowly");
    menuItem5.setLabel("very slowly");
    menuItem6.setLabel("mega slowly");
    menuItem6.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        menuItem6_actionPerformed(e);
      }
    });
    menuItem5.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        menuItem5_actionPerformed(e);
      }
    });
    menuItem4.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        menuItem4_actionPerformed(e);
      }
    });
    menuItem3.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        menuItem3_actionPerformed(e);
      }
    });
    gridLayout1.setRows(3);
    menuItem1.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        menuItem1_actionPerformed(e);
      }
    });
    this.setLayout(gridLayout1);
    ApplicationMenu.add(menu1);
    ApplicationMenu.add(menu3);
    ApplicationMenu.add(menu2);
    menu2.add(menuItem2);
    this.setMenuBar(ApplicationMenu);
    menu1.add(menuItem1);
    this.add(label1, null);
    this.add(label4, null);
    this.add(label2, null);
    this.add(nextterm, null);
    this.add(label3, null);
    this.add(currentpi, null);
    menu3.add(menu4);
    menu4.add(menuItem3);
    menu4.add(menuItem4);
    menu4.add(menuItem5);
    menu4.add(menuItem6);
    WindowListener listener = new WindowListener();
    addWindowListener(listener);

  }


  class WindowListener extends WindowAdapter
  {
    WindowListener()
    {
        super();
    }
    public void windowClosing(WindowEvent e)
    {
        System.exit(0);
    }
  }


  void menuItem1_actionPerformed(ActionEvent e)
  {
    System.exit(1);
  }

  void menuItem2_actionPerformed(ActionEvent e)
  {
    AboutBox aboutBox = new AboutBox(this,"COBCALL Parameter Passing Demo",true);
    aboutBox.show();
  }


  void calculate_pi(int delay)
  {
    boolean endOfDemo = true;

    label2.setText("Next Term Is");
    label3.setText("Value of PI is");

    endOfDemo=classicDemo.calc_term();
    while(endOfDemo != true)
    {
      currentpi.setText(classicDemo.currentPI);
      nextterm.setText(classicDemo.nextTerm);

      /* We need to slow it down a little to see the actual calculation of PI */
      try
      {
        Thread.sleep(delay);
      }
      catch(Exception e)
      {
        System.err.println("Exception during a thread.sleep");
      }
      endOfDemo=classicDemo.calc_term();
    }
  }


  void menuItem3_actionPerformed(ActionEvent e)
  {
    calculate_pi(0);
  }

  void menuItem4_actionPerformed(ActionEvent e)
  {
    calculate_pi(250);
  }

  void menuItem5_actionPerformed(ActionEvent e)
  {
    calculate_pi(500);
  }

  void menuItem6_actionPerformed(ActionEvent e)
  {
    calculate_pi(1000);
  }

  void this_windowClosed(WindowEvent e)
  {
    System.exit(0);
  }
 }

