import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BankAccountDialog extends JDialog {

    private BankAccount account;	// The model

    // The buttons and main panel
    private JButton okButton;
    private JButton cancelButton;
    private BankAccountPanel bankAccountPanel;


    // A constructor that takes the model and client as parameters
	public BankAccountDialog(Frame owner, String title, boolean modal, BankAccount acc){

		// Call the super constructor that does all the work of setting up the dialog
        super(owner,title,modal);

        account = acc; 	// Store the model 

        // Make a panel with two buttons (placed side by side)
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.add(okButton = new JButton("OK")); 
        buttonPanel.add(cancelButton = new JButton("CANCEL")); 
        
        // Make the dialog box by adding the bank account panel and the button panel
        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 
        bankAccountPanel = new BankAccountPanel(account); 
        getContentPane().add(bankAccountPanel); 
		getContentPane().add(buttonPanel); 
		
		// Prevent the window from being resized
		setResizable(false);

        // Listen for ok button click 
        okButton.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent event){ 
					okButtonClicked(); 
				} 
			} 
		); 

		// Listen for cancel button click 
		cancelButton.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent event){ 
					cancelButtonClicked(); 
				} 
			} 
		); 

		// Listen for window closing: treat like cancel button 
		addWindowListener(new WindowAdapter() { 
				public void windowClosing(WindowEvent event) { 
					cancelButtonClicked(); 
				} 
			} 
		);
		
		// Set the size of the dialog box
		setSize(300, 190);
	} 

	private void okButtonClicked(){ 
		// Update model to show changed owner name
		account.setName(bankAccountPanel.getNameTextField().getText());
		account.setAddress(bankAccountPanel.getAddressTextField().getText());
		account.setPhone(bankAccountPanel.getPhoneTextField().getText());
		
		// Tell the client that ok was clicked, in case something needs to be done there
		if (getOwner() != null) 
			((DialogClientInterface)getOwner()).dialogFinished();
		
		dispose();  // destroy this dialog box
	}

	private void cancelButtonClicked(){
		// Tell the client that cancel was clicked, in case something needs to be done there
		if (getOwner() != null)
			((DialogClientInterface)getOwner()).dialogCancelled();
		
		dispose();  // destroy this dialog box
	}
}