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

/*
 * jfrmParrafo.java
 *
 * Created on 16-ene-2012, 20:19:35
 */
package vista;

import javax.swing.DefaultListModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import modelo.DocumentoReq;
import modelo.Parrafo;

/**
 *
 * @author UHU
 */
public class jfrmParrafo extends javax.swing.JFrame {
    DocumentoReq auxDocPpal;
    jfrmPrincipal jfaux;
    MutableTreeNode auxinsertNode;
    int auxinsertIndex;
    Parrafo auxPrf;
    boolean edicion;
    private DefaultListModel modelo_destino;
    private DefaultListModel modelo_origen;
    
    /** Creates new form jfrmParrafo */
    public jfrmParrafo (jfrmPrincipal jf, DocumentoReq docPpal, MutableTreeNode insertNode, int insertIndex) {
        initComponents();
        auxinsertNode=insertNode;
        auxinsertIndex=insertIndex;
        jfaux=jf;
        auxDocPpal=docPpal;
        modelo_destino = new DefaultListModel();
        modelo_origen = new DefaultListModel();
        jlstDestino.setModel(modelo_destino);
        jlstOrigen.setModel(modelo_origen);
        edicion=false;
    }

    jfrmParrafo(jfrmPrincipal jf, Parrafo Prf, MutableTreeNode nodo, DocumentoReq docPpal) {
        initComponents();
        edicion=true;
        jfaux=jf;
        auxPrf=Prf;
        auxDocPpal=docPpal;
        auxinsertNode=nodo;
        jtxtNombre.setText(auxPrf.getNombre());
        jtxaParrafo.setText(auxPrf.getParrafo());
        modelo_destino = new DefaultListModel();
        modelo_origen = new DefaultListModel();
        jlstDestino.setModel(modelo_destino);
        jlstOrigen.setModel(modelo_origen);
        for (int i=0; i<auxPrf.getmDestino().size(); i++) {
            modelo_destino.addElement(auxPrf.getmDestino().get(i));
        } 
        for (int i=0; i<auxPrf.getmOrigen().size(); i++) {
            modelo_origen.addElement(auxPrf.getmOrigen().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() {

        jTabParrafo = new javax.swing.JTabbedPane();
        jpanParrafo = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtxaParrafo = new javax.swing.JTextArea();
        jtxtNombre = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jpanRastreabilidad = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jlstDestino = new javax.swing.JList();
        jbtnAñadirDestino = new javax.swing.JButton();
        jbtnEliminarDestino = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        jlstOrigen = new javax.swing.JList();
        jbtnAñadirOrigen = new javax.swing.JButton();
        jbtnEliminarOrigen = new javax.swing.JButton();
        jbtnCancelar = new javax.swing.JButton();
        jbtnAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Párrafo");
        setResizable(false);

        jLabel2.setText("Nuevo Párrafo:");

        jtxaParrafo.setColumns(20);
        jtxaParrafo.setRows(5);
        jScrollPane1.setViewportView(jtxaParrafo);

        jLabel1.setText("Título:");

        javax.swing.GroupLayout jpanParrafoLayout = new javax.swing.GroupLayout(jpanParrafo);
        jpanParrafo.setLayout(jpanParrafoLayout);
        jpanParrafoLayout.setHorizontalGroup(
            jpanParrafoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanParrafoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpanParrafoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpanParrafoLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpanParrafoLayout.createSequentialGroup()
                        .addGroup(jpanParrafoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpanParrafoLayout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtxtNombre))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        jpanParrafoLayout.setVerticalGroup(
            jpanParrafoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanParrafoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpanParrafoLayout.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)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabParrafo.addTab("Párrafo", jpanParrafo);

        jLabel7.setText("Destino:");

        jLabel8.setText("Origen:");

        jlstDestino.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane4.setViewportView(jlstDestino);

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

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

        jScrollPane5.setViewportView(jlstOrigen);

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

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

        javax.swing.GroupLayout jpanRastreabilidadLayout = new javax.swing.GroupLayout(jpanRastreabilidad);
        jpanRastreabilidad.setLayout(jpanRastreabilidadLayout);
        jpanRastreabilidadLayout.setHorizontalGroup(
            jpanRastreabilidadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanRastreabilidadLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpanRastreabilidadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addGroup(jpanRastreabilidadLayout.createSequentialGroup()
                        .addGroup(jpanRastreabilidadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jpanRastreabilidadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jbtnEliminarOrigen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbtnAñadirOrigen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbtnAñadirDestino, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbtnEliminarDestino, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpanRastreabilidadLayout.setVerticalGroup(
            jpanRastreabilidadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanRastreabilidadLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpanRastreabilidadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpanRastreabilidadLayout.createSequentialGroup()
                        .addComponent(jbtnAñadirDestino)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnEliminarDestino))
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpanRastreabilidadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jpanRastreabilidadLayout.createSequentialGroup()
                        .addComponent(jbtnAñadirOrigen)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnEliminarOrigen)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabParrafo.addTab("Rastreabilidad", jpanRastreabilidad);

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

        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()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTabParrafo, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .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(89, 89, 89))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabParrafo, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnCancelar)
                    .addComponent(jbtnAceptar))
                .addContainerGap())
        );

        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 jbtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAceptarActionPerformed
        if(edicion==false){
        Parrafo newParrafo= auxDocPpal.newParrafo(jtxtNombre.getText(), jtxaParrafo.getText());
        //Rastreabilidad
                for (int i=0; i<getJlstDestino().getModel().getSize(); i++) {
                    newParrafo.getmDestino().add((String) getJlstDestino().getModel().getElementAt(i).toString());
                    auxDocPpal.enlace_rastrea_destino(newParrafo,(String) getJlstDestino().getModel().getElementAt(i).toString());
                }
                for (int i=0; i<getJlstOrigen().getModel().getSize(); i++) {
                    newParrafo.getmOrigen().add((String) getJlstOrigen().getModel().getElementAt(i).toString());
                    auxDocPpal.enlace_rastrea_origen((String) getJlstOrigen().getModel().getElementAt(i).toString(), newParrafo);
                }
                //Fin Rastreabilidad
        MutableTreeNode node = new DefaultMutableTreeNode("[PRF]-"+ newParrafo.getNumero_id()+" "+ newParrafo.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{

            auxPrf.setNombre(jtxtNombre.getText());
            auxPrf.setParrafo(jtxaParrafo.getText());
            auxinsertNode.setUserObject("[PRF]-"+ auxPrf.getNumero_id()+" "+ auxPrf.getNombre());
        }

        auxDocPpal.actualizar();
        jfaux.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_jbtnAceptarActionPerformed

    private void jbtnAñadirDestinoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAñadirDestinoActionPerformed
        jfrmRastreabilidad jfRastreabilidad = new jfrmRastreabilidad(auxDocPpal, this, "destino");
        jfRastreabilidad.setLocationRelativeTo(null);
        jfRastreabilidad.setVisible(true);
        this.setEnabled(false);
    }//GEN-LAST:event_jbtnAñadirDestinoActionPerformed

    private void jbtnEliminarDestinoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEliminarDestinoActionPerformed
        if (!jlstDestino.isSelectionEmpty()) {
            getModelo_destino().removeElement(getJlstDestino().getSelectedValue().toString());
        }
    }//GEN-LAST:event_jbtnEliminarDestinoActionPerformed

