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

/*
 * jfrmRastreabilidad.java
 *
 * Created on 18-ene-2012, 11:40:51
 */

package vista;

import java.awt.Component;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import modelo.DocumentoReq;
import modelo.Rastreabilidad;

/**
 *
 * @author Ruben
 */
public class jfrmRastreabilidad extends javax.swing.JFrame {
    DocumentoReq auxDocPpal;
    jfrmPrincipal jfaux;
    MutableTreeNode auxinsertNode;
    int auxinsertIndex;
    DefaultListModel listaFilas = new DefaultListModel();
    DefaultListModel listaColumnas = new DefaultListModel();

    /** Creates new form jfrmRastreabilidad */
    public jfrmRastreabilidad(jfrmPrincipal jf, DocumentoReq docPpal, MutableTreeNode insertNode, int insertIndex) {
        auxinsertNode=insertNode;
        auxinsertIndex=insertIndex;
        jfaux=jf;
        auxDocPpal=docPpal;
        initComponents();
    }

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

        jTabRastreabilidad = new javax.swing.JTabbedPane();
        jpanGeneral = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtxtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtxtAutores = new javax.swing.JTextField();
        jtxtFuentes = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jpanDetalles = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jbtnAnadir1 = new javax.swing.JButton();
        jbtnAnadir2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jlstFilas = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        jlstColumnas = new javax.swing.JList();
        jpanComentarios = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtxpComentarios = new javax.swing.JTextPane();
        jbtnAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setText("Nombre:");

        jLabel2.setText("Autores:");

        jLabel3.setText("Fuentes:");

