// Form to search for phones
import  java.awt.*;
import  java.awt.event.*;
import  javax.swing.*;


public class SearchPhone extends JFrame
   implements ActionListener 
{

 JTextField tfName;
 JButton b1;
 PhoneBook pb;
 JTextArea taResult;
 JCheckBox cbIgnoreCase;

   public SearchPhone(PhoneBook pb)
   {
     super("Search Phones");
     this.pb = pb;
     JLabel l1 = new JLabel("Name");
     tfName = new JTextField(20);  
     taResult = new JTextArea();

     b1= new JButton("Go");
     b1.addActionListener(this);

     cbIgnoreCase = new JCheckBox("Ignore Case");

     Container c = getContentPane();
     JPanel p = new JPanel();
     p.setLayout( new FlowLayout());

     p.add( l1);
     p.add(tfName);
     p.add(cbIgnoreCase);
     p.add(b1);

     c.add(p,"North");

     JScrollPane sp = new JScrollPane(taResult,
                             JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                             JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	
     c.add(sp,"Center");

     setSize(500,200);
     setVisible(true);

     this.addWindowListener( new WinCode());

   } // end of init

   class WinCode extends WindowAdapter
   {
    public void windowClosing(WindowEvent evt) {  setVisible(false); }
   }


   public void actionPerformed(ActionEvent evt)
   {
     if ( evt.getSource() == b1 )  
     {
       if ( cbIgnoreCase.isSelected() )
         taResult.setText(pb.getSelectedEntries(tfName.getText(),true));
       else
         taResult.setText(pb.getSelectedEntries(tfName.getText(), false));
     }

  }

  public static void main(String args[])
  {
    PhoneBook pb = new PhoneBook();
    pb.readFromDisk();
    SearchPhone sp = new SearchPhone(pb);
  } 
 
} // end of class