/*
 * 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.utils.Pair;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author chm
 */
public class DlgDocumentsUtilisateur extends javax.swing.JDialog {
    private DataHelper.StdUser user;
    private DlgProfilsUtilisateur.PairListModel documentsUserModel;
    private DlgProfilsUtilisateur.PairListModel documentsModel;

    /** Creates new form DlgDocumentsUtilisateur */
    public DlgDocumentsUtilisateur(java.awt.Dialog parent, DataHelper.StdUser user, DocumentsModel docs) {
        super(parent, "Documents", true);
        this.user=user;
        List<Pair> documentsUser = user.getDocuments();
        for(Pair p: documentsUser) {
            p.libelle=docs.getDocumentById(p.key).getTitre();
        }
        List<Pair> documents = new ArrayList<Pair>();
        for(DocumentModel dm:docs.getDocuments()) {
            Pair p = new Pair(dm.getId(),dm.getTitre());
            if(!documentsUser.contains(p)) documents.add(p);
        }
        documentsUserModel = new DlgProfilsUtilisateur.PairListModel(documentsUser);
        documentsModel = new DlgProfilsUtilisateur.PairListModel(documents);
        initComponents();
        documentsUserModel.setList(lstDocumentsUser);
        documentsModel.setList(lstDocuments);
        lstDocumentsUser.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                JList lst = (JList)e.getSource();
                boolean selected = lst.getSelectedIndices().length>0;
                pbRemoveOne.setEnabled(selected);
            }
        });
        lstDocuments.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                JList lst = (JList)e.getSource();
                boolean selected = lst.getSelectedIndices().length>0;
                pbAddOne.setEnabled(selected);
            }
        });
        setLocationRelativeTo(parent);
    }
    public void run() {
        setVisible(true);
    }
    protected JRootPane createRootPane() {
        KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0);
        JRootPane rp = new JRootPane();
        AbstractAction escapeAction = new AbstractAction("Annuler") {
            /**
             * Comment for <code>serialVersionUID</code>
             */
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent evt) {
                pbCancelActionPerformed(evt);
            }
        };
        rp.registerKeyboardAction(escapeAction,"TOTOTOTO",ks,JComponent.WHEN_IN_FOCUSED_WINDOW);
        return rp;
    }

    /** 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();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstDocumentsUser = new javax.swing.JList();
        pbAddAll = new javax.swing.JButton();
        pbAddOne = new javax.swing.JButton();
        pbRemoveOne = new javax.swing.JButton();
        pbRemoveAll = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstDocuments = new javax.swing.JList();
        pbOk = new javax.swing.JButton();
        pbCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Documents de "+user.toString());

        lstDocumentsUser.setModel(documentsUserModel);
        jScrollPane1.setViewportView(lstDocumentsUser);

        pbAddAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fr/gouv/finances/cp/xemelios/ui/resources/addAll.png"))); // NOI18N
        pbAddAll.setToolTipText("Tout ajouter");
        pbAddAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbAddAllActionPerformed(evt);
            }
        });

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

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

        pbRemoveAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fr/gouv/finances/cp/xemelios/ui/resources/removeAll.png"))); // NOI18N
        pbRemoveAll.setToolTipText("Tout supprimer");
        pbRemoveAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbRemoveAllActionPerformed(evt);
            }
        });

        lstDocuments.setModel(documentsModel);
        jScrollPane2.setViewportView(lstDocuments);

        pbOk.setText("Ok");
        pbOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbOkActionPerformed(evt);
            }
        });

        pbCancel.setText("Annuler");
        pbCancel.setToolTipText("");
        pbCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbCancelActionPerformed(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(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(pbCancel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(pbOk))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 135, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, pbAddOne, 0, 0, Short.MAX_VALUE)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, pbAddAll, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, pbRemoveAll, 0, 0, Short.MAX_VALUE)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, pbRemoveOne, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .add(6, 6, 6)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 143, 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.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(pbAddAll)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(pbAddOne)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(pbRemoveOne)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(pbRemoveAll))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(pbOk)
                    .add(pbCancel))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void pbAddAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbAddAllActionPerformed
        while(documentsModel.getSize()>0) {
            Pair p = (Pair)documentsModel.getElementAt(0);
            documentsUserModel.addPair(p);
            documentsModel.removePair(p);
        }
}//GEN-LAST:event_pbAddAllActionPerformed

    private void pbAddOneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbAddOneActionPerformed
        int[] indices = lstDocuments.getSelectedIndices();
        Vector<Pair> toRemove = new Vector<Pair>();
        for(int pos:indices) {
            Pair p = (Pair)documentsModel.getElementAt(pos);
            documentsUserModel.addPair(p);
            toRemove.add(p);
        }
        for(Pair p:toRemove) documentsModel.removePair(p);
}//GEN-LAST:event_pbAddOneActionPerformed

    private void pbRemoveOneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbRemoveOneActionPerformed
        int[] indices = lstDocumentsUser.getSelectedIndices();
        Vector<Pair> toRemove = new Vector<Pair>();
        for(int pos:indices) {
            Pair p = (Pair)documentsUserModel.getElementAt(pos);
            documentsModel.addPair(p);
            toRemove.add(p);
        }
        for(Pair p:toRemove) documentsUserModel.removePair(p);
}//GEN-LAST:event_pbRemoveOneActionPerformed

    private void pbRemoveAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbRemoveAllActionPerformed
        while(documentsUserModel.getSize()>0) {
            Pair p = (Pair)documentsUserModel.getElementAt(0);
            documentsModel.addPair(p);
            documentsUserModel.removePair(p);
        }
}//GEN-LAST:event_pbRemoveAllActionPerformed

    private void pbOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbOkActionPerformed
        DataHelper.saveUserDocuments(user);
        setVisible(false);
}//GEN-LAST:event_pbOkActionPerformed

    private void pbCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbCancelActionPerformed
        user.setDocuments(null);
        setVisible(false);
}//GEN-LAST:event_pbCancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lstDocuments;
    private javax.swing.JList lstDocumentsUser;
    private javax.swing.JButton pbAddAll;
    private javax.swing.JButton pbAddOne;
    private javax.swing.JButton pbCancel;
    private javax.swing.JButton pbOk;
    private javax.swing.JButton pbRemoveAll;
    private javax.swing.JButton pbRemoveOne;
    // End of variables declaration//GEN-END:variables

}
