/**
 * 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 NumberButton button0, button1, button2, button3, button4, button5, button6, button7, button8,button9;                      
      public static OperatorButton buttonPlus, buttonMinus, buttonMul, buttonDiv, buttonEqual;                 
      public static MemoryButton buttonMemoryStore, buttonMemoryPlus, buttonMemoryMinus, buttonMemoryRecall, buttonMemoryClear;
      public static OtherButton buttonEnter, buttonPoint, buttonBackspace, buttonClear, buttonClearAll;
      public static Button buttonPositive2Negative;
      public static ConstantButton buttonPI, buttonE;
      public static SimpleModeMathFuncButton buttonSquareRoot, buttonSquare, buttonPower, buttonNaturalLogarithm,
                      buttonReciprocal, buttonPercent;
      public static Frame window;
      public static TextField memoryStatusField, display;
      public static MenuBar menuBar;
      public static Menu viewMenu, helpMenu;
      public static CheckboxMenuItem showStackStatus;
     

  
     /**
     * Constructor for objects of class CalculatorGui
     */
     
      public CalculatorGui()
      {

      window = new Frame("Java Calculator");
        
      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 NumberButton("0",'0'); 
      button0.reshape(64, 265, 35, 28);    
      
      button1 = new NumberButton("1",'1');      
      button1.reshape(64, 232, 35, 28);
       
      button2 = new NumberButton("2",'2');      
      button2.reshape(104, 232, 35, 28);

      button3 = new NumberButton("3",'3');      
      button3.reshape(144, 232, 35, 28);

      button4 = new NumberButton("4",'4');
      button4.reshape(64, 199, 35, 28);

      button5 = new NumberButton("5",'5');      
      button5.reshape(104, 199, 35, 28);
       
      button6 = new NumberButton("6",'6');      
      button6.reshape(144, 199, 35, 28);

      button7 = new NumberButton("7",'7');      
      button7.reshape(64, 166, 35, 28);

      button8 = new NumberButton("8",'8');
      button8.reshape(104, 166, 35, 28);

      button9 = new NumberButton("9",'9');      
      button9.reshape(144, 166, 35, 28);
        

                                    
                      
      // B  U  T  T  O  N  S    F  O  R    O  P  E  R  A  T  O  R  S

      buttonDiv = new OperatorButton("/",'/');
      buttonDiv.reshape(195, 166, 35, 28);

      buttonMul = new OperatorButton("X",'X');
      buttonMul.reshape(195, 199, 35, 28);

      buttonMinus = new OperatorButton("-",'-');
      buttonMinus.reshape(195, 232, 35, 28);

      buttonPlus = new OperatorButton("+",'+');
      buttonPlus.reshape(195, 265, 35, 28);

      buttonEqual = new OperatorButton("=",'=');
      buttonEqual.reshape(235, 265, 35, 28);



      // B  U  T  T  O  N  S    A  N  D    S  T  A  T  U  S    F  I  E  L  D    F  O  R    M  E  M  O  R  Y     

      buttonMemoryStore = new MemoryButton("MS");
      buttonMemoryStore.reshape(13, 133, 35, 28);
      
      buttonMemoryPlus = new MemoryButton("M+");
      buttonMemoryPlus.reshape(13, 166, 35, 28);
     
      buttonMemoryMinus = new MemoryButton("M-");
      buttonMemoryMinus.reshape(13, 199 , 35, 28);

      buttonMemoryRecall = new MemoryButton("MR");
      buttonMemoryRecall.reshape(13, 232, 35, 28);

      buttonMemoryClear = new MemoryButton("MC");
      buttonMemoryClear.reshape(13, 265, 35, 28);
      
      memoryStatusField = new TextField();
      memoryStatusField.setFont(new Font("TimesRoman", Font.BOLD, 14));
      memoryStatusField.setEditable(false);   //Gia na mhn mporei kaneis na grapsei h na sbhsei apo thn othoni
      memoryStatusField.reshape(18, 100, 25, 25);



      //O  T  H  E  R    Â  U  Ô  T  O  N  S
     
      buttonEnter = new OtherButton("ENTER");      
      buttonEnter.setBackground(Color.red);
      buttonEnter.setForeground(Color.white);
      buttonEnter.reshape(204, 100, 56, 28);

      buttonBackspace = new OtherButton("Backspace");
      buttonBackspace.setFont(new Font("TimesRoman", Font.PLAIN, 14));      
      buttonBackspace.setForeground(Color.red);
      buttonBackspace.reshape(64, 100, 75, 28);

      buttonPoint = new OtherButton(".");
      buttonPoint.setForeground(Color.blue);
      buttonPoint.reshape(144, 265, 35, 28);

      buttonClear = new OtherButton("C");
      buttonClear.setForeground(Color.red);
      buttonClear.reshape(144, 100, 35, 28);

      buttonClearAll = new OtherButton("CE");
      buttonClearAll.setForeground(Color.red);
      buttonClearAll.reshape(144, 133, 35, 28);



     //C  O  N  S  T  A  N  T    Â  U  Ô  T  O  N  S
     
      buttonPI = new ConstantButton("PI");      
      buttonPI.reshape(64, 133, 35,28);

      buttonPI = new ConstantButton("e");      
      buttonPI.reshape(104, 133, 35,28);


      // P  O  S  I  T  I  V  E    T  O    N  E  G  A  T  I  V  E    B  U  T  T  O  N

      buttonPositive2Negative = new Button("+ / -");
      buttonPositive2Negative.setForeground(Color.blue);      
      buttonPositive2Negative.addActionListener(new ButtonPositive2NegativeHandler());
      window.add(buttonPositive2Negative);
      buttonPositive2Negative.reshape(104, 265, 35, 28);

      
     //S  I  M  P  L  E    M  O  D  E    B  U  T  T  O  N  S

      buttonSquareRoot = new SimpleModeMathFuncButton ("Sqrt");
      buttonSquareRoot.reshape(195, 133, 35, 28);
      
      buttonPower = new SimpleModeMathFuncButton ("x^y");
      buttonPower.reshape(235, 133, 35, 28);
 
      buttonNaturalLogarithm = new SimpleModeMathFuncButton ("ln");
      buttonNaturalLogarithm.reshape(235, 166, 35, 28);

      buttonReciprocal = new SimpleModeMathFuncButton ("1/x");
      buttonReciprocal.reshape(235, 199, 35, 28);

      buttonPercent = new SimpleModeMathFuncButton ("%");
      buttonPercent.reshape(235, 232, 35, 28);


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

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

     
//                M          E          N          U          
//              --------------------------------------      
            
      //M  E  N  U     S  E  T  T  I  N  G  S     

      menuBar = new MenuBar();
     
      viewMenu = new Menu("View");
      showStackStatus = new CheckboxMenuItem("Show Stack Status");      
      showStackStatus.addItemListener(new ViewMenuHandler(showStackStatus));
      viewMenu.add(showStackStatus);
      
      
      helpMenu = new Menu("Help");
      menuBar.setHelpMenu(helpMenu);

      menuBar.add(viewMenu);
      menuBar.add(helpMenu);
                              
      //stackStatus.disable();                       
      

      //W  I  N  D  O  W     S  E  T  T  I  N  G  S
      window.add(display);      
      window.add(memoryStatusField);  
      window.setMenuBar(menuBar);    
      window.resize(283,287);
      window.show();
      window.toFront();            //Me to pou dhmiourgeitai neo antikeimeno (Calculator) anoigei to window tou frame
      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
      //window.setIconImage(Image <eikona pou tha emfanizetai otan to parathyro einai minimized>);
      }      
}		


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



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


