// File: StackApplet.java
// This applet is a small example to illustrate how to write an interactive 
// Applet to test the methods of another class. 
// -- Michael Main (main@colorado.edu)
// Modified by Bina Ramamurthy (bina@cse.buffalo.edu)

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JOptionPane;

public class StackApplet extends JApplet
{  

   Stack b  = new Stack();

   // These are the interactive Components that will appear in the Applet.
   // We declare one JButton for each Stack  method that we want to be able to
   // test. If the method has an argument, then there is also a TextField
   // where the user can enter the value of the argument.
   // At the bottom, there is a TextArea to write messages.
   JButton    sizeButton             = new JButton("size( )");
   JButton    pushButton             = new JButton("push( )");
   JButton    popButton              = new JButton("pop()");
   JButton    topButton              = new JButton("top()");
   JButton    emptyButton            = new JButton("empty()");
   TextField elementText            = new TextField(10);

   JTextField targetText             = new JTextField(10);
   JTextArea  feedback               = new JTextArea(7, 60);
   JButton[] contents = new JButton[10];
   Container c;

   public void init( )
   {      
      // Some messages for the top of the Apple
      c = getContentPane();
      c.setLayout(new FlowLayout());
      c.add(new JLabel("This test program has created a stack."));
      c.add(new JLabel("Press buttons to activate the stack's methods."));
      addHorizontalLine(Color.blue);
       
      // The JButton for testing the size method:
      c.add(sizeButton);

      
      // The JButton and TextField for testing the add method:
      c.add(pushButton);
 
      c.add(popButton);

      c.add(topButton);

      c.add(emptyButton);
      addNewLine( );
      c.add(elementText);

      


      // A TextArea at the bottom to write messages:
      addHorizontalLine(Color.blue);
      addNewLine( );
      feedback.setEditable(false);
      feedback.append("I am ready for your first action.\n");
      c.add(feedback);
      addNewLine();
   
      for (int j = contents.length-1; j >= 0; j--) {
	 contents[j] = new JButton();
	 c.add(contents[j]);
	 addNewLine();
      } // end of for ()
      for (int j = contents.length-1; j >= 0; j--) {
	 contents[j].setVisible(false);
	 
      }

      
      // Tell the JButtons what they should do when they are clicked:
      sizeButton.addActionListener(new SizeListener( ));
      pushButton.addActionListener(new PushListener( ));
      popButton.addActionListener(new PopListener( ));
      topButton.addActionListener(new TopListener( ));
      emptyButton.addActionListener(new EmptyListener( ));
      setSize(600,600);

   }


   class SizeListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         feedback.append("The stack has size " + b.size( ) + ".\n");
      }
   }

   class PopListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {   
	 if ( b.empty()) {
	    feedback.append("The stack is empty \n");
	    return;
	 } // end of if ()
	 
	 contents[b.size()-1].setVisible(false);
         feedback.append("The poped element is " + b.pop( ) + ".\n");
      }
   }

   class TopListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {     if ( b.empty()) {
	    feedback.append("The stack is empty \n");
	    return;
	 } // end of if ()
         feedback.append("The top of the stack is " + b.peek( ) + ".\n");
      }
   }
   
   class EmptyListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {  if ( b.empty()) {
         feedback.append("The stack is empty" + ".\n");}
	 else
         feedback.append("The stack is not empty" + ".\n");
      }
   }    


   class PushListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         try
         {
            String userInput = elementText.getText( );
            int element = Integer.parseInt(userInput);
            b.push(new Integer(element));
            feedback.append(element + " has been added to the stack.\n");
	    contents[b.size()-1].setText(userInput);
	    contents[b.size()-1].setVisible(true);
	    repaint();
	 }
         catch (NumberFormatException e)
         {
            feedback.append("Type an integer before clicking button.\n");
            elementText.requestFocus( );
            elementText.selectAll( );
         }
      }                   
   }

   
  

   private void addHorizontalLine(Color co)
   {  
      // Add a Canvas 10000 pixels wide but only 1 pixel high, which acts as
      // a horizontal line to separate one group of components from the next.
      Canvas line = new Canvas( );
      line.setSize(10000,1);
      line.setBackground(co);
      c.add(line);
   }

   
   private void addNewLine( ) 
   {  
      // Add a horizontal line in the background color. The line itself is
      // invisible, but it serves to force the next Component onto a new line.
      addHorizontalLine(getBackground( ));
   }


      
      

      
}





