/*
 *      Luis Israel Diéguez Arjona
 *      Sonia Cejas Soria
 *      Sergio Cabello García
 *      María José González Cordero
 *
 *      Ingeniería del Software de Gestión II
 *      Ingeniería Técnica Informática de Gestión
 *      Universidad de Huelva
 */

package vista;

import controlador.Utilidades;
import java.util.Observable;
import java.util.Observer;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import modelo.DatoEspecifico;





public class jfDatoEspecifico extends javax.swing.JFrame implements Observer {
    // Formulario padre
    jfHome home;
     boolean modificado = false;
   
    // Utilidades
    Utilidades utilidades = new Utilidades();
    // JTree
    MutableTreeNode currentNode;
    MutableTreeNode insertNode;
    int insertIndex;
    // Control del Update de Observers
    int contadorEjecuciones;
    private int vecesActualizo;
    
    private DatoEspecifico datoEspecifico;
    String identReqInformacion;
    String identDato;


    public jfDatoEspecifico() {
        initComponents();
    }

    public jfDatoEspecifico(jfHome home, String identReqInformacion) {
        // Inicializo
        initComponents();
        this.home = home;
        this.insertNode = home.getInsertNode();
        this.insertIndex = home.getInsertIndex(insertNode);
        this.identReqInformacion = identReqInformacion;

        // Configura el identificador
        String ident = this.home.documentoRequisitos.getNuevoIdentificadorDatoEspecifico(identReqInformacion);
        identDato = "DTE-" + ident;
        
        contadorEjecuciones = 0;
    }

    public void inicializarModificacion (DatoEspecifico datoEspecifico) {
        modificado = true;
        identDato = datoEspecifico.getIdentificacion();
        //Pestaña Descripcion
        jTextArea_descripcion.setText(datoEspecifico.getDescripcion());
     
        // Pestaña Comentarios
        jTextArea_comentarios.setText(datoEspecifico.getComentario());
    }

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

        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jPanel14 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea_descripcion = new javax.swing.JTextArea();
        jPanel7 = new javax.swing.JPanel();
        jPanel23 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        jTextArea_comentarios = new javax.swing.JTextArea();
        jButton_aceptar = new javax.swing.JButton();
        jButton_cancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Dato Especifico");
        setAlwaysOnTop(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setPreferredSize(new java.awt.Dimension(563, 306));

        jTabbedPane1.setPreferredSize(new java.awt.Dimension(564, 262));

        jPanel14.setBorder(javax.swing.BorderFactory.createTitledBorder("Descripción"));

        jTextArea_descripcion.setColumns(20);
        jTextArea_descripcion.setLineWrap(true);
        jTextArea_descripcion.setRows(5);
        jScrollPane3.setViewportView(jTextArea_descripcion);

        javax.swing.GroupLayout jPanel14Layout = new javax.swing.GroupLayout(jPanel14);
        jPanel14.setLayout(jPanel14Layout);
        jPanel14Layout.setHorizontalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel14Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel14Layout.setVerticalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel14Layout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Descripción", jPanel3);

        jPanel23.setBorder(javax.swing.BorderFactory.createTitledBorder("Comentarios"));

        jTextArea_comentarios.setColumns(20);
        jTextArea_comentarios.setLineWrap(true);
        jTextArea_comentarios.setRows(5);
        jScrollPane6.setViewportView(jTextArea_comentarios);

        javax.swing.GroupLayout jPanel23Layout = new javax.swing.GroupLayout(jPanel23);
        jPanel23.setLayout(jPanel23Layout);
        jPanel23Layout.setHorizontalGroup(
            jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel23Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel23Layout.setVerticalGroup(
            jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel23Layout.createSequentialGroup()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel23, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel23, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Comentarios", jPanel7);

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

        jButton_cancelar.setText("Cancelar");
        jButton_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_cancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(254, Short.MAX_VALUE)
                .addComponent(jButton_aceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton_cancelar)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_cancelar)
                    .addComponent(jButton_aceptar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
        );

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

    private void jButton_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_cancelarActionPerformed
        home.setEnabled(true);
        this.dispose();
}//GEN-LAST:event_jButton_cancelarActionPerformed
    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        home.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_formWindowClosed
    private void jButton_aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_aceptarActionPerformed
        datoEspecifico = new DatoEspecifico();
        boolean puedoCerrar = true;
        
        // Identificador
        datoEspecifico.setIdentificacion(identDato);

        datoEspecifico.setDescripcion(this.jTextArea_descripcion.getText());
        datoEspecifico.setComentario(this.jTextArea_comentarios.getText());

        // Fin de la captura de los datos desde el formulario
        if (puedoCerrar) {
            if (modificado)
                home.documentoRequisitos.modifyDatoEspecifico(identReqInformacion, datoEspecifico);
            else
                home.documentoRequisitos.addDatoEspecifico(identReqInformacion, datoEspecifico);

            // Cierro la ventana
            home.setEnabled(true);
            this.dispose();
        }
    }//GEN-LAST:event_jButton_aceptarActionPerformed

    private void actualizarJTree (DatoEspecifico datoEspecifico) {
        if(datoEspecifico != null){
            MutableTreeNode node = new DefaultMutableTreeNode(datoEspecifico.getDescripcion() + " (" + datoEspecifico.getIdentificacion() + ")");
            DefaultTreeModel model = (DefaultTreeModel) home.getJTree().getModel();


            if (modificado == false)
                model.insertNodeInto(node, insertNode, insertIndex);

            // Expande el JTree
            for (int j = 0; j < home.getJTree().getSize().height; j++)
                home.getJTree().expandPath(home.getJTree().getPathForRow(j));
            
            home.getJTree().setSelectionPath(null);
        }
    }


    
    private void jButton_anadirTrazaOrigenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_anadirTrazaOrigenActionPerformed
    }//GEN-LAST:event_jButton_anadirTrazaOrigenActionPerformed


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jfDatoEspecifico().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_aceptar;
    private javax.swing.JButton jButton_cancelar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel23;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextArea_comentarios;
    private javax.swing.JTextArea jTextArea_descripcion;
    // End of variables declaration//GEN-END:variables

    
    
    

    public void update(Observable o, Object arg) {
        if(vecesActualizo==0){
            actualizarJTree(datoEspecifico);
            home.actualizarTextPane();
            vecesActualizo++;
        }
    }

}
