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

/*
 * jfrmPrincipal.java
 *
 * Created on 15-nov-2011, 16:34:51
 */

package vista;

import java.io.File;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import modelo.*;
import modelo.Requisito_informacion;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


/**
 *
 * @author acc_local
 */
public class jfrmPrincipal extends javax.swing.JFrame implements Observer{
    //DocumentoReq docPpal=new DocumentoReq();
    //CasodeUso docCasodeUso=new CasodeUso();
    //PasoCU docPaso=new PasoCU();
    //Requisito_informacion docRI=new Requisito_informacion();
    DocumentoReq docPpal;
    CasodeUso docCasodeUso;
    //PasoCU docPaso;
    Requisito_funcional docRequisitofuncional=new Requisito_funcional();
    Requisito_no_funcional docRequisitonofuncional=new Requisito_no_funcional();
    Requisito_restriccion docRequisitoRestriccion=new Requisito_restriccion();
    Requisito_informacion docRequisitoInformacion=new Requisito_informacion();
    Organizacion docOrganizacion=new Organizacion();
    Participante docParticipante=new Participante();
    Reunion docReunion=new Reunion();
    Objetivo docObjetivo=new Objetivo();

    /** Creates new form jfrmPrincipal */
    public jfrmPrincipal(DocumentoReq documentoPpal) {

        initComponents();
        docPpal=documentoPpal;

        jtxpDocumento.setContentType("text/html");
//        jtxpDocumento.setText("<b>hola</b><br>" + "<i>adios</i><br>" +
//        "<font face=\"arial\">fuente arial</font><br>" +
//        "<font face=\"courier\">fuente courier</font><br>" +
//        "<font size=\"24\">fuente grande</font><br>" +
//        "<font color=\"red\">color rojo</font><br>");
    }

