/*
 * CadastroCliente.java
 *
 * Created on 4 de Maio de 2006, 13:24
 */

package veterinaria;

import java.io.IOException;
import java.sql.SQLException;

/**
 *
 * @author  idn
 */
public class JCadastroCliente extends javax.swing.JFrame {
    
    /** Creates new form CadastroCliente */
    public JCadastroCliente() {
        initComponents();
        try {
            SimpleDataSource.init("bd.properties");
            try {
                bd=new ClienteDAO(SimpleDataSource.getConnection());
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        btnOk.setVisible(false);
        btnCancelar.setVisible(false);
        btnExcluir.setEnabled(false);
        btnAlterar.setEnabled(false);
        txtNome.setEnabled(false);
        txtCPF.setEnabled(false);
        txtTelefone.setEnabled(false);
        txtEndereco.setEnabled(false);
    }
    
    /** 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 ">                          
    private void initComponents() {
        lblNome = new javax.swing.JLabel();
        lblCPF = new javax.swing.JLabel();
        lblTelefone = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblEndereco = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        txtCPF = new javax.swing.JTextField();
        txtTelefone = new javax.swing.JTextField();
        txtEndereco = new javax.swing.JTextField();
        btnIncluir = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        btnPesquisar = new javax.swing.JButton();
        btnOk = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        lblNome.setText("Nome:");

        lblCPF.setText("CPF:");

        lblTelefone.setText("Telefone:");

        lblEndereco.setText("Endere\u00e7o:");

        btnIncluir.setText("Incluir");
        btnIncluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIncluirActionPerformed(evt);
            }
        });

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

        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnPesquisar.setText("Pesquisar");

        btnOk.setText("Ok");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(34, 34, 34)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel4)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(lblNome, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 78, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lblCPF)
                            .add(lblTelefone)
                            .add(lblEndereco))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(txtEndereco, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                            .add(txtNome)
                            .add(txtCPF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 158, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(txtTelefone, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(btnOk)
                            .add(layout.createSequentialGroup()
                                .add(btnIncluir)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btnExcluir)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(btnAlterar)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btnPesquisar))
                            .add(btnCancelar))))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(51, 51, 51)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblNome)
                    .add(txtNome, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblCPF)
                    .add(txtCPF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(22, 22, 22)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblTelefone)
                    .add(txtTelefone, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(16, 16, 16)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(lblEndereco)
                        .add(29, 29, 29)
                        .add(jLabel4))
                    .add(txtEndereco, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnIncluir)
                    .add(btnExcluir)
                    .add(btnAlterar)
                    .add(btnPesquisar))
                .add(16, 16, 16)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnOk)
                    .add(btnCancelar))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>                        
    
    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {                                           
        limparCampos();
        if(umcliente != null) {
            bd.excluirCliente(umcliente);
            umcliente = null;
        }
    }                                          
    
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {                                            
        controlarBotoes(true);
        limparCampos();
        
    }                                           
    
    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {                                      
        controlarBotoes(true);
        if (operacaoAtual=="INC") {
            umcliente = new Cliente();
            umcliente.setNome(txtNome.getText());
            umcliente.setCPF(txtCPF.getText());
            umcliente.setTelefone(txtTelefone.getText());
            umcliente.setEndereco(txtEndereco.getText());
            if(bd.incluirCliente(umcliente)) {
                btnExcluir.setEnabled(true);
                btnAlterar.setEnabled(true);
            }
        }
    }                                     
    
    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {                                           
        controlarBotoes(false);
        operacaoAtual="ALT";
        controlarCampos(true);
    }                                          
    
    private void btnIncluirActionPerformed(java.awt.event.ActionEvent evt) {                                           
        controlarBotoes(false);
        operacaoAtual="INC";
        controlarCampos(true);
        
    }                                          
    
    private void controlarBotoes(boolean flag) {
        btnIncluir.setVisible(flag);
        btnExcluir.setVisible(flag);
        btnAlterar.setVisible(flag);
        btnPesquisar.setVisible(flag);
        btnOk.setVisible(! flag);
        btnCancelar.setVisible(! flag);
    }
    
    private void limparCampos() {
        txtNome.setText("");
        txtCPF.setText("");
        txtTelefone.setText("");
        txtEndereco.setText("");
        
    }
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JCadastroCliente().setVisible(true);
            }
        });
    }
    
    public void controlarCampos(boolean flag){
        txtNome.setEnabled(flag);
        txtCPF.setEnabled(flag);
        txtTelefone.setEnabled(flag);
        txtEndereco.setEnabled(flag);
    }
    
    String operacaoAtual;
    ClienteDAO bd;
    Cliente umcliente;
    
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnIncluir;
    private javax.swing.JButton btnOk;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lblCPF;
    private javax.swing.JLabel lblEndereco;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblTelefone;
    private javax.swing.JTextField txtCPF;
    private javax.swing.JTextField txtEndereco;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtTelefone;
    // End of variables declaration                   
    
}
