/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jfrmMatriz.java
 *
 * Created on 22-ene-2012, 20:00:31
 */
package vista;

import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import modelo.DocumentoReq;
import modelo.Matriz;

/**
 *
 * @author UHU
 */
public class jfrmMatriz extends javax.swing.JFrame {
    DocumentoReq auxDocPpal;
    jfrmPrincipal jfaux;
    MutableTreeNode auxinsertNode;
    int auxinsertIndex;
    Matriz auxmatriz;
    private DefaultListModel modelo_fila;
    private DefaultListModel modelo_columna;
    boolean edicion;
    Matriz auxMat;
    /** Creates new form jfrmMatriz */
    jfrmMatriz(jfrmPrincipal jf, DocumentoReq docPpal, MutableTreeNode insertNode, int insertIndex) {
        initComponents();
        auxinsertNode=insertNode;
        auxinsertIndex=insertIndex;
        jfaux=jf;
        auxDocPpal=docPpal;
        modelo_fila = new DefaultListModel();
        modelo_columna = new DefaultListModel();
        jlstFila.setModel(modelo_fila);
        jlstColumna.setModel(modelo_columna);
        edicion=false;
    }
    
    jfrmMatriz(jfrmPrincipal jf, Matriz Mat, MutableTreeNode nodo, DocumentoReq docPpal) {
        initComponents();
        edicion=true;
        edicion=true;
        jfaux=jf;
        auxMat=Mat;
        auxDocPpal=docPpal;
        auxinsertNode=nodo;
        jtxtNombre.setText(auxMat.getNombre());
        modelo_fila = new DefaultListModel();
        modelo_columna = new DefaultListModel();
        jlstFila.setModel(modelo_fila);
        jlstColumna.setModel(modelo_columna);
        for (int i=0; i<auxMat.getMfila().size(); i++) {
            modelo_fila.addElement(auxMat.getMfila().get(i));
        } 
        for (int i=0; i<auxMat.getMcolumna().size(); i++) {
            modelo_columna.addElement(auxMat.getMcolumna().get(i));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtxtNombre = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jbtnAñadirFila = new javax.swing.JButton();
        jbtnAñadirColumna = new javax.swing.JButton();
        jbtnAceptar = new javax.swing.JButton();
        jbtnCancelar = new javax.swing.JButton();
        jbtnEliminarFila = new javax.swing.JButton();
        jbtnEliminarColumna = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jlstFila = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        jlstColumna = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Matriz de rastreabilidad");
        setResizable(false);

        jLabel1.setText("Nombre:");

        jLabel2.setText("Filas de la matriz:");

        jLabel3.setText("Columnas de la matriz:");

        jbtnAñadirFila.setText("Añadir");
        jbtnAñadirFila.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAñadirFilaActionPerformed(evt);
            }
        });

        jbtnAñadirColumna.setText("Añadir");
        jbtnAñadirColumna.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAñadirColumnaActionPerformed(evt);
            }
        });

        jbtnAceptar.setText("Aceptar");
        jbtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAceptarActionPerformed(evt);
            }
        });

        jbtnCancelar.setText("Cancelar");
        jbtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCancelarActionPerformed(evt);
            }
        });

        jbtnEliminarFila.setText("Eliminar");
        jbtnEliminarFila.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnEliminarFilaActionPerformed(evt);
            }
        });

        jbtnEliminarColumna.setText("Eliminar");
        jbtnEliminarColumna.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnEliminarColumnaActionPerformed(evt);
            }
        });

        jScrollPane3.setViewportView(jlstFila);

        jScrollPane4.setViewportView(jlstColumna);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtxtNombre))
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)
                                    .addComponent(jScrollPane4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jbtnAñadirFila, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jbtnAñadirColumna, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jbtnEliminarFila, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jbtnEliminarColumna, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jbtnAceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnCancelar)
                .addGap(99, 99, 99))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtxtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtnAñadirFila)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnEliminarFila)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(jbtnAñadirColumna)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnEliminarColumna))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnAceptar)
                    .addComponent(jbtnCancelar))
                .addContainerGap(19, Short.MAX_VALUE))
        );

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

    private void jbtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCancelarActionPerformed
        jfaux.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_jbtnCancelarActionPerformed

    private void jbtnAñadirFilaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAñadirFilaActionPerformed
        jfrmElementosMatriz jfElementosMatriz = new jfrmElementosMatriz(auxDocPpal, jfaux, this, "fila");
        jfElementosMatriz.setLocationRelativeTo(null);
        jfElementosMatriz.setVisible(true);
        this.setEnabled(false);
    }//GEN-LAST:event_jbtnAñadirFilaActionPerformed

    private void jbtnAñadirColumnaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAñadirColumnaActionPerformed
        jfrmElementosMatriz jfElementosMatriz = new jfrmElementosMatriz(auxDocPpal, jfaux, this, "columna");
        jfElementosMatriz.setLocationRelativeTo(null);
        jfElementosMatriz.setVisible(true);
        this.setEnabled(false);
    }//GEN-LAST:event_jbtnAñadirColumnaActionPerformed

    private void jbtnEliminarFilaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEliminarFilaActionPerformed
        if (!jlstFila.isSelectionEmpty()) {
            getModelo_fila().removeElement(jlstFila.getSelectedValue().toString());
        }
    }//GEN-LAST:event_jbtnEliminarFilaActionPerformed

    private void jbtnEliminarColumnaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEliminarColumnaActionPerformed
        if (!jlstColumna.isSelectionEmpty()) {
            getModelo_columna().removeElement(jlstColumna.getSelectedValue().toString());
        }
    }//GEN-LAST:event_jbtnEliminarColumnaActionPerformed

    private void jbtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAceptarActionPerformed
        if(edicion==false){
        ArrayList<String> filas = new ArrayList();
        ArrayList<String> columnas = new ArrayList();
        for(int i=0;i<getJlstFila().getModel().getSize();i++){
             filas.add((String) getJlstFila().getModel().getElementAt(i));
        }
        for(int i=0;i<getJlstColumna().getModel().getSize();i++){
             columnas.add((String) getJlstColumna().getModel().getElementAt(i));
        }
        Matriz newMatriz= auxDocPpal.newMatriz(jtxtNombre.getText(), filas, columnas);
        MutableTreeNode node = new DefaultMutableTreeNode("[MAT]-"+ newMatriz.getNumero_id()+" "+ newMatriz.getNombre());
        DefaultTreeModel model = (DefaultTreeModel) jfaux.getJtreDocumento().getModel();
        jfaux.getJtreDocumento().setCellRenderer(new RendererArbol());
        model.insertNodeInto(node, auxinsertNode, auxinsertIndex);
        //mostrarmensaje("Insertada la Reunion " + newReunion.getNombre() + " correctamente.");
        }else{
            auxMat.setNombre(jtxtNombre.getText());
            ArrayList<String> filas = new ArrayList();
            ArrayList<String> columnas = new ArrayList();
            for(int i=0;i<getJlstFila().getModel().getSize();i++){
                filas.add((String) getJlstFila().getModel().getElementAt(i));
            }
            for(int i=0;i<getJlstColumna().getModel().getSize();i++){
                columnas.add((String) getJlstColumna().getModel().getElementAt(i));
            }
            auxMat.setMfila(filas);
            auxMat.setMcolumna(columnas);
            auxinsertNode.setUserObject("[MAT]-"+ auxMat.getNumero_id()+" "+ auxMat.getNombre());
        }
        auxDocPpal.actualizar();
        jfaux.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_jbtnAceptarActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(jfrmMatriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(jfrmMatriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(jfrmMatriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(jfrmMatriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new jfrmMatriz().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbtnAceptar;
    private javax.swing.JButton jbtnAñadirColumna;
    private javax.swing.JButton jbtnAñadirFila;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JButton jbtnEliminarColumna;
    private javax.swing.JButton jbtnEliminarFila;
    private javax.swing.JList jlstColumna;
    private javax.swing.JList jlstFila;
    private javax.swing.JTextField jtxtNombre;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the modelo_fila
     */
    public DefaultListModel getModelo_fila() {
        return modelo_fila;
    }

    /**
     * @param modelo_fila the modelo_fila to set
     */
    public void setModelo_fila(DefaultListModel modelo_fila) {
        this.modelo_fila = modelo_fila;
    }

    /**
     * @return the modelo_columna
     */
    public DefaultListModel getModelo_columna() {
        return modelo_columna;
    }

    /**
     * @param modelo_columna the modelo_columna to set
     */
    public void setModelo_columna(DefaultListModel modelo_columna) {
        this.modelo_columna = modelo_columna;
    }

    /**
     * @return the jlstColumna
     */
    public javax.swing.JList getJlstColumna() {
        return jlstColumna;
    }

    /**
     * @param jlstColumna the jlstColumna to set
     */
    public void setJlstColumna(javax.swing.JList jlstColumna) {
        this.jlstColumna = jlstColumna;
    }

    /**
     * @return the jlstFila
     */
    public javax.swing.JList getJlstFila() {
        return jlstFila;
    }

    /**
     * @param jlstFila the jlstFila to set
     */
    public void setJlstFila(javax.swing.JList jlstFila) {
        this.jlstFila = jlstFila;
    }
}
