/*
 * 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 Calculos_automaticos;


import arql2020.ARQL2020;
import static arql2020.ARQL2020.WHERE;
import static arql2020.ARQL2020.etiqueta_muestra;
import clases.InteraccionesBD;
import clases.conexion;
import java.awt.BorderLayout;
import java.awt.Color;
import java.beans.PropertyVetoException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author chuen
 */
public class A_Composicion_clase_artef extends javax.swing.JInternalFrame {

    /**
     * Creates new form A_Composicion_clase_artef
     */
    public A_Composicion_clase_artef() {
        initComponents();
        try {
            this.setMaximum(true);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(B_Composicion_puntasfilossup.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

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

        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbl = new javax.swing.JTable();
        jpanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jpanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txt_muestra = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txt_sentencia = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbl1 = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jpanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jpanel3 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jButton5 = new javax.swing.JButton();

        setClosable(true);
        setResizable(true);
        setTitle("Materia prima según clases artefactuales");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        tbl.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tbl.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbl.setColumnSelectionAllowed(true);
        tbl.setRowHeight(25);
        tbl.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblMouseClicked(evt);
            }
        });
        tbl.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tbl);
        tbl.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        jpanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jpanel.setLayout(new java.awt.BorderLayout());

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Gráficos por fila (Según materia prima)");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Gráficos por columna (según clase artefactual)");

        jpanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jpanel1.setLayout(new java.awt.BorderLayout());

        jButton1.setBackground(new java.awt.Color(204, 255, 0));
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/lentes.png"))); // NOI18N
        jButton1.setToolTipText("Realizar análisis");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/info.png"))); // NOI18N
        jButton4.setToolTipText("Información sobre el análisis");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arbol.png"))); // NOI18N
        jButton2.setToolTipText("Abrir árbol de piezas");
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/copiar.png"))); // NOI18N
        jButton3.setToolTipText("Copiar tabla en el portapapeles");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Cálculo realizado sobre muestra:");

        txt_muestra.setEditable(false);
        txt_muestra.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Sentencia:");

        txt_sentencia.setEditable(false);
        txt_sentencia.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        tbl1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tbl1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbl1.setRowHeight(25);
        tbl1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbl1MouseClicked(evt);
            }
        });
        tbl1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbl1KeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(tbl1);
        tbl1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setText("Pesos (g) representados según clases artefactuales y materias primas");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel6.setText("Materias primas según clases artefactuales");

        jpanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jpanel2.setLayout(new java.awt.BorderLayout());

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel7.setText("Gráficos por fila (Según materia prima)");

        jpanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jpanel3.setLayout(new java.awt.BorderLayout());

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel8.setText("Gráficos por columna (según clase artefactual)");

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/copiar.png"))); // NOI18N
        jButton5.setToolTipText("Copiar tabla en el portapapeles");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jSeparator1))
                        .addGap(23, 23, 23))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txt_muestra, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txt_sentencia, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jpanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE))
                                .addGap(29, 29, 29)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel8)
                                    .addComponent(jpanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jpanel, javax.swing.GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE))
                                .addGap(29, 29, 29)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jpanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 568, Short.MAX_VALUE)
                                        .addGap(3, 3, 3))
                                    .addComponent(jLabel1)))
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 1116, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1116, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txt_muestra, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)
                        .addComponent(jLabel4)
                        .addComponent(txt_sentencia, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(56, 56, 56)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jpanel, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                                .addComponent(jpanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(20, 20, 20)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(66, 66, 66)
                        .addComponent(jLabel5))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jpanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jpanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jScrollPane2.setViewportView(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane2)
                .addGap(0, 0, 0))
        );

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

    private void tblMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblMouseClicked
        int filaselec = tbl.getSelectedRow();
        int filas = tbl.getRowCount()-1;
        
        if(filaselec<filas){
        Graficos graf = new Graficos();
        graf.generargraficofila2(tbl, jpanel, tbl.getValueAt(filaselec,0).toString(), "Clases artef.");
            
        }
        
        int col=tbl.getColumnCount()-1;
        int columnaselec = tbl.getSelectedColumn();
        if((columnaselec>0)&&(columnaselec<col)){
            Graficos graf = new Graficos();
            graf.generargraficocolumna(tbl, jpanel1, tbl.getColumnName(columnaselec), "M.P.");
            
        }

    }//GEN-LAST:event_tblMouseClicked


    private void tblKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblKeyPressed

    }//GEN-LAST:event_tblKeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
  if(WHERE.equals("")){
                        JOptionPane.showMessageDialog(null, "Debe seleccionar una muestra previamente");
                    }else{
      
  
 jpanel.removeAll();
 jpanel2.removeAll();
 jpanel3.removeAll();
 jpanel1.removeAll();
DefaultTableModel dm = (DefaultTableModel) tbl.getModel();
 int rowCount = dm.getRowCount();
//Remove rows one by one from the end of the table
for (int i = rowCount - 1; i >= 0; i--) {
    dm.removeRow(i);
}

contabilizarconDTtbl();
CalculosBD calculos = new CalculosBD();
DefaultTableModel dm2 = (DefaultTableModel) tbl.getModel();
calculos.sumartotalesfila(dm2);
calculos.sumartotalescolumna(dm2);
calculos.porcentajesfilainferior(dm2);
txt_muestra.setText(ARQL2020.etiqueta_muestra);
txt_sentencia.setText(ARQL2020.WHERE);
cargartbl2();
jButton2.setEnabled(true);
}
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
Arbol ar = new Arbol();
ar.setVisible(true);

        for (int i = 0; i < tbl.getRowCount()-1; i++) {
        CalculosBD calc = new CalculosBD();
        String mp = tbl.getValueAt(i, 0).toString();
        calc.cargarArbolCriterioFORMATIZADOSf(ar.arbol_analisis_FORM, "FORMATIZADOS f", mp, "f.ID_FORMATIZADOS", "ID_FORMATIZADOS", "f.PIEZA", "PIEZA", "Gen", WHERE+" AND ( f.MP= '"+mp+"')", txt_muestra.getText());
        calc.cargarArbolCriterioDTf(ar.arbol_analisisDT, "DT_CONTALON2 f", mp+"-CT", "f.ID_DT_CONTALON", "ID_DT_CONTALON", "f.PIEZA","PIEZA", "CT", WHERE+" AND ( MP= '"+mp+"')", txt_muestra.getText());
        calc.cargarArbolCriterioDTf(ar.arbol_analisisDT, "DT_SINTALON2 f", mp+"-ST", "f.ID_DT_SINTALON", "ID_DT_SINTALON","f.LOTE", "LOTE", "ST", WHERE+" AND ( MP= '"+mp+"')", txt_muestra.getText());
            
        }
