import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BankAccountEditor extends JFrame implements DialogClientInterface, ActionListener {
	private BankAccount model;
	private JTextArea	info;
	
	public BankAccountEditor(String title, BankAccount account){
		super(title);
		
		// store the model
		model = account;
		
		// create a text area and an edit button
		info = new JTextArea();
		JButton editButton = new JButton("Edit Account");
		editButton.addActionListener(this);
		setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		add(info);
		add(editButton);
		
		update();	// fill-in text area with bank account info
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(200, 150);
	}


	//Handle the EDIT button
	public void actionPerformed(ActionEvent e) {
		// Create a new dialog box
		BankAccountDialog dialog =  new BankAccountDialog (this, "Edit Account", true, model);
                      
        System.out.println("About to open the dialog box ...");
        dialog.setVisible(true);	// Open the dialog box
        System.out.println("Dialog box has been closed.");
	}

	// Called by the dialog box, when it closes from an OK button press
	public void dialogFinished(){
		System.out.println("Changes accepted, Account has been changed");
		update();
	}

	// Called by the dialog box, when it closes from a CANCEL button press
	public void dialogCancelled(){
		System.out.println("Changes aborted, Account has not been changed");
		//no need to call update, since nothing has changed
	}
	
	private void update() {
		//update the info text area to reflect the account balance
		info.setText(
			model.getName() + '\n' + model.getAddress() + '\n' +
			model.getPhone() + '\n' + model.getAccountNumber() + '\n' +
			model.getBalance());
	}

	public static void main(String args[]) {
		BankAccount b = new BankAccount("Rob Banks", "279 Lois Lane", "555-1234");
		b.deposit(500);
		
		BankAccountEditor frame = new BankAccountEditor("Testing a DialogBox", b);
        frame.setVisible(true);
	}
}
