/**
 * 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 GridBagLayout Manager 
 *
 * This program respond to every keystroke
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AddrBookView4 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(20);
    ImageIcon   image       = new ImageIcon("button.gif");
    JButton     search      = new JButton(image);
    JTextArea   result      = new JTextArea(15, 55);    //height, width
    JScrollPane result2     = new JScrollPane(result);  //make it scrollable


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

    public void getUserInput()                          //Called by controller object            
    {
        JFrame frame = new JFrame();                            //create a new frame   

        frame.setTitle("Address Book - by keystrokes");         //set title               
        frame.setSize(640,300);                                 //set width, height
        frame.setLocationRelativeTo(null);                      //set location to center of screen
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);    //action when click on X

        GridBagLayout      mgr    = new GridBagLayout();        //create a GridBagLayout manager
        GridBagConstraints layout = new GridBagConstraints();   //create a constraints object 

        JPanel panel = new JPanel();                            //create a new panel
        panel.setLayout(mgr);                                   //use it to layout the panel

        Insets padding = new Insets(3,3,3,3);
        layout.insets  = padding;

        layout.gridy   = 0;                                     //row position
        layout.gridx   = 0;                                     //column position         
        layout.weightx = 0;       
        layout.fill    = GridBagConstraints.NONE;               //no fill
        panel.add(lastnameLbl, layout);

        layout.gridy   = 0;
        layout.gridx   = 1; 
        layout.weightx = 1;                                     //take all remaining space in that row       
        layout.fill    = GridBagConstraints.HORIZONTAL;         //stretch horizontally
        panel.add(lastname, layout);

        layout.gridy   = 0;
        layout.gridx   = 2; 
        layout.weightx = 0;       
        layout.fill    = GridBagConstraints.NONE;               //reset to no fill
        panel.add(search, layout);
        
//      layout.gridy     = 1;
//      layout.gridx     = 0;
//      layout.gridwidth = 3;                                   //3 column wide       
//      layout.weightx   = 1;                                   //take all remaining space       
//      layout.weighty   = 1;       
//      layout.fill      = GridBagConstraints.BOTH;             //stretch both vertically and horizontally
//      panel.add(result2, layout);
 
        frame.add(panel, BorderLayout.NORTH);                   //add the above panel to the frame
        frame.add(result2);                                     //add the result area to the frame
        
        result.setEditable(false);                      //cannot edit results

        frame.setVisible(true);                         //make window visible

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

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

    public void keyReleased(KeyEvent evt)
    {
        char key = evt.getKeyChar();                    //get the key released (do not use keyPressed or keyTyped)
                                                        //keyPressed and keyTyped fire too early to obtain text content

        searchAddrbook();                               //call searchAddrbook() on every keystroke
    }

    public void keyPressed(KeyEvent evt) { }           //must implement
    public void keyTyped(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
    }
}