
// BrowseRecGui class
// ==================

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

class BrowseRecGui extends Dialog implements WindowListener, ActionListener
{

	int recNum = 0;
	DirBase dirBase;

	// application window components

	Button	fwdButton, revButton, doneButton;
	TextField fName, sName, telNo;
	Label fNameLabel, sNameLabel, telNoLabel;

	public BrowseRecGui(Frame parent, DirBase db)	// constructor
	{
		super(parent, "Browse Entries", true);

		//resize(300,180);
		setSize(300,180);
		setLayout(new GridLayout(5,2));

		fName = new TextField(10);
		sName = new TextField(10);
		telNo = new TextField(10);

		fNameLabel = new Label ("First name");
		sNameLabel = new Label ("Surname");
		telNoLabel = new Label ("Telephone");

		add(sNameLabel);
		add(sName);
		add(fNameLabel);
		add(fName);
		add(telNoLabel);
		add(telNo);

		revButton = new Button("<<");
		fwdButton = new Button(">>");
		doneButton = new Button("done");
		add(revButton);
		add(fwdButton);
		add(doneButton);

		revButton.addActionListener(this);
		fwdButton.addActionListener(this);
		doneButton.addActionListener(this);
		dirBase = db;
	} // end of constructor


	public void actionPerformed (ActionEvent event)
	{
		DirEntry rec = new DirEntry();
		if (event.getSource() == doneButton)
		{
			setVisible(false);
		}
		else if (event.getSource() == revButton)
		{
			if (recNum != -1)
			{
				recNum--;
				// implement wrapround
				if (recNum < 0)
					recNum = dirBase.size() - 1;
				rec = dirBase.getEntry(recNum);
				load(rec,recNum);
			}
		}
		else if (event.getSource() == fwdButton)
		{
			if (recNum != -1)
			{
				recNum++;
				// implement wrapround
				if (recNum > dirBase.size()-1)
					recNum = 0;
				rec = dirBase.getEntry(recNum);
				load(rec,recNum);
			}
		}
	} // end of method "actionPerformed"



	public void load(DirEntry rec, int rNum)
	// given a "dirEntry", this loads up the graphic display accordingly
	{
		fName.setText(rec.getForename());
		sName.setText(rec.getSurname());
		telNo.setText(Integer.toString(rec.getNumber()));
		recNum = rNum;
	};

	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 BrowseRecGui

