pe.crvm.eurekacs.view.MantClientesView.java Source code

Java tutorial

Introduction

Here is the source code for pe.crvm.eurekacs.view.MantClientesView.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.crvm.eurekacs.view;

import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.Document;
import javax.swing.text.*;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import pe.crvm.eurekacs.controller.ClienteController;
import pe.crvm.eurekacs.domain.Cliente;
import pe.crvm.eurekacs.util.Dialogo;
import pe.crvm.eurekacs.util.Eureka;
import pe.crvm.eurekacs.util.Memoria;

/**
 *
 * @author Alumno
 */
public class MantClientesView extends javax.swing.JInternalFrame {

    private ClienteController control;
    private List<Cliente> lista;

    public MantClientesView() {
        initComponents();

        control = new ClienteController();
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtPaterno = new javax.swing.JTextField();
        txtMaterno = new javax.swing.JTextField();
        txtNombre = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnExcel = new javax.swing.JButton();
        btnPDF = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblData = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("MANTENIMIENTO DE CLIENTES");
        setVerifyInputWhenFocusTarget(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Paterno");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel2.setText("Materno");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel3.setText("Nombre");

        txtPaterno.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

        txtMaterno.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

        txtNombre.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

        btnBuscar.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/crvm/eurekacs/img/buscar.gif"))); // NOI18N
        btnBuscar.setToolTipText("Buscar Cliente");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        btnNuevo.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/crvm/eurekacs/img/new.png"))); // NOI18N
        btnNuevo.setToolTipText("Nuevo Cliente");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnEditar.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/crvm/eurekacs/img/edit.png"))); // NOI18N
        btnEditar.setToolTipText("Editar Cliente");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        btnEliminar.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/crvm/eurekacs/img/delete.png"))); // NOI18N
        btnEliminar.setToolTipText("Eliminar Cliente");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnExcel.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        btnExcel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/crvm/eurekacs/img/Excel.png"))); // NOI18N
        btnExcel.setToolTipText("Exportar a Excel");
        btnExcel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcelActionPerformed(evt);
            }
        });

        btnPDF.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        btnPDF.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/crvm/eurekacs/img/PDF.png"))); // NOI18N
        btnPDF.setToolTipText("Exportar a PDF");
        btnPDF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPDFActionPerformed(evt);
            }
        });

        tblData.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        tblData.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null, null, null, null, null }, { null, null, null, null, null, null },
                        { null, null, null, null, null, null }, { null, null, null, null, null, null } },
                new String[] { "Codigo", "Paterno", "Materno", "Nombre", "DNI", "Email" }) {
            Class[] types = new Class[] { java.lang.String.class, java.lang.String.class, java.lang.String.class,
                    java.lang.String.class, java.lang.String.class, java.lang.String.class };
            boolean[] canEdit = new boolean[] { false, false, false, false, false, false };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblData);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel4.setText("Exportar a:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(28, 28, 28).addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 105,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 105,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 105,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        163, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(btnBuscar)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(btnNuevo)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(btnEditar).addGap(18, 18, 18)
                                                .addComponent(btnEliminar))
                                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(txtMaterno, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        163, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGroup(layout.createSequentialGroup()
                                                        .addComponent(txtPaterno,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 163,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addGap(89, 89, 89)
                                                        .addComponent(jLabel4,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 105,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                        .addComponent(btnExcel).addGap(18, 18, 18)
                                                        .addComponent(btnPDF)))
                                                .addGap(0, 15, Short.MAX_VALUE)))))
                        .addGap(42, 42, 42)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(62, 62, 62).addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(txtPaterno, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel4)))
                                .addGap(37, 37, 37))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(btnPDF).addComponent(btnExcel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2).addComponent(txtMaterno,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addGap(54, 54, 54).addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(btnEditar).addComponent(btnNuevo)
                                                .addComponent(btnBuscar))
                                        .addGroup(layout.createSequentialGroup().addGap(2, 2, 2)
                                                .addComponent(btnEliminar))))
                                .addGroup(layout.createSequentialGroup().addGap(43, 43, 43)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel3).addComponent(txtNombre,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(47, 47, 47).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(95, 95, 95)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        try {
            // Datos
            Cliente cliente = new Cliente();

            cliente.setPaterno(txtPaterno.getText());
            cliente.setMaterno(txtMaterno.getText());
            cliente.setNombre(txtNombre.getText());
            // Proceso
            lista = control.getClientes(cliente);
            // Reporte
            mostrarLista(lista);
        } catch (Exception e) {
            Dialogo.error(rootPane, e.getMessage());
        }
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        Cliente bean = new Cliente();
        bean.setCodigo("NONE");
        EditarClienteView view = new EditarClienteView(null, true);
        view.setBean(bean);
        view.setAccion(Eureka.CRUD_NUEVO);
        Memoria.put("bean", null);
        view.setVisible(true);
        if (Memoria.get("bean") != null) {
            bean = (Cliente) Memoria.get("bean");
            lista.add(0, bean);
            mostrarLista(lista);
        }
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        int fila;
        fila = tblData.getSelectedRow();
        if (fila == -1)
            return;

        Cliente bean = lista.get(fila);
        EditarClienteView view = new EditarClienteView(null, true);
        view.setBean(bean);
        view.setAccion(Eureka.CRUD_EDITAR);
        Memoria.put("bean", null);
        view.setVisible(true);
        if (Memoria.get("bean") != null) {
            bean = (Cliente) Memoria.get("bean");
            lista.set(fila, bean);
            mostrarLista(lista);
        }

    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        int fila;
        fila = tblData.getSelectedRow();
        if (fila == -1)
            return;

        Cliente bean = lista.get(fila);
        EditarClienteView view = new EditarClienteView(null, true);
        view.setBean(bean);
        view.setAccion(Eureka.CRUD_ELIMINAR);
        view.setVisible(true);
        if (Memoria.get("bean") != null) {
            lista.remove(fila);
            mostrarLista(lista);
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnExcelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcelActionPerformed
        ////////////////////////
        if (lista == null || lista.size() == 0) {
            return;
        }
        try {
            // El libro
            //String plantilla = "/pe/egcc/eurekacs/plantillas/Clientes.xls";
            String plantilla = "/pe/crvm/eurekacs/plantilla/Clientes.xls";
            InputStream isPlantilla = Class.class.getResourceAsStream(plantilla);
            POIFSFileSystem fs = new POIFSFileSystem(isPlantilla);
            HSSFWorkbook wb = new HSSFWorkbook(fs, true);
            // Acceder a la hoja
            HSSFSheet sheet = wb.getSheetAt(0);
            // Llenar datos
            int fila = 0;
            for (Cliente bean : lista) {
                fila++;
                HSSFRow row = sheet.createRow(fila);
                row.createCell(0).setCellValue(bean.getCodigo());
                row.createCell(1).setCellValue(bean.getPaterno());
                row.createCell(2).setCellValue(bean.getMaterno());
                row.createCell(3).setCellValue(bean.getNombre());
                row.createCell(4).setCellValue(bean.getDni());
                row.createCell(5).setCellValue(bean.getCiudad());
                row.createCell(6).setCellValue(bean.getDireccion());
                row.createCell(7).setCellValue(bean.getTelefono());
                row.createCell(8).setCellValue(bean.getEmail());
            }
            // Grabar
            FileOutputStream fileOut = new FileOutputStream(
                    "e:/JAVA/ESCRITORIO/CLASE03/EurecaCS/EurecaCS/Cliente.xls");

            wb.write(fileOut);
            fileOut.close();
            Dialogo.info(rootPane, "Proceso ok.");
        } catch (Exception e) {
            e.printStackTrace();
            Dialogo.error(rootPane, e.getMessage());
        }

    }//GEN-LAST:event_btnExcelActionPerformed

    private void btnPDFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPDFActionPerformed
        if (lista == null || lista.isEmpty()) {
            return;
        }
        com.itextpdf.text.Document document = null;
        try {
            // Archivo destino
            String archivo = "e:/JAVA/ESCRITORIO/CLASE03/EurecaCS/EurecaCS/clientes.pdf";
            // step 1
            document = new com.itextpdf.text.Document();
            // step 2
            PdfWriter.getInstance(document, new FileOutputStream(archivo));
            // step 3
            document.open();
            // step 4: tabla
            PdfPTable table = new PdfPTable(6);
            // step 5: Cabeceras
            table.addCell("CODIGO");
            table.addCell("PATERNO");
            table.addCell("MATERNO");
            table.addCell("NOMBRE");
            table.addCell("DNI");
            table.addCell("EMAIL");
            // step 6: Data
            for (Cliente cliente : lista) {
                table.addCell(cliente.getCodigo());
                table.addCell(cliente.getPaterno());
                table.addCell(cliente.getMaterno());
                table.addCell(cliente.getNombre());
                table.addCell(cliente.getDni());
                table.addCell(cliente.getEmail());
            }
            // step 7: Cargar la tabla
            document.add(table);
            // step 8: Fin
            Dialogo.info(this, "Proceso ejecutado correctamente.");

        } catch (Exception ex) {
            ex.printStackTrace();
            Dialogo.error(this, "No se tiene permiso para crear el archivo.");
        } finally {
            try {
                if (document != null)
                    document.close();
            } catch (Exception e) {
            }
        }
    }//GEN-LAST:event_btnPDFActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnExcel;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton btnPDF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblData;
    private javax.swing.JTextField txtMaterno;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtPaterno;
    // End of variables declaration//GEN-END:variables

    private void mostrarLista(List<Cliente> lista) {
        DefaultTableModel tabla;
        tabla = (DefaultTableModel) tblData.getModel();
        tabla.setRowCount(0);
        for (Cliente cliente : lista) {
            Object[] rowData = { cliente.getCodigo(), cliente.getPaterno(), cliente.getMaterno(),
                    cliente.getNombre(), cliente.getDni(), cliente.getEmail() };
            tabla.addRow(rowData);
        }

    }

}