/**
 * 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
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AddrBookView2 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);
    JButton     search      = new JButton("Search");
    JTextArea   result      = new JTextArea(10, 30);    //height, width
    JScrollPane result2     = new JScrollPane(result);  //make it scrollable


    AddrBookView2(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

        Box topBox = Box.createHorizontalBox();         //create a box container 

        topBox.add(lastnameLbl);                        //add components to top box
        topBox.add(lastname);
        topBox.add(search);

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

        panel.add("North" ,topBox);                     //add top box to "north"  area
        panel.add("Center",result2);                    //add result  to "center" area

        pack();                                         //resize components apporiately
        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
    }
}