OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.erp.core.reports.history.ui;

import org.openconcerto.erp.core.common.ui.DeviseNiceTableCellRenderer;
import org.openconcerto.erp.core.common.ui.PanelFrame;
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
import org.openconcerto.erp.core.finance.accounting.ui.ListeGestCommEltPanel;
import org.openconcerto.erp.core.finance.accounting.ui.SuppressionEcrituresPanel;
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureElementXmlSheet;
import org.openconcerto.erp.core.sales.quote.ui.EtatDevisRenderer;
import org.openconcerto.erp.core.supplychain.order.action.ImportProductsToOrder;
import org.openconcerto.erp.generationDoc.AbstractSheetXml;
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.element.SQLElementDirectory;
import org.openconcerto.sql.model.AliasedTable;
import org.openconcerto.sql.model.SQLDataSource;
import org.openconcerto.sql.model.SQLField;
import org.openconcerto.sql.model.SQLFieldsSet;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowListRSH;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.ComboSQLRequest;
import org.openconcerto.sql.request.ListSQLRequest;
import org.openconcerto.sql.users.rights.JListSQLTablePanel;
import org.openconcerto.sql.view.IListPanel;
import org.openconcerto.sql.view.ListeAddPanel;
import org.openconcerto.sql.view.list.IListe;
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
import org.openconcerto.sql.view.list.ITableModel;
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.FrameUtil;
import org.openconcerto.ui.SwingThreadUtils;
import org.openconcerto.utils.ExceptionHandler;
import org.openconcerto.utils.cc.ITransformer;

import java.awt.Color;
import java.awt.Component;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

public class ListeHistoriquePanel extends JPanel {

    private final Vector<IListPanel> vectListePanel = new Vector<IListPanel>();
    private Map<Integer, List<TableModelListener>> mapListener = new HashMap<Integer, List<TableModelListener>>();
    private JListSQLTablePanel jListePanel;

    private Map<SQLTable, SQLField> listFieldMap = new HashMap<SQLTable, SQLField>();
    private Map<String, Where> whereList = new HashMap<String, Where>();
    private static Map<SQLElement, Class<? extends AbstractSheetXml>> elementSheet = new HashMap<SQLElement, Class<? extends AbstractSheetXml>>();
    private String undefinedLabel;

    static {
        SQLElementDirectory dir = Configuration.getInstance().getDirectory();
        elementSheet.put(dir.getElement("SAISIE_VENTE_FACTURE_ELEMENT"), VenteFactureElementXmlSheet.class);
    }

