com.xgestion2.vistas.informes.FormFechasFiltro.java Source code

Java tutorial

Introduction

Here is the source code for com.xgestion2.vistas.informes.FormFechasFiltro.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 com.xgestion2.vistas.informes;

import com.xgestion2.util.AbastractJasperReports;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import xgestion.FormPrincipal;
import org.hibernate.Session;
import org.hibernate.jdbc.Work;

/**
 *
 * @author dario
 */
public class FormFechasFiltro extends javax.swing.JDialog {

    /**
     * Creates new form FormFechasFiltro
     */
    public FormFechasFiltro(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * 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();
        jButton1 = new javax.swing.JButton();
        textFecha = new javax.swing.JFormattedTextField();
        textFecha1 = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("DESDE");

        jLabel2.setText("HASTA");

        jButton1.setText("FILTRAR");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        textFecha.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
                new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));
        textFecha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textFechaKeyReleased(evt);
            }
        });

        textFecha1.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
                new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));
        textFecha1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textFecha1KeyReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap(32, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jButton1)
                                .addGroup(layout.createSequentialGroup().addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(textFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 130,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel2)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textFecha1, javax.swing.GroupLayout.PREFERRED_SIZE, 130,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(63, 63, 63)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1).addComponent(jLabel2)
                                .addComponent(textFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 34,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(textFecha1, javax.swing.GroupLayout.PREFERRED_SIZE, 34,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18).addComponent(jButton1).addContainerGap(34, Short.MAX_VALUE)));

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (!textFecha.getText().isEmpty() && !textFecha1.getText().isEmpty()) {
            // TODO add your handling code here:
            String filePath = new File("src/Horas por Legajos.jasper").getAbsolutePath();

            Session session = FormPrincipal.sessionFactory.openSession();
            session.doWork(new Work() {
                public void execute(Connection connection) throws SQLException {

                    //PARAMETROS
                    Map<String, Object> parameters;
                    parameters = new HashMap<String, Object>();
                    parameters.put("fecha1", convertToDate(textFecha.getText()));
                    parameters.put("fecha2", convertToDate(textFecha1.getText()));

                    AbastractJasperReports.createReport(connection, filePath, parameters);
                    AbastractJasperReports.showViewer();
                }
            });
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void textFechaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textFechaKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_textFechaKeyReleased

    private void textFecha1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textFecha1KeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_textFecha1KeyReleased

    public static java.sql.Date convertToDate(String date) {
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        Date parsed;
        java.sql.Date sql = null;

        try {
            parsed = format.parse(date);
            sql = new java.sql.Date(parsed.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sql;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JFormattedTextField textFecha;
    private javax.swing.JFormattedTextField textFecha1;
    // End of variables declaration//GEN-END:variables
}