
// DirGui class
// ============

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class DirGui extends Frame implements WindowListener, ActionListener
{

	// link to directory component
	DirBase dirBase;

	// application window components

	Button newButton, doneButton;
	Label findLabel = new Label("Find : ");
	TextField sNameField;
	String sName;

	BrowseRecGui myBrowseRecGui ;
	NewRecGui myNewRecGui ;

	public DirGui(DirBase db) // constructor
	{
		super("Telephone Directory");
		dirBase = db;
		setup();
	}

	public void setup()
	// setup the window for the application
	{
		//resize(300,100);
		setSize(300,100);
		setLayout(new GridLayout(2,2));

		sNameField = new TextField(10);
		newButton = new Button("new");
		doneButton = new Button("done");

		add(findLabel);
		add(sNameField);
		add(newButton);
		add(doneButton);

		sNameField.addActionListener(this);
		newButton.addActionListener(this);
		doneButton.addActionListener(this);

		myBrowseRecGui = new BrowseRecGui(this, dirBase);
		myNewRecGui = new NewRecGui(dirBase);
	} // end of method "setup"


	public void actionPerformed (ActionEvent event)
	{
		DirEntry rec;
		int recNum;

		if (event.getSource() == doneButton)
		{
			setVisible(false);
		} else if (event.getSource() == newButton)
		{
			myNewRecGui.show();
		} else if (event.getSource() == sNameField)
		{
			sName = new String(sNameField.getText());
			System.out.println("request to find "+sName);
			recNum = dirBase.contains(sName);
			if (recNum != -1)
			{
				rec = dirBase.find(sName);
				System.out.println(rec);
				myBrowseRecGui.load(rec,recNum);
				myBrowseRecGui.show();
			} else	{
				rec = new DirEntry();
				rec.clear();
				myBrowseRecGui.load(rec,-1);
				myBrowseRecGui.show();
			};
		}
	} // end of method "actionPerformed"


	public void windowOpened(WindowEvent event) {}
        public void windowIconified(WindowEvent event) {}
        public void windowDeiconified(WindowEvent event) {}
        public void windowClosed(WindowEvent event) {}
        public void windowActivated(WindowEvent event) {}
        public void windowDeactivated(WindowEvent event) {}

	public void windowClosing(WindowEvent event)
	{
	dispose();
	} // end of method "windowClosing"

} // end of class DirGui