class NumberButton extends Button
  {
   String label;
   char numChar;
  
   public NumberButton(String label,char numChar)
    { 
      super(label);
      this.numChar = numChar;
      this.setFont(new Font("TimesRoman", Font.PLAIN, 14));
      //this.setBackground(Color.<epilogi xrwmatos>);      
      this.setForeground(Color.blue);
      this.addActionListener(new NumberButtonHandler(this.numChar));  
      CalculatorGui.window.add(this);
    }
  }
  

class OperatorButton extends Button
  {
   String label;
   char opChar;
   Color darkGreen = new Color(0,128,0);
       
   public OperatorButton(String label,char opChar)
    { 
      super(label);
      this.opChar = opChar;
      this.setFont(new Font("TimesRoman", Font.PLAIN, 14));
      this.setForeground(darkGreen);
      this.addActionListener(new OperatorButtonHandler(this.opChar));
      CalculatorGui.window.add(this);
    }
  }   


class MemoryButton extends Button
  {
   String label;
       
   public MemoryButton(String label)
    { 
      super(label);
      this.label = label;
      this.setFont(new Font("TimesRoman", Font.PLAIN, 14));      
      this.setForeground(Color.red);
      this.addActionListener(new MemoryButtonHandler(this.label));
      CalculatorGui.window.add(this);
    }
  }   



class OtherButton extends Button
  {
   String label;
       
   public OtherButton(String label)
    { 
      super(label);
      this.label = label;
      this.setFont(new Font("TimesRoman", Font.PLAIN, 14));      
      this.addActionListener(new OtherButtonHandler(this.label));
      CalculatorGui.window.add(this);
    }
  }   

 
class ConstantButton extends Button
  {
   String label;
       
   public ConstantButton(String label)
    { 
      super(label);
      this.label = label;
      this.setFont(new Font("TimesRoman", Font.PLAIN, 14));      
      this.setForeground(Color.blue);
      this.addActionListener(new ConstantButtonHandler(this.label));
      CalculatorGui.window.add(this);
    }
  }   


