/*
 * 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.awt.event.MouseEvent;
import java.util.Observer;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.UIManager;
import modelo.*;
import java.io.File;
import java.io.StringWriter;
import java.util.Enumeration;
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 modelo.DocumentoReq;
import java.util.Observable;
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 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();
    Document document = null;
    Element raiz = null;
    Result result = null;

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

        initComponents();
        docPpal=documentoPpal;
        //CasodeUso docCasodeUso;

        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>");
//        docPpal.addObserver(this);
//        jlstCasodeUso.setListData(docPpal.getData("CasodeUso").toArray(new Object[] {}));
    }

    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();
        jlblMensaje = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtxpDocumento = new javax.swing.JTextPane();
        jToolBar2 = new javax.swing.JToolBar();
        jbtnSeccion = new javax.swing.JButton();
        jbtnParrafo = new javax.swing.JButton();
        jbtnGrafico = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jbtnOrganizacion = new javax.swing.JButton();
        jbtnParticipante = new javax.swing.JButton();
        jbtnReunion = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jbtnObjetivo = new javax.swing.JButton();
        jbtnSubobjetivo = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        jbtnRInformacion = new javax.swing.JButton();
        jbtnDEspecifico = new javax.swing.JButton();
        jbtnRRestriccion = new javax.swing.JButton();
        jbtnRFuncional = new javax.swing.JButton();
        jbtnRNoFuncional = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        jbtnCasoUso = new javax.swing.JButton();
        jbtnPaso = new javax.swing.JButton();
        jbtnExcepcion = new javax.swing.JButton();
        jmnuBarraPpal = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = 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));
        jtreDocumento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jtreDocumentoMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jtreDocumento);

        jScrollPane2.setViewportView(jtxpDocumento);

        jToolBar2.setRollover(true);

        jbtnSeccion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nueva_Seccion.png"))); // NOI18N
        jbtnSeccion.setText("Seccion");
        jbtnSeccion.setFocusable(false);
        jbtnSeccion.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnSeccion.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnSeccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSeccionActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnSeccion);

        jbtnParrafo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nuevo_Parrafo.png"))); // NOI18N
        jbtnParrafo.setText("Parrafo");
        jbtnParrafo.setFocusable(false);
        jbtnParrafo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnParrafo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnParrafo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnParrafoActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnParrafo);

        jbtnGrafico.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nuevo_Grafico.png"))); // NOI18N
        jbtnGrafico.setText("Grafico");
        jbtnGrafico.setFocusable(false);
        jbtnGrafico.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnGrafico.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnGrafico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGraficoActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnGrafico);
        jToolBar2.add(jSeparator1);

        jbtnOrganizacion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nueva_Organizacion.png"))); // NOI18N
        jbtnOrganizacion.setText("Organizacion");
        jbtnOrganizacion.setFocusable(false);
        jbtnOrganizacion.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnOrganizacion.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnOrganizacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnOrganizacionActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnOrganizacion);

        jbtnParticipante.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nuevo_Participante.png"))); // NOI18N
        jbtnParticipante.setText("Participante");
        jbtnParticipante.setFocusable(false);
        jbtnParticipante.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnParticipante.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnParticipante.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnParticipanteActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnParticipante);

        jbtnReunion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nueva_Reunion.png"))); // NOI18N
        jbtnReunion.setText("Reunion");
        jbtnReunion.setFocusable(false);
        jbtnReunion.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnReunion.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnReunion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnReunionActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnReunion);
        jToolBar2.add(jSeparator2);

        jbtnObjetivo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nuevo_Objetivo.png"))); // NOI18N
        jbtnObjetivo.setText("Objetivo");
        jbtnObjetivo.setFocusable(false);
        jbtnObjetivo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnObjetivo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnObjetivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnObjetivoActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnObjetivo);

        jbtnSubobjetivo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nuevo_Subobjetivo.png"))); // NOI18N
        jbtnSubobjetivo.setText("SubObjetivo");
        jbtnSubobjetivo.setFocusable(false);
        jbtnSubobjetivo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnSubobjetivo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnSubobjetivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSubobjetivoActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnSubobjetivo);
        jToolBar2.add(jSeparator3);

        jbtnRInformacion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nuevo_RI.png"))); // NOI18N
        jbtnRInformacion.setText("R.Informacion");
        jbtnRInformacion.setFocusable(false);
        jbtnRInformacion.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnRInformacion.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnRInformacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRInformacionActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnRInformacion);

        jbtnDEspecifico.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nuevo_DEspecifico.png"))); // NOI18N
        jbtnDEspecifico.setText("Dato Especifico");
        jbtnDEspecifico.setFocusable(false);
        jbtnDEspecifico.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnDEspecifico.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnDEspecifico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDEspecificoActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnDEspecifico);

        jbtnRRestriccion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nuevo_RR.png"))); // NOI18N
        jbtnRRestriccion.setText("R.Restriccion");
        jbtnRRestriccion.setFocusable(false);
        jbtnRRestriccion.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnRRestriccion.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnRRestriccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRRestriccionActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnRRestriccion);

        jbtnRFuncional.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nuevo_Funcional.png"))); // NOI18N
        jbtnRFuncional.setText("R.Funcional");
        jbtnRFuncional.setFocusable(false);
        jbtnRFuncional.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnRFuncional.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnRFuncional.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRFuncionalActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnRFuncional);

        jbtnRNoFuncional.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nuevo_RNF.png"))); // NOI18N
        jbtnRNoFuncional.setText("R. No Funcional");
        jbtnRNoFuncional.setFocusable(false);
        jbtnRNoFuncional.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnRNoFuncional.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnRNoFuncional.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRNoFuncionalActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnRNoFuncional);
        jToolBar2.add(jSeparator4);

        jbtnCasoUso.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nuevo_CasoUso.png"))); // NOI18N
        jbtnCasoUso.setText("Caso de Uso");
        jbtnCasoUso.setFocusable(false);
        jbtnCasoUso.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnCasoUso.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnCasoUso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCasoUsoActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnCasoUso);

        jbtnPaso.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nuevo_Paso.png"))); // NOI18N
        jbtnPaso.setText("Paso");
        jbtnPaso.setFocusable(false);
        jbtnPaso.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnPaso.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnPaso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPasoActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnPaso);

        jbtnExcepcion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/iconos/Nueva_Excepcion.png"))); // NOI18N
        jbtnExcepcion.setText("Excepcion");
        jbtnExcepcion.setFocusable(false);
        jbtnExcepcion.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtnExcepcion.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtnExcepcion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnExcepcionActionPerformed(evt);
            }
        });
        jToolBar2.add(jbtnExcepcion);

        jMenu1.setText("Archivo");

        jMenuItem1.setText("Nuevo");
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Guardar");
        jMenu1.add(jMenuItem2);

        jMenuItem3.setText("Abrir");
        jMenu1.add(jMenuItem3);

        jMenuItem4.setText("Imprimir");
        jMenu1.add(jMenuItem4);

        jmnuBarraPpal.add(jMenu1);

        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()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(209, 209, 209)
                                .addComponent(jlblMensaje, javax.swing.GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 830, Short.MAX_VALUE))))
                    .addComponent(jToolBar2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 951, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .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, 230, Short.MAX_VALUE))
                .addGap(23, 23, 23))
        );

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

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

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


        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);
        } // TODO add your handling code here:
}//GEN-LAST:event_jbtnObjetivoActionPerformed

            private void jbtnRInformacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRInformacionActionPerformed
              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);

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

            private void jbtnCasoUsoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCasoUsoActionPerformed
                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);


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

            private void jbtnSeccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSeccionActionPerformed
                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;
            }
          jfrmSeccionApendice jfaux=new jfrmSeccionApendice(this, docPpal, insertNode, insertIndex);

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

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

            private void jbtnParrafoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnParrafoActionPerformed
        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;
               }
            jfrmParrafoGlosario jfaux=new jfrmParrafoGlosario(this, docPpal, insertNode, insertIndex);

            jfaux.setVisible(true);
            this.setEnabled(false);
            }
         // TODO add your handling code here:
            }//GEN-LAST:event_jbtnParrafoActionPerformed

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

        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;
            }
          jfrmGrafico jfaux=new jfrmGrafico(this, docPpal, insertNode, insertIndex);

            jfaux.setVisible(true);
            this.setEnabled(false);  // TODO add your handling code here:
            }//GEN-LAST:event_jbtnGraficoActionPerformed
        }

                              private void jbtnOrganizacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnOrganizacionActionPerformed
                                 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_jbtnOrganizacionActionPerformed

                              private void jbtnRRestriccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRRestriccionActionPerformed
                                            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_jbtnRRestriccionActionPerformed

                              private void jbtnRFuncionalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRFuncionalActionPerformed
               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);
        }                           // TODO add your handling code here:
                              }//GEN-LAST:event_jbtnRFuncionalActionPerformed

                              private void jbtnParticipanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnParticipanteActionPerformed
                            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);

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

                              private void jbtnReunionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnReunionActionPerformed
                           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);

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

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

           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_jbtnRNoFuncionalActionPerformed

                              private void jbtnDEspecificoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDEspecificoActionPerformed
                               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);
            }
        }           // TODO add your handling code here:
}//GEN-LAST:event_jbtnDEspecificoActionPerformed

                              private void jbtnSubobjetivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSubobjetivoActionPerformed
         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);
            }

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

                              private void jtreDocumentoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtreDocumentoMouseReleased
                                  // TODO add your handling code here:
                              }//GEN-LAST:event_jtreDocumentoMouseReleased

                              private void jbtnPasoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPasoActionPerformed
                                  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_jbtnPasoActionPerformed

                              private void jbtnExcepcionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnExcepcionActionPerformed
                                  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_jbtnExcepcionActionPerformed

 // }


      
    /**
    * @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.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JButton jbtnCasoUso;
    private javax.swing.JButton jbtnDEspecifico;
    private javax.swing.JButton jbtnExcepcion;
    private javax.swing.JButton jbtnGrafico;
    private javax.swing.JButton jbtnObjetivo;
    private javax.swing.JButton jbtnOrganizacion;
    private javax.swing.JButton jbtnParrafo;
    private javax.swing.JButton jbtnParticipante;
    private javax.swing.JButton jbtnPaso;
    private javax.swing.JButton jbtnRFuncional;
    private javax.swing.JButton jbtnRInformacion;
    private javax.swing.JButton jbtnRNoFuncional;
    private javax.swing.JButton jbtnRRestriccion;
    private javax.swing.JButton jbtnReunion;
    private javax.swing.JButton jbtnSeccion;
    private javax.swing.JButton jbtnSubobjetivo;
    private javax.swing.JLabel jlblMensaje;
    private javax.swing.JMenuItem jmniSalir;
    private javax.swing.JMenuBar jmnuBarraPpal;
    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);
        }
//jlstCasodeUso.setListData(docCasodeUso.getData("CasodeUso").toArray(new Object[] {}));
       
    
    }

   
}
