/*
 * 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.util.Enumeration;
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.*;


/**
 *
 * @author acc_local
 */
public class jfrmPrincipal extends javax.swing.JFrame {
    DocumentoReq docPpal=new DocumentoReq();
    CasodeUso docCasodeUso=new CasodeUso();
    PasoCU docPaso=new PasoCU();

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

        initComponents();
    }

    private void imprimirNodos(DefaultMutableTreeNode root) {
        jtxaDocumento.append(root.toString()+"\n");

        if(root.getChildCount()>0){
            for(Enumeration e=root.children(); e.hasMoreElements();){
                TreeNode node=(TreeNode) e.nextElement();
                imprimirNodos((DefaultMutableTreeNode) node);
            }
        }
    }

    /** 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();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtxaDocumento = new javax.swing.JTextArea();
        jbtnNuevoElemento = new javax.swing.JButton();
        jbtnLeeArbol = new javax.swing.JButton();
        jlblMensaje = new javax.swing.JLabel();
        jmnuBarraPpal = new javax.swing.JMenuBar();
        jmnuCU = new javax.swing.JMenu();
        jmniNuevoCU = new javax.swing.JMenuItem();
        jmniAñadirPaso = new javax.swing.JMenuItem();
        jmniAñadirExcepcion = 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);

        jtxaDocumento.setColumns(20);
        jtxaDocumento.setRows(5);
        jScrollPane2.setViewportView(jtxaDocumento);

        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);
            }
        });

        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);

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

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

        jmnuBarraPpal.add(jmnuCU);

        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
        DefaultTreeModel model=(DefaultTreeModel) this.getJtreDocumento().getModel();
        DefaultMutableTreeNode root=(DefaultMutableTreeNode) model.getRoot();
        imprimirNodos(root);
    }//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 {
            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 jmniAñadirPasoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniAñadirPasoActionPerformed
        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) tp.getLastPathComponent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
            //jfrmNuevoElemento jele = new jfrmNuevoElemento(this, insertNode, insertIndex);
            jfrmNuevoPaso jfpaso=new jfrmNuevoPaso(this, docPpal, insertNode, insertIndex, docCasodeUso);

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

    private void jmniAñadirExcepcionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniAñadirExcepcionActionPerformed
        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) tp.getLastPathComponent();
                insertIndex=parent.getIndex(insertNode)+1;
                insertNode=parent;
            }
            //jfrmNuevoElemento jele = new jfrmNuevoElemento(this, insertNode, insertIndex);
            jfrmNuevaExcepcion jaux=new jfrmNuevaExcepcion(this, docPpal, insertNode, insertIndex, docPaso);

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

    /**
    * @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.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.JMenuItem jmniAñadirExcepcion;
    private javax.swing.JMenuItem jmniAñadirPaso;
    private javax.swing.JMenuItem jmniNuevoCU;
    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.JTextArea jtxaDocumento;
    // 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);
        }

}