class SimpleModeMathFuncButton extends Button
  {
   String label;
   
   public SimpleModeMathFuncButton(String label)
    { 
      super(label);
      this.label = label;
      this.setFont(new Font("TimesRoman", Font.PLAIN, 14)); 
      this.setForeground(Color.magenta);     
      this.addActionListener(new SimpleModeMathFuncButtonHandler(this.label));
      CalculatorGui.window.add(this);
    }
  }   

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


class NumberButtonHandler implements ActionListener
  {
     char numChar;
  
     public NumberButtonHandler(char ch)
     { numChar = ch; }
 
     public void actionPerformed(ActionEvent pushingNumberButton)
       { Calculator.op.addDigit(numChar); }
   }


class OperatorButtonHandler implements ActionListener
  {
     char opChar;
  
     public OperatorButtonHandler(char ch)
     { opChar = ch; }
 
     public void actionPerformed(ActionEvent pushingOperatorButton)
      { 
        switch (opChar)
         {
           case '+':
               Calculator.add.operate();
               break;
           case '-':
               Calculator.sub.operate();
               break;
           case 'X':
               Calculator.mul.operate();
               break;
           case '/':
               Calculator.div.operate();
               break;
           case '=':
               Calculator.eq.operate();
               break;
           default :
               break;
         }
      }
   }


class MemoryButtonHandler implements ActionListener
  {
     String label;
  
     public MemoryButtonHandler(String s)
     { label = s; }
 
     public void actionPerformed(ActionEvent pushingMemoryButton)
       { 
         if (label.equals("MS"))
             Calculator.mem.store();
         else if (label.equals("M+"))
                  Calculator.mem.add();
              else if (label.equals("M-"))
                       Calculator.mem.sub();
                    else if (label.equals("MR"))
                             Calculator.mem.recall();
                         else  // Button "MC"
                             Calculator.mem.clear();
       }
   }


class OtherButtonHandler implements ActionListener
  {
     String label ;
     public OtherButtonHandler(String s)
     { label = s; }
 
     public void actionPerformed(ActionEvent pushingOtherButton)
       { 
         if (label.equals("ENTER"))
             Calculator.op.complete();
         else if (label.equals("Backspace"))
                  Calculator.op.deleteLastDigit();
              else if (label.equals("."))
                       Calculator.op.addDigit('.');
                   else if (label.equals("C"))
                            Calculator.op.reset();
                        else  // Button "CE"
                            {
                             Calculator.op.reset();
                             Calculator.s.removeAllElements();                    
                            }
       }
   }


class ConstantButtonHandler implements ActionListener
  {
     String label;
  
     public ConstantButtonHandler(String s)
     { label = s; }
 
     public void actionPerformed(ActionEvent pushingConstantButton)
       { 
         if (label.equals("PI"))
             Calculator.op.set(StrictMath.PI);
         else // Button "e" 
             Calculator.op.set(StrictMath.E);
      }
  }


class ButtonPositive2NegativeHandler implements ActionListener
  {
     double opValue; 

     public ButtonPositive2NegativeHandler()
      { ; }
      
     public void actionPerformed(ActionEvent pushingButtonPositive2Negative)
       { 
         opValue = (Double.valueOf(CalculatorGui.display.getText())).doubleValue();
         Calculator.op.reset();
         Calculator.op.set(opValue * (-1));             
       }
   }


class SimpleModeMathFuncButtonHandler implements ActionListener
  {
     String label;
  
     public SimpleModeMathFuncButtonHandler(String s)
     { label = s; }
 
     public void actionPerformed(ActionEvent pushingMemoryButton)
       { 
         if (label.equals("Sqrt"))
             Calculator.sim.squareRoot();
         else if (label.equals("x^y"))
                  Calculator.sim.power();
              else if (label.equals("ln"))
                       Calculator.sim.naturalLogarithm();
                    else if (label.equals("1/x"))
                             Calculator.sim.reciprocal();
                         else  // Button "%"
                             Calculator.sim.percent();
       }
   }



//              M     E     N     U               H     A     N     D     L     E     R     
//             ----------- C       L       A       S       S       E       S ------------
//                         -------------------------------------------------

class ViewMenuHandler implements ItemListener
  {
      CheckboxMenuItem viewMenuItem;
  
     public ViewMenuHandler(CheckboxMenuItem vmi)
       { viewMenuItem = vmi; }
     
 
     public void itemStateChanged(ItemEvent SelectingViewMenuItem)
       { 
         if (viewMenuItem.getLabel().equals("Show Stack Status"))
              if (viewMenuItem.getState())
                  Calculator.s_gui.stackStatus.show();                  
              else
                  Calculator.s_gui.stackStatus.hide();
         //else                         
       }
   }