import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class BuddyDetailsDialog extends JDialog {

	// This is a pointer to the email buddy that is being edited
	private EmailBuddy aBuddy;

	// These are the components of the dialog box
	private JLabel					aLabel;
	private JTextField				nameField;
	private JTextField				addressField;
	private JCheckBox				hotListButton;
	private JButton					okButton;
	private JButton					cancelButton;


	public BuddyDetailsDialog(Frame owner, String title, boolean modal, EmailBuddy bud){
		super(owner,title,modal);

		// Store these parameters into the instance variables
		aBuddy = bud;

		// Put all the components onto the window and given them initial values
		buildDialogWindow(aBuddy);

		// Add listeners for the Ok and Cancel buttons as well as window closing
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event){
				okButtonClicked();
			}});

		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event){
				cancelButtonClicked();
			}});

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				cancelButtonClicked();
			}});

		setSize(526, 214);
	}

	// This code adds the necessary components to the interface
	private void buildDialogWindow(EmailBuddy aBuddy) {
		// Use no layout
		setLayout(null);

		// Add the name label
		aLabel = new JLabel("Name:");
		aLabel.setLocation(10,10);
		aLabel.setSize(80, 30);
		add(aLabel);

		// Add the name field
		nameField = new JTextField(aBuddy.getName());
		nameField.setLocation(110, 10);
		nameField.setSize(400, 30);
		add(nameField);

		// Add the address label
		aLabel = new JLabel("Address:");
		aLabel.setHorizontalAlignment(JLabel.LEFT);
		aLabel.setLocation(10,50);
		aLabel.setSize(80, 30);
		add(aLabel);

		// Add the address field
		addressField = new JTextField(aBuddy.getAddress());
		addressField.setLocation(110, 50);
		addressField.setSize(400, 30);
		add(addressField);

		// Add the onHotList button
		hotListButton = new JCheckBox("On Hot List");
		hotListButton.setSelected(aBuddy.onHotList());
		hotListButton.setLocation(110, 100);
		hotListButton.setSize(120, 30);
		add(hotListButton);

		// Add the Ok button
		okButton = new JButton("Ok");
		okButton.setLocation(300, 130);
		okButton.setSize(100, 40);
		add(okButton);

		// Add the Cancel button
		cancelButton = new JButton("Cancel");
		cancelButton.setLocation(410, 130);
		cancelButton.setSize(100, 40);
		add(cancelButton);
	}


	private void okButtonClicked(){
		aBuddy.setName(nameField.getText());
		aBuddy.setAddress(addressField.getText());
		aBuddy.onHotList(hotListButton.isSelected());
		if (getOwner() != null)
			((DialogClientInterface)getOwner()).dialogFinished();
		dispose();
	}

	private void cancelButtonClicked(){
		if (getOwner() != null)
			((DialogClientInterface)getOwner()).dialogCancelled();
		dispose();
	}
}