/**
 * Class CalculatorGui - write a description of the class here
 * 
 * @author: Kleanthis Thramboulidis
 * date: 
 */

import java.awt.*;
import java.awt.event.*;


public class CalculatorGui extends Frame
{
      public MyDigitButton button0, button1, button2, button3;
      public Button buttonBs;
      public TextField display;
      public Operand op;
      
      public CalculatorGui(Operand op) {
	   super("JAVA Polish Reverse Calculator");

		this.op = op;
      this.setFont(new Font("TimesRoman", Font.PLAIN, 12));
      this.setBackground(Color.blue);
      
		this.setSize(new Dimension(283,297));
      this.setLocation(50,100);      

      this.setVisible(true);
      this.toFront();            
      this.setResizable(false);  
      this.addWindowListener(new CloseWindowAndExit());
      
      MyDigitButton.cg = this;
      button0=new MyDigitButton("0",64, 265, 35, 28);
      button1=new MyDigitButton("1",64, 232, 35, 28);
      button2=new MyDigitButton("2",104, 232, 35, 28);
      button3=new MyDigitButton("3",144, 232, 35, 28);	
      

      buttonBs=new Button("BackSpace");
 		this.add(buttonBs);
		buttonBs.setBounds(new Rectangle(64, 100, 75, 28));
		buttonBs.setFont(new Font("TimesRoman",Font.PLAIN,14));
		buttonBs.setBackground(Color.cyan);
		buttonBs.setForeground(Color.blue);		
		buttonBs.addActionListener(new BsButtonHandler(this,"BackSpace"));
      		
      			
		display = new TextField("0",14);
      this.add(display);
      display.setEditable(false);  
		display.setBounds(new Rectangle(13, 55, 257, 30));
	}  
	

}		



class CloseWindowAndExit extends WindowAdapter
  {
    public void windowClosing (WindowEvent closeWindowAndExit)
     {
      System.exit(0);
     }
  } 


class DigitButtonHandler implements ActionListener {
		public CalculatorGui calcGui;
		String label;

		
		public DigitButtonHandler(CalculatorGui calcGui, String label) {
			this.calcGui = calcGui;
			this.label = label;
		}
		
     	public void actionPerformed(ActionEvent pushingButton0){ 
			calcGui.op.addDigit(label.charAt(0));
       	}
   }
   
class BsButtonHandler implements ActionListener {
		public CalculatorGui calcGui;
		String label;

		
		public BsButtonHandler(CalculatorGui calcGui, String label) {
			this.calcGui = calcGui;
			this.label = label;
			}
		
     	public void actionPerformed(ActionEvent pushingButton0){ 
			calcGui.op.deleteLastDigit();
       	}
   }
   
   
   
class MyDigitButton extends Button {
		String label;
		static CalculatorGui cg;		
		
		public MyDigitButton(String label, int x, int y, int width, int height){   
			super(label);
			this.label = label;
			setBounds(new Rectangle(x, y, width, height));
			setFont(new Font("TimesRoman",Font.PLAIN,14));
			setBackground(Color.cyan);
			setForeground(Color.blue);		
         addActionListener(new DigitButtonHandler(cg,label));
         cg.add(this);
      	}
     	}