/**
 * Use a JFrame window that accept a last name
 * Search an address book for that lastname
 * Get result and display in the window
 * This program also listens to key stroke events
 *
 * This layout uses a BorderLayout Manager with a tool bar pallet
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AddrBookView3 extends JFrame 
                           implements ActionListener, KeyListener, AddrBookView 
{
    private AddrBookControl control;            //A reference to the control object
    private String searchStr;                   //The last name to search for

    JLabel      lastnameLbl = new JLabel("Enter Lastname:  ");
    JTextField  lastname    = new JTextField(15);
    ImageIcon   image       = new ImageIcon("button.gif");
    JButton     search      = new JButton(image);
    JTextArea   result      = new JTextArea(10, 25);    //height, width
    JScrollPane result2     = new JScrollPane(result);  //make it scrollable


    AddrBookView3(AddrBookControl c)               	   //constructor
    {
        this.control = c;
    }

    public void getUserInput()                          //Called by controller object            
    {
        setTitle("Address Book");                       //set title               
        setDefaultCloseOperation(EXIT_ON_CLOSE);        //action when click on X

        Container panel = getContentPane();             //get reference to the content pane

        BorderLayout lay = new BorderLayout();          //create a BorderLayout manager
        panel.setLayout(lay);                           //use it to layout the panel

        JToolBar toolBox = new JToolBar();              //create a tool bar

        toolBox.add(lastnameLbl);                       //add components to tool bar
        toolBox.add(lastname);
        toolBox.add(search);

        result.setEditable(false);                      //cannot edit results

        panel.add("North" ,toolBox);                    //add tool bar to "north"  area
        panel.add("Center",result2);                    //add result   to "center" area

        pack();                                         //shrink them to proper size
        setVisible(true);                               //make window visible

        search.addActionListener(this);                 //add an event handler on button
        lastname.addKeyListener(this);                  //add a key handler on lastname
    }

    public void actionPerformed(ActionEvent evt) 
    {
        searchAddrbook();                               //call searchAddrBook()
    }

    public void keyPressed(KeyEvent evt)
    {
        char key = evt.getKeyChar();                    //get the key pressed
 
        if (key == '\n')                                //if enter key
            searchAddrbook();                           //call searchAddrbook()
    }

    public void keyTyped(KeyEvent evt)    { }           //must implement
    public void keyReleased(KeyEvent evt) { }           //even if empty


    private void searchAddrbook() 
    {
        String lname  = lastname.getText();             //get text from window
 
        control.search(lname);              //call the controller search method
    } 


    public void display(AddrBookEntry[] entry, int numMatches, String msg)    //Called by the controller object
    {
        String str = msg + "\n";

        if (numMatches == 1)                    //if one entry was found
            str = "";                           //no need to display number of matches
                
        for (int i=0; i<numMatches; i++)        //loop and create a proper display
            str += entry[i] + "\n";
                    
        result.setText(str);                    //place formatted text in window                
        result.setCaretPosition(0);             //scroll back to top
    }
}