/*
 * CadastroClientesForm.java
 *
 * Created on 27 de Maio de 2006, 08:42
 */

package projeto;

import javax.swing.JOptionPane;

/**
 *
 * @author  awolf
 */
public class CadastroClientesForm extends javax.swing.JDialog {
    
    public Pessoas cadastro;
    
    /** Creates new form CadastroClientesForm */
    public CadastroClientesForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setSize(600,172);
        setTitle("Cadastro Clientes");
        
        cadastro = new Pessoas();
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel5 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        teCodigo = new javax.swing.JTextField();
        teNome = new javax.swing.JTextField();
        teFone = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        btFechar = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        btIncluir = new javax.swing.JButton();
        btAlterar = new javax.swing.JButton();
        btExcluir = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        btRecuar = new javax.swing.JButton();
        btAvancar = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        btPrimeiro = new javax.swing.JButton();
        btUltimo = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.BorderLayout(5, 0));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel9.setLayout(new java.awt.GridLayout(3, 1, 0, 9));

        jLabel1.setText("C\u00f3digo");
        jPanel9.add(jLabel1);

        jLabel2.setText("Nome");
        jPanel9.add(jLabel2);

        jLabel3.setText("Fone");
        jPanel9.add(jLabel3);

        jPanel1.add(jPanel9, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.WEST);

        jPanel2.setLayout(new java.awt.GridLayout());

        jPanel10.setLayout(new java.awt.GridLayout(3, 1, 0, 2));

        teCodigo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                Digitos(evt);
            }
        });

        jPanel10.add(teCodigo);

        jPanel10.add(teNome);

        jPanel10.add(teFone);

        jPanel2.add(jPanel10);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel4.setText("Informa\u00e7\u00f5es Cadastrais");
        jPanel3.add(jLabel4);

        getContentPane().add(jPanel3, java.awt.BorderLayout.NORTH);

        jPanel4.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        btFechar.setMnemonic('F');
        btFechar.setText("Fechar");
        btFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btFecharActionPerformed(evt);
            }
        });

        jPanel4.add(btFechar);

        getContentPane().add(jPanel4, java.awt.BorderLayout.SOUTH);

        jPanel6.setLayout(new java.awt.GridLayout(5, 1));

        btIncluir.setMnemonic('I');
        btIncluir.setText("Incluir");
        btIncluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btIncluirActionPerformed(evt);
            }
        });

        jPanel6.add(btIncluir);

        btAlterar.setMnemonic('A');
        btAlterar.setText("Alterar");
        btAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarActionPerformed(evt);
            }
        });

        jPanel6.add(btAlterar);

        btExcluir.setText("Excluir");
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });

        jPanel6.add(btExcluir);

        jPanel7.setLayout(new java.awt.GridLayout(1, 2));

        btRecuar.setText("<");
        btRecuar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRecuarActionPerformed(evt);
            }
        });

        jPanel7.add(btRecuar);

        btAvancar.setText(">");
        btAvancar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAvancarActionPerformed(evt);
            }
        });

        jPanel7.add(btAvancar);

        jPanel6.add(jPanel7);

        jPanel8.setLayout(new java.awt.GridLayout(1, 2));

        btPrimeiro.setText("<<");
        btPrimeiro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPrimeiroActionPerformed(evt);
            }
        });

        jPanel8.add(btPrimeiro);

        btUltimo.setText(">>");
        btUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btUltimoActionPerformed(evt);
            }
        });

        jPanel8.add(btUltimo);

        jPanel6.add(jPanel8);

        getContentPane().add(jPanel6, java.awt.BorderLayout.EAST);

        pack();
    }
    // </editor-fold>//GEN-END:initComponents
    
    private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarActionPerformed
        RegistraCadastro();
        cadastro.Altera();
    }//GEN-LAST:event_btAlterarActionPerformed
    
    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
        cadastro.Exclui();
        MostraCadastro();
    }//GEN-LAST:event_btExcluirActionPerformed
    
    private void btUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btUltimoActionPerformed
        cadastro.Ultimo();
        MostraCadastro();
    }//GEN-LAST:event_btUltimoActionPerformed
    
    private void btPrimeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPrimeiroActionPerformed
        cadastro.Primeiro();
        MostraCadastro();
    }//GEN-LAST:event_btPrimeiroActionPerformed
    
    private void MostraCadastro() {
        teCodigo.setText(cadastro.getCodigo());
        teNome.setText(cadastro.getNome());
        teFone.setText(cadastro.getFone());
    }
    private void btRecuarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRecuarActionPerformed
        cadastro.Retrocede();
        MostraCadastro();
    }//GEN-LAST:event_btRecuarActionPerformed
    
    private void btAvancarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAvancarActionPerformed
        cadastro.Avanca();
        MostraCadastro();
    }//GEN-LAST:event_btAvancarActionPerformed
    
    private void RegistraCadastro(){
        cadastro.setCodigo(teCodigo.getText());
        cadastro.setNome(teNome.getText());
        cadastro.setFone(teFone.getText());
    }
    
    private void btIncluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btIncluirActionPerformed
        // if (teCodigo.getText().length() < 1 )
        if (teCodigo.getText().equals("")) {
            JOptionPane.showMessageDialog(
                    this,
                    "Código Inválido",
                    "Aviso",
                    JOptionPane.INFORMATION_MESSAGE );
        } else{
            if(!cadastro.TemCodigo(teCodigo.getText())){
                RegistraCadastro();
                cadastro.Insere();
            } else {
                JOptionPane.showMessageDialog(
                        this,
                        "Código já Cadastrado",
                        "Aviso",
                        JOptionPane.INFORMATION_MESSAGE );
            }
        }
    }//GEN-LAST:event_btIncluirActionPerformed
    
    private void Digitos(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_Digitos
        
    }//GEN-LAST:event_Digitos
    
    private void btFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btFecharActionPerformed
        dispose();
    }//GEN-LAST:event_btFecharActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CadastroClientesForm(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAlterar;
    private javax.swing.JButton btAvancar;
    private javax.swing.JButton btExcluir;
    private javax.swing.JButton btFechar;
    private javax.swing.JButton btIncluir;
    private javax.swing.JButton btPrimeiro;
    private javax.swing.JButton btRecuar;
    private javax.swing.JButton btUltimo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JTextField teCodigo;
    private javax.swing.JTextField teFone;
    private javax.swing.JTextField teNome;
    // End of variables declaration//GEN-END:variables
    
}