ar.txt_mp.setText("Todas las materias primas presentes en la muestra");

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
  Copiartabla copy = new Copiartabla();
  copy.copiartabla(tbl, txt_muestra.getText(), WHERE, "Composición de la muestra según materia prima y clase artefactual", "");
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
  info_frame info = new info_frame();
  info.setVisible(true);
  info.info_mp.setText("Todas las materias primas presentes en la muestra.");
  info.info_analisis.setText("Se contabilizan las piezas de acuerdo al 100% de fichas generales y de Desechos de talla (CT y ST) . Se distribuyen de acuerdo a: clases artefactuales por materia prima (Tabla1), y pesos (Tabla2).");
  info.info_procedencia.setText("ETIQUETA---->"+txt_muestra.getText()+"\nSENTENCIA---->"+txt_sentencia.getText());
    }//GEN-LAST:event_jButton4ActionPerformed

    private void tbl1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbl1MouseClicked
        int filaselec = tbl1.getSelectedRow();
        int filas = tbl1.getRowCount()-1;
        
        if(filaselec<filas){
        Graficos graf = new Graficos();
        graf.generargraficofila2FLOAT(tbl1, jpanel2, tbl1.getValueAt(filaselec,0).toString(), "Clases artef.");
            
        }
        
        int col=tbl1.getColumnCount()-1;
        int columnaselec = tbl1.getSelectedColumn();
        if((columnaselec>0)&&(columnaselec<col)){
            Graficos graf = new Graficos();
            graf.generargraficocolumnaFLOAT(tbl1, jpanel3, tbl1.getColumnName(columnaselec), "M.P.");
            
        }
    }//GEN-LAST:event_tbl1MouseClicked

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

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
    }//GEN-LAST:event_formInternalFrameOpened

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
Copiartabla copy = new Copiartabla();
copy.copiartabla(tbl1, txt_muestra.getText(), WHERE, "Composición de la muestra según materia prima y clase artefactual, por peso", "");

    }//GEN-LAST:event_jButton5ActionPerformed