    private void imprimirNodos(DefaultMutableTreeNode root) {

//        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//        try{
//        DocumentBuilder builder = factory.newDocumentBuilder();
//        DOMImplementation implementation = builder.getDOMImplementation();
//        Document document = implementation.createDocument(null, "xml", null);
//        Element raiz = document.createElement("Documento"); // creamos el elemento raiz
//        document.setXmlVersion("1.0"); // asignamos la version de nuestro XML
//        document.getDocumentElement().appendChild(raiz); //pegamos la raiz al documento
//        Result result = new StreamResult(new java.io.File("resultado.xml"));

        if(root.getChildCount()>0){
        for(Enumeration e=root.children(); e.hasMoreElements();){
        TreeNode node=(TreeNode) e.nextElement();
                imprimirNodos((DefaultMutableTreeNode) node);
        //docPpal.toXML(node.getUserObject(),result,document,raiz);
        }

        //try {
        // DefaultTreeModel model=(DefaultTreeModel) this.getJtreDocumento().getModel();
        // DefaultMutableTreeNode root=(DefaultMutableTreeNode) model.getRoot();
        // imprimirNodos(root);
        
        //docPpal.toXML(node.getUserObject(),result,document,raiz);

        //this.transformar();
        
//        } catch (Exception ex)
//        {
//        Logger.getLogger(jfrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        }
//        }catch(Exception e){System.err.println("Error: "+e);}
//        jtxpDocumento.append(root.toString()+"\n");
//
        
    }
    }
    /** 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() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtreDocumento = new javax.swing.JTree();
        jbtnNuevoElemento = new javax.swing.JButton();
        jbtnLeeArbol = new javax.swing.JButton();
        jlblMensaje = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtxpDocumento = new javax.swing.JTextPane();
        jmnuBarraPpal = new javax.swing.JMenuBar();
        jmnuCU = new javax.swing.JMenu();
        jmniNuevoCU = new javax.swing.JMenuItem();
        jmniAnnadirPaso = new javax.swing.JMenuItem();
        jmniAnnadirExcepcion = new javax.swing.JMenuItem();
        jmnRequisito = new javax.swing.JMenu();
        jmniFuncional = new javax.swing.JMenuItem();
        jmniNoFuncional = new javax.swing.JMenuItem();
        jmniRestriccion = new javax.swing.JMenuItem();
        jmniInformacion = new javax.swing.JMenuItem();
        jmniDatoEspecifico = new javax.swing.JMenuItem();
        jOrganizacion = new javax.swing.JMenu();
        jNuevaOrganizacion = new javax.swing.JMenuItem();
        jReunion = new javax.swing.JMenu();
        jNuevaReunion = new javax.swing.JMenuItem();
        jParticipante = new javax.swing.JMenu();
        jNuevoParticipante = new javax.swing.JMenuItem();
        jObjetivo = new javax.swing.JMenu();
        jNuevoObjetivo = new javax.swing.JMenuItem();
        jNuevoSubObjetivo = new javax.swing.JMenuItem();
        jmnuSalir = new javax.swing.JMenu();
        jmniSalir = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Documento ER");
        jtreDocumento.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane1.setViewportView(jtreDocumento);

        jbtnNuevoElemento.setText("Nuevo elemento");
        jbtnNuevoElemento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNuevoElementoActionPerformed(evt);
            }
        });

        jbtnLeeArbol.setText("Lee arbol");
        jbtnLeeArbol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLeeArbolActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(jtxpDocumento);

        jmnuCU.setText("CU");

        jmniNuevoCU.setText("Nuevo");
        jmniNuevoCU.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniNuevoCUActionPerformed(evt);
            }
        });
        jmnuCU.add(jmniNuevoCU);

        jmniAnnadirPaso.setText("Añadir Paso");
        jmniAnnadirPaso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniAnnadirPasoActionPerformed(evt);
            }
        });
        jmnuCU.add(jmniAnnadirPaso);

        jmniAnnadirExcepcion.setText("Añadir Excepcion");
        jmniAnnadirExcepcion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniAnnadirExcepcionActionPerformed(evt);
            }
        });
        jmnuCU.add(jmniAnnadirExcepcion);

        jmnuBarraPpal.add(jmnuCU);

        jmnRequisito.setText("Requisito");

        jmniFuncional.setText("Funcional");
        jmniFuncional.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniFuncionalActionPerformed(evt);
            }
        });
        jmnRequisito.add(jmniFuncional);

        jmniNoFuncional.setText("No Funcional");
        jmniNoFuncional.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniNoFuncionalActionPerformed(evt);
            }
        });
        jmnRequisito.add(jmniNoFuncional);

        jmniRestriccion.setText("Restriccion");
        jmniRestriccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniRestriccionActionPerformed(evt);
            }
        });
        jmnRequisito.add(jmniRestriccion);

        jmniInformacion.setText("Informacion");
        jmniInformacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniInformacionActionPerformed(evt);
            }
        });
        jmnRequisito.add(jmniInformacion);

        jmniDatoEspecifico.setText("Dato Especifico");
        jmniDatoEspecifico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniDatoEspecificoActionPerformed(evt);
            }
        });
        jmnRequisito.add(jmniDatoEspecifico);

        jmnuBarraPpal.add(jmnRequisito);

        jOrganizacion.setText("Organizacion");

        jNuevaOrganizacion.setText("Nueva Organizacion");
        jNuevaOrganizacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNuevaOrganizacionActionPerformed(evt);
            }
        });
        jOrganizacion.add(jNuevaOrganizacion);

        jmnuBarraPpal.add(jOrganizacion);

        jReunion.setText("Reunion");

        jNuevaReunion.setText("Nueva Reunion");
        jNuevaReunion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNuevaReunionActionPerformed(evt);
            }
        });
        jReunion.add(jNuevaReunion);

        jmnuBarraPpal.add(jReunion);

        jParticipante.setText("Participante");

        jNuevoParticipante.setText("Nuevo Participante");
        jNuevoParticipante.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNuevoParticipanteActionPerformed(evt);
            }
        });
        jParticipante.add(jNuevoParticipante);

        jmnuBarraPpal.add(jParticipante);

        jObjetivo.setText("Objetivo");

        jNuevoObjetivo.setText("Nuevo Objetivo");
        jNuevoObjetivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNuevoObjetivoActionPerformed(evt);
            }
        });
        jObjetivo.add(jNuevoObjetivo);

        jNuevoSubObjetivo.setText("Nuevo SubObjetivo");
        jNuevoSubObjetivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNuevoSubObjetivoActionPerformed(evt);
            }
        });
        jObjetivo.add(jNuevoSubObjetivo);

        jmnuBarraPpal.add(jObjetivo);

        jmnuSalir.setText("Salir");

        jmniSalir.setText("Salir");
        jmniSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniSalirActionPerformed(evt);
            }
        });
        jmnuSalir.add(jmniSalir);

        jmnuBarraPpal.add(jmnuSalir);

        setJMenuBar(jmnuBarraPpal);

        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.TRAILING, false)
                    .addComponent(jbtnNuevoElemento, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtnLeeArbol, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(97, 97, 97)
                        .addComponent(jlblMensaje, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                        .addContainerGap())
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(24, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnNuevoElemento)
                    .addComponent(jbtnLeeArbol)
                    .addComponent(jlblMensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)))
        );

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

    private void jbtnNuevoElementoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNuevoElementoActionPerformed
        TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) insertNode.getParent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
            jfrmNuevoElemento jele = new jfrmNuevoElemento(this, insertNode, insertIndex);
            jele.setVisible(true);
            this.setEnabled(false);

        }



    }//GEN-LAST:event_jbtnNuevoElementoActionPerformed

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

//        try {
//        // DefaultTreeModel model=(DefaultTreeModel) this.getJtreDocumento().getModel();
//        // DefaultMutableTreeNode root=(DefaultMutableTreeNode) model.getRoot();
//        // imprimirNodos(root);
//        docPpal.toXML();
//        String res = transformar();
//        } catch (Exception ex) {
//        Logger.getLogger(jfrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }//GEN-LAST:event_jbtnLeeArbolActionPerformed

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

        TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
           if(tp.getLastPathComponent().toString().contains("Documento ")){
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) insertNode.getParent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
            //jfrmNuevoElemento jele = new jfrmNuevoElemento(this, insertNode, insertIndex);
            jfrmCasodeUso jfCU=new jfrmCasodeUso(this, docPpal, insertNode, insertIndex);
            
            jfCU.setVisible(true);
            this.setEnabled(false);
            
        
        }
        }


    }//GEN-LAST:event_jmniNuevoCUActionPerformed

    private void jmniSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniSalirActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jmniSalirActionPerformed

    private void jmniAnnadirPasoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniAnnadirPasoActionPerformed
        TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
            if(tp.getLastPathComponent().toString().contains("CasodeUso ")){
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) tp.getLastPathComponent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }

            CasodeUso tipocu= (CasodeUso) docPpal.buscarpornombre(insertNode.toString());

            
            //buscar cu por nombre y pasar instancia a formulario
            //jfrmNuevoElemento jele = new jfrmNuevoElemento(this, insertNode, insertIndex);
            jfrmNuevoPaso jfpaso=new jfrmNuevoPaso(this, docPpal, insertNode, insertIndex, tipocu);

            jfpaso.setVisible(true);
            this.setEnabled(false);
        }
        }
}//GEN-LAST:event_jmniAnnadirPasoActionPerformed

    private void jmniAnnadirExcepcionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniAnnadirExcepcionActionPerformed
        TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
            if(tp.getLastPathComponent().toString().contains("CasodeUso ")){
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) tp.getLastPathComponent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
            CasodeUso tipocu= (CasodeUso) docPpal.buscarpornombre(insertNode.toString());
            //PasoCU tipopaso= (PasoCU) docPpal.buscarpornombre(insertNode.toString());
            //jfrmNuevoElemento jele = new jfrmNuevoElemento(this, insertNode, insertIndex);
            jfrmNuevaExcepcion jaux=new jfrmNuevaExcepcion(this, docPpal, insertNode, insertIndex, tipocu);

            jaux.setVisible(true);
            this.setEnabled(false);
        }
        }
}//GEN-LAST:event_jmniAnnadirExcepcionActionPerformed

    private void jmniFuncionalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniFuncionalActionPerformed
        TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) insertNode.getParent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
            //jfrmNuevoElemento jele = new jfrmNuevoElemento(this, insertNode, insertIndex);
            jfrmRequisitoFuncional jaux=new jfrmRequisitoFuncional(this, docPpal, insertNode, insertIndex);

            jaux.setVisible(true);
            this.setEnabled(false);
        }
    }//GEN-LAST:event_jmniFuncionalActionPerformed

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

           TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) insertNode.getParent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
          jfrmRequisitoNoFuncional jaux=new jfrmRequisitoNoFuncional(this, docPpal, insertNode, insertIndex);

            jaux.setVisible(true);
            this.setEnabled(false);

        }

        // TODO add your handling code here:
    }//GEN-LAST:event_jmniNoFuncionalActionPerformed

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

           TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) insertNode.getParent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
          jfrmRequisitoRestriccion jaux=new jfrmRequisitoRestriccion(this, docPpal, insertNode, insertIndex);

            jaux.setVisible(true);
            this.setEnabled(false);

        }


        // TODO add your handling code here:
    }//GEN-LAST:event_jmniRestriccionActionPerformed

    private void jmniInformacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniInformacionActionPerformed
        TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) insertNode.getParent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
          jfrmRequisitoInformacion jaux=new jfrmRequisitoInformacion(this, docPpal, insertNode, insertIndex);

            jaux.setVisible(true);
            this.setEnabled(false);

        }
    }//GEN-LAST:event_jmniInformacionActionPerformed


    private void jmniDatoEspecificoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniDatoEspecificoActionPerformed
        TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
            if(tp.getLastPathComponent().toString().contains("RequisitoInformacion ")){
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) tp.getLastPathComponent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
            Requisito_informacion tipori= (Requisito_informacion) docPpal.buscarpornombre(insertNode.toString());
            //jfrmNuevoElemento jele = new jfrmNuevoElemento(this, insertNode, insertIndex);
            jfrmDatoEspecifico jfdato=new jfrmDatoEspecifico(this, docPpal, insertNode, insertIndex, tipori);

            jfdato.setVisible(true);
            this.setEnabled(false);
            }
        }
    }//GEN-LAST:event_jmniDatoEspecificoActionPerformed

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

         TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
           //if(tp.getLastPathComponent().toString().contains("Documento ")){
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) insertNode.getParent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
            //jfrmNuevoElemento jele = new jfrmNuevoElemento(this, insertNode, insertIndex);
            jfrmOrganizacion jfOrg=new jfrmOrganizacion(this, docPpal, insertNode, insertIndex);

            jfOrg.setVisible(true);
            this.setEnabled(false);


        //}
        }


        // TODO add your handling code here:
    }//GEN-LAST:event_jNuevaOrganizacionActionPerformed

    private void jNuevoParticipanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNuevoParticipanteActionPerformed
        TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) insertNode.getParent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
          jfrmParticipantes jfaux=new jfrmParticipantes(this, docPpal, insertNode, insertIndex);

            jfaux.setVisible(true);
            this.setEnabled(false);

        }
    }//GEN-LAST:event_jNuevoParticipanteActionPerformed

    private void jNuevaReunionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNuevaReunionActionPerformed
        TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) insertNode.getParent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
          jfrmReunion jaux=new jfrmReunion(this, docPpal, insertNode, insertIndex);

            jaux.setVisible(true);
            this.setEnabled(false);

        }
    }//GEN-LAST:event_jNuevaReunionActionPerformed

    private void jNuevoObjetivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNuevoObjetivoActionPerformed
        // TODO add your handling code here:
        TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) insertNode.getParent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
          jfrmObjetivo jaux=new jfrmObjetivo(this, docPpal, insertNode, insertIndex);

            jaux.setVisible(true);
            this.setEnabled(false);
        }
    }//GEN-LAST:event_jNuevoObjetivoActionPerformed

        private void jNuevoSubObjetivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNuevoSubObjetivoActionPerformed
            // TODO add your handling code here:
            TreePath tp = getJtreDocumento().getSelectionPath();
        if(tp==null){
            jlblMensaje.setText("Seleccione un nodo, antes");
        } else {
            if(tp.getLastPathComponent().toString().contains("Obj ")){
            MutableTreeNode insertNode= (MutableTreeNode) tp.getLastPathComponent();

            int insertIndex=0;
            if(insertNode.getParent()!=null){
                MutableTreeNode parent=(MutableTreeNode) tp.getLastPathComponent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
            Objetivo tiposo= (Objetivo) docPpal.buscarpornombre(insertNode.toString());
            //jfrmNuevoElemento jele = new jfrmNuevoElemento(this, insertNode, insertIndex);
            jfrmSubObjetivo jfso=new jfrmSubObjetivo(this, docPpal, insertNode, insertIndex, tiposo);

            jfso.setVisible(true);
            this.setEnabled(false);
            }

        }//GEN-LAST:event_jNuevoSubObjetivoActionPerformed

    }
      
    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new jfrmPrincipal().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem jNuevaOrganizacion;
    private javax.swing.JMenuItem jNuevaReunion;
    private javax.swing.JMenuItem jNuevoObjetivo;
    private javax.swing.JMenuItem jNuevoParticipante;
    private javax.swing.JMenuItem jNuevoSubObjetivo;
    private javax.swing.JMenu jObjetivo;
    private javax.swing.JMenu jOrganizacion;
    private javax.swing.JMenu jParticipante;
    private javax.swing.JMenu jReunion;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnLeeArbol;
    private javax.swing.JButton jbtnNuevoElemento;
    private javax.swing.JLabel jlblMensaje;
    private javax.swing.JMenu jmnRequisito;
    private javax.swing.JMenuItem jmniAnnadirExcepcion;
    private javax.swing.JMenuItem jmniAnnadirPaso;
    private javax.swing.JMenuItem jmniDatoEspecifico;
    private javax.swing.JMenuItem jmniFuncional;
    private javax.swing.JMenuItem jmniInformacion;
    private javax.swing.JMenuItem jmniNoFuncional;
    private javax.swing.JMenuItem jmniNuevoCU;
    private javax.swing.JMenuItem jmniRestriccion;
    private javax.swing.JMenuItem jmniSalir;
    private javax.swing.JMenuBar jmnuBarraPpal;
    private javax.swing.JMenu jmnuCU;
    private javax.swing.JMenu jmnuSalir;
    private javax.swing.JTree jtreDocumento;
    private javax.swing.JTextPane jtxpDocumento;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the jtreDocumento
     */
    public javax.swing.JTree getJtreDocumento() {
        return jtreDocumento;
    }

