// Main Frame for Phone Book

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

public class MainFrame extends JFrame
     implements ActionListener
{
   JPanel toppanel =new JPanel();
   PhoneBook pb = new PhoneBook();
   JMenu mnuAction, mnuReport;
   JMenuItem optNewPhone,
             optDeletePhone,
             optUpdatePhone, 
	     optExit,
             optPhoneList,
             optSearch;

   JButton  btnNewPhone, btnDeletePhone, btnUpdatePhone, btnPhoneList;

   public MainFrame()
   {
      super("Phone Book");
      pb.readFromDisk();
      addMenu();
      addToolbar();
      setVisible(true);
      setSize(600,400);
     
   } // end of constructor

   public void addMenu()
   {
     JMenuBar mb = new JMenuBar();
      
      mnuAction = new JMenu("Action");
      mnuReport = new JMenu("Report");

      optNewPhone = new JMenuItem("New Phone Entry...");
      optDeletePhone = new JMenuItem("Delete Phone Entry...");
      optUpdatePhone = new JMenuItem("Update Phone Entry...");
      optUpdatePhone.addActionListener(this);
      optExit = new JMenuItem("Exit");

      

      optSearch = new JMenuItem("Search...");
      optSearch.addActionListener(this);

      optPhoneList = new JMenuItem("Phones List");
      optPhoneList.addActionListener(this);
      optExit.addActionListener(this);
      optNewPhone.addActionListener(this);

      mnuAction.add(optNewPhone);
      mnuAction.add(optDeletePhone);
      mnuAction.add(optUpdatePhone);
      mnuAction.addSeparator();
      mnuAction.add(optExit);

      mnuReport.add(optPhoneList);
      mnuReport.add(optSearch);

      mb.add(mnuAction);
      mb.add(mnuReport);

      Container c = getContentPane();
      c.setLayout( new BorderLayout());
  
      toppanel = new JPanel();
      toppanel.setLayout( new GridLayout(2,1));    
      toppanel.add(mb);

      c.add(toppanel,"North");
           
  } // end of addMenu  

  public void addToolbar()
  {
     
   // load images from disk
   Icon  img1 = new ImageIcon("new.gif");
   Icon  img2 = new ImageIcon("cut.gif");
   Icon  img3 = new ImageIcon("open.gif");

   // create command buttons and assign images to buttons
   btnNewPhone = new JButton(img1);
   btnNewPhone.addActionListener(this);

   btnDeletePhone = new JButton(img2);
   btnDeletePhone.addActionListener(this);

   btnUpdatePhone = new JButton(img3);
   btnUpdatePhone.addActionListener(this);


   // create new toolbar
   JToolBar  tb = new JToolBar();
   // add buttons to toolbar
   tb.add(btnNewPhone);
   tb.add(btnDeletePhone);
   tb.add(btnUpdatePhone);

   toppanel.add(tb);  
  
 } 
  
  public void actionPerformed(ActionEvent evt)
  {
    Object source = evt.getSource();
     if ( source == optExit)
           exitProgram();
     else
        if (source == optNewPhone || source == btnNewPhone)
        {
           NewPhone nf = new NewPhone(pb);
        }
       else
         if ( source == optPhoneList)
          {
            ListPhones lf = new ListPhones(pb);
           }
         else
          if ( source == optSearch)
          {   SearchPhone sf = new SearchPhone(pb); }

  }
  
  public void exitProgram()
  {
     int r= JOptionPane.showConfirmDialog(this,  
            "Do you want to Quit?",
            "Exit",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE);

     if ( r == JOptionPane.YES_OPTION)
            System.exit(0);
  } 
                            
  
  public static void main(String args[])
  {
    MainFrame mf = new MainFrame();
  }

}  // end of class