package simplemanager;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import soft_master.codebox.DM;
import java.awt.HeadlessException;
import com.borland.jbcl.layout.*;
import com.borland.dbswing.*;

/**
 * <p>Title: Example</p>
 * <p>Description: A default JBuilder application</p>
 * <p>Copyright: Copyright (c) 2008</p>
 * <p>Company: EscortMaster</p>
 * @author Sanchess
 * @version 1.0
 */

public class EditClient extends JDialog implements ActionListener {
   private String fName, lName, ftrName;
   private DialogState dlgState = DialogState.FIRST_LAST_FATHER;
   private DM dMod;
   MainListener listen = new MainListener(this);
   JLabel lb_LastName = new JLabel("Фамилия"),
            lb_FirstName = new JLabel("Имя"),
            lb_FatherName = new JLabel("Отчество");
   JTextField fld_LastName = new JTextField(),
              fld_FirstName = new JTextField(),
              fld_FatherName = new JTextField();
   JButton btn_OK = new JButton("OK"),
           btn_Cancel = new JButton("Cancel");
   JRadioButton btn_LastName = new JRadioButton("Фамилия"),
                btn_FirstName = new JRadioButton("Имя"),
                btn_FatherName = new JRadioButton("Отчество"),
                btn_FIO = new JRadioButton("ФИО");
   ButtonGroup btn_Group = new ButtonGroup();
   GridBagLayout mainLayout = new GridBagLayout();

   public EditClient(JFrame parent, DM dMod) throws HeadlessException {
      super(parent);
      this.fName = dMod.getFirstName();
      this.lName = dMod.getLastName();
      this.ftrName = dMod.getFatherName();
      this.dMod = dMod;
      enableEvents(AWTEvent.WINDOW_EVENT_MASK);
      try {
         jbInit();
      }
      catch (Exception except) {
         except.printStackTrace();
      }
      fld_FirstName.setText(fName);
      fld_LastName.setText(lName);
      fld_FatherName.setText(ftrName);
   }