        javax.swing.GroupLayout jpanGeneralLayout = new javax.swing.GroupLayout(jpanGeneral);
        jpanGeneral.setLayout(jpanGeneralLayout);
        jpanGeneralLayout.setHorizontalGroup(
            jpanGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanGeneralLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpanGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpanGeneralLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(12, 12, 12)
                        .addComponent(jtxtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE))
                    .addGroup(jpanGeneralLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtxtAutores, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE))
                    .addGroup(jpanGeneralLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtxtFuentes, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jpanGeneralLayout.setVerticalGroup(
            jpanGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanGeneralLayout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jpanGeneralLayout.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.UNRELATED)
                .addGroup(jpanGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtAutores, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpanGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtxtFuentes, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabRastreabilidad.addTab("General", jpanGeneral);

        jLabel5.setText("Filas de la Matriz : ");

        jLabel4.setText("Columnas de la Matriz :");

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

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

        jScrollPane2.setViewportView(jlstFilas);

        jScrollPane3.setViewportView(jlstColumnas);

        javax.swing.GroupLayout jpanDetallesLayout = new javax.swing.GroupLayout(jpanDetalles);
        jpanDetalles.setLayout(jpanDetallesLayout);
        jpanDetallesLayout.setHorizontalGroup(
            jpanDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanDetallesLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jpanDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addGroup(jpanDetallesLayout.createSequentialGroup()
                        .addGroup(jpanDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane3)
                            .addComponent(jScrollPane2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jpanDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jbtnAnadir1)
                            .addComponent(jbtnAnadir2))))
                .addContainerGap(52, Short.MAX_VALUE))
        );
        jpanDetallesLayout.setVerticalGroup(
            jpanDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanDetallesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpanDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpanDetallesLayout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(15, 15, 15)
                        .addComponent(jLabel4))
                    .addComponent(jbtnAnadir1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpanDetallesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtnAnadir2)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        jTabRastreabilidad.addTab("Detalles", jpanDetalles);

        jLabel6.setText("Comentarios: ");

        jScrollPane1.setViewportView(jtxpComentarios);

        javax.swing.GroupLayout jpanComentariosLayout = new javax.swing.GroupLayout(jpanComentarios);
        jpanComentarios.setLayout(jpanComentariosLayout);
        jpanComentariosLayout.setHorizontalGroup(
            jpanComentariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanComentariosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpanComentariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
                    .addComponent(jLabel6))
                .addContainerGap())
        );
        jpanComentariosLayout.setVerticalGroup(
            jpanComentariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanComentariosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabRastreabilidad.addTab("Comentarios", jpanComentarios);

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

        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()
                        .addComponent(jTabRastreabilidad, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(jbtnAceptar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jTabRastreabilidad, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnAceptar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        jfaux.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_formWindowClosed

    private void jbtnAnadir1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAnadir1ActionPerformed
      jfrmElementosRas jele=new jfrmElementosRas(this,auxDocPpal,jfaux,0);
      jele.setVisible(true);
      this.setEnabled(false);
    }//GEN-LAST:event_jbtnAnadir1ActionPerformed

    private void jbtnAnadir2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAnadir2ActionPerformed
    jfrmElementosRas jele=new jfrmElementosRas(this,auxDocPpal,jfaux,1);
      jele.setVisible(true);
      this.setEnabled(false);
    }//GEN-LAST:event_jbtnAnadir2ActionPerformed

    private void jbtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAceptarActionPerformed
        //Comprobar que todos los campos están rellenos
        //Saco los arrayLists!
        if(comprobarTexto())
        {
            ArrayList<String> filAux = new ArrayList();
            ArrayList<String> colAux = new ArrayList();
            for(int i=0; i<jlstFilas.getModel().getSize(); i++)
            {
                filAux.add(jlstFilas.getModel().getElementAt(i).toString());
            }
            for(int i=0; i<jlstColumnas.getModel().getSize(); i++)
            {
                colAux.add(jlstColumnas.getModel().getElementAt(i).toString());
            }
            //crear la nueva instancia
            Rastreabilidad newRas = auxDocPpal.nuevoRastreabilidad(jtxtNombre.getText(),
                                                                   jtxtAutores.getText(),
                                                                   jtxtFuentes.getText(),
                                                                   jtxpComentarios.getText(),
                                                                   filAux,
                                                                   colAux);
            MutableTreeNode node=new DefaultMutableTreeNode("MRast" + newRas.getNombre());
            DefaultTreeModel model =(DefaultTreeModel) jfaux.getJtreDocumento().getModel();
            model.insertNodeInto(node, auxinsertNode, auxinsertIndex);
            mostrarmensaje("Insertada la Matriz "+newRas.getNombre());
            jfaux.setEnabled(true);
            jfaux.update(null, evt);
            this.dispose();
        }
        else this.mostrarmensaje("Debes Rellenar todos los campos!");
    }//GEN-LAST:event_jbtnAceptarActionPerformed

    /**
    * @param args the command line arguments
    */
   /* public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jfrmRastreabilidad().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.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabRastreabilidad;
    private javax.swing.JButton jbtnAceptar;
    private javax.swing.JButton jbtnAnadir1;
    private javax.swing.JButton jbtnAnadir2;
    private javax.swing.JList jlstColumnas;
    private javax.swing.JList jlstFilas;
    private javax.swing.JPanel jpanComentarios;
    private javax.swing.JPanel jpanDetalles;
    private javax.swing.JPanel jpanGeneral;
    private javax.swing.JTextPane jtxpComentarios;
    private javax.swing.JTextField jtxtAutores;
    private javax.swing.JTextField jtxtFuentes;
    private javax.swing.JTextField jtxtNombre;
    // End of variables declaration//GEN-END:variables
public void setTxtFila(Object t)
{
    listaFilas.addElement(t);
    jlstFilas.setModel((ListModel) listaFilas);
    jlstFilas.validate();
}
public void setTxtColumna(Object t)
{
    
    listaColumnas.addElement(t);
    jlstColumnas.setModel((ListModel) listaColumnas);
    jlstColumnas.validate();
}

    private void mostrarmensaje(String mensaje) {
        JOptionPane.showMessageDialog(this, mensaje, "Advertencia",
                                JOptionPane.WARNING_MESSAGE);
    }

    private boolean comprobarTexto() {
        if(jtxtNombre.getText().equals("") ||
           jtxtAutores.getText().equals("") ||
           jtxtFuentes.getText().equals("") ||
           jtxpComentarios.getText().equals(""))
            return false;
        else
            return true;
    }
}