private void contabilizarconDTtbl(){
DefaultTableModel dm = new DefaultTableModel();
String primeracol="MP";
//int cantidad_cols = 9;
InteraccionesBD interac = new InteraccionesBD();
Object [][] lista = interac.solicitarlista_X2f("f.MP", "MP", "f.CLASE_ARTEF", "CLASE_ARTEF", "FORMATIZADOS f", WHERE);
String [] mpform = interac.selectDisctintf("f.MP", "MP", "FORMATIZADOS f" ,WHERE);
String [] mpdtCON = interac.selectDisctintf("f.MP", "MP", "DT_CONTALON2 f" ,WHERE);
String [] mpdtSIN = interac.selectDisctintf("f.MP", "MP", "DT_SINTALON2 f" ,WHERE);


ArrayList <String> mp = new ArrayList<>();

    for (int i = 0; i < mpform.length; i++) {
        mp.add(mpform[i]);
//        System.out.println("MP FORM:" +mpform[i]);
    }
    for (int i = 0; i < mpdtCON.length; i++) {
        mp.add(mpdtCON[i]);
//        System.out.println("DT CON:" +mpdtCON[i]);
    }
    for (int i = 0; i < mpdtSIN.length; i++) {
        mp.add(mpdtSIN[i]);
//        System.out.println("DT siN:" +mpdtSIN[i]);
    }

Set<String> hashSet = new HashSet<String>(mp);
        mp.clear();
        mp.addAll(hashSet);
        

    
String [] cantidadcols = interac.selectDisctintf("f.CLASE_ARTEF", "CLASE_ARTEF", "FORMATIZADOS f", WHERE);
String [][] conteo = new String[mp.size()][cantidadcols.length+3];

ArrayList<String> dt_contalon = interac.solicitarlistaX1f("f.MP", "MP", "DT_CONTALON2 f", WHERE);
ArrayList<String> dt_sintalon = interac.solicitarlistaX1f("f.MP","MP", "DT_SINTALON2 f",WHERE);
ArrayList<String> dt = new ArrayList<>();
dt.addAll(dt_contalon);
dt.addAll(dt_sintalon);


//setea todos los valores a cero, salvo la col inicial
    for (int i = 0; i < conteo.length; i++) {
       conteo[i][0]=mp.get(i);
        for (int j = 1; j < conteo[i].length; j++) {
            conteo[i][j]="0";
        }
    }

    
    dm.addColumn(primeracol);
for (int j = 0; j < cantidadcols.length; j++) {
  dm.addColumn(cantidadcols[j]);
                }
  dm.addColumn("DT");
  dm.addColumn("Total");

//contabiliza combinaciones
        for (int i = 0; i < lista.length; i++) {

        for (int k = 0; k < conteo.length; k++) {
            if(conteo[k][0].equals(lista[i][0])){
                for (int j = 1; j < dm.getColumnCount()-1; j++) {
                            
                if(dm.getColumnName(j).equals(lista[i][1])){
//                    System.out.println("se repite: " + dm.getColumnName(j)+"--"+lista[i][1]);
                    int aux = Integer.parseInt(conteo[k][j])+1;
                    conteo[k][j]=String.valueOf(aux);
                }}
                
        } }
    }
        
        
        ///////contabiliza DT
        for (int i = 0; i < conteo.length; i++) {
            for (int j = 0; j < dt.size(); j++) {
                if(conteo[i][0].toString().equals(dt.get(j))){
                    int aux = Integer.parseInt(conteo[i][dm.getColumnCount()-2])+1;
                    conteo[i][dm.getColumnCount()-2]=String.valueOf(aux);
                }
            }
    }

Object [] fila = new Object[dm.getColumnCount()];
    for (int j = 0; j < conteo.length; j++) {
        for (int i = 0; i < conteo[j].length; i++) {
       
            fila[i]= conteo[j][i];
}
        dm.addRow(fila);
    }
  
    tbl.setModel(dm);
    
    
    
}



private void contabilizarautom(String primeracol){

DefaultTableModel dm = new DefaultTableModel();
//int cantidad_cols = 9;
InteraccionesBD interac = new InteraccionesBD();
Object [][] lista = interac.solicitarlista_X2("MP", "CLASE_ARTEF", "FORMATIZADOS", "");
String [] mp = interac.selectDisctint("MP", "FORMATIZADOS", "");
String [] cantidadcols = interac.selectDisctint("CLASE_ARTEF", "FORMATIZADOS", "");

String [][] conteo = new String[mp.length][cantidadcols.length+2];

//setea todos los valores a cero, salvo la col inicial
    for (int i = 0; i < conteo.length; i++) {
       conteo[i][0]=mp[i];
        for (int j = 1; j < conteo[i].length; j++) {
            conteo[i][j]="0";
        }
    }

    
    dm.addColumn(primeracol);
for (int j = 0; j < cantidadcols.length; j++) {
  dm.addColumn(cantidadcols[j]);
                }
  dm.addColumn("Total");

//contabiliza combinaciones
        for (int i = 0; i < lista.length; i++) {

        for (int k = 0; k < conteo.length; k++) {
            if(conteo[k][0].equals(lista[i][0])){
                for (int j = 1; j < dm.getColumnCount()-1; j++) {
                            
                if(dm.getColumnName(j).equals(lista[i][1])){
//                    System.out.println("se repite: " + dm.getColumnName(j)+"--"+lista[i][1]);
                    int aux = Integer.parseInt(conteo[k][j])+1;
                    conteo[k][j]=String.valueOf(aux);
                }}
                
        } }
    }
//        System.out.println("conteo1: "+conteo[0][0]+";"+conteo[0][1]+";"+conteo[0][2]+";"+conteo[0][3]+";"+conteo[0][4]+";"+conteo[0][5]+";"+conteo[0][6]+";"+conteo[0][7]+";");
//        System.out.println("conteo2: "+conteo[1][0]+";"+conteo[1][1]+";"+conteo[1][2]+";"+conteo[1][3]+";"+conteo[1][4]+";"+conteo[1][5]+";"+conteo[1][6]+";"+conteo[1][7]+";");
//        System.out.println("conteo3: "+conteo[2][0]+";"+conteo[2][1]+";"+conteo[2][2]+";"+conteo[2][3]+";"+conteo[2][4]+";"+conteo[2][5]+";"+conteo[2][6]+";"+conteo[2][7]+";");

Object [] fila = new Object[8];
    for (int j = 0; j < conteo.length; j++) {
        for (int i = 0; i < conteo[j].length; i++) {
       
            fila[i]= conteo[j][i];
}
        dm.addRow(fila);
    }
  
    tbl.setModel(dm);
    
}

