/*
 * 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 java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;

/**
 *
 * @author chm
 */
public class PnlSearchCollectivite extends javax.swing.JPanel {
    private static final int PAGE_SIZE = 100;
    private String lastNatId;
    private String lastCode;
    private String lastLibelle;
    private int currentPage;
    private int foundRecords = 0;
    private int lastPage;
    private DlgCollectivitesUtilisateur.TableCollectivitesModel model;
    private ArrayList<DataHelper.CollectiviteLib> selectedCollectivites;
    private Vector<Pair> natIds;

    /** Creates new form PnlSearchCollectivite */
    public PnlSearchCollectivite() {
        model = new DlgCollectivitesUtilisateur.TableCollectivitesModel(new ArrayList<DataHelper.CollectiviteLib>());
        natIds = new Vector<Pair>();
        natIds.add(new Pair("*","Tous"));
        for(String key: DlgCollectivitesUtilisateur.naturesIdentifiants.keySet()) {
            natIds.add(new Pair(key,DlgCollectivitesUtilisateur.naturesIdentifiants.get(key)));
        }
        initComponents();
    }
    public ArrayList<DataHelper.CollectiviteLib> getSelectedData() {
        selectedCollectivites = new ArrayList<DataHelper.CollectiviteLib>();
        int [] rows = tblCollectivite.getSelectedRows();
        for(int i:rows) {
            DataHelper.CollectiviteLib coll = model.getElementAt(i);
            selectedCollectivites.add(coll);
        }
        return selectedCollectivites;
    }

