/*
 * PnlFields.java
 *
 * Created on 15 septembre 2008, 11:29
 */

package fr.gouv.finances.cp.xemelios.ui.searchParams;

import fr.gouv.finances.dgfip.xemelios.common.config.ChampModel;
import fr.gouv.finances.dgfip.xemelios.common.config.ListeResultatModel;
import java.util.Vector;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author  chm
 */
public class PnlFields extends javax.swing.JPanel implements CommonDataUpdater {
    private ListeResultatModel lrm;
    private DisplayableFieldsTableModel tblModel;
    
    /** Creates new form PnlFields */
    public PnlFields(ListeResultatModel lrm) {
        super();
        this.lrm=lrm;
        initComponents();
        tblFields.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    
    /** 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() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblFields = new javax.swing.JTable();
        pbUp = new javax.swing.JButton();
        pbDown = new javax.swing.JButton();

        tblFields.setModel(tblModel = new PnlFields.DisplayableFieldsTableModel(lrm));
        jScrollPane1.setViewportView(tblFields);

        pbUp.setText("^");
        pbUp.setToolTipText("Monter");
        pbUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbUpActionPerformed(evt);
            }
        });

        pbDown.setText("V");
        pbDown.setToolTipText("Descendre");
        pbDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pbDownActionPerformed(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(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(pbUp)
                    .add(pbDown))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(13, 13, 13)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(pbUp)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(pbDown)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void pbUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbUpActionPerformed
        int i = tblFields.getSelectedRow();
        if(i>0) {
            ChampModel cm = tblModel.getData().remove(i);
            tblModel.getData().insertElementAt(cm, i-1);
            tblModel.fireTableDataChanged();
            tblFields.setRowSelectionInterval(i-1, i-1);
        }
}//GEN-LAST:event_pbUpActionPerformed

    private void pbDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pbDownActionPerformed
        int i = tblFields.getSelectedRow();
        if(i<tblModel.getData().size()-1) {
            ChampModel cm = tblModel.getData().remove(i);
            tblModel.getData().insertElementAt(cm, i+1);
            tblModel.fireTableDataChanged();
            tblFields.setRowSelectionInterval(i+1, i+1);
        }
    }//GEN-LAST:event_pbDownActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton pbDown;
    private javax.swing.JButton pbUp;
    private javax.swing.JTable tblFields;
    // End of variables declaration//GEN-END:variables

    public void doUpdate() {
        for(int i=lrm.getListeChamps().size()-1;i>=0;i--) {
            ChampModel cm = lrm.getListeChamps().elementAt(i);
            if(cm.isAffichable()) {
                lrm.getChamps().remove(cm);
                lrm.getListeChamps().remove(cm);
            }
        }
        for(ChampModel cm:tblModel.getData()) {
            try {
                lrm.addChild(cm, ChampModel.QN);
            } catch(Throwable t) {}
        }
    }
    
    public static class DisplayableFieldsTableModel extends AbstractTableModel {
        Vector<ChampModel> data;
        
        public DisplayableFieldsTableModel (ListeResultatModel lrm) {
            data = new Vector<ChampModel>();
            for(ChampModel cm: lrm.getListeChamps()) {
                if(cm.isAffichable()) data.add(cm); // cm.clone() ?
            }
        }

        public int getRowCount() {
            return data.size();
        }

        public int getColumnCount() {
            return 2;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            ChampModel cm = data.elementAt(rowIndex);
            if(columnIndex==0) return cm.isAfficheDefaut();
            else return cm.getLibelle();
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if(columnIndex==0) return Boolean.class;
            else return String.class;
        }

        @Override
        public String getColumnName(int column) {
            if(column==0) return "Afficher";
            else return "Champ";
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex==0;
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            ChampModel cm = data.elementAt(rowIndex);
            if(columnIndex==0) cm.setAfficheDefaut(((Boolean)aValue).booleanValue());
        }
        
        public Vector<ChampModel> getData() { return data; }
    }
    
}
