/*
 * Copyright
 *   2009 axYus - www.axyus.com
 *   2009 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.xemelios.auth.impl.std.db.ui;

import fr.gouv.finances.dgfip.xemelios.auth.impl.std.db.DataHelper;
import fr.gouv.finances.dgfip.xemelios.common.Crypter;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
import fr.gouv.finances.dgfip.xemelios.common.config.Loader;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.log4j.Logger;

/**
 *
 * @author chm
 */
public class PnlUsers extends javax.swing.JPanel {
    private static final Logger logger = Logger.getLogger(PnlUsers.class);
    private DlgAdmin dlgAdmin;
    private DataHelper.StdUser currentUser;
    private DataHelper.StdUser[] users;

    /** Creates new form PnlUsers */
    public PnlUsers(DlgAdmin dlgAdmin) {
        this.dlgAdmin=dlgAdmin;
        initComponents();
        lstUsers.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                boolean hasSelectedItem = (lstUsers.getSelectedIndex()>=0);
                chkAllFonctionalities.setEnabled(hasSelectedItem);
                chkAllDocuments.setEnabled(hasSelectedItem);
                chkAllCollectivites.setEnabled(hasSelectedItem);
                dfNom.setEnabled(hasSelectedItem);
                dfPassword1.setEnabled(hasSelectedItem);
                dfPassword1.setText("");
                dfPassword2.setEnabled(hasSelectedItem);
                dfPassword2.setText("");
                dfPrenom.setEnabled(hasSelectedItem);
                pbSave.setEnabled(false);
                if(hasSelectedItem) {
                    currentUser = (DataHelper.StdUser)lstUsers.getSelectedValue();
                    pbRemoveUser.setEnabled(!PnlUsers.this.dlgAdmin.getConnectedUser().getId().equals(currentUser.getLogin()));
                    dfLogin.setText(currentUser.getLogin());
                    dfNom.setText(currentUser.getNom());
                    dfPrenom.setText(currentUser.getPrenom());
                    chkAllCollectivites.setSelected(currentUser.isAllCollectivites());
                    chkAllDocuments.setSelected(currentUser.isAlldocuments());
                    chkAllFonctionalities.setSelected(currentUser.isAllFonctionalities());
                    if(currentUser.getId()==-1) {
                        dfLogin.setEnabled(true);
                        dfLogin.setEditable(true);
                    } else {
                        dfLogin.setEnabled(false);
                        dfLogin.setEditable(false);
                    }
                } else {
                    pbRemoveUser.setEnabled(false);
                    dfLogin.setText("");
                    dfNom.setText("");
                    dfPrenom.setText("");
                    dfPassword1.setText("");
                    dfPassword2.setText("");
                    chkAllCollectivites.setSelected(false);
                    chkAllDocuments.setSelected(false);
                    chkAllFonctionalities.setSelected(false);
                    pbChooseCollectivites.setEnabled(false);
                    pbChooseDocuments.setEnabled(false);
                    pbChooseProfils.setEnabled(false);
                }
            }
        });
    }

    /** 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();
        dfUser = new javax.swing.JTextField();
        pbSearch = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstUsers = new javax.swing.JList();
        pbAddUser = new javax.swing.JButton();
        pbRemoveUser = new javax.swing.JButton();
        lblLogin = new javax.swing.JLabel();
        dfLogin = new javax.swing.JTextField();
        lblNom = new javax.swing.JLabel();
        dfNom = new javax.swing.JTextField();
        lblPrenom = new javax.swing.JLabel();
        dfPrenom = new javax.swing.JTextField();
        lblPassword = new javax.swing.JLabel();
        dfPassword1 = new javax.swing.JPasswordField();
        lblPwd2 = new javax.swing.JLabel();
        dfPassword2 = new javax.swing.JPasswordField();
        chkAllFonctionalities = new javax.swing.JCheckBox();
        pbChooseProfils = new javax.swing.JButton();
        chkAllDocuments = new javax.swing.JCheckBox();
        pbChooseDocuments = new javax.swing.JButton();
        chkAllCollectivites = new javax.swing.JCheckBox();
        pbChooseCollectivites = new javax.swing.JButton();
        pbSave = new javax.swing.JButton();

        jLabel1.setText("Chercher des utilisateurs");

        pbSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fr/gouv/finances/cp/xemelios/ui/resources/search.png"))); // NOI18N
        pbSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbSearchActionPerformed(evt);
            }
        });

        lstUsers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(lstUsers);

        pbAddUser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fr/gouv/finances/cp/xemelios/ui/resources/add.png"))); // NOI18N
        pbAddUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbAddUserActionPerformed(evt);
            }
        });

        pbRemoveUser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fr/gouv/finances/cp/xemelios/ui/resources/deleteRow.png"))); // NOI18N
        pbRemoveUser.setEnabled(false);
        pbRemoveUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbRemoveUserActionPerformed(evt);
            }
        });

        lblLogin.setLabelFor(dfLogin);
        lblLogin.setText("Login");

        dfLogin.setEditable(false);

        lblNom.setLabelFor(dfNom);
        lblNom.setText("Nom");

        dfNom.setEnabled(false);
        dfNom.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                dfNomCaretUpdate(evt);
            }
        });

        lblPrenom.setLabelFor(dfPrenom);
        lblPrenom.setText("Prénom");

        dfPrenom.setEnabled(false);
        dfPrenom.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                dfPrenomCaretUpdate(evt);
            }
        });

        lblPassword.setLabelFor(dfPassword1);
        lblPassword.setText("Mot de passe");

        dfPassword1.setEnabled(false);
        dfPassword1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                dfPassword1CaretUpdate(evt);
            }
        });

        lblPwd2.setLabelFor(dfPassword2);
        lblPwd2.setText("Répéter");

        dfPassword2.setEnabled(false);
        dfPassword2.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                dfPassword2CaretUpdate(evt);
            }
        });

        chkAllFonctionalities.setText("Toutes fonctionnalités");
        chkAllFonctionalities.setEnabled(false);
        chkAllFonctionalities.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkAllFonctionalitiesActionPerformed(evt);
            }
        });
        chkAllFonctionalities.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                chkAllFonctionalitiesStateChanged(evt);
            }
        });

        pbChooseProfils.setText("Profils...");
        pbChooseProfils.setEnabled(false);
        pbChooseProfils.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbChooseProfilsActionPerformed(evt);
            }
        });

        chkAllDocuments.setText("Tous documents");
        chkAllDocuments.setEnabled(false);
        chkAllDocuments.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkAllDocumentsActionPerformed(evt);
            }
        });
        chkAllDocuments.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                chkAllDocumentsStateChanged(evt);
            }
        });

        pbChooseDocuments.setText("Documents...");
        pbChooseDocuments.setEnabled(false);
        pbChooseDocuments.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbChooseDocumentsActionPerformed(evt);
            }
        });

        chkAllCollectivites.setText("Toutes collectivités");
        chkAllCollectivites.setEnabled(false);
        chkAllCollectivites.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkAllCollectivitesActionPerformed(evt);
            }
        });
        chkAllCollectivites.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                chkAllCollectivitesStateChanged(evt);
            }
        });

        pbChooseCollectivites.setText("Collectivités...");
        pbChooseCollectivites.setEnabled(false);
        pbChooseCollectivites.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbChooseCollectivitesActionPerformed(evt);
            }
        });

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

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                            .add(dfUser, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(pbRemoveUser, 0, 0, Short.MAX_VALUE)
                            .add(pbAddUser, 0, 0, Short.MAX_VALUE)
                            .add(pbSearch, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, Short.MAX_VALUE))
                        .add(8, 8, 8)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(lblLogin)
                        .add(281, 281, 281))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(lblPrenom)
                            .add(lblNom)
                            .add(lblPassword)
                            .add(lblPwd2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(dfLogin, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                                .addContainerGap())
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(dfNom, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 209, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                        .add(dfPrenom, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                        .add(org.jdesktop.layout.GroupLayout.LEADING, dfPassword2)
                                        .add(org.jdesktop.layout.GroupLayout.LEADING, dfPassword1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)))
                                .add(12, 12, 12))))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createSequentialGroup()
                                        .add(chkAllCollectivites)
                                        .add(22, 22, 22))
                                    .add(chkAllDocuments))
                                .add(2, 2, 2))
                            .add(layout.createSequentialGroup()
                                .add(chkAllFonctionalities)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)))
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, pbSave, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, pbChooseCollectivites, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, pbChooseDocuments, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                            .add(pbChooseProfils, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(dfUser, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(pbSearch)
                    .add(lblLogin)
                    .add(dfLogin, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblNom)
                            .add(dfNom, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(lblPrenom)
                            .add(dfPrenom, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(dfPassword1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lblPassword))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(dfPassword2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lblPwd2))
                        .add(18, 18, 18)
                        .add(pbSave)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 34, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(pbChooseProfils)
                            .add(chkAllFonctionalities))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(pbChooseDocuments)
                            .add(chkAllDocuments))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(pbChooseCollectivites)
                            .add(chkAllCollectivites)))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(pbAddUser)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(pbRemoveUser)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void pbSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbSearchActionPerformed
        if(dfUser.getText()==null || dfUser.getText().length()==0) {
            JOptionPane.showMessageDialog(this, "Vous devez saisir un critère de recherche.\nPar exemple * pour chercher tous les utilisateurs");
            return;
        }
        ArrayList<DataHelper.StdUser> __users = DataHelper.getUsers(dfUser.getText());
        users = new DataHelper.StdUser[__users.size()];
        __users.toArray(users);
        lstUsers.removeAll();
        lstUsers.setListData(users);
    }//GEN-LAST:event_pbSearchActionPerformed

    private void dfNomCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_dfNomCaretUpdate
        if(!dfNom.isEnabled()) return;
        if(!dfNom.getText().equals(currentUser.getNom()) && (dfNom.getText().length()!=0 || currentUser.getNom()!=null)) pbSave.setEnabled(true);
    }//GEN-LAST:event_dfNomCaretUpdate

    private void dfPrenomCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_dfPrenomCaretUpdate
        if(!dfPrenom.isEnabled()) return;
        if(!dfPrenom.getText().equals(currentUser.getPrenom()) && (dfPrenom.getText().length()!=0 || currentUser.getNom()!=null)) pbSave.setEnabled(true);
    }//GEN-LAST:event_dfPrenomCaretUpdate

    private void dfPassword1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_dfPassword1CaretUpdate
        if(!dfPassword1.isEnabled()) return;
        if(dfPassword1.getText().length()!=0) pbSave.setEnabled(true);
    }//GEN-LAST:event_dfPassword1CaretUpdate

    private void dfPassword2CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_dfPassword2CaretUpdate
        if(!dfPassword2.isEnabled()) return;
        if(dfPassword2.getText().length()!=0) pbSave.setEnabled(true);
    }//GEN-LAST:event_dfPassword2CaretUpdate

    private void chkAllFonctionalitiesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkAllFonctionalitiesActionPerformed
        if(!chkAllFonctionalities.isEnabled()) return;
        pbSave.setEnabled(true);        
    }//GEN-LAST:event_chkAllFonctionalitiesActionPerformed

    private void chkAllCollectivitesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkAllCollectivitesActionPerformed
        if(!chkAllCollectivites.isEnabled()) return;
        pbSave.setEnabled(true);
    }//GEN-LAST:event_chkAllCollectivitesActionPerformed

    private void chkAllDocumentsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkAllDocumentsActionPerformed
        if(!chkAllDocuments.isEnabled()) return;
        pbSave.setEnabled(true);
    }//GEN-LAST:event_chkAllDocumentsActionPerformed

    private void chkAllDocumentsStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_chkAllDocumentsStateChanged
        boolean selected = chkAllDocuments.isSelected();
        pbChooseDocuments.setEnabled(!selected);
    }//GEN-LAST:event_chkAllDocumentsStateChanged

    private void chkAllFonctionalitiesStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_chkAllFonctionalitiesStateChanged
        boolean selected = chkAllFonctionalities.isSelected();
        pbChooseProfils.setEnabled(!selected);
    }//GEN-LAST:event_chkAllFonctionalitiesStateChanged

    private void chkAllCollectivitesStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_chkAllCollectivitesStateChanged
        boolean selected = chkAllCollectivites.isSelected();
        pbChooseCollectivites.setEnabled(!selected);
    }//GEN-LAST:event_chkAllCollectivitesStateChanged

    private void pbSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbSaveActionPerformed
        if(!pbSave.isEnabled()) return;
        if(dfLogin.isEnabled() && dfPassword1.getPassword().length==0) {
            JOptionPane.showMessageDialog(this,"Les deux mots de passe sont obligatoires pour créer un utilisateur.", "Erreur", JOptionPane.ERROR_MESSAGE);
        }
        if(dfLogin.getText()==null || dfLogin.getText().length()==0) {
            JOptionPane.showMessageDialog(this,"Le login est obligatoire pour créer un utilisateur.", "Erreur", JOptionPane.ERROR_MESSAGE);
        }
        if((dfPassword1.getPassword()!=null || dfPassword2.getPassword()!=null) && !Arrays.equals(dfPassword1.getPassword(), dfPassword2.getPassword())) {
            JOptionPane.showMessageDialog(this,"Les deux mots de passe doivent être identiques.", "Erreur", JOptionPane.ERROR_MESSAGE);
        }
        currentUser.setLogin(dfLogin.getText());
        currentUser.setNom(dfNom.getText());
        currentUser.setPrenom(dfPrenom.getText());
        currentUser.setAllCollectivites(chkAllCollectivites.isSelected());
        currentUser.setAllFonctionalities(chkAllFonctionalities.isSelected());
        currentUser.setAlldocuments(chkAllDocuments.isSelected());
        if(dfPassword1.getPassword().length>0) {
            char[] clearPass = dfPassword1.getPassword();
            String cryptedPass = Crypter.cryptPassword(new String(clearPass));
            DataHelper.saveUser(currentUser,cryptedPass);
        } else {
            DataHelper.saveUser(currentUser);
        }
        dfLogin.setEditable(false);

        for(ListSelectionListener lsl:lstUsers.getListSelectionListeners()) {
            lsl.valueChanged(null);
        }
    }//GEN-LAST:event_pbSaveActionPerformed

    private void pbAddUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbAddUserActionPerformed
        DataHelper.StdUser user = new DataHelper.StdUser();
        DataHelper.StdUser[] tmpUsers = new DataHelper.StdUser[users!=null?users.length+1:1];
        if(users!=null)
            System.arraycopy(users, 0, tmpUsers, 0, users.length);
        tmpUsers[tmpUsers.length-1] = user;
        users=tmpUsers;
        lstUsers.setListData(users);
        lstUsers.setSelectedIndex(users.length-1);
    }//GEN-LAST:event_pbAddUserActionPerformed

    private void pbRemoveUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbRemoveUserActionPerformed
        if(JOptionPane.showConfirmDialog(this, "Etes-vous sur de vouloir supprimer cet utilisateur : "+currentUser.toString()+" ?")==JOptionPane.YES_OPTION) {
            DataHelper.removeUser(currentUser);
            DataHelper.StdUser[] tmpUsers = new DataHelper.StdUser[users.length-1];
            int pos = lstUsers.getSelectedIndex();
            if(pos>0)
                System.arraycopy(users, 0, tmpUsers, 0, pos);
            if(pos<users.length-1)
                System.arraycopy(users, pos+1, tmpUsers, pos, users.length-pos-1);
            users=tmpUsers;
            lstUsers.setListData(users);
        }
    }//GEN-LAST:event_pbRemoveUserActionPerformed

    private void pbChooseProfilsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbChooseProfilsActionPerformed
        if(currentUser==null) return;
        if(!pbChooseProfils.isEnabled()) return;
        if(currentUser.getId()==-1) {
            JOptionPane.showMessageDialog(this,"Vous devez enregistrer l'utilisateur avant\nde pouvoir lui définir ses profils.");
            return;
        }
        DlgProfilsUtilisateur dlg = new DlgProfilsUtilisateur(dlgAdmin, currentUser);
        dlg.run();
    }//GEN-LAST:event_pbChooseProfilsActionPerformed

    private void pbChooseDocumentsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbChooseDocumentsActionPerformed
        try {
            DocumentsModel dm = Loader.getDocumentsInfos(null);
            DlgDocumentsUtilisateur dlg = new DlgDocumentsUtilisateur(dlgAdmin, currentUser, dm);
            dlg.run();
        } catch(Exception ex) {
            logger.error("pbChooseDocuemntsActionPerformed",ex);
        }
    }//GEN-LAST:event_pbChooseDocumentsActionPerformed

    private void pbChooseCollectivitesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbChooseCollectivitesActionPerformed
        try {
            DlgCollectivitesUtilisateur dlg = new DlgCollectivitesUtilisateur(dlgAdmin, currentUser);
            dlg.run();
        } catch(Exception ex) {
            logger.error("pbChooseCollectivitesActionPerformed",ex);
        }
    }//GEN-LAST:event_pbChooseCollectivitesActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox chkAllCollectivites;
    private javax.swing.JCheckBox chkAllDocuments;
    private javax.swing.JCheckBox chkAllFonctionalities;
    private javax.swing.JTextField dfLogin;
    private javax.swing.JTextField dfNom;
    private javax.swing.JPasswordField dfPassword1;
    private javax.swing.JPasswordField dfPassword2;
    private javax.swing.JTextField dfPrenom;
    private javax.swing.JTextField dfUser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblLogin;
    private javax.swing.JLabel lblNom;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblPrenom;
    private javax.swing.JLabel lblPwd2;
    private javax.swing.JList lstUsers;
    private javax.swing.JButton pbAddUser;
    private javax.swing.JButton pbChooseCollectivites;
    private javax.swing.JButton pbChooseDocuments;
    private javax.swing.JButton pbChooseProfils;
    private javax.swing.JButton pbRemoveUser;
    private javax.swing.JButton pbSave;
    private javax.swing.JButton pbSearch;
    // End of variables declaration//GEN-END:variables

}
