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

/*
 * jfrmCasodeUso.java
 *
 * Created on 22-nov-2011, 17:05:03
 */

package vista;

import java.awt.Component;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import modelo.CasodeUso;
import modelo.DocumentoReq;
import modelo.RendererArbol;

/**
 *
 * @author acc_local
 */
public class jfrmCasodeUso extends javax.swing.JFrame {
    DocumentoReq auxDocPpal;
    jfrmPrincipal jfaux;
    jfrmElementoCreado jElemento;
    MutableTreeNode auxinsertNode;
    int auxinsertIndex;

    static int instancias=0;
    boolean editable=false;
    CasodeUso CUaux;

    DefaultListModel lista = new DefaultListModel();

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

    //Constructor para poder editar luego
public jfrmCasodeUso(jfrmPrincipal jf, CasodeUso CU, boolean verdadero)
    {
        initComponents();
        auxinsertNode=(MutableTreeNode) jf.getJtreDocumento().getSelectionPath().getLastPathComponent();
        editable=verdadero;
        CUaux=CU;
        jfaux=jf;

        jtxtNombreCU.setText(CU.getNombre());
        jtxaEventoActivacion.setText(CU.getEventoActivacion());
        jtxtFrecuencia.setText(Integer.toString(CU.getFrecuencia()));
        jtxtUnidadFrecuencia.setText(CU.getUnidadFrecuencia());
        jtxaPrecondicion.setText(CU.getPrecondicion());
        jtxaPostcondicion.setText(CU.getPostcondicion());
        jtxtimportancia.setText(CU.getImportancia());
        jtxturgencia.setText(CU.getUrgencia());
        jtxtestadoDesarrollo.setText(CU.getEstadodeDesarrollo());
        jtxtestabilidad.setText(CU.getEstabilidad());
    }
    /** 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() {

        jTabCU = new javax.swing.JTabbedPane();
        jpanGeneral = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtxtNombreCU = new javax.swing.JTextField();
        jchbesAbstracto = new javax.swing.JCheckBox();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtxaEventoActivacion = new javax.swing.JTextArea();
        jtxtFrecuencia = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jtxtUnidadFrecuencia = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jpanDescripcion = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtxaPrecondicion = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtxaPostcondicion = new javax.swing.JTextArea();
        jpanPrioridad = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jtxtimportancia = new javax.swing.JTextField();
        jtxturgencia = new javax.swing.JTextField();
        jtxtestadoDesarrollo = new javax.swing.JTextField();
        jtxtestabilidad = new javax.swing.JTextField();
        jpanRastreabilidad = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jlstRastreable = new javax.swing.JList();
        jLabel11 = new javax.swing.JLabel();
        jbtnAnadir = new javax.swing.JButton();
        jbtnAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nuevo Caso de uso");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setText("Nombre:");

        jtxtNombreCU.setText("Prueba CU");

        jchbesAbstracto.setText("CU Abstracto");
        jchbesAbstracto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jchbesAbstractoActionPerformed(evt);
            }
        });

        jLabel2.setText("Evento de activacion:");

        jtxaEventoActivacion.setColumns(20);
        jtxaEventoActivacion.setRows(5);
        jtxaEventoActivacion.setText("PD");
        jScrollPane1.setViewportView(jtxaEventoActivacion);

        jtxtFrecuencia.setText("1");

        jLabel4.setText("veces al ");

        jtxtUnidadFrecuencia.setText("PD");
        jtxtUnidadFrecuencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtUnidadFrecuenciaActionPerformed(evt);
            }
        });

        jLabel3.setText("Frecuencia:");

        javax.swing.GroupLayout jpanGeneralLayout = new javax.swing.GroupLayout(jpanGeneral);
        jpanGeneral.setLayout(jpanGeneralLayout);
        jpanGeneralLayout.setHorizontalGroup(
            jpanGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanGeneralLayout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(jpanGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jchbesAbstracto, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jpanGeneralLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtxtNombreCU, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpanGeneralLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jpanGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                            .addGroup(jpanGeneralLayout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(4, 4, 4)
                                .addComponent(jtxtFrecuencia, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtxtUnidadFrecuencia, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(15, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        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(jtxtNombreCU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchbesAbstracto)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpanGeneralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtFrecuencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jtxtUnidadFrecuencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(16, 16, 16))
        );

        jTabCU.addTab("General", jpanGeneral);

        jLabel5.setText("Precondicion:");

        jtxaPrecondicion.setColumns(20);
        jtxaPrecondicion.setRows(5);
        jtxaPrecondicion.setText("PD");
        jScrollPane2.setViewportView(jtxaPrecondicion);

        jLabel6.setText("Postcondicion:");

        jtxaPostcondicion.setColumns(20);
        jtxaPostcondicion.setRows(5);
        jtxaPostcondicion.setText("PD");
        jScrollPane3.setViewportView(jtxaPostcondicion);

        javax.swing.GroupLayout jpanDescripcionLayout = new javax.swing.GroupLayout(jpanDescripcion);
        jpanDescripcion.setLayout(jpanDescripcionLayout);
        jpanDescripcionLayout.setHorizontalGroup(
            jpanDescripcionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanDescripcionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpanDescripcionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap(49, Short.MAX_VALUE))
        );
        jpanDescripcionLayout.setVerticalGroup(
            jpanDescripcionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanDescripcionLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36))
        );

        jTabCU.addTab("Pre/Post", jpanDescripcion);

        jLabel7.setText("Importancia:");

        jLabel8.setText("Urgencia:");

        jLabel9.setText("Estado de desarrollo:");

        jLabel10.setText("Estabilidad:");

        jtxtimportancia.setText("PD");

        jtxturgencia.setText("PD");
        jtxturgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxturgenciaActionPerformed(evt);
            }
        });

        jtxtestadoDesarrollo.setText("PD");

        jtxtestabilidad.setText("PD");
        jtxtestabilidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtestabilidadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpanPrioridadLayout = new javax.swing.GroupLayout(jpanPrioridad);
        jpanPrioridad.setLayout(jpanPrioridadLayout);
        jpanPrioridadLayout.setHorizontalGroup(
            jpanPrioridadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanPrioridadLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpanPrioridadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addGap(22, 22, 22)
                .addGroup(jpanPrioridadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtxtestabilidad)
                    .addComponent(jtxtestadoDesarrollo)
                    .addComponent(jtxturgencia)
                    .addComponent(jtxtimportancia, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))
                .addContainerGap(62, Short.MAX_VALUE))
        );
        jpanPrioridadLayout.setVerticalGroup(
            jpanPrioridadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanPrioridadLayout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(jpanPrioridadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jtxtimportancia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpanPrioridadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jtxturgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpanPrioridadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jtxtestadoDesarrollo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpanPrioridadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jtxtestabilidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(106, Short.MAX_VALUE))
        );

        jTabCU.addTab("Prioridad", jpanPrioridad);

        jScrollPane4.setViewportView(jlstRastreable);

        jLabel11.setText("Origen :");

        jbtnAnadir.setText("Añadir");
        jbtnAnadir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAnadirActionPerformed(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()
                .addGap(26, 26, 26)
                .addGroup(jpanRastreabilidadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11)
                    .addGroup(jpanRastreabilidadLayout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)
                        .addComponent(jbtnAnadir)))
                .addContainerGap(53, Short.MAX_VALUE))
        );
        jpanRastreabilidadLayout.setVerticalGroup(
            jpanRastreabilidadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanRastreabilidadLayout.createSequentialGroup()
                .addGroup(jpanRastreabilidadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpanRastreabilidadLayout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpanRastreabilidadLayout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(jbtnAnadir)))
                .addContainerGap(137, Short.MAX_VALUE))
        );

        jTabCU.addTab("Rastreabilidad", jpanRastreabilidad);

        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()
                        .addGap(156, 156, 156)
                        .addComponent(jbtnAceptar))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTabCU, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(56, Short.MAX_VALUE)
                .addComponent(jTabCU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnAceptar)
                .addContainerGap())
        );

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

    private void jchbesAbstractoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jchbesAbstractoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jchbesAbstractoActionPerformed

    private void jtxtUnidadFrecuenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtUnidadFrecuenciaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxtUnidadFrecuenciaActionPerformed

    private void jbtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAceptarActionPerformed

        if(!editable)
        {
            if(comprobarTexto())
            {
                if(!this.isNumeric(jtxtFrecuencia.getText()))
                {
                    mostrarmensaje("El campo frecuencia debe ser un número");
                }
                else
                {
                    ArrayList<String> listaAux = new ArrayList();
                    for(int i=0; i<jlstRastreable.getModel().getSize(); i++)
                    {
                        listaAux.add(jlstRastreable.getModel().getElementAt(i).toString());
                    }
                    CasodeUso newCU= auxDocPpal.nuevoCU("CU "+jtxtNombreCU.getText(),jchbesAbstracto.isSelected(),jtxaEventoActivacion.getText(),Integer.parseInt(jtxtFrecuencia.getText()),jtxtUnidadFrecuencia.getText(),jtxaPrecondicion.getText(),jtxaPostcondicion.getText(),jtxtimportancia.getText(),jtxturgencia.getText(),jtxtestadoDesarrollo.getText(),jtxtestabilidad.getText(), listaAux);
                    MutableTreeNode node=new DefaultMutableTreeNode(newCU.getNombre());
                    DefaultTreeModel model =(DefaultTreeModel) jfaux.getJtreDocumento().getModel();
                    model.insertNodeInto(node, auxinsertNode, auxinsertIndex);
                    mostrarmensaje("Insertado el "+newCU.getNombre());
                    jfaux.setEnabled(true);
                    jfaux.update(null, evt);
                    this.dispose();
                }
            }
            else
            {
                mostrarmensaje("Debes rellenar todos los campos!!");
            }
        }
        else
        {
            if(comprobarTexto())
            {
                int indice=jfaux.getDocPpal().getCasodeUso().lastIndexOf(CUaux);
                if(indice!=-1)
                {
                    //// Asigno los valores a los atributos
                    CUaux.setNombre(jtxtNombreCU.getText());
                    CUaux.setEsAbstracto(jchbesAbstracto.isSelected());
                    CUaux.setEventoActivacion(jtxaEventoActivacion.getText());
                    CUaux.setFrecuencia(Integer.parseInt(jtxtFrecuencia.getText()));
                    CUaux.setUnidadFrecuencia(jtxtUnidadFrecuencia.getText());
                    CUaux.setPrecondicion(jtxaPrecondicion.getText());
                    CUaux.setPostcondicion(jtxaPostcondicion.getText());
                    CUaux.setImportancia(jtxtimportancia.getText());
                    CUaux.setUrgencia(jtxturgencia.getText());
                    CUaux.setEstadodeDesarrollo(jtxtestadoDesarrollo.getText());
                    CUaux.setEstabilidad(jtxtestabilidad.getText());
                    ArrayList<String> listaAux = new ArrayList();
                    for(int j=0; j<jlstRastreable.getModel().getSize(); j++)
                    {
                        listaAux.add(jlstRastreable.getModel().getElementAt(j).toString());
                    }
                    CUaux.setDestinoRastreable(listaAux);

                    jfaux.getDocPpal().getCasodeUso().set(indice, CUaux);
                    //refrescamos el documento
                    jfaux.update(CUaux, evt);
                    //modificamos el nodo del jtree con el nuevo nombre
                    TreePath tp = jfaux.getJtreDocumento().getSelectionPath();
                    //el elemento a borrar
                    //MutableTreeNode seleccionado= (MutableTreeNode)
                    tp.getLastPathComponent();
                    MutableTreeNode seleccionado = (MutableTreeNode) (TreeNode)tp.getLastPathComponent();
                    
                    for(int i=0; i<tp.getPathCount();i++)
                    {
                        if(tp.getPathComponent(i).toString().equals(seleccionado.toString()))
                        {

                            MutableTreeNode node=new DefaultMutableTreeNode(CUaux.getNombre());
                            DefaultTreeModel model =(DefaultTreeModel) jfaux.getJtreDocumento().getModel();
                            MutableTreeNode padre_a_insertar = (MutableTreeNode) seleccionado.getParent();
                            int indice_a_insertar=model.getIndexOfChild(seleccionado.getParent(),seleccionado);
                            model.removeNodeFromParent(seleccionado);
                            
                            model.insertNodeInto(node, padre_a_insertar,indice_a_insertar);
                        }
                    }
                }
                jfaux.setEnabled(true);
                jfaux.update(null, evt);
                this.dispose();
            }

            else
            {
                mostrarmensaje("Debes rellenar todos los campos!!");
            }
        }
        
    }//GEN-LAST:event_jbtnAceptarActionPerformed

    private boolean comprobarTexto()
    {
        if(jtxtNombreCU.getText().equals("") ||
                jtxaEventoActivacion.getText().equals("") ||
                jtxtFrecuencia.getText().equals("") ||
                jtxaPrecondicion.getText().equals("") ||
                jtxaPostcondicion.getText().equals("") ||
                jtxtimportancia.getText().equals("") ||
                jtxturgencia.getText().equals("") ||
                jtxtestadoDesarrollo.getText().equals("") ||
                jtxtestabilidad.getText().equals(""))
        {
            return false;
        }

        return true;
    }

    private static boolean isNumeric(String  cadena){
	try {
		Integer .parseInt(cadena);
		return true;
	} catch (NumberFormatException  nfe){
		return false;
	}
    }

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

    private void jtxtestabilidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtestabilidadActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxtestabilidadActionPerformed

    private void jbtnAnadirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAnadirActionPerformed
       jElemento.setVisible(true);
       jElemento.setDestino(this);
       jElemento.setNombre_tipo("CASOUSO");
    }//GEN-LAST:event_jbtnAnadirActionPerformed

    private void jtxturgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxturgenciaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxturgenciaActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    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.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabCU;
    private javax.swing.JButton jbtnAceptar;
    private javax.swing.JButton jbtnAnadir;
    private javax.swing.JCheckBox jchbesAbstracto;
    private javax.swing.JList jlstRastreable;
    private javax.swing.JPanel jpanDescripcion;
    private javax.swing.JPanel jpanGeneral;
    private javax.swing.JPanel jpanPrioridad;
    private javax.swing.JPanel jpanRastreabilidad;
    private javax.swing.JTextArea jtxaEventoActivacion;
    private javax.swing.JTextArea jtxaPostcondicion;
    private javax.swing.JTextArea jtxaPrecondicion;
    private javax.swing.JTextField jtxtFrecuencia;
    private javax.swing.JTextField jtxtNombreCU;
    private javax.swing.JTextField jtxtUnidadFrecuencia;
    private javax.swing.JTextField jtxtestabilidad;
    private javax.swing.JTextField jtxtestadoDesarrollo;
    private javax.swing.JTextField jtxtimportancia;
    private javax.swing.JTextField jtxturgencia;
    // End of variables declaration//GEN-END:variables


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

    public void setRastre(Object t)
    {
        lista.addElement(t);
        jlstRastreable.setModel((ListModel) lista);
        jlstRastreable.validate();
    }
}
