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

public class EmailBuddyApp extends JFrame implements DialogClientInterface {

	// Store the model as a vector of email buddies
	private Vector			buddies;
	private EmailBuddy		selectedBuddy;
	private boolean			inAddMode;

	// Store the view that contains the components
	EmailBuddyPanel 		view;

	// Here are the component listeners
	ActionListener			theAddButtonListener;
	ActionListener			theRemoveButtonListener;
	ActionListener			hotListListener;
	ListSelectionListener	buddyListSelectionListener;
	MouseListener			doubleClickSelectionListener;

	// Here is the default constructor
	public EmailBuddyApp(String title) {
		super(title);

		buddies = new Vector();
		selectedBuddy = null;
		inAddMode = false;

		// Make a new viewing panel and add it to the pane
		add(view = new EmailBuddyPanel());

		// Add a listener for the add button
		theAddButtonListener = new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				addBuddy();
			}};

		// Add a listener for the revove button
		theRemoveButtonListener = new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				removeBuddy();
			}};

		// Add a listener for the hot list button
		hotListListener = new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				toggleHotList();
			}};

		// Add a listener to allow selection of buddies from the list
		buddyListSelectionListener = new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent event) {
				selectBuddy();
			}};

		// Add a listener to allow double click selections from the list for editing
		doubleClickSelectionListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent event) {
				if (event.getClickCount() == 2)
					editBuddy();
			}};

        setDefaultCloseOperation(EXIT_ON_CLOSE);                      
		setSize(600,300);

		// Start off with everything updated properly
		update();
	}

	// Enable all listeners
	private void enableListeners() {
		view.getAddButton().addActionListener(theAddButtonListener);
		view.getRemoveButton().addActionListener(theRemoveButtonListener);
		view.getHotListButton().addActionListener(hotListListener);
		view.getBuddyList().addListSelectionListener(buddyListSelectionListener);
		view.getBuddyList().addMouseListener(doubleClickSelectionListener);
	}

	// Disable all listeners
	private void disableListeners() {
		view.getAddButton().removeActionListener(theAddButtonListener);
		view.getRemoveButton().removeActionListener(theRemoveButtonListener);
		view.getHotListButton().removeActionListener(hotListListener);
		view.getBuddyList().removeListSelectionListener(buddyListSelectionListener);
		view.getBuddyList().removeMouseListener(doubleClickSelectionListener);
	}


	// This is called when the user clicks the add button
	private void addBuddy() {
		EmailBuddy		aBuddy = new EmailBuddy();

		// Add the buddy to the end of the Vector
		buddies.addElement(aBuddy);
		selectedBuddy = aBuddy;
		inAddMode = true;

		// Now bring up the dialog box
		BuddyDetailsDialog dialog = new BuddyDetailsDialog(this, "Buddy Details Dialog", true, aBuddy);         
		dialog.setVisible(true);
	}

	// This is called when the user clicks the remove button
	private void removeBuddy() {
		// Remove the currently selected buddy from the list
		EmailBuddy aBuddy = (EmailBuddy)(view.getBuddyList().getSelectedValue());

		if (aBuddy != null) {
			buddies.remove(aBuddy);
			update();
			selectBuddy();
		}
	}

	// This is called when the user selects a buddy from the list
	private void selectBuddy() {
		selectedBuddy = (EmailBuddy)(view.getBuddyList().getSelectedValue());

		update();
	}

	// This is called when the user double clicks on an item in the list
	private void editBuddy() {
		//selectedBuddy = (EmailBuddy)(view.getBuddyList().getSelectedValue());

		// Now bring up the dialog box
		inAddMode = false;
		if (selectedBuddy == null) return;
		
		BuddyDetailsDialog dialog = new BuddyDetailsDialog(this, "Buddy Details Dialog", true, selectedBuddy);         
		dialog.setVisible(true);
	}

	// This is called when the user clicks the hot list button
	private void toggleHotList() {
		// toggle the hot list
		update();
	}

	// Called when the dialog box is closed with the Ok button
	public void dialogFinished() {
		update();
	}

	// Called when the dialog box is closed with the cancel button or manually closed
	public void dialogCancelled() {
		if (inAddMode) {
			// Remove the latest buddy that was added if in add mode
			buddies.remove(buddies.get(buddies.size()-1));
			selectedBuddy = null;
		}
		inAddMode = false;
		update();
	}


	// Update the remove button 
	private void updateRemove() {
		view.getRemoveButton().setEnabled(selectedBuddy != null);
	}

	// Update the list
	private void updateList() {
		boolean		foundSelected = false;

		// If the hot list is on, find all buddies that are on the hot list
		if (view.getHotListButton().isSelected()) {
			Vector	temp = new Vector();

			for (int i=0; i<buddies.size(); i++) {
				EmailBuddy aBuddy = (EmailBuddy)buddies.get(i);
				if (aBuddy.onHotList()) {
					temp.add(aBuddy);
					if (aBuddy == selectedBuddy)
						foundSelected = true;
				}

			}
			view.getBuddyList().setListData(temp);
			if (!foundSelected)
				selectedBuddy = null;
		}
		else
			view.getBuddyList().setListData(buddies);

		if (selectedBuddy != null)
			view.getBuddyList().setSelectedValue(selectedBuddy, true);
	}

	// Update the components
	private void update() {
		disableListeners();
		updateList();
		updateRemove();
		enableListeners();
	}

	// Code that starts the application
	public static void main(String args[]) {
		EmailBuddyApp frame =  new EmailBuddyApp("Email Buddy Application");
		frame.setVisible(true);
	}
}