private void cargartbl2(){
    DefaultTableModel dtm1 = (DefaultTableModel) tbl.getModel();
    DefaultTableModel dtm2 = new DefaultTableModel();
    
    Object [] fila = new Object[dtm1.getColumnCount()];
    for (int i = 0; i <dtm1.getColumnCount(); i++) {
        dtm2.addColumn(dtm1.getColumnName(i));
    }
    

    for (int i = 0; i <dtm1.getRowCount()-1; i++) {
        fila[0]= dtm1.getValueAt(i, 0);
        for (int j = 1; j < dtm1.getColumnCount(); j++) {
            fila[j]=0;
        }
        dtm2.addRow(fila);
    }    
    
    tbl1.setModel(dtm2);
    
    int colDT = 0;
    for (int i = 0; i < dtm2.getRowCount(); i++) {
            for (int j = 1; j <dtm2.getColumnCount()-1; j++) {
            if(dtm2.getColumnName(j).toString().equals("DT")){
            Object peso = consultapesos(WHERE, dtm2.getValueAt(i, 0).toString(), "", "DT_SINTALON2");
            dtm2.setValueAt(peso, i, j);
            colDT=j;       
                }else{
            Object peso = consultapesos(WHERE, dtm2.getValueAt(i, 0).toString(), "AND (f.CLASE_ARTEF='"+dtm2.getColumnName(j).toString()+"')", "FORMATIZADOS");
            dtm2.setValueAt(peso, i, j);
                    
                }

        }
        
    }
    for (int i = 0; i < dtm2.getRowCount(); i++) {
            float peso = consultapesos(WHERE, dtm2.getValueAt(i, 0).toString(), "", "DT_CONTALON2");
            float aux = Float.parseFloat(dtm2.getValueAt(i, colDT).toString());
            Object suma = peso+aux;
            dtm2.setValueAt(suma, i, colDT);
    }
    
    
    
CalculosBD c = new CalculosBD();
c.sumartotalesfilaFLOAT(dtm2);
c.sumartotalescolumnaFLOAT(dtm2);
c.porcentajesfilainferiorFLOAT(dtm2);
    
    
}

private float consultapesos(String where, String MP, String clase_artef, String tabla){
    float peso = 0;
    String speso = "";
       ResultSet rs = null; 
     String consulta = "SELECT f.PESO  FROM "+tabla+" f " + where + " AND (f.MP='"+MP+"')" + clase_artef ;
    
         try {
            conexion c = new conexion();
            c.getConexion();
            
             rs = c.consulta(consulta);

            while(rs.next()){
                speso = rs.getString("PESO");
                
                
                if(!speso.equals("CN")){
                    Float aux = Float.parseFloat(speso);
                    peso = peso + aux;
                }
                
            }       
        
        c.closeconnection();
        
        
        } catch (SQLException ex) {
            Logger.getLogger(InteraccionesBD.class.getName()).log(Level.SEVERE, null, ex);
        }
     
    
    
    
    
    return peso;
}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel jpanel;
    private javax.swing.JPanel jpanel1;
    private javax.swing.JPanel jpanel2;
    private javax.swing.JPanel jpanel3;
    private javax.swing.JTable tbl;
    private javax.swing.JTable tbl1;
    private javax.swing.JTextField txt_muestra;
    private javax.swing.JTextField txt_sentencia;
    // End of variables declaration//GEN-END:variables
}
