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

/*
 * jfrmPaso.java
 *
 * Created on 28-nov-2011, 20:11:08
 */

package vista;

import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import modelo.CasodeUso;
import modelo.DocumentoReq;
import modelo.PasoCU;

/**
 *
 * @author Ruben
 */
public class jfrmPaso extends javax.swing.JFrame {
    jfrmPrincipal jfaux;
    MutableTreeNode auxinsertNode;
    int auxinsertIndex;
    /** Creates new form jfrmPaso */

    jfrmPaso(jfrmPrincipal jf, MutableTreeNode insertNode, int insertIndex) {
        auxinsertNode=insertNode;
        auxinsertIndex=insertIndex;
        jfaux=jf;
        
        initComponents();
        int tama = this.jfaux.getDocPpal().getCasodeUso().size();
            for(int i=0; i< tama; i++)
            {
                //
                CasodeUso auxCU = this.jfaux.getDocPpal().getCasodeUso().get(i);
                if(this.auxinsertNode.toString().equals(auxCU.getNombre()))
                {
                    int numerodepasos=auxCU.getPasoCU().size()+1;
                    jtxtNumPaso.setText(String.valueOf(numerodepasos));
                }
            }
    }

    /** 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() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtxtNumPaso = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtxaDesPaso = new javax.swing.JTextArea();
        jbtnAceptarPaso = new javax.swing.JButton();

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

        jLabel1.setText("Numero de Paso : ");

        jLabel2.setText("Descripcion del Paso : ");

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

        jbtnAceptarPaso.setText("Aceptar");
        jbtnAceptarPaso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAceptarPasoActionPerformed(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(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtxtNumPaso, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(162, 162, 162)
                        .addComponent(jbtnAceptarPaso)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtxtNumPaso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jbtnAceptarPaso)
                .addContainerGap(19, Short.MAX_VALUE))
        );

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

    private void jbtnAceptarPasoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAceptarPasoActionPerformed
        if (comprobarTexto())
        {
            int tama = this.jfaux.getDocPpal().getCasodeUso().size();
            for(int i=0; i< tama; i++)
            {
                //
                CasodeUso auxCU = this.jfaux.getDocPpal().getCasodeUso().get(i);
                if(this.auxinsertNode.toString().equals(auxCU.getNombre()))
                {
                    //si lo hemos encontrado
                    auxCU.nuevoPaso(Integer.parseInt(jtxtNumPaso.getText()),jtxaDesPaso.getText());
                    MutableTreeNode node=new DefaultMutableTreeNode("Paso " + auxCU.getPasoCU().get(auxCU.getPasoCU().size()-1).getNumeroPaso());
                    DefaultTreeModel model =(DefaultTreeModel) jfaux.getJtreDocumento().getModel();
                    model.insertNodeInto(node, auxinsertNode, auxinsertIndex);
                    mostrarmensaje("Insertado el Paso numero :  "+auxCU.getPasoCU().get(auxCU.getPasoCU().size()-1).getNumeroPaso());
                    jfaux.setEnabled(true);
                    jfaux.update(null, evt);
                    this.dispose();
                }
            }
        }
        else
        {
            mostrarmensaje("Debes rellenar todos los campos!!!");
        }
    }//GEN-LAST:event_jbtnAceptarPasoActionPerformed

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

    private boolean comprobarTexto()
    {
        if(jtxtNumPaso.getText().equals("") ||
                jtxaDesPaso.getText().equals(""))
        {
            return false;
        }
        return true;
    }
    /**
    * @param args the command line arguments
    */
   /* public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jfrmPaso().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnAceptarPaso;
    private javax.swing.JTextArea jtxaDesPaso;
    private javax.swing.JTextField jtxtNumPaso;
    // End of variables declaration//GEN-END:variables

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