    private void jbtnAñadirOrigenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAñadirOrigenActionPerformed
        jfrmRastreabilidad jfRastreabilidad = new jfrmRastreabilidad(auxDocPpal, this, "origen");
        jfRastreabilidad.setLocationRelativeTo(null);
        jfRastreabilidad.setVisible(true);
        this.setEnabled(false);
    }//GEN-LAST:event_jbtnAñadirOrigenActionPerformed

    private void jbtnEliminarOrigenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEliminarOrigenActionPerformed
        if (!jlstOrigen.isSelectionEmpty()) {
            getModelo_origen().removeElement(getJlstOrigen().getSelectedValue().toString());
        }
    }//GEN-LAST:event_jbtnEliminarOrigenActionPerformed

    /**
     * @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(jfrmParrafo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(jfrmParrafo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(jfrmParrafo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(jfrmParrafo.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 jfrmParrafo().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabParrafo;
    private javax.swing.JButton jbtnAceptar;
    private javax.swing.JButton jbtnAñadirDestino;
    private javax.swing.JButton jbtnAñadirOrigen;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JButton jbtnEliminarDestino;
    private javax.swing.JButton jbtnEliminarOrigen;
    private javax.swing.JList jlstDestino;
    private javax.swing.JList jlstOrigen;
    private javax.swing.JPanel jpanParrafo;
    private javax.swing.JPanel jpanRastreabilidad;
    private javax.swing.JTextArea jtxaParrafo;
    private javax.swing.JTextField jtxtNombre;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the jlstDestino
     */
    public javax.swing.JList getJlstDestino() {
        return jlstDestino;
    }

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

    /**
     * @return the jlstOrigen
     */
    public javax.swing.JList getJlstOrigen() {
        return jlstOrigen;
    }

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

    /**
     * @return the modelo_destino
     */
    public DefaultListModel getModelo_destino() {
        return modelo_destino;
    }

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

    /**
     * @return the modelo_origen
     */
    public DefaultListModel getModelo_origen() {
        return modelo_origen;
    }

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