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

/*
 * jfrmAñadirExcepcion.java
 *
 * Created on 26/11/2011, 06:04:54 PM
 */

package vista;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import modelo.DocumentoReq;
import modelo.ExcepcionCU;
import modelo.PasoCU;

/**
 *
 * @author Administrador
 */
public class jfrmExcepcion extends javax.swing.JFrame {
    DocumentoReq auxDocPpal;
    PasoCU auxPaso;
    jfrmPrincipal jfaux;
    MutableTreeNode auxinsertNode;
    int auxinsertIndex;
    boolean edicion;
    ExcepcionCU auxExc;

    jfrmExcepcion(jfrmPrincipal jf, ExcepcionCU Exc, MutableTreeNode nodo, DocumentoReq docPpal) {
        initComponents();
        edicion=true;
        jfaux=jf;
        auxExc=Exc;
        auxDocPpal=docPpal;
        auxinsertNode=nodo;
        jtxaAccion.setText(auxExc.getAccion());
        jtxaCondicionEx.setText(auxExc.getCondicion());
        jradSistema.setSelected(auxExc.isAccion_sistema());
        jradActor.setSelected(auxExc.isAccion_actor());
        jcmbTerminacion.setSelectedItem(auxExc.getTerminacion());
        jcomActor.setSelectedItem(auxExc.getActor());
        for (int i=0; i<auxDocPpal.getmActor().size(); i++) {
            jcomActor.addItem(auxDocPpal.getmActor().get(i).getNombre());
        }

    }
    /** Creates new form jfrmAñadirExcepcion */
    
    jfrmExcepcion(jfrmPrincipal jf, PasoCU Paso, MutableTreeNode insertNode, int insertIndex, DocumentoReq DocPpal) {
        initComponents();
        edicion=false;
        auxinsertNode=insertNode;
        auxinsertIndex=insertIndex;
        jfaux=jf;
        auxPaso=Paso;
        auxDocPpal=DocPpal;
        jradSistema.setSelected(true);
        jradActor.setSelected(false);
        this.jcomActor.setEnabled(false);
        for (int i=0; i<DocPpal.getmActor().size(); i++) {
            jcomActor.addItem(DocPpal.getmActor().get(i).getNombre());
        }
    }

    private void mostrarmensaje(String mensaje) {
         JOptionPane.showMessageDialog(this, mensaje, "Información",
                                JOptionPane.INFORMATION_MESSAGE);
    }


