import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import javax.swing.border.*;
import java.util.EventObject;
import java.io.*;


public class Introducere extends JDialog { 
  JPanel bottom;
  JButton okButton, cancelButton;
  PanouDeIntroducere dialogPanel;
  String defaultName = ""; 
  String defaultAddress = "";
  String defaultPhone = "";

  String newName; 
  String newAddress;
  String newPhone;
   
  Introducere(JFrame owner) {
    super(owner, true);
    okButton = new JButton("Ok");
    cancelButton = new JButton("Renunta");
    ButtonHandler bHandler = new ButtonHandler();
    okButton.addActionListener(bHandler);
    cancelButton.addActionListener(bHandler);
      
    bottom = new JPanel();
    bottom.add(okButton);
    bottom.add(cancelButton);
    /* Note implicit flow layout */
    bottom.setBorder(BorderFactory.createEtchedBorder());
    dialogPanel = new PanouDeIntroducere();
    getContentPane().setLayout(new BorderLayout());
    /* Dialog is given a Border Layout */
    getContentPane().add(bottom, BorderLayout.SOUTH);
    /* Put bottom panel at base of dialog */ 
    getContentPane().add(dialogPanel, BorderLayout.CENTER); 
  }


  public void setOldFields(String nme, String addr, String ph) { 
    defaultName = nme;
    defaultAddress = addr;
    defaultPhone = ph;
    dialogPanel.setOldFields(nme,addr,ph); 
  }

  public void setFields(String nme, String addr, String ph) {
    newName = nme; 
    newAddress = addr;
    newPhone = ph; 
  }
 
  public String getName() { return newName; } 
  public String getAddress() { return newAddress; }
  public String getPhone() { return newPhone; }
   
  class ButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent ev) {
      JButton button = (JButton) ev.getSource();
      String label = button.getText();
      if ("Ok".equals(label)) {
        setFields(dialogPanel.getName(),
          dialogPanel.getAddress(),
          dialogPanel.getPhone());
      }
      else {
        setFields(null,null,null); 
      }
      dialogPanel.reset();
      setVisible(false); 
    } 
  }
}

class PanouDeIntroducere extends JPanel { 
  JLabel nameLabel; 
  JTextField nameField; 
  JLabel addressLabel;
  JTextField addressField;  
  JLabel phoneLabel;
  JTextField phoneField;  

  PanouDeIntroducere() {
    nameLabel = new JLabel("Name:"); 
    nameField = new JTextField(); 
    addressLabel = new JLabel("Address:");
    addressField = new JTextField();
    phoneLabel = new JLabel("Phone:");
    phoneField = new JTextField(); 
    setBorder(BorderFactory.createTitledBorder("Campurile inregistrarii")); 
    add(nameLabel); 
    add(nameField); 
    add(addressLabel);
    add(addressField);
    add(phoneLabel);
    add(phoneField); 
  }

  public void setOldFields(String nme, String addr, String ph) {
    nameField.setText(nme); 
    addressField.setText(addr);
    phoneField.setText(ph); 
  }

  public Dimension getPreferredSize() {
    return new Dimension(400,120); 
  }

  public Dimension getMinimumSize() {
    return new Dimension(400,120); 
  }

  public void doLayout() {
    nameLabel.setBounds(10,10,90,30);
    nameField.setBounds(100,15,270,20);
    addressLabel.setBounds(10,40,90,30);
    addressField.setBounds(100,45,270,20);
    phoneLabel.setBounds(10,70,90,30);
    phoneField.setBounds(100,75,270,20); 
  }

  
  public void reset() {
    nameField.setText(""); 
    addressField.setText("");
    phoneField.setText(""); 
  }

  public String getName() {
    return nameField.getText(); 
  } 

  public String getAddress() {
    return addressField.getText(); 
  }

  public String getPhone() {
    return phoneField.getText();
  }
  
}

