Cuentas.editaCuentas.java Source code

Java tutorial

Introduction

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

import Tipo.*;
import Hibernate.Util.HibernateUtil;
import Hibernate.entidades.Tipo;
import Hibernate.entidades.Usuario;
import Integral.ExtensionFileFilter;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.List;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import Integral.Herramientas;
import Integral.Render1;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * 
 *
 * @author ESPECIALIZADO TOLUCA
 */
public class editaCuentas extends javax.swing.JPanel {
    /**
     * Creates new form editaConceptos
     */
    private Tipo actor;
    DefaultTableModel model;
    String[] columnas = new String[] { "Tipo", "Equipo Pesado" };
    Usuario usr;
    String sessionPrograma = "";
    Herramientas h;
    public String ic = "";
    public Boolean im = null;
    int x = 0;

    public editaCuentas(Usuario usuario, String ses) {
        initComponents();
        sessionPrograma = ses;
        usr = usuario;
        formatoTabla();
        buscaDato();
    }

    DefaultTableModel ModeloTablaReporte(int renglones, String columnas[]) {
        model = new DefaultTableModel(new Object[renglones][2], columnas) {
            Class[] types = new Class[] { java.lang.String.class, java.lang.Boolean.class };
            boolean[] canEdit = new boolean[] { false, false };

            public void setValueAt(Object value, int row, int col) {
                Vector vector = (Vector) this.dataVector.elementAt(row);
                Object celda = ((Vector) this.dataVector.elementAt(row)).elementAt(col);
                switch (col) {
                case 0:
                    vector.setElementAt(value, col);
                    this.dataVector.setElementAt(vector, row);
                    fireTableCellUpdated(row, col);
                    break;
                default:
                    vector.setElementAt(value, col);
                    this.dataVector.setElementAt(vector, row);
                    fireTableCellUpdated(row, col);
                    break;
                }
            }

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

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        };
        return model;
    }

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

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        t_datos = new javax.swing.JTable();
        bt_actualiza1 = new javax.swing.JButton();
        Selecciona2 = new javax.swing.JButton();
        Eliminar1 = new javax.swing.JButton();
        Selecciona3 = new javax.swing.JButton();
        bt_actualiza2 = new javax.swing.JButton();
        t_busca = new javax.swing.JTextField();
        b_busca = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        Nombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        b_cancelar1 = new javax.swing.JButton();
        b_guardar = new javax.swing.JButton();
        cb_ep = new javax.swing.JCheckBox();

        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Edita de Tipo",
                javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP,
                new java.awt.Font("Arial", 1, 12))); // NOI18N

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Lista",
                javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP,
                new java.awt.Font("Arial", 1, 11))); // NOI18N

        t_datos.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "Tipo", "Equipo Pesado" }) {
            Class[] types = new Class[] { java.lang.String.class, java.lang.Boolean.class };
            boolean[] canEdit = new boolean[] { false, false };

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

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        t_datos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        t_datos.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(t_datos);

        bt_actualiza1.setBackground(new java.awt.Color(2, 135, 242));
        bt_actualiza1.setForeground(new java.awt.Color(254, 254, 254));
        bt_actualiza1.setIcon(new ImageIcon("imagenes/tabla.png"));
        bt_actualiza1.setText("Actualizar");
        bt_actualiza1.setToolTipText("Actualizar los datos de la tabla");
        bt_actualiza1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        bt_actualiza1.setMaximumSize(new java.awt.Dimension(87, 23));
        bt_actualiza1.setMinimumSize(new java.awt.Dimension(87, 23));
        bt_actualiza1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_actualiza1ActionPerformed(evt);
            }
        });

        Selecciona2.setBackground(new java.awt.Color(2, 135, 242));
        Selecciona2.setForeground(new java.awt.Color(254, 254, 254));
        Selecciona2.setIcon(new ImageIcon("imagenes/update-user.png"));
        Selecciona2.setText("Seleccionar");
        Selecciona2.setToolTipText("Seleccionar un registro de la tabla para editar");
        Selecciona2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        Selecciona2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Selecciona2ActionPerformed(evt);
            }
        });

        Eliminar1.setBackground(new java.awt.Color(2, 135, 242));
        Eliminar1.setForeground(new java.awt.Color(254, 254, 254));
        Eliminar1.setIcon(new ImageIcon("imagenes/del-user.png"));
        Eliminar1.setText("Eliminar");
        Eliminar1.setToolTipText("Eliminar el registro actual");
        Eliminar1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        Eliminar1.setMaximumSize(new java.awt.Dimension(87, 23));
        Eliminar1.setMinimumSize(new java.awt.Dimension(87, 23));
        Eliminar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Eliminar1ActionPerformed(evt);
            }
        });

        Selecciona3.setBackground(new java.awt.Color(2, 135, 242));
        Selecciona3.setForeground(new java.awt.Color(254, 254, 254));
        Selecciona3.setIcon(new ImageIcon("imagenes/add-user.png"));
        Selecciona3.setText("Nuevo");
        Selecciona3.setToolTipText("Agregar un registo actual");
        Selecciona3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        Selecciona3.setMaximumSize(new java.awt.Dimension(87, 23));
        Selecciona3.setMinimumSize(new java.awt.Dimension(87, 23));
        Selecciona3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Selecciona3ActionPerformed(evt);
            }
        });

        bt_actualiza2.setBackground(new java.awt.Color(2, 135, 242));
        bt_actualiza2.setForeground(new java.awt.Color(254, 254, 254));
        bt_actualiza2.setIcon(new ImageIcon("imagenes/guardar-documento.png"));
        bt_actualiza2.setText("Exportar");
        bt_actualiza2.setToolTipText("Actualizar los datos de la tabla");
        bt_actualiza2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        bt_actualiza2.setMaximumSize(new java.awt.Dimension(87, 23));
        bt_actualiza2.setMinimumSize(new java.awt.Dimension(87, 23));
        bt_actualiza2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_actualiza2ActionPerformed(evt);
            }
        });

        t_busca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                t_buscaActionPerformed(evt);
            }
        });

        b_busca.setIcon(new ImageIcon("imagenes/buscar1.png"));
        b_busca.setToolTipText("Busca una partida");
        b_busca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_buscaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(jPanel3Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        jPanel3Layout.createSequentialGroup().addGap(0, 52, Short.MAX_VALUE)
                                                .addComponent(bt_actualiza2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(bt_actualiza1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(Selecciona2)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(Eliminar1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(Selecciona3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        jPanel3Layout.createSequentialGroup().addComponent(t_busca)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(b_busca, javax.swing.GroupLayout.PREFERRED_SIZE, 23,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0,
                                        Short.MAX_VALUE))
                        .addContainerGap()));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(t_busca, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(b_busca, javax.swing.GroupLayout.PREFERRED_SIZE, 23,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(Selecciona3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(Eliminar1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(Selecciona2)
                                .addComponent(bt_actualiza1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(bt_actualiza2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap()));

        jPanel4.setBackground(new java.awt.Color(254, 254, 254));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Actualizar",
                javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.TOP,
                new java.awt.Font("Arial", 1, 11))); // NOI18N

        Nombre.setEnabled(false);
        Nombre.setNextFocusableComponent(cb_ep);
        Nombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NombreActionPerformed(evt);
            }
        });
        Nombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                NombreKeyTyped(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(51, 0, 255));
        jLabel2.setText("Tipo");

        b_cancelar1.setBackground(new java.awt.Color(2, 135, 242));
        b_cancelar1.setForeground(new java.awt.Color(254, 254, 254));
        b_cancelar1.setIcon(new ImageIcon("imagenes/cancelar.png"));
        b_cancelar1.setText("Cancelar");
        b_cancelar1.setEnabled(false);
        b_cancelar1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        b_cancelar1.setNextFocusableComponent(Nombre);
        b_cancelar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_cancelar1ActionPerformed(evt);
            }
        });

        b_guardar.setBackground(new java.awt.Color(2, 135, 242));
        b_guardar.setForeground(new java.awt.Color(254, 254, 254));
        b_guardar.setIcon(new ImageIcon("imagenes/guardar.png"));
        b_guardar.setText("Actualizar");
        b_guardar.setEnabled(false);
        b_guardar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        b_guardar.setName(""); // NOI18N
        b_guardar.setNextFocusableComponent(b_cancelar1);
        b_guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_guardarActionPerformed(evt);
            }
        });

        cb_ep.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        cb_ep.setText("Equipo Pesado");
        cb_ep.setEnabled(false);
        cb_ep.setNextFocusableComponent(b_guardar);
        cb_ep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_epActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(jPanel4Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap(46, Short.MAX_VALUE)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        jPanel4Layout.createSequentialGroup().addComponent(b_cancelar1)
                                                .addGap(18, 18, 18).addComponent(b_guardar))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout
                                        .createSequentialGroup().addComponent(jLabel2).addGap(18, 18, 18)
                                        .addGroup(jPanel4Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(cb_ep).addComponent(Nombre,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 303,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap()));
        jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap(131, Short.MAX_VALUE)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(Nombre, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2))
                        .addGap(48, 48, 48).addComponent(cb_ep).addGap(59, 59, 59)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(b_cancelar1, javax.swing.GroupLayout.PREFERRED_SIZE, 36,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(b_guardar, javax.swing.GroupLayout.PREFERRED_SIZE, 36,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(66, 66, 66)));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));
        jPanel1Layout
                .setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(0, 32, Short.MAX_VALUE)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents

    private void NombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NombreActionPerformed
        // TODO add your handling code here:
        cb_ep.requestFocus();
    }//GEN-LAST:event_NombreActionPerformed

    private void NombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_NombreKeyTyped
        // TODO add your handling code here:
        evt.setKeyChar(Character.toUpperCase(evt.getKeyChar()));
        if (Nombre.getText().length() >= 20)
            evt.consume();
    }//GEN-LAST:event_NombreKeyTyped

    private void b_guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_guardarActionPerformed
        h = new Herramientas(usr, 0);
        h.session(sessionPrograma);
        if (Nombre.getText().compareTo("") != 0) {
            boolean respuesta = modifica();
            if (respuesta == true) {
                borra_cajas();
                cajas(false, false, false, false);
                buscaDato();
            }
        } else {
            JOptionPane.showMessageDialog(null, "Selecciona el tipo que desees modificar!");
            Nombre.requestFocus();
        }
    }//GEN-LAST:event_b_guardarActionPerformed

    private void b_cancelar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_cancelar1ActionPerformed
        int opt = JOptionPane.showConfirmDialog(this, "Los datos capturados se eliminarn!");
        if (opt == 0) {
            borra_cajas();
            cajas(false, false, false, false);
        }
    }//GEN-LAST:event_b_cancelar1ActionPerformed

    private void cb_epActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_epActionPerformed
        b_guardar.requestFocus();
    }//GEN-LAST:event_cb_epActionPerformed

    private void bt_actualiza1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_actualiza1ActionPerformed
        borra_cajas();
        cajas(false, false, false, false);
        buscaDato();
    }//GEN-LAST:event_bt_actualiza1ActionPerformed

    private void Selecciona2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Selecciona2ActionPerformed
        h = new Herramientas(usr, 0);
        h.session(sessionPrograma);
        if (t_datos.getSelectedRow() >= 0) {
            borra_cajas();
            String nom = (String) t_datos.getValueAt(t_datos.getSelectedRow(), 0);
            Nombre.setText(nom);
            ic = nom;
            Boolean ep = (Boolean) t_datos.getValueAt(t_datos.getSelectedRow(), 1);
            cb_ep.setSelected(ep);
            im = ep;
            cajas(true, true, true, true);
        } else
            JOptionPane.showMessageDialog(null, "No hay un tipo seleccionado!");
    }//GEN-LAST:event_Selecciona2ActionPerformed

    private void Eliminar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Eliminar1ActionPerformed
        h = new Herramientas(usr, 0);
        h.session(sessionPrograma);
        if (this.t_datos.getSelectedRow() >= 0) {
            DefaultTableModel model = (DefaultTableModel) t_datos.getModel();
            int a = t_datos.getSelectedRow();
            int opt = JOptionPane.showConfirmDialog(this, "Los datos capturados se eliminarn!");
            if (JOptionPane.YES_OPTION == opt) {
                boolean respuesta = elimina(t_datos.getValueAt(t_datos.getSelectedRow(), 0).toString());
                if (respuesta == true) {
                    this.cajas(false, false, false, false);
                    this.borra_cajas();
                    JOptionPane.showMessageDialog(null, "Registro eliminado");
                    buscaDato();
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "Selecciona el tipo que desees eliminar!");
            Nombre.requestFocus();
        }
    }//GEN-LAST:event_Eliminar1ActionPerformed

    private void Selecciona3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Selecciona3ActionPerformed
        h = new Herramientas(usr, 0);
        h.session(sessionPrograma);
        altaCuentas obj = new altaCuentas(new javax.swing.JFrame(), true, usr, sessionPrograma);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        obj.setLocation((d.width / 2) - (obj.getWidth() / 2), (d.height / 2) - (obj.getHeight() / 2));
        obj.setVisible(true);
        borra_cajas();
        cajas(false, false, false, false);
        buscaDato();
    }//GEN-LAST:event_Selecciona3ActionPerformed

    private void bt_actualiza2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_actualiza2ActionPerformed
        // TODO add your handling code here:
        javax.swing.JFileChooser jF1 = new javax.swing.JFileChooser();
        jF1.setFileFilter(new ExtensionFileFilter("Excel document (*.xls)", new String[] { "xls" }));
        String ruta = null;
        if (jF1.showSaveDialog(null) == jF1.APPROVE_OPTION) {
            ruta = jF1.getSelectedFile().getAbsolutePath();
            if (ruta != null) {
                File archivoXLS = new File(ruta + ".xls");
                try {
                    if (archivoXLS.exists())
                        archivoXLS.delete();
                    archivoXLS.createNewFile();
                    Workbook libro = new HSSFWorkbook();
                    FileOutputStream archivo = new FileOutputStream(archivoXLS);
                    Sheet hoja = libro.createSheet("Clientes");
                    for (int ren = 0; ren < (t_datos.getRowCount() + 1); ren++) {
                        Row fila = hoja.createRow(ren);
                        for (int col = 0; col < t_datos.getColumnCount(); col++) {
                            Cell celda = fila.createCell(col);
                            if (ren == 0) {
                                celda.setCellValue(columnas[col]);
                            } else {
                                if (t_datos.getValueAt(ren - 1, col) == null)
                                    celda.setCellValue("");
                                else {
                                    if (t_datos.getValueAt(ren - 1, col).toString()
                                            .compareToIgnoreCase("false") == 0)
                                        celda.setCellValue("");
                                    else {
                                        if (t_datos.getValueAt(ren - 1, col).toString()
                                                .compareToIgnoreCase("true") == 0)
                                            celda.setCellValue("x");
                                        else
                                            celda.setCellValue(t_datos.getValueAt(ren - 1, col).toString());
                                    }
                                }
                            }
                        }
                    }
                    libro.write(archivo);
                    archivo.close();
                    Desktop.getDesktop().open(archivoXLS);
                } catch (Exception e) {
                    System.out.println(e);
                    JOptionPane.showMessageDialog(this, "No se pudo realizar el reporte");
                }
            }
        }
    }//GEN-LAST:event_bt_actualiza2ActionPerformed

    private void t_buscaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_t_buscaActionPerformed
        // TODO add your handling code here:
        if (this.t_busca.getText().compareToIgnoreCase("") != 0) {
            //buscaCuentas();
            if (x >= t_datos.getRowCount()) {
                x = 0;
                java.awt.Rectangle r = t_datos.getCellRect(x, 1, true);
                t_datos.scrollRectToVisible(r);
            }
            for (; x < t_datos.getRowCount(); x++) {
                if (t_datos.getValueAt(x, 0).toString().indexOf(t_busca.getText().toUpperCase()) != -1) {
                    t_datos.setRowSelectionInterval(x, x);
                    t_datos.setColumnSelectionInterval(1, 1);
                    java.awt.Rectangle r = t_datos.getCellRect(x, 1, true);
                    t_datos.scrollRectToVisible(r);
                    break;
                }
            }
            x++;
        }
    }//GEN-LAST:event_t_buscaActionPerformed

    private void b_buscaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_buscaActionPerformed
        // TODO add your handling code here:
        if (this.t_busca.getText().compareToIgnoreCase("") != 0) {
            //buscaCuentas();
            if (x >= t_datos.getRowCount()) {
                x = 0;
                java.awt.Rectangle r = t_datos.getCellRect(x, 1, true);
                t_datos.scrollRectToVisible(r);
            }
            for (; x < t_datos.getRowCount(); x++) {
                if (t_datos.getValueAt(x, 0).toString().indexOf(t_busca.getText().toUpperCase()) != -1) {
                    t_datos.setRowSelectionInterval(x, x);
                    t_datos.setColumnSelectionInterval(1, 1);
                    java.awt.Rectangle r = t_datos.getCellRect(x, 1, true);
                    t_datos.scrollRectToVisible(r);
                    break;
                }
            }
            x++;
        }
    }//GEN-LAST:event_b_buscaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Eliminar1;
    public javax.swing.JTextField Nombre;
    private javax.swing.JButton Selecciona2;
    private javax.swing.JButton Selecciona3;
    private javax.swing.JButton b_busca;
    private javax.swing.JButton b_cancelar1;
    private javax.swing.JButton b_guardar;
    private javax.swing.JButton bt_actualiza1;
    private javax.swing.JButton bt_actualiza2;
    public javax.swing.JCheckBox cb_ep;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    public javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField t_busca;
    private javax.swing.JTable t_datos;
    // End of variables declaration//GEN-END:variables

    private List<Object[]> executeHQLQuery(String hql) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            Query q = session.createQuery(hql);
            List resultList = q.list();
            session.getTransaction().commit();
            session.disconnect();
            return resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
            List lista = null;
            return lista;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    private void buscaDato() {
        String consulta = "from Tipo";
        List<Object[]> resultList = executeHQLQuery(consulta);
        if (resultList.size() > 0) {
            t_datos.setModel(ModeloTablaReporte(resultList.size(), columnas));
            int i = 0;
            for (Object o : resultList) {
                Tipo actor = (Tipo) o;
                model.setValueAt(actor.getTipoNombre(), i, 0);
                if (actor.getEPesado() == 0)
                    model.setValueAt(false, i, 1);
                else
                    model.setValueAt(true, i, 1);
                i++;
            }
        } else
            t_datos.setModel(ModeloTablaReporte(0, columnas));
        formatoTabla();
    }

    private boolean modifica() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            Object resp = session.createQuery("from Tipo obj where obj.tipoNombre='" + Nombre.getText()
                    + "' and obj.tipoNombre!='" + ic + "'").uniqueResult();
            if (resp == null) {
                Query q;
                if (cb_ep.isSelected())
                    q = session.createQuery("update Tipo obj set obj.EPesado=1, obj.tipoNombre='" + Nombre.getText()
                            + "'where obj.tipoNombre='" + ic + "'");
                else
                    q = session.createQuery("update Tipo obj set obj.EPesado=0, obj.tipoNombre='" + Nombre.getText()
                            + "'where obj.tipoNombre='" + ic + "'");
                q.executeUpdate();
                session.beginTransaction().commit();
                JOptionPane.showMessageDialog(null, "Registro modificado");
                return true;
            } else {
                JOptionPane.showMessageDialog(null, "No se pueden guardar nombres duplicados!");
                return false;
            }
        } catch (HibernateException he) {
            he.printStackTrace();
            System.out.println(he.hashCode());
            session.getTransaction().rollback();
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    private boolean elimina(String idTipo) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            actor = (Tipo) session.get(Tipo.class, idTipo);
            if (actor.getOrdens().isEmpty() == false || actor.getEjemplars().isEmpty() == false) {
                session.getTransaction().rollback();
                JOptionPane.showMessageDialog(null, "El Tipo esta en uso en una orden, no se puede eliminar!");
                return false;
            } else {
                session.delete(actor);
                session.getTransaction().commit();
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
            return false;
        } finally {
            if (session.isOpen())
                session.close();
        }
    }

    public void cajas(boolean tipo, boolean ep, boolean guardar, boolean cancelar) {
        this.Nombre.setEnabled(tipo);
        this.cb_ep.setEnabled(ep);
        this.b_guardar.setEnabled(guardar);
        this.b_cancelar1.setEnabled(cancelar);
    }

    public void borra_cajas() {
        this.Nombre.setText("");
        this.cb_ep.setSelected(false);
    }

public void tabla_tamaos()
{
    TableColumnModel col_model = t_datos.getColumnModel();
    for (int i=0; i<t_datos.getColumnCount(); i++)
    {
        TableColumn column = col_model.getColumn(i);
        switch(i)
        {
            case 0:
                column.setPreferredWidth(150);
                break;
            case 1:
                column.setPreferredWidth(10);
                break;
            default:
                column.setPreferredWidth(40);
                break;
        }
    }
    JTableHeader header = t_datos.getTableHeader();
    header.setForeground(Color.white);
}

public void formatoTabla()
{
    Color c1 = new java.awt.Color(2, 135, 242);
    for(int x=0; x<t_datos.getColumnModel().getColumnCount(); x++)
        t_datos.getColumnModel().getColumn(x).setHeaderRenderer(new Render1(c1));
    tabla_tamaos();
    t_datos.setShowVerticalLines(true);
    t_datos.setShowHorizontalLines(true);
}
}