   private void jbInit() throws Exception {
      setSize(400, 220);
      setTitle("Добавить/Редактировать клиента");
      this.getContentPane().setLayout(mainLayout);
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension frameSize = this.getSize();
      if (frameSize.height > screenSize.height) {
         frameSize.height = screenSize.height;
      }
      if (frameSize.width > screenSize.width) {
         frameSize.width = screenSize.width;
      }
      this.setLocation( (screenSize.width - frameSize.width) / 2,
                       (screenSize.height - frameSize.height) / 2);
      lb_LastName.setFont(new java.awt.Font("Dialog", 1, 12));
      lb_FirstName.setFont(new java.awt.Font("Dialog", 1, 12));
      lb_FatherName.setFont(new java.awt.Font("Dialog", 1, 12));
      btn_Cancel.addActionListener(this);
      btn_OK.addActionListener(this);
      btn_LastName.addItemListener(listen);
      btn_FirstName.addItemListener(listen);
      btn_FatherName.addItemListener(listen);
      btn_FIO.addItemListener(listen);
      fld_LastName.setFont(new java.awt.Font("Dialog", 1, 14));
      fld_FirstName.setFont(new java.awt.Font("Dialog", 1, 14));
      fld_FatherName.setFont(new java.awt.Font("Dialog", 1, 14));
      this.getContentPane().add(lb_LastName,
                                new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
          , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
          new Insets(10, 21, 0, 95), 2, 0));
      this.getContentPane().add(fld_LastName,
                                   new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 21, 0, 0), 14, 0));
      this.getContentPane().add(lb_FirstName,
                                new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
          , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
          new Insets(0, 21, 0, 118), 11, 0));
      this.getContentPane().add(fld_FirstName,
                                   new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 21, 0, 0), 14, 0));
      this.getContentPane().add(lb_FatherName,
                                new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0
          , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
          new Insets(0, 21, 0, 97), 2, 0));
      this.getContentPane().add(fld_FatherName,
                                new GridBagConstraints(0, 5, 1, 1, 1.0, 0.0
          , GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
          new Insets(0, 21, 0, 0), 14, 0));
      this.getContentPane().add(btn_OK,
                                new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
          , GridBagConstraints.CENTER, GridBagConstraints.NONE,
          new Insets(8, 31, 0, 23), 22, -3));
      this.getContentPane().add(btn_Cancel,
                                new GridBagConstraints(2, 2, 1, 2, 0.0, 0.0
          , GridBagConstraints.CENTER, GridBagConstraints.NONE,
          new Insets(8, 31, 0, 23), 0, -3));
      this.getContentPane().add(btn_FatherName,
                                new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0
          , GridBagConstraints.CENTER, GridBagConstraints.NONE,
          new Insets(0, 20, 0, 0), 1, 0));
      this.getContentPane().add(btn_FirstName,
                                new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
          , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
          new Insets(0, 20, 0, 30), 0, 0));
      this.getContentPane().add(btn_LastName,
                                new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
          , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
          new Insets(0, 20, 0, 0), 0, 0));
      this.getContentPane().add(btn_FIO,    new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 1, 0));
      btn_Group.add(btn_LastName);
      btn_Group.add(btn_FirstName);
      btn_Group.add(btn_FatherName);
      btn_Group.add(btn_FIO);
      pack();
   }

   protected void processWindowEvent(WindowEvent e) {
      if (e.getID() == WindowEvent.WINDOW_CLOSING) {
         cancel();
      }
      super.processWindowEvent(e);
   }

   void cancel() {
      this.dispose();
   }

   public String getFirst() {
      return fld_FirstName.getText();
   }

   public String getLast() {
      return fld_LastName.getText();
   }

   public String getFather() {
      return fld_FatherName.getText();
   }

   public void actionPerformed(ActionEvent e) {
      if (e.getSource() == btn_Cancel) {
         cancel();
      }
      else {
         if (dlgState == DialogState.FIRST_ONLY) {
            dMod.setFirstName(getFirst());
         } else if (dlgState == DialogState.LAST_ONLY) {
            dMod.setLastName(getLast());
         } else if (dlgState == DialogState.FATHER_ONLY) {
            dMod.setFatherName(getFather());
         } else if (dlgState == DialogState.FIRST_LAST_FATHER) {
            dMod.setFIO(getFirst(), getLast(), getFather());
         } else cancel();
      }
   }

   private void configureState() {
      if (dlgState == DialogState.FIRST_ONLY) {
         fld_FirstName.setEnabled(true);
         lb_FirstName.setEnabled(true);
         fld_LastName.setEnabled(false);
         fld_FatherName.setEnabled(false);
         lb_FatherName.setEnabled(false);
         lb_LastName.setEnabled(false);
      } else if (dlgState == DialogState.LAST_ONLY) {
         fld_LastName.setEnabled(true);
         lb_LastName.setEnabled(true);
         fld_FirstName.setEnabled(false);
         fld_FatherName.setEnabled(false);
         lb_FatherName.setEnabled(false);
         lb_FirstName.setEnabled(false);
      } else if (dlgState == DialogState.FATHER_ONLY) {
         fld_FatherName.setEnabled(true);
         lb_FatherName.setEnabled(true);
         fld_LastName.setEnabled(false);
         fld_FirstName.setEnabled(false);
         lb_LastName.setEnabled(false);
         lb_FirstName.setEnabled(false);
      } else if (dlgState == DialogState.FIRST_LAST_FATHER) {
         fld_FirstName.setEnabled(true);
         fld_LastName.setEnabled(true);
         fld_FatherName.setEnabled(true);
         lb_FirstName.setEnabled(true);
         lb_LastName.setEnabled(true);
         lb_FatherName.setEnabled(true);
      }
   }

   private final static class DialogState {
      private final String name;
      private DialogState(String name) {
         this.name = name;
      }

      public String toString() {
         return name;
      }

      static final DialogState FIRST_ONLY = new DialogState("FIRST_ONLY");
      static final DialogState LAST_ONLY = new DialogState("LAST_ONLY");
      static final DialogState FATHER_ONLY = new DialogState("FATHER_ONLY");
      static final DialogState FIRST_LAST_FATHER = new DialogState("FIRST_LAST_FATHER");
   }

   public class MainListener implements ItemListener {
      EditClient adaptee;
      MainListener (EditClient adaptee) {
          this.adaptee = adaptee;
      }
      public void itemStateChanged(ItemEvent e) {
         if (e.getSource() == adaptee.btn_LastName && e.getStateChange() == ItemEvent.SELECTED) {
            dlgState = DialogState.LAST_ONLY;
            configureState();
         } else if (e.getSource() == adaptee.btn_FirstName && e.getStateChange() == ItemEvent.SELECTED) {
            dlgState = DialogState.FIRST_ONLY;
            configureState();
         } else if (e.getSource() == adaptee.btn_FatherName && e.getStateChange() == ItemEvent.SELECTED) {
            dlgState = DialogState.FATHER_ONLY;
            configureState();
         } else if (e.getSource() == adaptee.btn_FIO && e.getStateChange() == ItemEvent.SELECTED) {
            dlgState = DialogState.FIRST_LAST_FATHER;
            configureState();
         }
      }
   }
}
