/**
* Class ButtonEx5 is an example of a java gui program that uses another class to store data.
* It uses the Cartesian class to store a pair of numbers.
* Works when Cartesian Class is in same folder
*
* @author G. Greer modified M. Booth
* @version 1.0
*/

//import section
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class ButtonEx5 extends JFrame
{
    //fields
    private Cartesian car;// cartesion object available to listeners
    private JTextField xTextField;// define a text field
    private JTextField yTextField;
    private JLabel aLabel; // define a label

    //constructor
    public ButtonEx5 ()
    {
        //variables for gui and events
        JFrame aFrame; // define frame
        JButton button1;//define buttons
        JButton button2;
        JButton button3;
        ActionListener button1Listener; // define listeners
        ActionListener button2Listener;
        ActionListener button3Listener;

        car = new Cartesian(); //construct cartesian object

        // create, size and name the frame
        aFrame = new JFrame ("Button Example 5");
        aFrame.setSize (400, 350);
        aFrame.setResizable(false); //cannot resize frame
        // set up frame to exit when it closes
        aFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        // create the label
        aLabel = new JLabel ("<html><i>Output</i></html>");

        // create a text field
        xTextField = new JTextField ("0", 10);
        yTextField = new JTextField ("0", 10);

        // create buttons
        button1 = new JButton ("Save");
        button2 = new JButton ("Retrieve");
        button3 = new JButton ("Exit");
        button1.setToolTipText ("Save the cartesian coordinates");
        button2.setToolTipText ("Retrieve the cartesian coordinates");
        button3.setToolTipText ("Exit the program");

        // creating the action listeners
        button1Listener = new Button1Listener ();
        button2Listener = new Button2Listener ();
        button3Listener = new Button3Listener ();

        // connecting buttons to the action listeners
        button1.addActionListener (button1Listener);
        button2.addActionListener (button2Listener);
        button3.addActionListener (button3Listener);

        // add components to frame
        aFrame.getContentPane ().setLayout (new FlowLayout ());
        aFrame.getContentPane ().add (xTextField);//add text
        aFrame.getContentPane ().add (yTextField);
        aFrame.getContentPane ().add (button1);//add buttons
        aFrame.getContentPane ().add (button2);
        aFrame.getContentPane ().add (button3);
        aFrame.getContentPane ().add (aLabel);//add label
        aFrame.setVisible (true); //make visible
    }




    // the inner classes that do the work
    //listen for activity at save button
    public class Button1Listener implements ActionListener
    {
        //when values are entered and button clicked
        public void actionPerformed (ActionEvent e)
        {
            try //make attempt to get values
            {
                int intX; //declare x and y variables
                int intY;
                //get x and y variables from textfield
                intX = Integer.valueOf (xTextField.getText ()).intValue ();
                intY = Integer.valueOf (yTextField.getText ()).intValue ();
                car.setX(intX);//save values as cartesian x, y values
                car.setY(intY);
                //display values saved in cartesian x, y
                aLabel.setText ("Saved ( " + Integer.toString (car.getX())
                               + " , " + Integer.toString (car.getY())+ " )");

            }
            catch (Exception anError) //no value entered
            {
                aLabel.setText ( "Error: you must enter a number:" );
            }
        } //end of action to get values from textfields
    } //end of save button listener

    //listen to retrieve button for activity
    public class Button2Listener implements ActionListener
    {
        //when there is action
        public void actionPerformed (ActionEvent e)
        {
            //display cartesian x, y values
            aLabel.setText ("Retrieved ( " + Integer.toString (car.getX())
            + " , " + Integer.toString (car.getY())+ " )");
        }
    } //end of retrieve button listener

    //listen to exit button
    public class Button3Listener implements ActionListener
    {
        //do this when exit button clicked
        public void actionPerformed (ActionEvent e)
        {
            System.exit (0); //exit
        }
    } //end exit button listener
}
