// Form to add new phone entry

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

public class NewPhone extends JFrame
   implements ActionListener
{
 GridBagConstraints gbc;
 GridBagLayout gbl;
 JTextField tf1,tf2;
 JRadioButton rindv, rcomp;
 JButton b1,b2;
 PhoneBook pb;
   public NewPhone(PhoneBook pb)
   {
     super("New Phone Entry");
     this.pb = pb;
     JLabel l1 = new JLabel("Name");
     tf1 = new JTextField(20);
     JLabel l2 = new JLabel("Phone No. ");
     tf2 = new JTextField(20);
    
     JLabel l3 = new JLabel("Type");

     rindv = new JRadioButton("Individual");
     rcomp = new JRadioButton("Company");

     ButtonGroup bg = new ButtonGroup();
     bg.add(rindv);
     bg.add(rcomp);
     
     b1= new JButton("Add");
     b1.addActionListener(this);
     b2= new JButton("Exit");
     b2.addActionListener(this);
     
     gbl = new GridBagLayout();
     gbc  = new GridBagConstraints();
     getContentPane().setLayout(gbl);
     displayComponent(l1,0,0,1,1);
     displayComponent(tf1,1,0,2,1);
     displayComponent(l2,0,1,1,1);
     displayComponent(tf2,1,1,2,1);
     displayComponent(l3,0,2,1,1);
     displayComponent(rindv,1,2,1,1);
     displayComponent(rcomp,2,2,1,1);
     displayComponent(b1,1,3,1,1);     
     displayComponent(b2,2,3,1,1);     

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

   } // end of init

   public void actionPerformed(ActionEvent evt)
   {
     if ( evt.getSource() == b2 )
           this.setVisible(false);
        else
     {
      String name,phoneno;
      char ptype;

      name = tf1.getText();
      phoneno = tf2.getText();
      if ( rindv.isSelected())
          ptype  = 'i';
      else
         ptype = 'c';
 
     PhoneEntry pe = new PhoneEntry(name,phoneno,ptype);
     pb.add(pe);
     pb.writeToDisk();
     } // end of else

   }

 public void displayComponent(Component c, int x, int y, int w, int h)
 {
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = w;
    gbc.gridheight = h;
    gbl.setConstraints(c,gbc);
    getContentPane().add(c);
 } // end of displayComponent
 
 
} // end of class