
/* 
   This applet is simple statistics calculator.  It will compute
   and display various statistics about a set of numbers entered
   by the user.
*/


import java.awt.*;
import java.applet.Applet;

public class StatsApplet extends Applet {

   TextField dataField;   // A text-input box where the user will enter the numbers

   Button enterButton;   // The user can click on this button to enter a numbers
                         //    into the data set.  (Numbers can also be entered
                         //    just by pressing return.)

   Button clearButton;   // The user can clear the dataset by clicking on this button.
   
   Label countText; // Uneditable text that shows the number of numbers in the dataset.
   Label sumText;   // Uneditable text that shows the sum of the numbers.

   StatCalc2 stats; // A statistics object object to keep track of the data.


   public void init() {
         // The init method is called by the system when the applet object
         // is first created.  It is used here to create and lay out
         // the components that make up the applet.

      setBackground(Color.lightGray);
      
      setLayout( new GridLayout(3,2,3,3) );
         // Applet will show a grid of components in three rows and two
         // columns.  (The third and fourth parameters specify space
         // between components in the grid.)

      stats = new StatCalc2();  // create the StatCalc2 object


      dataField = new TextField();        // create componets
      enterButton = new Button("Enter");
      clearButton = new Button("Clear");
      clearButton.disable();  // The clear button is initially
                              // disabled because there is no
                              // data to clear.
      countText = new Label("0");
      sumText = new Label("0");


      Panel buttonPanel = new Panel();  // The two buttons will be placed
                                        // in a sub-panel that uses a grid
                                        // layout with a single row and two
                                        // columns.  The sub-panel will then
                                        // be added as a single component in
                                        // the applet layout.
      buttonPanel.setLayout( new GridLayout(1,2,3,3) );
      buttonPanel.add(enterButton);
      buttonPanel.add(clearButton);


      add(dataField);                      // Add components to the applet.
      add(buttonPanel);                    //    The components are laid out
      add( new Label("Items entered:") );  //    by the GridLayout layout manager.
      add(countText);                      //    It fills in the grid row by row.
      add( new Label("Sum:") );            //    There should be as many components
      add(sumText);                        //    as there are spaces in the grid.

   }  // end of init() method.


   public Insets insets() {
         // This method is called by the system to find out how much space
         // there should be between the edges of the applets and the components
         // contained in the applet.
      return new Insets(3,3,3,3);
   }

   public void start() {
         // This is called by the system just before the applet starts running
      dataField.requestFocus();
   }


   void doEnterCommand() {
         // This method is called by the action() method, defined below,
         // when the user enters a number into the data set (by clicking
         // on the Enter button or by pressing return while typing in
         // the dataField text-input box.

      String dataText = dataField.getText();    // Get the text from the input box.

      double n;                                 // This section of the method converts
      try {                                     //    the string from the input box
         Double d = new Double(dataText);       //    into a double number.  It's kind
         n = d.doubleValue();                   //    of tricky.
      }
      catch (NumberFormatException e) {         // If an error is found in the input,
         dataField.setText("Illegal Entry!");   //    A message is put in the input box.
         dataField.selectAll();
         dataField.requestFocus();
         return;
      }

      stats.enter(n);   // Add the user's number to the dataset.

      int count = stats.getCount();                // Show the stats.
      countText.setText( String.valueOf(count) );

      double sum = stats.getSum();
      sumText.setText( String.valueOf(sum) );

      dataField.selectAll();     // Hilites the contents of the text box.
      dataField.requestFocus();  // Asks for the text box to get the input focus.

      if (count == 1)            // If the first item has just been entered,
         clearButton.enable();   //   then the clear button can be enabled.

   }  // end doEnterCommand()


   void doClearCommand() {
         // This is called by the action() method when the user clicks on the Clear button.
      dataField.setText("Not Implemented");
   }


   public boolean action(Event evt, Object arg) {
         // This is called by the system when the user performs some action on
         // one of the components in the applet.

      if (evt.target == enterButton) {  // use clicked Enter button
         doEnterCommand();
         return true;
      }

      else if (evt.target == clearButton) {  // user clicked Clear button
         doClearCommand();
         return true;
      }

      else if (evt.target == dataField) {  // user pressed return while typing
         doEnterCommand();                 //         in the data-input box
         return true;
      }

      else
         return super.action(evt,arg);

   }  // end action()


} // end of class StatsApplet
