/*
 * DlgMakePJ.java
 *
 * Created on 9 janvier 2008, 10:32
 */


package fr.gouv.finances.cp.xemelios.plugin.generatorPesPjs;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.JFrame;

/**
 *
 * @author  jp.tessier
 */
public class DlgMakePJ extends javax.swing.JDialog {
    private JFrame parentWindow;
    private TableModelRefCompta tableModel = new TableModelRefCompta();
    private PJ piece_justificative;
    private String idcol,idpost,codcol,codbud;
    private boolean bRet;
    private boolean isEdit=false;
    
    /** Creates new form DlgMakePJ */
    public DlgMakePJ(javax.swing.JFrame parent, boolean modal) {
        super(parent, modal);
        parentWindow=parent;
               
        initComponents();
        setLocationRelativeTo(parent);
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        PanelPj = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        type_pj = new javax.swing.JComboBox();
        pieceJustificative = new javax.swing.JTextField();
        id_unique = new javax.swing.JTextField();
        PjName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        description = new javax.swing.JTextArea();
        browse_button = new javax.swing.JButton();
        PanelRefsComptas = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableRefsComptas = new javax.swing.JTable();
        add_refCompta_button = new javax.swing.JButton();
        edit_refcompta_button = new javax.swing.JButton();
        deleteRefCompta = new javax.swing.JButton();
        cancel_button = new javax.swing.JButton();
        save_button = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Pi�ces Justificative");

        jLabel2.setText("Identifiant Unique");

        jLabel3.setText("Nom PJ");

        jLabel4.setText("Type PJ");

        jLabel5.setText("Description");

        type_pj.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "001 - Document Budg�taire", "002 - Facture de Recette", "003 - Facture de D�pense", "004 - Etat de Paye", "005 - Etat d'Aide sociale" }));
        type_pj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                type_pjActionPerformed(evt);
            }
        });

        PjName.setFocusable(false);
        PjName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                PjNameFocusLost(evt);
            }
        });

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

        browse_button.setText("...");
        browse_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browse_buttonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout PanelPjLayout = new org.jdesktop.layout.GroupLayout(PanelPj);
        PanelPj.setLayout(PanelPjLayout);
        PanelPjLayout.setHorizontalGroup(
            PanelPjLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(PanelPjLayout.createSequentialGroup()
                .addContainerGap()
                .add(PanelPjLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel4)
                    .add(jLabel3)
                    .add(jLabel2)
                    .add(jLabel1)
                    .add(jLabel5))
                .add(18, 18, 18)
                .add(PanelPjLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                    .add(PjName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                    .add(pieceJustificative, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                    .add(id_unique, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                    .add(type_pj, 0, 239, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(browse_button)
                .add(75, 75, 75))
        );
        PanelPjLayout.setVerticalGroup(
            PanelPjLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(PanelPjLayout.createSequentialGroup()
                .addContainerGap()
                .add(PanelPjLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(browse_button)
                    .add(pieceJustificative, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(PanelPjLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(id_unique, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(PanelPjLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(PjName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(PanelPjLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(type_pj, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(PanelPjLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel5)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Pi�ce Justificative", PanelPj);

        tableRefsComptas.setModel(tableModel);
        tableRefsComptas.setColumnSelectionAllowed(true);
        jScrollPane2.setViewportView(tableRefsComptas);

        add_refCompta_button.setText("Ajouter");
        add_refCompta_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                add_refCompta_buttonActionPerformed(evt);
            }
        });

        edit_refcompta_button.setText("Editer");
        edit_refcompta_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                edit_refcompta_buttonActionPerformed(evt);
            }
        });

        deleteRefCompta.setText("Supprimer");
        deleteRefCompta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteRefComptaActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout PanelRefsComptasLayout = new org.jdesktop.layout.GroupLayout(PanelRefsComptas);
        PanelRefsComptas.setLayout(PanelRefsComptasLayout);
        PanelRefsComptasLayout.setHorizontalGroup(
            PanelRefsComptasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(PanelRefsComptasLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 361, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(PanelRefsComptasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, deleteRefCompta, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, edit_refcompta_button, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                    .add(add_refCompta_button, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))
                .addContainerGap())
        );
        PanelRefsComptasLayout.setVerticalGroup(
            PanelRefsComptasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, PanelRefsComptasLayout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 221, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(33, 33, 33))
            .add(PanelRefsComptasLayout.createSequentialGroup()
                .add(64, 64, 64)
                .add(add_refCompta_button)
                .add(26, 26, 26)
                .add(edit_refcompta_button)
                .add(27, 27, 27)
                .add(deleteRefCompta)
                .addContainerGap(79, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("R�f�rences Comptables", PanelRefsComptas);

        cancel_button.setText("Annuler");
        cancel_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_buttonActionPerformed(evt);
            }
        });

        save_button.setText("Enregistrer");
        save_button.setEnabled(false);
        save_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                save_buttonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 508, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(cancel_button)
                        .add(18, 18, 18)
                        .add(save_button))
                    .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 488, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 350, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 287, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(save_button)
                    .add(cancel_button))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void type_pjActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_type_pjActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_type_pjActionPerformed
    
    
    public boolean run() {
        initComportement();
        setVisible(true);
        return bRet;
    }
    
    public boolean runEdit(PJ pj) {
        isEdit=true;
        piece_justificative=pj;
        save_button.setEnabled(true);
        initDlg();
        setVisible(true);
        return bRet;
    }
    
    private void initComportement(){
        PanelPj.getParent().setEnabled(false);
        tableRefsComptas.setModel(tableModel);
    }
    
    private void initDlg(){
        pieceJustificative.setText(piece_justificative.getPjFile().getAbsolutePath());
        id_unique.setText(piece_justificative.getIdentifiant());
        PjName.setText(piece_justificative.getNamePJ());
        for(int i=0;i<type_pj.getItemCount();i++){
            if(type_pj.getItemAt(i).equals(piece_justificative.getTypePJ())){
                type_pj.setSelectedIndex(i);
            }
        }
        description.setText(piece_justificative.getDescription());
        for(RefCompta rc:piece_justificative.getVRefsComptas()){
            tableModel.add(rc);
        }
    }
    
    private void browse_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browse_buttonActionPerformed
        JFileChooser fc = new JFileChooser();
        
            /*fc.addChoosableFileFilter(new FileFilter() {

                public boolean accept(File f) {
                    return f.isDirectory() || f.getName().toUpperCase().endsWith("DOC");
                }

                public String getDescription() {
                    return "Document Word";
                }
            });
            fc.addChoosableFileFilter(new FileFilter() {

                public boolean accept(File f) {
                    return f.isDirectory() || f.getName().toUpperCase().endsWith("PDF");
                }

                public String getDescription() {
                    return "Document PDF";
                }
            });*/
      
        int reponse = fc.showOpenDialog(this);
        if(reponse==JFileChooser.APPROVE_OPTION) {
            pieceJustificative.setText(fc.getSelectedFile().getAbsolutePath());
            PjName.setText(fc.getSelectedFile().getName().split("[.]")[0]);
            save_button.setEnabled(true);
            PanelPj.getParent().setEnabled(true);
        }
    }//GEN-LAST:event_browse_buttonActionPerformed

    private void add_refCompta_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_add_refCompta_buttonActionPerformed
        DlgEditRefCompta dlgRefCompta = new DlgEditRefCompta(parentWindow,true);
        if(dlgRefCompta.run()) {
            tableModel.add(dlgRefCompta.getReferenceComptable());
        }
}//GEN-LAST:event_add_refCompta_buttonActionPerformed

    private void cancel_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_buttonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_cancel_buttonActionPerformed

    private void save_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_save_buttonActionPerformed
        Vector<RefCompta> vrefcompta = new Vector<RefCompta>();
        for(int i=0;i<tableModel.getRowCount();i++){
            vrefcompta.add(tableModel.getData(i));
        }
        if(id_unique.getText().length() == 0){
            SimpleDateFormat d = new SimpleDateFormat("ddyyMMHHmm");
            id_unique.setText(idcol+"_"+idpost+"_"+d.format(new Date())+"_"+codcol+"_"+codbud);
        }
        if(!isEdit){
            piece_justificative = new PJ(new File(pieceJustificative.getText()),id_unique.getText(),PjName.getText(),type_pj.getSelectedItem().toString(),description.getText(),vrefcompta);
        }else{
            piece_justificative.modifPJ(new File(pieceJustificative.getText()),id_unique.getText(),PjName.getText(),type_pj.getSelectedItem().toString(),description.getText(),vrefcompta);
        }
            bRet = true;
        setVisible(false);
    }//GEN-LAST:event_save_buttonActionPerformed

    private void edit_refcompta_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_edit_refcompta_buttonActionPerformed
        DlgEditRefCompta dlgRefCompta = new DlgEditRefCompta(parentWindow,true);
        int rowSeleted = tableRefsComptas.getSelectedRow();
        if(dlgRefCompta.runEdit(tableModel.getData(rowSeleted))) {
            tableModel.refresh();
        }
    }//GEN-LAST:event_edit_refcompta_buttonActionPerformed

    private void PjNameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_PjNameFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_PjNameFocusLost

    private void deleteRefComptaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteRefComptaActionPerformed
        tableModel.remove(tableRefsComptas.getSelectedRow());
}//GEN-LAST:event_deleteRefComptaActionPerformed
    public void setIdCollIdPostCodBud(String idc,String idp,String codc,String codb){
        idcol=idc;
        idpost=idp;
        codcol=codc;
        codbud=codb;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DlgMakePJ dialog = new DlgMakePJ(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel PanelPj;
    private javax.swing.JPanel PanelRefsComptas;
    private javax.swing.JTextField PjName;
    private javax.swing.JButton add_refCompta_button;
    private javax.swing.JButton browse_button;
    private javax.swing.JButton cancel_button;
    private javax.swing.JButton deleteRefCompta;
    private javax.swing.JTextArea description;
    private javax.swing.JButton edit_refcompta_button;
    private javax.swing.JTextField id_unique;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField pieceJustificative;
    private javax.swing.JButton save_button;
    private javax.swing.JTable tableRefsComptas;
    private javax.swing.JComboBox type_pj;
    // End of variables declaration//GEN-END:variables

    public PJ getPiece_justificative() {
        return piece_justificative;
    }
    
}
