// Form to delete phone entry

import  java.awt.*;
import  java.awt.event.*;
import  javax.swing.*;

public class DeletePhone extends JFrame  implements ActionListener
{
 JButton b1,b2;
 PhoneBook pb;
 JComboBox jcbPhones;

   public DeletePhone(PhoneBook pb)
   {
     super("Delete Phone Entry");
     this.pb = pb;
     JLabel l1 = new JLabel("Name");
     jcbPhones = new JComboBox(pb.getNames());
     b1= new JButton("Delete");
     b1.addActionListener(this);
     b2= new JButton("Exit");
     b2.addActionListener(this);
     JPanel p1 = new JPanel();
     p1.setLayout( new FlowLayout());

     JPanel p2 = new JPanel();
     p2.setLayout( new FlowLayout());
     Container c =   getContentPane();
     c.setLayout( new BorderLayout());
     p1.add(l1);
     p1.add(jcbPhones);
 
     p2.add(b1); 
     p2.add(b2);

     c.add(p1,"North");
     c.add(p2,"South");
       
     
   setSize(300,200);
   setVisible(true);
  } // end of constructor

   public void actionPerformed(ActionEvent evt)
   {
     if ( evt.getSource() == b2 )
           setVisible(false);
     else  // delete
     {
      String name;

      name = (String)jcbPhones.getSelectedItem();
      int res  = JOptionPane.showConfirmDialog(this,"Do you want to delete?",
          "Delete",
          JOptionPane.YES_NO_OPTION,
          JOptionPane.QUESTION_MESSAGE);
      if ( res == JOptionPane.YES_OPTION)
      {  pb.delete(name);
         pb.writeToDisk();
         jcbPhones = new JComboBox(pb.getNames());
      }
    } // end of else

  }

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