/*
 * 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 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 B_Composicion_filos_puntas_sup_grupo_artef extends javax.swing.JInternalFrame {


    
    public B_Composicion_filos_puntas_sup_grupo_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() {

        botones = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbx_mp = new javax.swing.JComboBox<>();
        jPanel1 = new javax.swing.JPanel();
        jpanel = new javax.swing.JPanel();
        jpanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        btn_copiar = new javax.swing.JButton();
        btn_arbol = new javax.swing.JButton();
        btn_info = new javax.swing.JButton();
        txt_sentencia = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        txt_muestra = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        txt_mp_selec = new javax.swing.JTextField();
        btn_calcular = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbl2 = new javax.swing.JTable();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Clases artefactuales según filos, puntas, superficies y materias primas");
        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);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Materia prima ");

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

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jpanel.setBackground(new java.awt.Color(204, 204, 204));
        jpanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jpanel.setLayout(new java.awt.BorderLayout());

        jpanel1.setBackground(new java.awt.Color(204, 204, 204));
        jpanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jpanel1.setLayout(new java.awt.BorderLayout());

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Según fila (Clase artefactual)");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("Según columna (filo, punta o sup.)");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jpanel, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(39, 39, 39)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 451, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(53, 53, 53))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jpanel, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton1.setText("MP PRESENTES EN LA MUESTRA");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

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

        btn_arbol.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arbol.png"))); // NOI18N
        btn_arbol.setToolTipText("Arbol: CLASES ARTEFACTUALES PRESENTES EN LA MP SELECCIONADA");
        btn_arbol.setEnabled(false);
        btn_arbol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_arbolActionPerformed(evt);
            }
        });

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

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

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

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

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

        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel15.setText("Materia prima:");

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

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

        tbl2.setBackground(new java.awt.Color(204, 204, 204));
        tbl2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        tbl2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"AF-Artefacto formatizado", null, null, null, null, null, null},
                {"Nu-Núcleo", null, null, null, null, null, null},
                {"ARC-Artefacto con rastros complementarios", null, null, null, null, null, null},
                {"NdRC-Nódulo con rastros complementarios", null, null, null, null, null, null},
                {"NdT-Nódulos transportados", null, null, null, null, null, null},
                {"CN", null, null, null, null, null, null}
            },
            new String [] {
                "Clase artefactual", "Filos", "Puntas de mano", "Superficies activas", "Puntas de proy.", "Núcleos", "Total"
            }
        ));
        tbl2.setGridColor(new java.awt.Color(255, 102, 102));
        tbl2.setRowHeight(30);
        tbl2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbl2MouseClicked(evt);
            }
        });
        tbl2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbl2KeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(tbl2);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbx_mp, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)
                        .addGap(66, 66, 66))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(btn_calcular, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel14)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txt_muestra)
                                .addGap(32, 32, 32)
                                .addComponent(jLabel16)
                                .addGap(18, 18, 18)
                                .addComponent(txt_sentencia, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(166, 166, 166)
                                .addComponent(jLabel15)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txt_mp_selec)))
                        .addGap(14, 14, 14)
                        .addComponent(btn_info, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btn_arbol, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_copiar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbx_mp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(46, 46, 46)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btn_calcular, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btn_arbol, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn_copiar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(btn_info, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel14)
                        .addComponent(txt_sentencia, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel16)
                        .addComponent(txt_muestra, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_mp_selec, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addGap(43, 43, 43)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(jPanel2);

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

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        CalculosBD c = new CalculosBD();

        c.cargarmpformdistinct(cbx_mp, WHERE);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btn_copiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_copiarActionPerformed
        
        Copiartabla copy = new Copiartabla();
        copy.copiartabla(tbl2, txt_muestra.getText(), WHERE, "Composición de la muestra: Clases artefactuales según filos, puntas de mano, puntas de proyectil, superficies activas y núcleos", "Materia prima: "+txt_mp_selec.getText());
    }//GEN-LAST:event_btn_copiarActionPerformed

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

        for (int i = 0; i < tbl2.getRowCount()-1; i++) {
        CalculosBD calc = new CalculosBD();
        String claseart = tbl2.getValueAt(i, 0).toString();
        calc.cargarArbolCriterioFORMATIZADOSf(ar.arbol_analisis_FORM, "FORMATIZADOS f", claseart, "f.ID_FORMATIZADOS", "ID_FORMATIZADOS", "f.PIEZA", "PIEZA", "Gen", WHERE+" AND ( f.MP= '"+txt_mp_selec.getText()+"' AND f.CLASE_ARTEF='"+claseart+"')", txt_muestra.getText()+"/ MP:" + txt_mp_selec.getText());
            
        }
ar.txt_mp.setText(txt_mp_selec.getText());
ar.txt_nota.setText("El árbol se conforma de acuerdo a clases artefactuales presentes en la materia prima seleccionada. En este caso, no se discrimina entre filos, puntas y superficies");

    }//GEN-LAST:event_btn_arbolActionPerformed

    private void btn_infoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_infoActionPerformed
        info_frame info = new info_frame();
        info.setVisible(true);
        info.info_procedencia.setText("MUESTRA---->" + txt_muestra.getText() +"\n SENTENCIA---->" +txt_sentencia.getText() );
        info.info_mp.setText(txt_mp_selec.getText());
        info.info_analisis.setText("CLASES ARTEFACTUALES POR MP, FILOS, PUNTAS DE PROYECTIL, PUNTAS DE MANO, SUPERFICIES Y NUCLEOS: Se contabilizan las piezas de acuerdo al 100% de fichas: segmental estandar, núcleos, puntas de proyectil, en base a la muestra y la materia prima seleccionada."
            + "\n"
            + "\n El conteo de la tabla debe entenderse como 'Cantidad de filos, puntas o superficies, realizados sobre diversas clases artefactuales'"
            + "\n - Ejemplo: si la pieza N°22 se registró como <Artefacto Formatizado> en la clase artefactual, y la misma tiene asociada una ficha segmental de Cuchillo y una de Raedera, entonces el conteo de filos en relación a Artefactos formatizados será de 2."
               );
           
    }//GEN-LAST:event_btn_infoActionPerformed

    private void btn_calcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_calcularActionPerformed
if(WHERE.equals("")){
    JOptionPane.showMessageDialog(null, "Seleccione previamente una muestra");
}else{
        if(cbx_mp.getSelectedIndex()==0){
            JOptionPane.showMessageDialog(null, "Seleccione previamente una materia prima");
        }else{
            txt_muestra.setText(ARQL2020.etiqueta_muestra);
            txt_sentencia.setText(ARQL2020.WHERE);
            txt_mp_selec.setText(cbx_mp.getSelectedItem().toString());
            
            
//            DefaultTableModel dtm = (DefaultTableModel) tbl.getModel();
//            int cant = contarclasefilopuntasup(WHERE, txt_mp_selec.getText(), txt_filo_punta_sup.getText(), "AF-Artefacto formatizado");
//            dtm.setValueAt(cant, 0, 1);
//            cant = contarclasefilopuntasup(WHERE, txt_mp_selec.getText(), txt_filo_punta_sup.getText(), "Nu-Núcleo");
//            dtm.setValueAt(cant, 1, 1);
//            cant = contarclasefilopuntasup(WHERE, txt_mp_selec.getText(), txt_filo_punta_sup.getText(), "ARC-Artefacto con rastros complementarios");
//            dtm.setValueAt(cant, 2, 1);
//            cant = contarclasefilopuntasup(WHERE, txt_mp_selec.getText(), txt_filo_punta_sup.getText(), "NdRC-Nódulo con rastros complementarios");
//            dtm.setValueAt(cant, 3, 1);
//            cant = contarclasefilopuntasup(WHERE, txt_mp_selec.getText(), txt_filo_punta_sup.getText(), "NdT-Nódulos transportados");
//            dtm.setValueAt(cant, 4, 1);
//            cant = contarclasefilopuntasup(WHERE, txt_mp_selec.getText(), txt_filo_punta_sup.getText(), "CN");
//            dtm.setValueAt(cant, 5, 1);
//            
//    
//            
//            int aux = 0;
//            for (int i = 0; i < 6; i++) {
//                aux = aux+ Integer.parseInt(dtm.getValueAt(i, 1).toString());
//            }
//            dtm.setValueAt(String.valueOf(aux), 6, 1);
// 
//            
//    int filas = dtm.getRowCount();
//    int cols = dtm.getColumnCount();
//    Object suma = new Object();
//
//            CalculosBD calc = new CalculosBD();
//            
//    suma=0;
//            for (int i = 0; i < filas; i++) {
//            String valor = dtm.getValueAt(i, 1).toString();
//            String cien = dtm.getValueAt(6, 1).toString();
//            float porcentaje = calc.porcentajeCIENPORCIENTO(valor, cien);
//            String nuevovalor = String.valueOf(porcentaje)+"%";
//            dtm.setValueAt(nuevovalor, i, 2);
//            
//            
//    }
            cargartabla2();
            cargargrafico();
            btn_arbol.setEnabled(true);
    }        
    }        
    }//GEN-LAST:event_btn_calcularActionPerformed
private void cargartabla2(){
String []pedido = new String[3];
    pedido[0]= "Filo";
    pedido[1]= "Punta de mano";
    pedido[2]= "Superficie activa";
    DefaultTableModel dtm = (DefaultTableModel) tbl2.getModel();
//filos, puntas de mano, sup act            
    for (int i = 0; i < 3; i++) {
           int cant = contarclasefilopuntasup(WHERE, txt_mp_selec.getText(), pedido[i], "AF-Artefacto formatizado");
            dtm.setValueAt(cant, 0, i+1);
            cant = contarclasefilopuntasup(WHERE, txt_mp_selec.getText(), pedido[i], "Nu-Núcleo");
            dtm.setValueAt(cant, 1, i+1);
            cant = contarclasefilopuntasup(WHERE, txt_mp_selec.getText(), pedido[i], "ARC-Artefacto con rastros complementarios");
            dtm.setValueAt(cant, 2, i+1);
            cant = contarclasefilopuntasup(WHERE, txt_mp_selec.getText(), pedido[i], "NdRC-Nódulo con rastros complementarios");
            dtm.setValueAt(cant, 3, i+1);
            cant = contarclasefilopuntasup(WHERE, txt_mp_selec.getText(), pedido[i], "NdT-Nódulos transportados");
            dtm.setValueAt(cant, 4, i+1);
            cant = contarclasefilopuntasup(WHERE, txt_mp_selec.getText(), pedido[i], "CN");
            dtm.setValueAt(cant, 5, i+1);
    }  
    
//puntas de proy
           int cant = contarclasepuntasproy(WHERE, txt_mp_selec.getText(), "AF-Artefacto formatizado");
            dtm.setValueAt(cant, 0, 4);
            cant = contarclasepuntasproy(WHERE, txt_mp_selec.getText(),  "Nu-Núcleo");
            dtm.setValueAt(cant, 1, 4);
            cant = contarclasepuntasproy(WHERE, txt_mp_selec.getText(),  "ARC-Artefacto con rastros complementarios");
            dtm.setValueAt(cant, 2, 4);
            cant = contarclasepuntasproy(WHERE, txt_mp_selec.getText(), "NdRC-Nódulo con rastros complementarios");
            dtm.setValueAt(cant, 3, 4);
            cant = contarclasepuntasproy(WHERE, txt_mp_selec.getText(), "NdT-Nódulos transportados");
            dtm.setValueAt(cant, 4, 4);
            cant = contarclasepuntasproy(WHERE, txt_mp_selec.getText(),  "CN");
            dtm.setValueAt(cant, 5, 4);
    
//NUCLEOS
           cant = contarclaseNUCLEOS(WHERE, txt_mp_selec.getText(), "AF-Artefacto formatizado");
            dtm.setValueAt(cant, 0, 5);
            cant = contarclaseNUCLEOS(WHERE, txt_mp_selec.getText(),  "Nu-Núcleo");
            dtm.setValueAt(cant, 1, 5);
            cant = contarclaseNUCLEOS(WHERE, txt_mp_selec.getText(),  "ARC-Artefacto con rastros complementarios");
            dtm.setValueAt(cant, 2, 5);
            cant = contarclaseNUCLEOS(WHERE, txt_mp_selec.getText(), "NdRC-Nódulo con rastros complementarios");
            dtm.setValueAt(cant, 3, 5);
            cant = contarclaseNUCLEOS(WHERE, txt_mp_selec.getText(), "NdT-Nódulos transportados");
            dtm.setValueAt(cant, 4, 5);
            cant = contarclaseNUCLEOS(WHERE, txt_mp_selec.getText(),  "CN");
            dtm.setValueAt(cant, 5, 5);
    
//    sumarcol(1,dtm);
//    sumarcol(2,dtm);
//    sumarcol(3,dtm);
//    sumarcol(4,dtm);
//    sumarcol(5,dtm);
//    
//
//        int aux1 = Integer.parseInt(dtm.getValueAt(6, 1).toString());
//        int aux2 = Integer.parseInt(dtm.getValueAt(6, 2).toString());
//        int aux3 = Integer.parseInt(dtm.getValueAt(6, 3).toString());
//        int aux4 = Integer.parseInt(dtm.getValueAt(6, 4).toString());
//        int aux5 = Integer.parseInt(dtm.getValueAt(6, 5).toString());
//        int aux6 = aux1+aux2+aux3+aux4+aux5;
//        String aux7 = String.valueOf(aux6);
//        dtm.setValueAt(aux7, 6, 6);
//        
for (int i = 0; i < 6; i++) {
        dtm.setValueAt("0", i, 6);
    }
    CalculosBD calc = new CalculosBD();
    calc.sumartotalesfila(dtm);
    calc.sumartotalescolumna(dtm);
    calc.porcentajesfilainferior(dtm);
}
private void sumarcol(int num, DefaultTableModel dtm){
                int aux = 0;
            for (int i = 0; i < 6; i++) {
                aux = aux+ Integer.parseInt(dtm.getValueAt(i, num).toString());
            }
            dtm.setValueAt(String.valueOf(aux), 6, num);
}
private void cargargrafico(){
//    try{
//
//DefaultCategoryDataset ds = new DefaultCategoryDataset();
//
//         int valor = Integer.parseInt(tbl.getValueAt(0, 1).toString());
//         ds.addValue(valor, (tbl.getValueAt(0, 0).toString()), "");
//         
//         valor = Integer.parseInt(tbl.getValueAt(1, 1).toString());
//         ds.addValue(valor,  (tbl.getValueAt(1, 0).toString()), "");
//         
//         valor = Integer.parseInt(tbl.getValueAt(2, 1).toString());
//         ds.addValue(valor,  (tbl.getValueAt(2, 0).toString()), "");
//         
//         valor = Integer.parseInt(tbl.getValueAt(3, 1).toString());
//         ds.addValue(valor,  (tbl.getValueAt(3, 0).toString()), "");
//         
//         valor = Integer.parseInt(tbl.getValueAt(4, 1).toString());
//         ds.addValue(valor,  (tbl.getValueAt(4, 0).toString()), "");
// 
//
//    JFreeChart jf =ChartFactory.createBarChart3D("Composición de la muestra", "Clases artef.", "Cantidad", ds, PlotOrientation.HORIZONTAL, true, true, true);
//
//    
//    CategoryPlot barchrt = jf.getCategoryPlot();
//    barchrt.setRangeGridlinePaint(Color.ORANGE);
//    ChartPanel barpanel = new ChartPanel(jf);
//    jpanel.removeAll();
//    jpanel.add(barpanel, BorderLayout.CENTER);
//    jpanel.validate();
//    
//
//}catch (Exception ex){
//    JOptionPane.showMessageDialog(null, "No se puede generar gráfico");
//}
    
}    
    
    
    private int contarclasefilopuntasup (String where, String MP, String filo_punta_sup, String clase_artef){
 
    int cant = 0;
     ResultSet rs = null; 
     String consulta = "SELECT COUNT(*)  FROM FORMATIZADOS f\n" +
"INNER JOIN\n" +
"SEGMENTAL_ESTANDAR2 s\n" +
"ON f.ID_FORMATIZADOS = s.ID_FORMATIZADOS " +where + " AND (f.MP='"+MP+"') AND (s.FILO_PUNTA_SUP='"+filo_punta_sup+"')  AND (f.CLASE_ARTEF='"+clase_artef+"') ";
         try {
            conexion c = new conexion();
            c.getConexion();
            
             rs = c.consulta(consulta);

            if(rs.next()){
                cant = rs.getInt("1");
            }       
        
        c.closeconnection();
        
        
        } catch (SQLException ex) {
            Logger.getLogger(InteraccionesBD.class.getName()).log(Level.SEVERE, null, ex);
        }
     
    
    return cant;
}
    private int contarclasepuntasproy (String where, String MP, String clase_artef){
 
    int cant = 0;
     ResultSet rs = null; 
     String consulta = "SELECT COUNT(*)  FROM FORMATIZADOS f\n" +
"INNER JOIN\n" +
"PUNTAS_DE_PROYECTIL2 s\n" +
"ON f.ID_FORMATIZADOS = s.ID_FORMATIZADOS " +where + " AND (f.MP='"+MP+"')  AND (f.CLASE_ARTEF='"+clase_artef+"') ";
         try {
            conexion c = new conexion();
            c.getConexion();
            
             rs = c.consulta(consulta);

            if(rs.next()){
                cant = rs.getInt("1");
            }       
        
        c.closeconnection();
        
        
        } catch (SQLException ex) {
            Logger.getLogger(InteraccionesBD.class.getName()).log(Level.SEVERE, null, ex);
        }
     
    
    return cant;
}
    private int contarclaseNUCLEOS (String where, String MP, String clase_artef){
 ArrayList<String> n = new ArrayList<>();
    int cant = 0;
     ResultSet rs = null; 
     String consulta = "SELECT f.ID_FORMATIZADOS  FROM FORMATIZADOS f\n" +
"INNER JOIN\n" +
"NUCLEOS s\n" +
"ON f.ID_FORMATIZADOS = s.ID_FORMATIZADOS " +where + " AND (f.MP='"+MP+"') AND (f.CLASE_ARTEF='"+clase_artef+"') ";
         try {
            conexion c = new conexion();
            c.getConexion();
            
             rs = c.consulta(consulta);

            while(rs.next()){
                String aux = String.valueOf(rs.getInt("ID_FORMATIZADOS"));
                n.add(aux);
            }       
        
        c.closeconnection();
        
        
        } catch (SQLException ex) {
            Logger.getLogger(InteraccionesBD.class.getName()).log(Level.SEVERE, null, ex);
        }
     
    Set<String> hashSet = new HashSet<String>(n);
        n.clear();
        n.addAll(hashSet);
        
        cant = n.size();
    return cant;
}
    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
CalculosBD c = new CalculosBD();
c.cargarmp(cbx_mp);
    }//GEN-LAST:event_formInternalFrameOpened

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

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




    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup botones;
    private javax.swing.JButton btn_arbol;
    private javax.swing.JButton btn_calcular;
    private javax.swing.JButton btn_copiar;
    private javax.swing.JButton btn_info;
    private javax.swing.JComboBox<String> cbx_mp;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPanel jpanel;
    private javax.swing.JPanel jpanel1;
    private javax.swing.JTable tbl2;
    private javax.swing.JTextField txt_mp_selec;
    private javax.swing.JTextField txt_muestra;
    private javax.swing.JTextField txt_sentencia;
    // End of variables declaration//GEN-END:variables
}