    /** 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() {

        jPanel1 = new javax.swing.JPanel();
        lblNatId = new javax.swing.JLabel();
        lblId = new javax.swing.JLabel();
        lblLib = new javax.swing.JLabel();
        dfLib = new javax.swing.JTextField();
        dfId = new javax.swing.JTextField();
        cbxNatIds = new javax.swing.JComboBox(natIds);
        pbSearch = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCollectivite = new org.jdesktop.swingx.JXTable();
        jPanel2 = new javax.swing.JPanel();
        pbFirstPage = new javax.swing.JButton();
        pbPreviousPage = new javax.swing.JButton();
        lblPages = new javax.swing.JLabel();
        pbNextPage = new javax.swing.JButton();
        pbLastPage = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Critères"));

        lblNatId.setText("Nature Identifiant");

        lblId.setText("Identifiant");

        lblLib.setText("Libellé");

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

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblNatId)
                    .add(lblId)
                    .add(lblLib))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(cbxNatIds, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 115, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 90, Short.MAX_VALUE)
                        .add(pbSearch, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(dfId, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, dfLib, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblNatId)
                    .add(cbxNatIds, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(pbSearch))
                .add(7, 7, 7)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblId)
                    .add(dfId, 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(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblLib)
                    .add(dfLib, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );

        tblCollectivite.setModel(model);
        tblCollectivite.setColumnControlVisible(true);
        jScrollPane1.setViewportView(tblCollectivite);

        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));

        pbFirstPage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fr/gouv/finances/cp/xemelios/ui/resources/page-first.png"))); // NOI18N
        pbFirstPage.setToolTipText("Première page");
        pbFirstPage.setEnabled(false);
        pbFirstPage.setMargin(new java.awt.Insets(2, 0, 2, 0));
        pbFirstPage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbFirstPageActionPerformed(evt);
            }
        });
        jPanel2.add(pbFirstPage);

        pbPreviousPage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fr/gouv/finances/cp/xemelios/ui/resources/page-previous.png"))); // NOI18N
        pbPreviousPage.setToolTipText("Page précédente");
        pbPreviousPage.setEnabled(false);
        pbPreviousPage.setMargin(new java.awt.Insets(2, 0, 2, 0));
        pbPreviousPage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbPreviousPageActionPerformed(evt);
            }
        });
        jPanel2.add(pbPreviousPage);

        lblPages.setText("0 / 0");
        jPanel2.add(lblPages);

        pbNextPage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fr/gouv/finances/cp/xemelios/ui/resources/page-next.png"))); // NOI18N
        pbNextPage.setToolTipText("Page suivante");
        pbNextPage.setEnabled(false);
        pbNextPage.setMargin(new java.awt.Insets(2, 0, 2, 0));
        pbNextPage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbNextPageActionPerformed(evt);
            }
        });
        jPanel2.add(pbNextPage);

        pbLastPage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fr/gouv/finances/cp/xemelios/ui/resources/page-last.png"))); // NOI18N
        pbLastPage.setToolTipText("Dernière page");
        pbLastPage.setEnabled(false);
        pbLastPage.setMargin(new java.awt.Insets(2, 0, 2, 0));
        pbLastPage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbLastPageActionPerformed(evt);
            }
        });
        jPanel2.add(pbLastPage);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, 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(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void pbSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbSearchActionPerformed
        lastNatId = ((Pair)cbxNatIds.getSelectedItem()).key;
        lastCode = dfId.getText().length()>0?dfId.getText():null;
        lastLibelle = dfLib.getText().length()>0?dfLib.getText():null;

        DataHelper.CollectiviteResponse resp = DataHelper.searchCollectivites(
                lastNatId,
                lastCode,
                lastLibelle,
                0, PAGE_SIZE);
        model.setData(resp.getData());
        foundRecords = resp.getFoundRecords();
        pbFirstPage.setEnabled(false);
        pbPreviousPage.setEnabled(false);
        pbNextPage.setEnabled(resp.getFoundRecords()>PAGE_SIZE);
        pbLastPage.setEnabled(resp.getFoundRecords()>PAGE_SIZE);
        currentPage = 0;
        lastPage = foundRecords / PAGE_SIZE;
        updatePageDisplay();
    }//GEN-LAST:event_pbSearchActionPerformed

    private void updatePageDisplay () {
        lblPages.setText((currentPage+1)+" / "+(lastPage+1));
    }
    private void pbFirstPageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbFirstPageActionPerformed
        DataHelper.CollectiviteResponse resp = DataHelper.searchCollectivites(
                lastNatId,
                lastCode,
                lastLibelle,
                0, PAGE_SIZE);
        model.setData(resp.getData());
        foundRecords = resp.getFoundRecords();
        pbFirstPage.setEnabled(false);
        pbPreviousPage.setEnabled(false);
        pbNextPage.setEnabled(resp.getFoundRecords()>PAGE_SIZE);
        pbLastPage.setEnabled(resp.getFoundRecords()>PAGE_SIZE);
        currentPage = 0;
        updatePageDisplay();
    }//GEN-LAST:event_pbFirstPageActionPerformed

    private void pbPreviousPageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbPreviousPageActionPerformed
        DataHelper.CollectiviteResponse resp = DataHelper.searchCollectivites(
                lastNatId,
                lastCode,
                lastLibelle,
                (currentPage--), PAGE_SIZE);
        model.setData(resp.getData());
        foundRecords = resp.getFoundRecords();
        pbFirstPage.setEnabled(currentPage>0);
        pbPreviousPage.setEnabled(currentPage>0);
        pbNextPage.setEnabled(lastPage > currentPage);
        pbLastPage.setEnabled(lastPage > currentPage);
        updatePageDisplay();
    }//GEN-LAST:event_pbPreviousPageActionPerformed

    private void pbNextPageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbNextPageActionPerformed
        DataHelper.CollectiviteResponse resp = DataHelper.searchCollectivites(
                lastNatId,
                lastCode,
                lastLibelle,
                (currentPage++), PAGE_SIZE);
        model.setData(resp.getData());
        foundRecords = resp.getFoundRecords();
        pbFirstPage.setEnabled(currentPage>0);
        pbPreviousPage.setEnabled(currentPage>0);
        pbNextPage.setEnabled(lastPage > currentPage);
        pbLastPage.setEnabled(lastPage > currentPage);
        updatePageDisplay();
    }//GEN-LAST:event_pbNextPageActionPerformed

    private void pbLastPageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbLastPageActionPerformed
        DataHelper.CollectiviteResponse resp = DataHelper.searchCollectivites(
                lastNatId,
                lastCode,
                lastLibelle,
                (currentPage = lastPage), PAGE_SIZE);
        model.setData(resp.getData());
        foundRecords = resp.getFoundRecords();
        pbFirstPage.setEnabled(currentPage>0);
        pbPreviousPage.setEnabled(currentPage>0);
        pbNextPage.setEnabled(lastPage > currentPage);
        pbLastPage.setEnabled(lastPage > currentPage);
        updatePageDisplay();
    }//GEN-LAST:event_pbLastPageActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbxNatIds;
    private javax.swing.JTextField dfId;
    private javax.swing.JTextField dfLib;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblId;
    private javax.swing.JLabel lblLib;
    private javax.swing.JLabel lblNatId;
    private javax.swing.JLabel lblPages;
    private javax.swing.JButton pbFirstPage;
    private javax.swing.JButton pbLastPage;
    private javax.swing.JButton pbNextPage;
    private javax.swing.JButton pbPreviousPage;
    private javax.swing.JButton pbSearch;
    private org.jdesktop.swingx.JXTable tblCollectivite;
    // End of variables declaration//GEN-END:variables

}