    /**
     * @param jtreDocumento the jtreDocumento to set
     */

    public void setJtreDocumento(javax.swing.JTree jtreDocumento) {
        this.jtreDocumento = jtreDocumento;
    }

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

    public String transformar() throws Exception
    {
        String xmlOrigen = "resultado.xml";
        String xslOrigen = "src/modelo/XMLDocumentoReq1.xsl";
        Source xmlSource = new StreamSource(new File(xmlOrigen));
        Source xsltSource = new StreamSource(new File(xslOrigen));
        StringWriter cadenaSalida = new StringWriter();
        Result bufferResultado = new StreamResult(cadenaSalida);
        TransformerFactory factoriaTrans = TransformerFactory.newInstance();
        Transformer transformador = factoriaTrans.newTransformer(xsltSource);
        transformador.transform(xmlSource, bufferResultado);
        jtxpDocumento.setText(cadenaSalida.toString());
        //guardar html en fichero físico
        return cadenaSalida.toString();
    }

    public void update(Observable o, Object arg) {

       
//        DefaultTreeModel model =(DefaultTreeModel) this.getJtreDocumento().getModel();
//        DefaultMutableTreeNode root=(DefaultMutableTreeNode) model.getRoot();
//        this.imprimirNodos(root);
        try{
        docPpal.toXML();
            String res = transformar();
        } catch (Exception ex) {
            Logger.getLogger(jfrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }

       
    
    }

   
}
