/**
 * Class CalculatorGui - write a description of the class here
 * 
 * @author: Kleanthis Thramboulidis
 * date: 
 */
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class CalculatorGui extends Frame
{
      public static Operand op;
      public Button button0, button1, button2, button3, button4;
      public Button button5, button6, button7, button8, button9;
      public Button buttonEnter, buttonBackSpace, buttonClear, buttonPoint;
      public static Frame window;
      public static TextField display;

     /**
     * Constructor for objects of class CalculatorGui
     */
     
      public CalculatorGui(Operand op)
      {
       CalculatorGui.op = op;
       window = new Frame("Java Calculator - ΑΣΚΗΣΗ 7");
        
       window.setLayout(null);
       window.setFont(new Font("TimesRoman", Font.PLAIN, 14));
       window.setBackground(Color.blue);

    
//                B          U          T          T          O          N          S
//              -----------------------------------------------------------------------
      
       // B  U  T  T  O  N  S    F  O  R    N  U  M  B  E  R  S     
      
       button0 = new Button("0"); 
       button0.reshape(64, 265, 35, 28);
       button0.setFont(new Font("TimesRoman", Font.PLAIN, 14));
       button0.setForeground(Color.blue);    
       button0.addActionListener(new Button0Handler());
       CalculatorGui.window.add(button0);
      
       button1 = new Button("1");      
       button1.reshape(64, 232, 35, 28);
       button1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
       button1.setForeground(Color.blue);    
       button1.addActionListener(new Button1Handler());
       CalculatorGui.window.add(button1);
       
       button2 = new Button("2");      
       button2.reshape(104, 232, 35, 28);
       button2.setFont(new Font("TimesRoman", Font.PLAIN, 14));
       button2.setForeground(Color.blue);    
       button2.addActionListener(new Button2Handler());
       CalculatorGui.window.add(button2);
       
       button3 = new Button("3");      
       button3.reshape(144, 232, 35, 28);
       button3.setFont(new Font("TimesRoman", Font.PLAIN, 14));
       button3.setForeground(Color.blue);    
       button3.addActionListener(new Button3Handler());
       CalculatorGui.window.add(button3);
       
       button4 = new Button("4");
       button4.reshape(64, 199, 35, 28);
       button4.setFont(new Font("TimesRoman", Font.PLAIN, 14));
       button4.setForeground(Color.blue);    
       button4.addActionListener(new Button4Handler());
       CalculatorGui.window.add(button4);
       
       button5 = new Button("5");      
       button5.reshape(104, 199, 35, 28);
       button1.setFont(new Font("TimesRoman", Font.PLAIN, 14));
       button5.setForeground(Color.blue);    
       button5.addActionListener(new Button5Handler());
       CalculatorGui.window.add(button5);
              
       button6 = new Button("6");      
       button6.reshape(144, 199, 35, 28);
       button6.setFont(new Font("TimesRoman", Font.PLAIN, 14));
       button6.setForeground(Color.blue);    
       button6.addActionListener(new Button6Handler());
       CalculatorGui.window.add(button6);
       
       button7 = new Button("7");      
       button7.reshape(64, 166, 35, 28);
       button7.setFont(new Font("TimesRoman", Font.PLAIN, 14));
       button7.setForeground(Color.blue);    
       button7.addActionListener(new Button7Handler());
       CalculatorGui.window.add(button7);
       
       button8 = new Button("8");
       button8.reshape(104, 166, 35, 28);
       button8.setFont(new Font("TimesRoman", Font.PLAIN, 14));
       button8.setForeground(Color.blue);    
       button8.addActionListener(new Button8Handler());
       CalculatorGui.window.add(button8);
       
       button9 = new Button("9");      
       button9.reshape(144, 166, 35, 28);
       button9.setFont(new Font("TimesRoman", Font.PLAIN, 14));
       button9.setForeground(Color.blue);    
       button9.addActionListener(new Button9Handler());
       CalculatorGui.window.add(button9);
       
     
       //O  T  H  E  R    Β  U  Τ  T  O  N  S
     
       buttonEnter = new Button("ENTER");  
       buttonEnter.setFont(new Font("TimesRoman", Font.PLAIN, 14));    
       buttonEnter.setBackground(Color.red);
       buttonEnter.setForeground(Color.white);
       buttonEnter.reshape(204, 100, 56, 28);
       buttonEnter.addActionListener(new ButtonEnterHandler());
       CalculatorGui.window.add(buttonEnter);

       buttonBackSpace = new Button("BackSpace");
       buttonBackSpace.setFont(new Font("TimesRoman", Font.PLAIN, 14));    
       buttonBackSpace.setForeground(Color.red);
       buttonBackSpace.reshape(64, 100, 75, 28);
       buttonBackSpace.addActionListener(new ButtonBackSpaceHandler());
       CalculatorGui.window.add(buttonBackSpace);

       buttonPoint = new Button(".");
       buttonPoint.setFont(new Font("TimesRoman", Font.PLAIN, 14)); 
       buttonPoint.setForeground(Color.blue);
       buttonPoint.reshape(144, 265, 35, 28);
       buttonPoint.addActionListener(new ButtonPointHandler());
       CalculatorGui.window.add(buttonPoint);
/*
       buttonClearAll = new Button("C");
       buttonClearAll.setFont(new Font("TimesRoman", Font.PLAIN, 14)); 
       buttonClearAll.setForeground(Color.red);
       buttonClearAll.reshape(144, 133, 35, 28);
       buttonClearAll.addActionListener(new ButtonClearAllHandler());
       CalculatorGui.window.add(buttonClearAll);
*/
       buttonClear = new Button("CE");
       buttonClear.setFont(new Font("TimesRoman", Font.PLAIN, 14));
       buttonClear.setForeground(Color.red);
       buttonClear.reshape(144, 100, 35, 28);
       buttonClear.addActionListener(new ButtonClearHandler());
       CalculatorGui.window.add(buttonClear);


       //D  I  S  P  L  A  Y     S  E  T  T  I  N  G  S

       display = new TextField("0"); 
       display.setEditable(false);  //Gia na mhn mporei kaneis na grapsei h na sbhsei apo thn othoni
       display.reshape(13, 55, 257, 30);


       //W  I  N  D  O  W     S  E  T  T  I  N  G  S
       window.add(display);         
       window.resize(283,297);
       window.setLocation(40,80);      
       window.show();      
       window.setResizable(false);  //Gia na mhn metabaletai to megethos tou frame kai xanontai ta plhktra
       window.addWindowListener(new CloseWindowAndExit()); //Gia na mporei kaneis na kleisei (Alt-F4) to window tou frame
                         
      }      
}		


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


