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

/*
 * jfrmExcepcion.java
 *
 * Created on 30-nov-2011, 12:36:29
 */

package vista;

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

/**
 *
 * @author Diego
 */
public class jfrmExcepcion extends javax.swing.JFrame {
    PasoCU auxpaso;
    jfrmPrincipal jfaux;
    MutableTreeNode auxinsertNode;
    int auxinsertIndex;
    /** Creates new form jfrmExcepcion */
    public jfrmExcepcion(jfrmPrincipal jf, PasoCU pasoprin, MutableTreeNode insertNode, int insertIndex) {
        auxinsertNode=insertNode;
        auxinsertIndex=insertIndex;
        jfaux=jf;
        auxpaso=pasoprin;
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtxaDesExcepcion = new javax.swing.JTextArea();
        jbtnAceptarExcepcion = new javax.swing.JButton();
        jtxtNumExcepcion = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jtxaDesExcepcion.setColumns(20);
        jtxaDesExcepcion.setRows(5);
        jScrollPane1.setViewportView(jtxaDesExcepcion);

        jbtnAceptarExcepcion.setText("Aceptar");
        jbtnAceptarExcepcion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAceptarExcepcionActionPerformed(evt);
            }
        });

        jLabel1.setText("Número de la excepción:");

        jLabel2.setText("Descripción:");

        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(160, 160, 160)
                        .addComponent(jbtnAceptarExcepcion))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(22, 22, 22)
                        .addComponent(jtxtNumExcepcion, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtNumExcepcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnAceptarExcepcion)
                .addGap(15, 15, 15))
        );

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

    private void jbtnAceptarExcepcionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAceptarExcepcionActionPerformed
       if (comprobarTexto())
       {
        ExcepcionCU newExcepcionCU= auxpaso.nuevaExcepcion(Integer.parseInt(jtxtNumExcepcion.getText()),jtxaDesExcepcion.getText());
        MutableTreeNode node=new DefaultMutableTreeNode("Excep. " + newExcepcionCU.getNumeroExcep());
        DefaultTreeModel model =(DefaultTreeModel) jfaux.getJtreDocumento().getModel();
        model.insertNodeInto(node, auxinsertNode, auxinsertIndex);
        mostrarmensaje("Insertada la Excepcion numero :  "+newExcepcionCU.getNumeroExcep());
        jfaux.setEnabled(true);
        jfaux.update(null, evt);
        this.dispose();
       }
       else
       {
            mostrarmensaje("Debes rellenar todos los campos!!");
       }
    }//GEN-LAST:event_jbtnAceptarExcepcionActionPerformed

    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(jtxtNumExcepcion.getText().equals("") ||
                jtxaDesExcepcion.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 jfrmExcepcion().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 jbtnAceptarExcepcion;
    private javax.swing.JTextArea jtxaDesExcepcion;
    private javax.swing.JTextField jtxtNumExcepcion;
    // End of variables declaration//GEN-END:variables
private void mostrarmensaje(String mensaje) {
         JOptionPane.showMessageDialog(this, mensaje, "Advertencia",
                                JOptionPane.WARNING_MESSAGE);
    }
}
