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

/*
 * jfrmParticipante.java
 *
 * Created on 08-ene-2012, 18:31:19
 */

package vista;

import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import modelo.DocumentoReq;
import modelo.Organizacion;
import modelo.Participante;

/**
 *
 * @author Carlos
 */
public class jfrmParticipante extends javax.swing.JFrame {
    DocumentoReq auxDocPpal;
    Organizacion auxOrg;
    jfrmPrincipal jfaux;
    MutableTreeNode auxinsertNode;
    int auxinsertIndex;
    boolean edicion;
    Participante auxPar;
    /** Creates new form jfrmParticipante */
    

    public jfrmParticipante(jfrmPrincipal jf, Organizacion Org, MutableTreeNode insertNode, int insertIndex, DocumentoReq docPpal) {
        initComponents();
        auxinsertNode=insertNode;
        auxinsertIndex=insertIndex;
        jfaux=jf;
        auxOrg=Org;
        auxDocPpal=docPpal;
        edicion=false;
    }

    jfrmParticipante(jfrmPrincipal jf, Participante PAR, MutableTreeNode nodo, DocumentoReq docPpal) {
        initComponents();
        edicion=true;
        jfaux=jf;
        auxPar=PAR;
        auxDocPpal=docPpal;
        auxinsertNode=nodo;
        jtxtNombre.setText(auxPar.getNombre());
        jtxtRol.setText(auxPar.getRol());
        jradDesarrollador.setSelected(auxPar.isEsDesarrollador());
        jradCliente.setSelected(auxPar.isEsCliente());
        jradUsuario.setSelected(auxPar.isEsUsuario());
    }

    /** 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();
        jtxtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtxtRol = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jradDesarrollador = new javax.swing.JRadioButton();
        jradCliente = new javax.swing.JRadioButton();
        jradUsuario = new javax.swing.JRadioButton();
        jbtnAceptar = new javax.swing.JButton();
        jbtnCancelar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Participante");
        setResizable(false);

        jLabel1.setText("Nombre:");

        jLabel2.setText("Rol:");

        jtxtRol.setText("PD");
        jtxtRol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtRolActionPerformed(evt);
            }
        });

        jLabel4.setText("El participante es:");

        jradDesarrollador.setText("Un desarrollador");

        jradCliente.setText("Un cliente");

        jradUsuario.setText("Un usuario");

        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()
                        .addGap(55, 55, 55)
                        .addComponent(jbtnAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtxtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jradCliente)
                                            .addComponent(jradDesarrollador)
                                            .addComponent(jradUsuario))))
                                .addGap(90, 90, 90))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtxtRol, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtRol, 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.UNRELATED)
                .addComponent(jradDesarrollador)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jradCliente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jradUsuario)
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnAceptar)
                    .addComponent(jbtnCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

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

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

    private void jbtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAceptarActionPerformed
        if(edicion==false){
            Participante newParticipante=auxOrg.newParticipante(jtxtNombre.getText(), jtxtRol.getText(), jradDesarrollador.isSelected(), jradCliente.isSelected(), jradUsuario.isSelected());
            MutableTreeNode node=new DefaultMutableTreeNode("[PAR]-"+ newParticipante.getNumero_id()+" "+ newParticipante.getNombre());
            DefaultTreeModel model =(DefaultTreeModel) jfaux.getJtreDocumento().getModel();
            jfaux.getJtreDocumento().setCellRenderer(new RendererArbol());
            model.insertNodeInto(node, auxinsertNode, auxinsertIndex);
            mostrarmensaje("Insertado el Participante " + newParticipante.getNombre() + " correctamente.");
        }else{
            auxPar.setNombre(jtxtNombre.getText());
            auxPar.setRol(jtxtRol.getText());
            auxPar.setEsDesarrollador(jradDesarrollador.isSelected());
            auxPar.setEsCliente(jradCliente.isSelected());
            auxPar.setEsUsuario(jradUsuario.isSelected());
            auxinsertNode.setUserObject("[PAR]-"+ auxPar.getNumero_id()+" "+ auxPar.getNombre());
        }
        auxDocPpal.actualizar();
        jfaux.setEnabled(true);
        this.dispose();

    }//GEN-LAST:event_jbtnAceptarActionPerformed
    private void mostrarmensaje(String mensaje) {
         JOptionPane.showMessageDialog(this, mensaje, "Información",
                                JOptionPane.INFORMATION_MESSAGE/*WARNING_MESSAGE*/);
    }
    /**
    * @param args the command line arguments
//    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new jfrmParticipante().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbtnAceptar;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JRadioButton jradCliente;
    private javax.swing.JRadioButton jradDesarrollador;
    private javax.swing.JRadioButton jradUsuario;
    private javax.swing.JTextField jtxtNombre;
    private javax.swing.JTextField jtxtRol;
    // End of variables declaration//GEN-END:variables

}
