visao.FrameListaCliente.java Source code

Java tutorial

Introduction

Here is the source code for visao.FrameListaCliente.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 visao;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import static controle.Util.reduzString;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import modelo.Cliente;

/**
 *
 * @author Dcio
 */
public class FrameListaCliente extends javax.swing.JFrame {

    public FrameListaCliente() {
    }

    /**
     * Creates new form FrameListaCliente
     */
    public FrameListaCliente(List<Cliente> lista) {
        initComponents();
        String title = "Relatrios Clientes";
        this.setLocation(50, 50);
        this.carregarListaCliente(lista);
        this.setTitle(title);
    }

    /**
     * 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() {

        btnSairClienteRelatorio = new javax.swing.JButton();
        btnImprimirRelatorioCliente = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtCliente = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnSairClienteRelatorio.setBackground(new java.awt.Color(204, 204, 204));
        btnSairClienteRelatorio.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnSairClienteRelatorio
                .setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/saidaDireitaRed.png"))); // NOI18N
        btnSairClienteRelatorio.setText("Voltar");
        btnSairClienteRelatorio.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnSairClienteRelatorio.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnSairClienteRelatorio.setPreferredSize(new java.awt.Dimension(100, 50));
        btnSairClienteRelatorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairClienteRelatorioActionPerformed(evt);
            }
        });

        btnImprimirRelatorioCliente.setBackground(new java.awt.Color(204, 204, 204));
        btnImprimirRelatorioCliente.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        btnImprimirRelatorioCliente
                .setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/infraero/pdf2.png"))); // NOI18N
        btnImprimirRelatorioCliente.setText("Imprimir em PDF");
        btnImprimirRelatorioCliente.setToolTipText("Salvar Alteraes");
        btnImprimirRelatorioCliente.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnImprimirRelatorioCliente.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnImprimirRelatorioCliente.setIconTextGap(2);
        btnImprimirRelatorioCliente.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnImprimirRelatorioCliente.setPreferredSize(new java.awt.Dimension(100, 50));
        btnImprimirRelatorioCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirRelatorioClienteActionPerformed(evt);
            }
        });

        txtCliente.setEditable(false);
        txtCliente.setBackground(new java.awt.Color(125, 181, 199));
        txtCliente.setColumns(20);
        txtCliente.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        txtCliente.setRows(7);
        txtCliente.setBorder(
                javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(51, 51, 255)));
        jScrollPane1.setViewportView(txtCliente);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(btnSairClienteRelatorio, javax.swing.GroupLayout.PREFERRED_SIZE, 171,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnImprimirRelatorioCliente, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        171, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(30, 30, 30).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 791,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(48, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(23, 23, 23).addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(btnSairClienteRelatorio, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(208, 208, 208)
                                .addComponent(btnImprimirRelatorioCliente, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 308,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(76, Short.MAX_VALUE)));

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

    private void carregarListaCliente(List<Cliente> ListaClientes) {
        String newline = System.getProperty("line.separator"); //verificar propriedades de System.getProperty
        String resultado = "";

        for (Cliente c : ListaClientes) {

            resultado += "" + "\n\tCdigo Cliente: " + c.getIdCliente() + "\n" + "\n\tNome: " + c.getNome()
                    + "\n\tEndereo: " + c.getEndereco().trim() + "," + c.getNumero() + "\n\tBairro: "
                    + c.getBairro().trim() + " - Cep: " + c.getCep() + "\n\tCidade: " + c.getCidade().trim()
                    + " - UF: " + c.getUf() + "\n\tEmail: " + c.getEmail().trim() + "\n\tTelefone: "
                    + c.getTelefone() + "\n\tRG: " + c.getRg() + " - CPF: " + c.getCpf();

            resultado += newline;

        }
        resultado = "\n\n\t\t\t CADASTRO CLIENTES\n" + resultado;
        txtCliente.setText(resultado);
        txtCliente.setEditable(false);

    }

    private void btnSairClienteRelatorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairClienteRelatorioActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairClienteRelatorioActionPerformed

    private void btnImprimirRelatorioClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirRelatorioClienteActionPerformed
        //criamos um documento vazio
        String msg = "";
        String tituloMsg = "Relatrio PDF";
        String passagemTexto = txtCliente.getText();
        Document documento = new Document() {
        };

        try {
            //criar o documento no diretrio do projeto Netbeans AeroFast
            PdfWriter.getInstance(documento, new FileOutputStream("documentoAeroFastClientes.pdf"));

            //abrir o documento criado.
            documento.open();

            //ajustar o tamanho da pagina
            documento.setPageSize(PageSize.A4);

            //Adicionar um paragrafo
            documento.add(new Paragraph(passagemTexto));

        } catch (DocumentException | FileNotFoundException ex) {
            msg = msg + ex;
            msg = reduzString(msg);
            Logger.getLogger(FrameListaCliente.class.getName()).log(Level.SEVERE, null, ex);

        } finally {
            documento.close();

            if (!"".equals(msg)) {
                JOptionPane.showMessageDialog(this, msg, tituloMsg, JOptionPane.WARNING_MESSAGE);
                msg = "";
            } else {
                msg = "Relatrio criado em PDF com sucesso";
                JOptionPane.showMessageDialog(this, msg, tituloMsg, JOptionPane.WARNING_MESSAGE);
                msg = "";
            }
        }
    }//GEN-LAST:event_btnImprimirRelatorioClienteActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrameListaCliente.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrameListaCliente.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrameListaCliente.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrameListaCliente.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrameListaCliente().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnImprimirRelatorioCliente;
    private javax.swing.JButton btnSairClienteRelatorio;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtCliente;
    // End of variables declaration//GEN-END:variables
}