    /** 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() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jpanCondicion = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtxaCondicionEx = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jcmbTerminacion = new javax.swing.JComboBox();
        jpanAccion = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jradSistema = new javax.swing.JRadioButton();
        jradActor = new javax.swing.JRadioButton();
        jcomActor = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtxaAccion = new javax.swing.JTextArea();
        jbtnAceptar = new javax.swing.JButton();
        jbtnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Excepción");
        setResizable(false);

        jLabel1.setText("Condicion de la Excepcion:");

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

        jLabel2.setText("Terminacion de la Excepcion:");

        jcmbTerminacion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PD", "continúa", "queda sin efecto" }));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtxaAccion, org.jdesktop.beansbinding.ObjectProperty.create(), jcmbTerminacion, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        jcmbTerminacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcmbTerminacionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpanCondicionLayout = new javax.swing.GroupLayout(jpanCondicion);
        jpanCondicion.setLayout(jpanCondicionLayout);
        jpanCondicionLayout.setHorizontalGroup(
            jpanCondicionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanCondicionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpanCondicionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jcmbTerminacion, 0, 288, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE))
                .addContainerGap())
        );
        jpanCondicionLayout.setVerticalGroup(
            jpanCondicionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanCondicionLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1)
                .addGap(23, 23, 23)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcmbTerminacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(58, 58, 58))
        );

        jTabbedPane1.addTab("Condicion/Terminacion", jpanCondicion);

        jLabel3.setText("Tipo de Accion:");

        jradSistema.setText("Accion del Sistema.");
        jradSistema.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jradSistemaActionPerformed(evt);
            }
        });

        jradActor.setText("Accion de un Actor.");
        jradActor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jradActorActionPerformed(evt);
            }
        });

        jcomActor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " " }));
        jcomActor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcomActorActionPerformed(evt);
            }
        });

        jLabel4.setText("Accion realizada por el sistema o el actor:");

        jtxaAccion.setColumns(20);
        jtxaAccion.setRows(5);
        jtxaAccion.setText("PD");
        jScrollPane2.setViewportView(jtxaAccion);

        javax.swing.GroupLayout jpanAccionLayout = new javax.swing.GroupLayout(jpanAccion);
        jpanAccion.setLayout(jpanAccionLayout);
        jpanAccionLayout.setHorizontalGroup(
            jpanAccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanAccionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpanAccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpanAccionLayout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jpanAccionLayout.createSequentialGroup()
                        .addGroup(jpanAccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addGroup(jpanAccionLayout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(jpanAccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jpanAccionLayout.createSequentialGroup()
                                        .addComponent(jradActor)
                                        .addGap(18, 18, 18)
                                        .addComponent(jcomActor, 0, 85, Short.MAX_VALUE))
                                    .addComponent(jradSistema)))
                            .addComponent(jLabel4))
                        .addGap(67, 67, 67))))
        );
        jpanAccionLayout.setVerticalGroup(
            jpanAccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanAccionLayout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jradSistema)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpanAccionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jradActor)
                    .addComponent(jcomActor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Accion", jpanAccion);

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

        jbtnCancelar.setText("Cancelar");
        jbtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCancelarActionPerformed(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()
                        .addContainerGap()
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(83, 83, 83)
                        .addComponent(jbtnAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtnCancelar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnAceptar)
                    .addComponent(jbtnCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

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

    private void jcmbTerminacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmbTerminacionActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jcmbTerminacionActionPerformed

    private void jcomActorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcomActorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcomActorActionPerformed

    private void jbtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAceptarActionPerformed
        if(edicion==false){
            ExcepcionCU newExcepcion= auxPaso.nuevoExcepcion(jtxaAccion.getText(), jtxaCondicionEx.getText(), jradSistema.isSelected(), jradActor.isSelected(), String.valueOf(jcmbTerminacion.getSelectedItem()), String.valueOf(jcomActor.getSelectedItem()));
            MutableTreeNode node=new DefaultMutableTreeNode("[EXC]-"+ newExcepcion.getNumero_id()+" Excepcion");
            DefaultTreeModel model =(DefaultTreeModel) jfaux.getJtreDocumento().getModel();
            jfaux.getJtreDocumento().setCellRenderer(new RendererArbol());
            model.insertNodeInto(node, auxinsertNode, auxinsertIndex);
            mostrarmensaje("Insertada la Excepcion correctamente");
        }else{
            auxExc.setAccion(jtxaAccion.getText());
            auxExc.setCondicion(jtxaCondicionEx.getText());
            auxExc.setAccion_sistema(jradSistema.isSelected());
            auxExc.setAccion_actor(jradActor.isSelected());
            auxExc.setTerminacion(String.valueOf(jcmbTerminacion.getSelectedItem()));
            auxExc.setActor(String.valueOf(jcomActor.getSelectedItem()));
            auxinsertNode.setUserObject("[EXC]-"+ auxExc.getNumero_id()+" Excepcion");
        }
//        auxExc.persist(auxExc);
        auxDocPpal.actualizar();
        jfaux.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_jbtnAceptarActionPerformed

    private void jbtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCancelarActionPerformed
        jfaux.setEnabled(true);    
        this.dispose();        
    }//GEN-LAST:event_jbtnCancelarActionPerformed

    private void jradActorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jradActorActionPerformed
        if(this.jradActor.isSelected()){
            this.jcomActor.setEnabled(true);
            this.jradSistema.setSelected(false);
        }else{
            this.jcomActor.setEnabled(false);
            this.jradSistema.setSelected(true);
        }
    }//GEN-LAST:event_jradActorActionPerformed

    private void jradSistemaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jradSistemaActionPerformed
        if(this.jradSistema.isSelected()){
            this.jcomActor.setEnabled(false);
            this.jradActor.setSelected(false);
        }else{
            this.jcomActor.setEnabled(true);
            this.jradActor.setSelected(true);
        }

    }//GEN-LAST:event_jradSistemaActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton jbtnAceptar;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JComboBox jcmbTerminacion;
    private javax.swing.JComboBox jcomActor;
    private javax.swing.JPanel jpanAccion;
    private javax.swing.JPanel jpanCondicion;
    private javax.swing.JRadioButton jradActor;
    private javax.swing.JRadioButton jradSistema;
    private javax.swing.JTextArea jtxaAccion;
    private javax.swing.JTextArea jtxaCondicionEx;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