    // Filtre à partir de la JList sur les IListe
    private final ListSelectionListener listListener = new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent e) {
            if (e != null && e.getValueIsAdjusting()) {
                return;
            }
            int selectIndex = ListeHistoriquePanel.this.jListePanel.getSelectedIndex();

            SQLRowAccessor row = ListeHistoriquePanel.this.jListePanel.getModel().getRowAt(selectIndex);

            if ((row == null || row.isUndefined()) && ListeHistoriquePanel.this.undefinedLabel == null) {
                return;
            }

            int id = SQLRow.NONEXISTANT_ID;
            if (row != null) {
                id = row.getID();
            }

            final int size = ListeHistoriquePanel.this.vectListePanel.size();
            for (int i = 0; i < size; i++) {
                IListPanel liste = ListeHistoriquePanel.this.vectListePanel.get(i);
                // TODO : verifier la pertinence de remove / add
                // remove listener
                if (ListeHistoriquePanel.this.mapListener.get(i) != null) {
                    List<TableModelListener> l = ListeHistoriquePanel.this.mapListener.get(i);
                    for (TableModelListener listener : l) {
                        liste.getListe().getTableModel().removeTableModelListener(listener);
                    }
                }

                Where w = null;
                final SQLTable table = liste.getElement().getTable();
                for (final Where wTmp : ListeHistoriquePanel.this.whereList.values()) {
                    if (new SQLFieldsSet(wTmp.getFields()).getTables().contains(liste.getListe().getRequest().getPrimaryTable())) {
                        w = wTmp.and(w);
                    }
                }

                if (id > 1) {
                    if (ListeHistoriquePanel.this.listFieldMap != null && ListeHistoriquePanel.this.listFieldMap.get(table) != null) {
                        SQLField field = ListeHistoriquePanel.this.listFieldMap.get(table);
                        Where w2 = new Where(field, "=", table.getForeignTable(field.getName()).getKey());
                        w2 = w2.and(new Where(table.getForeignTable(field.getName()).getField("ID_" + ListeHistoriquePanel.this.jListePanel.getModel().getTable().getName()), "=", id));
                        liste.getListe().getRequest().setWhere(w2.and(w));
                    } else {
                        if (liste.getElement().getTable().equals(ListeHistoriquePanel.this.jListePanel.getModel().getTable())) {
                            final Where whereMatch = new Where(table.getField("ID_" + ListeHistoriquePanel.this.jListePanel.getModel().getTable().getName()), "=", id);
                            if ((table.getName().equals("COMMANDE_ELEMENT") || table.getName().equals("BON_RECEPTION_ELEMENT"))
                                    && ListeHistoriquePanel.this.jListePanel.getModel().getTable().getName().equals("AFFAIRE")) {

                                // FIXME alias forcé à la main, voir pour remplacer avec un
                                // selectTransformer
                                final String tablePere = table.getName().replaceAll("_ELEMENT", "");
                                AliasedTable tableAlias = new AliasedTable(table.getTable(tablePere), "tAlias__ID_" + tablePere + "__" + tablePere);
                                final int idAffaire = id;
                                Where wPere = new Where(tableAlias.getField("ID_AFFAIRE"), "=", idAffaire);
                                liste.getListe().getRequest().setWhere(whereMatch.or(wPere).and(w));
                                // whereMatch = whereMatch.or(new Where(new
                                // AliasedTable(table.getForeignTable("ID_"+tablePere),
                                // alias).getField("ID_AFFAIRE"), "=", id));

                            } else {
                                liste.getListe().getRequest().setWhere(whereMatch.and(w));
                            }
                        } else {
                            if ((table.getName().equals("MOUVEMENT_STOCK")) && ListeHistoriquePanel.this.jListePanel.getModel().getTable().getName().equals("AFFAIRE")) {

                                final String tableStock = "STOCK";
                                AliasedTable tableAlias = new AliasedTable(table.getTable(tableStock), "tAlias__ID_" + tableStock + "__" + tableStock);
                                final int idAffaire = table.getTable("AFFAIRE").getRow(id).getForeignID("ID_DEPOT_STOCK");
                                Where w2 = new Where(table.getField("REEL"), "=", Boolean.TRUE);
                                w2 = w2.and(new Where(table.getField("SOURCE"), "=", ""));
                                Where wPere = new Where(tableAlias.getField("ID_DEPOT_STOCK"), "=", idAffaire).and(w2);
                                liste.getListe().getRequest().setWhere(wPere);

                            } else {
                                final Where whereMatch = new Where(table.getField("ID_" + ListeHistoriquePanel.this.jListePanel.getModel().getTable().getName()), "=", id);
                                if ((table.getName().equals("FACTURE_FOURNISSEUR_ELEMENT") || table.getName().equals("COMMANDE_ELEMENT") || table.getName().equals("BON_RECEPTION_ELEMENT"))
                                        && ListeHistoriquePanel.this.jListePanel.getModel().getTable().getName().equals("AFFAIRE")) {

                                    // FIXME alias forcé à la main, voir pour remplacer avec un
                                    // selectTransformer
                                    final String tablePere = table.getName().replaceAll("_ELEMENT", "");
                                    AliasedTable tableAlias = new AliasedTable(table.getTable(tablePere), "tAlias__ID_" + tablePere + "__" + tablePere);
                                    final int idAffaire = id;
                                    Where wPere = new Where(tableAlias.getField("ID_AFFAIRE"), "=", idAffaire);
                                    liste.getListe().getRequest().setWhere(whereMatch.or(wPere).and(w).and(new Where(table.getField("PA_HT"), "!=", BigDecimal.ZERO)));

                                } else {
                                    liste.getListe().getRequest().setWhere(whereMatch.and(w));
                                }
                            }
                        }
                    }
                } else {
                    liste.getListe().getRequest().setWhere(w);
                }
                liste.getListe().getModel().setCellsEditable(false);
                // Set renderer
                setRenderer(liste);

                // Set listener
                if (ListeHistoriquePanel.this.mapListener.get(i) != null) {
                    List<TableModelListener> l = ListeHistoriquePanel.this.mapListener.get(i);
                    for (TableModelListener listener : l) {
                        liste.getListe().getTableModel().addTableModelListener(listener);
                        if (elementSheet.get(liste.getElement()) != null) {
                            liste.getListe().addIListeActions(new MouseSheetXmlListeListener(elementSheet.get(liste.getElement())).getRowActions());
                        }
                    }
                }

            }
        }
    };

    public ListeHistoriquePanel(final String title, final ComboSQLRequest req, Map<String, List<String>> listTableOnglet, JPanel panelBottom, Map<SQLTable, SQLField> listFieldMap, Where where) {
        this(title, req, listTableOnglet, panelBottom, listFieldMap, "Tous", where);
    }

    public ListeHistoriquePanel(final String title, final ComboSQLRequest req, Map<String, List<String>> listTableOnglet, JPanel panelBottom, Map<SQLTable, SQLField> listFieldMap,
            String undefinedLabel, Where where) {
        this(title, req, listTableOnglet, panelBottom, listFieldMap, undefinedLabel, false, where, null);
    }

    public ListeHistoriquePanel(final String title, final ComboSQLRequest req, Map<String, List<String>> listTableOnglet, JPanel panelBottom, Map<SQLTable, SQLField> listFieldMap,
            String undefinedLabel, final boolean sourceWithOutTransformer, Where where, SQLTableModelSourceOnline tableSource) {
        this(title, req, listTableOnglet, panelBottom, listFieldMap, undefinedLabel, sourceWithOutTransformer, where, tableSource, null);
    }

    // TODO verifier que les tables contiennent bien la clef etrangere
    /**
     * @param title titre de la JList
     * @param req table à afficher la JListSQLTablePanel
     * @param listTableOnglet liste des tables à afficher
     * @param panelBottom panel à afficher en bas de la frame
     * @param listFieldMap jointure d'une table pour utiliser le filtre si la table ne contient pas
     *        de foreignKey pointant sur tableList
     * @param undefinedLabel label pour l'indéfini permettant de tout sélectionner, null si
     *        l'undefined n'est pas à inclure.
     * @param where
     */
    public ListeHistoriquePanel(final String title, final ComboSQLRequest req, Map<String, List<String>> listTableOnglet, JPanel panelBottom, Map<SQLTable, SQLField> listFieldMap,
            String undefinedLabel, final boolean sourceWithOutTransformer, final Where where, SQLTableModelSourceOnline tableSource, Map<String, String> splitWithField) {
        super();
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(2, 2, 1, 2);
        c.anchor = GridBagConstraints.WEST;
        c.fill = GridBagConstraints.BOTH;
        c.gridheight = 1;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;

        this.listFieldMap = listFieldMap;

        // Onglet de IListe
        JTabbedPane tabbedPane = new JTabbedPane();

        for (String key : listTableOnglet.keySet()) {

            List<String> listPanelTable = listTableOnglet.get(key);

            JPanel tabbedPanel = new JPanel(new GridBagLayout());
            tabbedPanel.setOpaque(false);
            GridBagConstraints c2 = new DefaultGridBagConstraints();
            c2.fill = GridBagConstraints.BOTH;
            c2.weightx = 1;
            c2.weighty = 1;
            c2.gridy = GridBagConstraints.RELATIVE;

            for (int i = 0; i < listPanelTable.size(); i++) {
                final SQLElement elt = Configuration.getInstance().getDirectory().getElement(listPanelTable.get(i));

                List<SQLRow> splitter = new ArrayList<SQLRow>();

                final String splitField = (splitWithField == null ? null : splitWithField.get(elt.getTable().getName()));
                if (splitField != null) {
                    SQLTable tableSplit = elt.getTable().getForeignTable(splitField);
                    SQLSelect sel = new SQLSelect();
                    sel.addSelectStar(tableSplit);
                    List<SQLRow> result = SQLRowListRSH.execute(sel);
                    splitter.addAll(result);
                } else {
                    splitter.add(null);
                }
                JTabbedPane tabbed = new JTabbedPane();
                for (int splitIndex = 0; splitIndex < splitter.size(); splitIndex++) {
                    final SQLRow sqlRow = splitter.get(splitIndex);
                    IListPanel liste;
                    final SQLTableModelSourceOnline createTableSource;
                    if (tableSource == null) {
                        createTableSource = elt.getTableSource(true);
                    } else {
                        createTableSource = tableSource;
                    }
                    final ListSQLRequest request = createTableSource.getReq();
                    if (sourceWithOutTransformer) {
                        request.setSelectTransf(null);
                    }
                    if (sqlRow != null) {
                        if (splitIndex == 0) {
                            request.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {

                                @Override
                                public SQLSelect transformChecked(SQLSelect input) {
                                    Where w = new Where(input.getTable(createTableSource.getPrimaryTable().getName()).getField(splitField), "=", sqlRow.getID());
                                    Where w2 = new Where(input.getTable(createTableSource.getPrimaryTable().getName()).getField(splitField), "=", sqlRow.getTable().getUndefinedID());
                                    w2 = w2.or(new Where(input.getTable(createTableSource.getPrimaryTable().getName()).getField(splitField), "=", (Object) null));
                                    input.setWhere(w.or(w2));
                                    return input;
                                }
                            });
                        } else {
                            request.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {

                                @Override
                                public SQLSelect transformChecked(SQLSelect input) {
                                    Where w = new Where(input.getTable(createTableSource.getPrimaryTable().getName()).getField(splitField), "=", sqlRow.getID());
                                    input.setWhere(w);
                                    return input;
                                }
                            });
                        }
                    }
                    if (where != null) {
                        if (new SQLFieldsSet(where.getFields()).getTables().contains(request.getPrimaryTable())) {
                            request.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {

                                @Override
                                public SQLSelect transformChecked(SQLSelect input) {
                                    input.setWhere(where);
                                    return input;
                                }
                            });
                            // request.setWhere(where);
                        }
                    }
                    // Evite de charger les listes completes à la création de la fenetre
                    request.setWhere(Where.FALSE);

                    if (elt.getTable().contains("ID_MOUVEMENT")) {

                        liste = new ListeGestCommEltPanel(elt, new IListe(createTableSource), "historique-" + title) {

                            protected void handleAction(JButton source, ActionEvent evt) {

                                if (elt.getTable().contains("ID_MOUVEMENT")) {
                                    SQLRow row = getListe().fetchSelectedRow();
                                    if (source == this.buttonModifier) {
                                        MouvementSQLElement.showSource(row.getInt("ID_MOUVEMENT"));
                                    } else {
                                        if (source == this.buttonEffacer) {
                                            PanelFrame f = new PanelFrame(new SuppressionEcrituresPanel(row.getInt("ID_MOUVEMENT")), "Suppresion d'une pièce");
                                            f.setLocationRelativeTo(null);
                                            f.setResizable(false);
                                            f.setVisible(true);
                                        } else {
                                            super.handleAction(source, evt);
                                        }
                                    }
                                } else {
                                    super.handleAction(source, evt);
                                }
                            }
                        };

                    } else

                    {

                        liste = new ListeAddPanel(elt, new IListe(createTableSource), "historique-" + title) {
                            @Override
                            protected void handleAction(JButton source, ActionEvent evt) {
                                if (source == this.buttonAjouter) {
                                    // toujours remplir la createFrame avec la ligne sélectionnée
                                    // car la frame écoute la sélection mais pas les modif, et se
                                    // reset
                                    // qd on la ferme
                                    // donc si on clic ajouter, on ferme, on modif la ligne, on clic
                                    // ajouter
                                    // on doit reremplir l'EditFrame
                                    int selectIndex = ListeHistoriquePanel.this.jListePanel.getSelectedIndex();
                                    SQLRowAccessor row = ListeHistoriquePanel.this.jListePanel.getModel().getRowAt(selectIndex);
                                    if (row != null && !row.isUndefined()) {
                                        SQLTable table = this.getCreateFrame().getSQLComponent().getElement().getTable();
                                        Set<SQLField> fields = table.getForeignKeys(ListeHistoriquePanel.this.jListePanel.getModel().getTable());
                                        if (fields != null && fields.size() > 0) {
                                            SQLRowValues rowVals = new SQLRowValues(table);
                                            rowVals.put(((SQLField) fields.toArray()[0]).getName(), row.getID());
                                            this.getCreateFrame().getSQLComponent().resetValue();
                                            this.getCreateFrame().getSQLComponent().select(rowVals);
                                        }
                                    }
                                    FrameUtil.show(this.getCreateFrame());
                                } else {
                                    super.handleAction(source, evt);
                                }
                            }
                        };
                        if (createTableSource.getPrimaryTable().getName().equals("DEMANDE_ACHAT_ELEMENT")) {
                            final ListeAddPanel listeDmd = (ListeAddPanel) liste;
                            PredicateRowAction actionDrop = new PredicateRowAction(new AbstractAction("Importer depuis Fichier Inventor") {

                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
                                    final FileDialog fd = new FileDialog(frame, "Import fichier inventor", FileDialog.LOAD);
                                    fd.setFilenameFilter(new FilenameFilter() {
                                        @Override
                                        public boolean accept(File dir, String name) {
                                            return name.endsWith(".xls");
                                        }
                                    });
                                    fd.setVisible(true);
                                    if (fd.getFile() != null) {

                                        ImportProductsToOrder importer = new ImportProductsToOrder();
                                        if (getSelectedRow() == null) {
                                            JOptionPane.showMessageDialog(frame, "Import impossible! Aucune affaire sélectionnée.");
                                        } else {
                                            int a = JOptionPane.showConfirmDialog(frame,
                                                    "Etes vous sûr de vouloir importer ces éléments dans l'affaire N°" + getSelectedRow().getString("NUMERO") + "?", "Import invertor",
                                                    JOptionPane.YES_NO_OPTION);
                                            if (a == JOptionPane.YES_OPTION) {
                                                importer.setRowAffaire(getSelectedRow());
                                                importer.setRowFamille(sqlRow);
                                                try {
                                                    importer.importFile(new File(fd.getDirectory(), fd.getFile()), createTableSource.getPrimaryTable().getDBRoot());
                                                    listeDmd.getListe().getModel().updateAll();
                                                } catch (IOException e1) {
                                                    ExceptionHandler.handle("Erreur lors de l'import du fichier!", e1);
                                                } catch (SQLException e1) {
                                                    ExceptionHandler.handle("Erreur lors de l'import du fichier!", e1);
                                                }
                                            }
                                        }
                                    }
                                }
                            }, true);
                            actionDrop.setPredicate(IListeEvent.createSelectionCountPredicate(0, Integer.MAX_VALUE));
                            liste.getListe().addIListeAction(actionDrop);
                        }
                    }

                    this.vectListePanel.add(liste);

                    setRenderer(liste);
                    if (elementSheet.get(liste.getElement()) != null) {
                        liste.getListe().addIListeActions(new MouseSheetXmlListeListener(elementSheet.get(liste.getElement())).getRowActions());
                    }
                    liste.getListe().getModel().setCellsEditable(false);
                    liste.setOpaque(false);

                    liste.setBorder(null);
                    liste.getListe().getModel().setHibernateDelay(-1);

                    if (listPanelTable.size() > 1) {
                        Font f = UIManager.getFont("TitledBorder.font");
                        f = f.deriveFont(Font.BOLD);
                        Border b = UIManager.getBorder("TitledBorder.border");
                        b = BorderFactory.createLineBorder(Color.BLACK);
                        liste.setBorder(BorderFactory.createTitledBorder(b, elt.getPluralName(), TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION, f));
                    }
                    if (sqlRow != null) {
                        String titleTab;
                        if (splitIndex == 0) {
                            titleTab = sqlRow.getString("NOM") + " et non classés";
                            tabbedPanel.add(tabbed, c2);
                        } else {
                            titleTab = sqlRow.getString("NOM");
                        }
                        tabbed.add(titleTab, liste);

                    } else {
                        tabbedPanel.add(liste, c2);
                    }
                }
            }

            tabbedPane.add(key, tabbedPanel);
        }

        // Left Panel
        this.undefinedLabel = undefinedLabel;
        this.jListePanel = new JListSQLTablePanel(req, undefinedLabel);

        // Right panel
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new GridBagLayout());
        GridBagConstraints cRight = new DefaultGridBagConstraints();
        cRight.fill = GridBagConstraints.BOTH;
        cRight.weightx = 1;
        cRight.weighty = 1;
        rightPanel.add(tabbedPane, cRight);

        if (panelBottom != null) {
            cRight.fill = GridBagConstraints.HORIZONTAL;
            cRight.weightx = 1;
            cRight.weighty = 0;
            cRight.gridy++;
            panelBottom.setBorder(BorderFactory.createTitledBorder("Récapitulatif"));
            rightPanel.add(panelBottom, cRight);
        }
        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, this.jListePanel, rightPanel);
        split.setBorder(null);
        split.setDividerLocation(275);
        this.add(split, c);

        JButton buttonClose = new JButton("Fermer");
        c.gridy++;
        c.weightx = 0;
        c.weighty = 0;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.EAST;
        this.add(buttonClose, c);

        buttonClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                ((JFrame) SwingUtilities.getRoot(ListeHistoriquePanel.this)).dispose();
            }
        });

        this.addAncestorListener(new AncestorListener() {
            @Override
            public void ancestorAdded(AncestorEvent event) {
                ListeHistoriquePanel.this.jListePanel.addListSelectionListener(ListeHistoriquePanel.this.listListener);
            }

            @Override
            public void ancestorMoved(AncestorEvent event) {

            }

            @Override
            public void ancestorRemoved(AncestorEvent event) {
                ListeHistoriquePanel.this.jListePanel.removeListSelectionListener(ListeHistoriquePanel.this.listListener);
            }
        });

    }

    public void selectIDinJList(int id) {
        this.jListePanel.selectID(id);
    }

    public void selectFirstIDinJList() {
        this.jListePanel.selectFirstID();
    }

    private void setRenderer(IListPanel liste) {

        SQLElement propositionItemElement = Configuration.getInstance().getDirectory().getElement("PROPOSITION_ELEMENT");
        SQLElement devisElement = Configuration.getInstance().getDirectory().getElement("DEVIS");
        JTable table = liste.getListe().getJTable();
        TableCellRenderer rend = null;
        if (liste.getElement().getClass() == devisElement.getClass()) {
            rend = new EtatDevisRenderer();
        } else {
                rend = new DeviseNiceTableCellRenderer();
        }
        for (int j = 0; j < table.getColumnCount(); j++) {
            if (rend instanceof EtatDevisRenderer) {
                table.getColumnModel().getColumn(j).setCellRenderer(rend);
            } else {
                if (table.getColumnClass(j) == Long.class || table.getColumnClass(j) == BigInteger.class) {
                    table.getColumnModel().getColumn(j).setCellRenderer(rend);
                }
            }
        }
    }

    public void addListSelectionListener(ListSelectionListener l) {
        this.jListePanel.addListSelectionListener(l);
        System.out.println("ListeHistoriquePanel.addListSelectionListener()" + this.jListePanel);
    }

    public void removeListSelectionListener(ListSelectionListener l) {
        this.jListePanel.removeListSelectionListener(l);
    }

    public SQLRowAccessor getSelectedRow() {
        return this.jListePanel.getModel().getRowAt(this.jListePanel.getSelectedIndex());
    }

    public void addWhere(String key, Where w) {
        this.whereList.put(key, w);
        this.listListener.valueChanged(null);
    }

    public void removeWhere(String key) {
        this.whereList.remove(key);
        this.listListener.valueChanged(null);
    }

    public void removeAllWhere() {
        for (String key : this.whereList.keySet()) {
            this.whereList.remove(key);
        }
        this.listListener.valueChanged(null);
    }

    public Map<String, Where> getWhere() {
        return this.whereList;
    }

    public List<Integer> getListId(String tableName) {
        IListe liste = getIListeFromTableName(tableName);
        List<Integer> listeIds = null;
        if (liste != null) {
            ITableModel m = liste.getModel();
            int size = m.getRowCount();
            listeIds = new ArrayList<>(size);
            for (int i = 0; i < size; i++) {
                try {
                    listeIds.add(m.idFromIndex(i));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        } else {
            listeIds = Collections.emptyList();
        }
        return listeIds;
    }

    public void removeAllTableListener() {
        this.jListePanel.removeAllTableListener();
        for (Integer i : this.mapListener.keySet()) {
            IListPanel panel = this.vectListePanel.get(i);
            List<TableModelListener> l = this.mapListener.get(i);
            for (TableModelListener tableModelListener : l) {
                final IListe liste = panel.getListe();
                if (liste != null) {
                    final TableModel tableModel = liste.getTableModel();
                    if (tableModel != null) {
                        tableModel.removeTableModelListener(tableModelListener);
                    }
                }
            }

        }
    }

    public void addListenerTable(TableModelListener listener, String tableName) {
        IListe liste = getIListeFromTableName(tableName);
        int index = getIndexFromTableName(tableName);
        if (liste != null) {
            liste.getModel().addTableModelListener(listener);
            List<TableModelListener> l = this.mapListener.get(index);
            if (l == null) {
                l = new ArrayList<TableModelListener>();
                this.mapListener.put(index, l);
            }
            l.add(listener);
        }
    }

    /**
     * Permet d'obtenir la IListe correspondant au nom d'une table
     * 
     * @param tableName nom de la table
     * @return la Iliste associée, dans le cas échéant null
     */

    public IListe getIListeFromTableName(String tableName) {
        IListPanel liste = getIListePanelFromTableName(tableName);
        if (liste == null) {
            return null;
        } else {
            return liste.getListe();
        }
    }

    /**
     * Permet d'obtenir la IListe correspondant au nom d'une table
     * 
     * @param tableName nom de la table
     * @return la Iliste associée, dans le cas échéant null
     */

    public IListPanel getIListePanelFromTableName(String tableName) {
        IListPanel liste = null;
        final int size = this.vectListePanel.size();
        for (int i = 0; i < size; i++) {
            IListPanel listeTmp = this.vectListePanel.get(i);
            // FIXME Null pointer Exception when client deleted
            if (listeTmp != null) {
                IListe list = listeTmp.getListe();
                if (list != null) {
                    final ITableModel model = list.getModel();
                    if (model != null) {
                        if (model.getTable().getName().equalsIgnoreCase(tableName)) {
                            liste = listeTmp;
                        }
                    }
                }
            }
        }
        return liste;
    }

    /**
     * Permet d'obtenir la IListe correspondant au nom d'une table
     * 
     * @param tableName nom de la table
     * @return la Iliste associée, dans le cas échéant null
     */

    public List<IListPanel> getAllIListePanelFromTableName(String tableName) {
        List<IListPanel> liste = new ArrayList<IListPanel>();
        final int size = this.vectListePanel.size();
        for (int i = 0; i < size; i++) {
            IListPanel listeTmp = this.vectListePanel.get(i);
            // FIXME Null pointer Exception when client deleted
            if (listeTmp != null) {
                IListe list = listeTmp.getListe();
                if (list != null) {
                    final ITableModel model = list.getModel();
                    if (model != null) {
                        if (model.getTable().getName().equalsIgnoreCase(tableName)) {
                            liste.add(listeTmp);
                        }
                    }
                }
            }
        }
        return liste;
    }

    /**
     * Permet d'obtenir la position dans le vecteur correspondant au nom d'une table
     * 
     * @param tableName nom de la table
     * @return -1 si la table n'est pas dans le vecteur
     */
    private int getIndexFromTableName(String tableName) {
        final int size = this.vectListePanel.size();
        for (int i = 0; i < size; i++) {
            final IListPanel listeTmp = this.vectListePanel.get(i);
            final String name = listeTmp.getListe().getModel().getTable().getName();
            if (name.equalsIgnoreCase(tableName)) {
                return i;
            }
        }
        return -1;
    }

    public IListe getListe(int index) {
        return this.vectListePanel.get(index).getListe();
    }

    public IListPanel getListePanel(int index) {
        return this.vectListePanel.get(index);
    }

    public void fireListesChanged() {
        final int size = this.vectListePanel.size();
        for (int i = 0; i < size; i++) {
            final IListPanel listeTmp = this.vectListePanel.get(i);
            final ITableModel model = listeTmp.getListe().getModel();
            model.fireTableDataChanged();
            model.fireTableStructureChanged();
        }

    }

}