import java.awt.event.*;
import javax.swing.*;
import java.awt.GridLayout;

class NumberButtonPanel extends JPanel
{
    private CalculatorModel myModel;
    
    /**
     * make model
     */

    NumberButtonPanel(CalculatorModel model)
    {
        // construct superclass and make button array
        super(new GridLayout(4,4));
        myModel = model;
        makeButtons();
    }

    /**
     * Makes the buttons for this GUI/view. The number
     * of buttons is determined my the size of the array
     * myButtons. This helper function takes some of
     * the busy work out of the ButtonPanel constructor.
     */
    protected void makeButtons()
    {
        // make listener to do the move chosen by user
        ActionListener digitProc = new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    String dig = e.getActionCommand();
                    myModel.addDigit(Integer.parseInt(dig));
                }
            };
        ActionListener opProc = new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    myModel.binaryOp(e.getActionCommand());
                }
            };

        String[] labels = {
            "7", "8", "9", "/",
            "4", "5", "6", "*",
            "1", "2", "3", "-",
            "0", "+/-","=","+"
        };

        for(int k=0; k < labels.length; k++){
            CalcButton button = new CalcButton(labels[k],labels[k]);
            String s = labels[k];
            if (s.equals("+/-")){
                button.addActionListener(
                           new ActionListener(){
                             public void actionPerformed(ActionEvent e)
                             {
                                 myModel.toggleSign();
                             }
                });
            }            
            else if (Character.isDigit(s.charAt(0))){
                button.addActionListener(digitProc);
            }
            else {
                button.addActionListener(opProc);
            }
            add(button);
        }
    }
}