//           B     U     T     T    O    N               H     A     N     D     L     E     R     
//          ------------- C       L       A       S       S       E       S --------------------
//                      ----------------------------------------------------


class Button0Handler implements ActionListener
  {     
     public Button0Handler() { }
 
     public void actionPerformed(ActionEvent pushingButton0)
       { CalculatorGui.op.addDigit('0'); }
   }


class Button1Handler implements ActionListener
  {     
     public Button1Handler() { }
 
     public void actionPerformed(ActionEvent pushingButton1)
       { CalculatorGui.op.addDigit('1'); }
   }


class Button2Handler implements ActionListener
  {     
     public Button2Handler() { }
 
     public void actionPerformed(ActionEvent pushingButton2)
       { CalculatorGui.op.addDigit('2'); }
   }


class Button3Handler implements ActionListener
  {     
     public Button3Handler() { }
 
     public void actionPerformed(ActionEvent pushingButton3)
       { CalculatorGui.op.addDigit('3'); }
   }


class Button4Handler implements ActionListener
  {     
     public Button4Handler() { }
 
     public void actionPerformed(ActionEvent pushingButton4)
       { CalculatorGui.op.addDigit('4'); }
   }


class Button5Handler implements ActionListener
  {     
     public Button5Handler() { }
 
     public void actionPerformed(ActionEvent pushingButton5)
       { CalculatorGui.op.addDigit('5'); }
   }


class Button6Handler implements ActionListener
  {     
     public Button6Handler() { }
 
     public void actionPerformed(ActionEvent pushingButton6)
       { CalculatorGui.op.addDigit('6'); }
   }


class Button7Handler implements ActionListener
  {     
     public Button7Handler() { }
 
     public void actionPerformed(ActionEvent pushingButton7)
       { CalculatorGui.op.addDigit('7'); }
   }


class Button8Handler implements ActionListener
  {     
     public Button8Handler() { }
 
     public void actionPerformed(ActionEvent pushingButton8)
       { CalculatorGui.op.addDigit('8'); }
   }


class Button9Handler implements ActionListener
  {     
     public Button9Handler() { }
 
     public void actionPerformed(ActionEvent pushingButton9)
       { CalculatorGui.op.addDigit('9'); }
   }


class ButtonEnterHandler implements ActionListener
  {     
     public ButtonEnterHandler() { }
 
     public void actionPerformed(ActionEvent pushingButtonEnter)
       { CalculatorGui.op.complete(); }
   }


class ButtonBackSpaceHandler implements ActionListener
  {     
     public ButtonBackSpaceHandler() { }
 
     public void actionPerformed(ActionEvent pushingButtonBackSpace)
       { CalculatorGui.op.deleteLastDigit(); }
   }



class ButtonPointHandler implements ActionListener
  {     
     public ButtonPointHandler() { }
 
     public void actionPerformed(ActionEvent pushingButtonPoint)
       { CalculatorGui.op.addDigit('.'); }
   }

/*
class ButtonClearAllHandler implements ActionListener
  {     
     public ButtonClearAllHandler() { }
 
     public void actionPerformed(ActionEvent pushingButtonClearAll)
       { 
         CalculatorGui.op.reset();
         CalculatorGui.s.removeAllElements(); 
       }
   }
*/

class ButtonClearHandler implements ActionListener
  {     
     public ButtonClearHandler() { }
 
     public void actionPerformed(ActionEvent pushingButtonClear)
       { CalculatorGui.op.reset(); }
   }