OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Rev 182 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.reports.history.ui;
15
 
16
import org.openconcerto.erp.core.common.ui.DeviseNiceTableCellRenderer;
17
import org.openconcerto.erp.core.common.ui.PanelFrame;
18
import org.openconcerto.erp.core.finance.accounting.element.MouvementSQLElement;
19
import org.openconcerto.erp.core.finance.accounting.ui.ListeGestCommEltPanel;
20
import org.openconcerto.erp.core.finance.accounting.ui.SuppressionEcrituresPanel;
21
import org.openconcerto.erp.core.sales.invoice.report.VenteFactureElementXmlSheet;
22
import org.openconcerto.erp.core.sales.quote.ui.EtatDevisRenderer;
149 ilm 23
import org.openconcerto.erp.core.supplychain.order.action.ImportProductsToOrder;
18 ilm 24
import org.openconcerto.erp.generationDoc.AbstractSheetXml;
25
import org.openconcerto.erp.model.MouseSheetXmlListeListener;
26
import org.openconcerto.sql.Configuration;
27
import org.openconcerto.sql.element.SQLElement;
28
import org.openconcerto.sql.element.SQLElementDirectory;
177 ilm 29
import org.openconcerto.sql.model.AliasedTable;
80 ilm 30
import org.openconcerto.sql.model.SQLDataSource;
18 ilm 31
import org.openconcerto.sql.model.SQLField;
142 ilm 32
import org.openconcerto.sql.model.SQLFieldsSet;
18 ilm 33
import org.openconcerto.sql.model.SQLRow;
34
import org.openconcerto.sql.model.SQLRowAccessor;
144 ilm 35
import org.openconcerto.sql.model.SQLRowListRSH;
41 ilm 36
import org.openconcerto.sql.model.SQLRowValues;
144 ilm 37
import org.openconcerto.sql.model.SQLSelect;
18 ilm 38
import org.openconcerto.sql.model.SQLTable;
39
import org.openconcerto.sql.model.Where;
65 ilm 40
import org.openconcerto.sql.request.ComboSQLRequest;
67 ilm 41
import org.openconcerto.sql.request.ListSQLRequest;
18 ilm 42
import org.openconcerto.sql.users.rights.JListSQLTablePanel;
43
import org.openconcerto.sql.view.IListPanel;
44
import org.openconcerto.sql.view.ListeAddPanel;
45
import org.openconcerto.sql.view.list.IListe;
149 ilm 46
import org.openconcerto.sql.view.list.IListeAction.IListeEvent;
18 ilm 47
import org.openconcerto.sql.view.list.ITableModel;
149 ilm 48
import org.openconcerto.sql.view.list.RowAction.PredicateRowAction;
65 ilm 49
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
18 ilm 50
import org.openconcerto.ui.DefaultGridBagConstraints;
41 ilm 51
import org.openconcerto.ui.FrameUtil;
149 ilm 52
import org.openconcerto.ui.SwingThreadUtils;
53
import org.openconcerto.utils.ExceptionHandler;
144 ilm 54
import org.openconcerto.utils.cc.ITransformer;
18 ilm 55
 
28 ilm 56
import java.awt.Color;
149 ilm 57
import java.awt.Component;
58
import java.awt.FileDialog;
28 ilm 59
import java.awt.Font;
149 ilm 60
import java.awt.Frame;
18 ilm 61
import java.awt.GridBagConstraints;
62
import java.awt.GridBagLayout;
63
import java.awt.Insets;
64
import java.awt.event.ActionEvent;
65
import java.awt.event.ActionListener;
149 ilm 66
import java.io.File;
67
import java.io.FilenameFilter;
68
import java.io.IOException;
177 ilm 69
import java.math.BigDecimal;
18 ilm 70
import java.math.BigInteger;
149 ilm 71
import java.sql.SQLException;
18 ilm 72
import java.util.ArrayList;
73
import java.util.Collections;
74
import java.util.HashMap;
75
import java.util.List;
76
import java.util.Map;
80 ilm 77
import java.util.Random;
41 ilm 78
import java.util.Set;
18 ilm 79
import java.util.Vector;
80
 
149 ilm 81
import javax.swing.AbstractAction;
18 ilm 82
import javax.swing.BorderFactory;
83
import javax.swing.JButton;
84
import javax.swing.JFrame;
80 ilm 85
import javax.swing.JOptionPane;
18 ilm 86
import javax.swing.JPanel;
87
import javax.swing.JSplitPane;
88
import javax.swing.JTabbedPane;
89
import javax.swing.JTable;
90
import javax.swing.SwingUtilities;
28 ilm 91
import javax.swing.UIManager;
92
import javax.swing.border.Border;
93
import javax.swing.border.TitledBorder;
18 ilm 94
import javax.swing.event.AncestorEvent;
95
import javax.swing.event.AncestorListener;
96
import javax.swing.event.ListSelectionEvent;
97
import javax.swing.event.ListSelectionListener;
98
import javax.swing.event.TableModelListener;
99
import javax.swing.table.TableCellRenderer;
41 ilm 100
import javax.swing.table.TableModel;
18 ilm 101
 
102
public class ListeHistoriquePanel extends JPanel {
103
 
104
    private final Vector<IListPanel> vectListePanel = new Vector<IListPanel>();
105
    private Map<Integer, List<TableModelListener>> mapListener = new HashMap<Integer, List<TableModelListener>>();
106
    private JListSQLTablePanel jListePanel;
107
 
108
    private Map<SQLTable, SQLField> listFieldMap = new HashMap<SQLTable, SQLField>();
109
    private Map<String, Where> whereList = new HashMap<String, Where>();
110
    private static Map<SQLElement, Class<? extends AbstractSheetXml>> elementSheet = new HashMap<SQLElement, Class<? extends AbstractSheetXml>>();
61 ilm 111
    private String undefinedLabel;
18 ilm 112
 
113
    static {
114
        SQLElementDirectory dir = Configuration.getInstance().getDirectory();
115
        elementSheet.put(dir.getElement("SAISIE_VENTE_FACTURE_ELEMENT"), VenteFactureElementXmlSheet.class);
116
    }
117
 
118
    // Filtre à partir de la JList sur les IListe
119
    private final ListSelectionListener listListener = new ListSelectionListener() {
120
 
121
        public void valueChanged(ListSelectionEvent e) {
67 ilm 122
            if (e != null && e.getValueIsAdjusting()) {
123
                return;
124
            }
18 ilm 125
            int selectIndex = ListeHistoriquePanel.this.jListePanel.getSelectedIndex();
126
 
127
            SQLRowAccessor row = ListeHistoriquePanel.this.jListePanel.getModel().getRowAt(selectIndex);
61 ilm 128
 
177 ilm 129
            if ((row == null || row.isUndefined()) && ListeHistoriquePanel.this.undefinedLabel == null) {
61 ilm 130
                return;
131
            }
132
 
19 ilm 133
            int id = SQLRow.NONEXISTANT_ID;
18 ilm 134
            if (row != null) {
135
                id = row.getID();
136
            }
137
 
94 ilm 138
            final int size = ListeHistoriquePanel.this.vectListePanel.size();
139
            for (int i = 0; i < size; i++) {
18 ilm 140
                IListPanel liste = ListeHistoriquePanel.this.vectListePanel.get(i);
132 ilm 141
                // TODO : verifier la pertinence de remove / add
18 ilm 142
                // remove listener
143
                if (ListeHistoriquePanel.this.mapListener.get(i) != null) {
144
                    List<TableModelListener> l = ListeHistoriquePanel.this.mapListener.get(i);
145
                    for (TableModelListener listener : l) {
146
                        liste.getListe().getTableModel().removeTableModelListener(listener);
147
                    }
148
                }
149
 
150
                Where w = null;
151
                final SQLTable table = liste.getElement().getTable();
142 ilm 152
                for (final Where wTmp : ListeHistoriquePanel.this.whereList.values()) {
153
                    if (new SQLFieldsSet(wTmp.getFields()).getTables().contains(liste.getListe().getRequest().getPrimaryTable())) {
154
                        w = wTmp.and(w);
80 ilm 155
                    }
156
                }
157
 
18 ilm 158
                if (id > 1) {
159
                    if (ListeHistoriquePanel.this.listFieldMap != null && ListeHistoriquePanel.this.listFieldMap.get(table) != null) {
160
                        SQLField field = ListeHistoriquePanel.this.listFieldMap.get(table);
161
                        Where w2 = new Where(field, "=", table.getForeignTable(field.getName()).getKey());
162
                        w2 = w2.and(new Where(table.getForeignTable(field.getName()).getField("ID_" + ListeHistoriquePanel.this.jListePanel.getModel().getTable().getName()), "=", id));
19 ilm 163
                        liste.getListe().getRequest().setWhere(w2.and(w));
18 ilm 164
                    } else {
177 ilm 165
                        if (liste.getElement().getTable().equals(ListeHistoriquePanel.this.jListePanel.getModel().getTable())) {
166
                            final Where whereMatch = new Where(table.getField("ID_" + ListeHistoriquePanel.this.jListePanel.getModel().getTable().getName()), "=", id);
167
                            if ((table.getName().equals("COMMANDE_ELEMENT") || table.getName().equals("BON_RECEPTION_ELEMENT"))
168
                                    && ListeHistoriquePanel.this.jListePanel.getModel().getTable().getName().equals("AFFAIRE")) {
169
 
170
                                // FIXME alias forcé à la main, voir pour remplacer avec un
171
                                // selectTransformer
172
                                final String tablePere = table.getName().replaceAll("_ELEMENT", "");
173
                                AliasedTable tableAlias = new AliasedTable(table.getTable(tablePere), "tAlias__ID_" + tablePere + "__" + tablePere);
174
                                final int idAffaire = id;
175
                                Where wPere = new Where(tableAlias.getField("ID_AFFAIRE"), "=", idAffaire);
176
                                liste.getListe().getRequest().setWhere(whereMatch.or(wPere).and(w));
177
                                // whereMatch = whereMatch.or(new Where(new
178
                                // AliasedTable(table.getForeignTable("ID_"+tablePere),
179
                                // alias).getField("ID_AFFAIRE"), "=", id));
180
 
181
                            } else {
182
                                liste.getListe().getRequest().setWhere(whereMatch.and(w));
183
                            }
28 ilm 184
                        } else {
177 ilm 185
                            if ((table.getName().equals("MOUVEMENT_STOCK")) && ListeHistoriquePanel.this.jListePanel.getModel().getTable().getName().equals("AFFAIRE")) {
186
 
187
                                final String tableStock = "STOCK";
188
                                AliasedTable tableAlias = new AliasedTable(table.getTable(tableStock), "tAlias__ID_" + tableStock + "__" + tableStock);
189
                                final int idAffaire = table.getTable("AFFAIRE").getRow(id).getForeignID("ID_DEPOT_STOCK");
190
                                Where w2 = new Where(table.getField("REEL"), "=", Boolean.TRUE);
191
                                w2 = w2.and(new Where(table.getField("SOURCE"), "=", ""));
192
                                Where wPere = new Where(tableAlias.getField("ID_DEPOT_STOCK"), "=", idAffaire).and(w2);
193
                                liste.getListe().getRequest().setWhere(wPere);
194
 
195
                            } else {
196
                                final Where whereMatch = new Where(table.getField("ID_" + ListeHistoriquePanel.this.jListePanel.getModel().getTable().getName()), "=", id);
197
                                if ((table.getName().equals("FACTURE_FOURNISSEUR_ELEMENT") || table.getName().equals("COMMANDE_ELEMENT") || table.getName().equals("BON_RECEPTION_ELEMENT"))
198
                                        && ListeHistoriquePanel.this.jListePanel.getModel().getTable().getName().equals("AFFAIRE")) {
199
 
200
                                    // FIXME alias forcé à la main, voir pour remplacer avec un
201
                                    // selectTransformer
202
                                    final String tablePere = table.getName().replaceAll("_ELEMENT", "");
203
                                    AliasedTable tableAlias = new AliasedTable(table.getTable(tablePere), "tAlias__ID_" + tablePere + "__" + tablePere);
204
                                    final int idAffaire = id;
205
                                    Where wPere = new Where(tableAlias.getField("ID_AFFAIRE"), "=", idAffaire);
206
                                    liste.getListe().getRequest().setWhere(whereMatch.or(wPere).and(w).and(new Where(table.getField("PA_HT"), "!=", BigDecimal.ZERO)));
207
 
208
                                } else {
209
                                    liste.getListe().getRequest().setWhere(whereMatch.and(w));
210
                                }
211
                            }
28 ilm 212
                        }
18 ilm 213
                    }
214
                } else {
19 ilm 215
                    liste.getListe().getRequest().setWhere(w);
18 ilm 216
                }
132 ilm 217
                liste.getListe().getModel().setCellsEditable(false);
18 ilm 218
                // Set renderer
219
                setRenderer(liste);
220
 
221
                // Set listener
222
                if (ListeHistoriquePanel.this.mapListener.get(i) != null) {
223
                    List<TableModelListener> l = ListeHistoriquePanel.this.mapListener.get(i);
224
                    for (TableModelListener listener : l) {
225
                        liste.getListe().getTableModel().addTableModelListener(listener);
226
                        if (elementSheet.get(liste.getElement()) != null) {
25 ilm 227
                            liste.getListe().addIListeActions(new MouseSheetXmlListeListener(elementSheet.get(liste.getElement())).getRowActions());
18 ilm 228
                        }
229
                    }
230
                }
231
 
232
            }
233
        }
234
    };
235
 
67 ilm 236
    public ListeHistoriquePanel(final String title, final ComboSQLRequest req, Map<String, List<String>> listTableOnglet, JPanel panelBottom, Map<SQLTable, SQLField> listFieldMap, Where where) {
237
        this(title, req, listTableOnglet, panelBottom, listFieldMap, "Tous", where);
61 ilm 238
    }
239
 
65 ilm 240
    public ListeHistoriquePanel(final String title, final ComboSQLRequest req, Map<String, List<String>> listTableOnglet, JPanel panelBottom, Map<SQLTable, SQLField> listFieldMap,
67 ilm 241
            String undefinedLabel, Where where) {
83 ilm 242
        this(title, req, listTableOnglet, panelBottom, listFieldMap, undefinedLabel, false, where, null);
65 ilm 243
    }
244
 
144 ilm 245
    public ListeHistoriquePanel(final String title, final ComboSQLRequest req, Map<String, List<String>> listTableOnglet, JPanel panelBottom, Map<SQLTable, SQLField> listFieldMap,
246
            String undefinedLabel, final boolean sourceWithOutTransformer, Where where, SQLTableModelSourceOnline tableSource) {
247
        this(title, req, listTableOnglet, panelBottom, listFieldMap, undefinedLabel, sourceWithOutTransformer, where, tableSource, null);
248
    }
249
 
18 ilm 250
    // TODO verifier que les tables contiennent bien la clef etrangere
251
    /**
252
     * @param title titre de la JList
65 ilm 253
     * @param req table à afficher la JListSQLTablePanel
18 ilm 254
     * @param listTableOnglet liste des tables à afficher
255
     * @param panelBottom panel à afficher en bas de la frame
256
     * @param listFieldMap jointure d'une table pour utiliser le filtre si la table ne contient pas
257
     *        de foreignKey pointant sur tableList
61 ilm 258
     * @param undefinedLabel label pour l'indéfini permettant de tout sélectionner, null si
259
     *        l'undefined n'est pas à inclure.
67 ilm 260
     * @param where
18 ilm 261
     */
65 ilm 262
    public ListeHistoriquePanel(final String title, final ComboSQLRequest req, Map<String, List<String>> listTableOnglet, JPanel panelBottom, Map<SQLTable, SQLField> listFieldMap,
144 ilm 263
            String undefinedLabel, final boolean sourceWithOutTransformer, final Where where, SQLTableModelSourceOnline tableSource, Map<String, String> splitWithField) {
18 ilm 264
        super();
265
        this.setLayout(new GridBagLayout());
266
        GridBagConstraints c = new GridBagConstraints();
267
        c.insets = new Insets(2, 2, 1, 2);
268
        c.anchor = GridBagConstraints.WEST;
269
        c.fill = GridBagConstraints.BOTH;
270
        c.gridheight = 1;
271
        c.gridwidth = 1;
272
        c.gridx = 0;
273
        c.gridy = 0;
274
        c.weightx = 1;
275
        c.weighty = 1;
276
 
277
        this.listFieldMap = listFieldMap;
278
 
279
        // Onglet de IListe
280
        JTabbedPane tabbedPane = new JTabbedPane();
281
 
28 ilm 282
        for (String key : listTableOnglet.keySet()) {
18 ilm 283
 
28 ilm 284
            List<String> listPanelTable = listTableOnglet.get(key);
18 ilm 285
 
28 ilm 286
            JPanel tabbedPanel = new JPanel(new GridBagLayout());
41 ilm 287
            tabbedPanel.setOpaque(false);
28 ilm 288
            GridBagConstraints c2 = new DefaultGridBagConstraints();
289
            c2.fill = GridBagConstraints.BOTH;
290
            c2.weightx = 1;
291
            c2.weighty = 1;
292
            c2.gridy = GridBagConstraints.RELATIVE;
18 ilm 293
 
28 ilm 294
            for (int i = 0; i < listPanelTable.size(); i++) {
295
                final SQLElement elt = Configuration.getInstance().getDirectory().getElement(listPanelTable.get(i));
296
 
144 ilm 297
                List<SQLRow> splitter = new ArrayList<SQLRow>();
298
 
299
                final String splitField = (splitWithField == null ? null : splitWithField.get(elt.getTable().getName()));
300
                if (splitField != null) {
301
                    SQLTable tableSplit = elt.getTable().getForeignTable(splitField);
302
                    SQLSelect sel = new SQLSelect();
303
                    sel.addSelectStar(tableSplit);
304
                    List<SQLRow> result = SQLRowListRSH.execute(sel);
305
                    splitter.addAll(result);
83 ilm 306
                } else {
144 ilm 307
                    splitter.add(null);
83 ilm 308
                }
144 ilm 309
                JTabbedPane tabbed = new JTabbedPane();
310
                for (int splitIndex = 0; splitIndex < splitter.size(); splitIndex++) {
311
                    final SQLRow sqlRow = splitter.get(splitIndex);
312
                    IListPanel liste;
313
                    final SQLTableModelSourceOnline createTableSource;
314
                    if (tableSource == null) {
315
                        createTableSource = elt.getTableSource(true);
316
                    } else {
317
                        createTableSource = tableSource;
67 ilm 318
                    }
144 ilm 319
                    final ListSQLRequest request = createTableSource.getReq();
320
                    if (sourceWithOutTransformer) {
321
                        request.setSelectTransf(null);
322
                    }
323
                    if (sqlRow != null) {
324
                        if (splitIndex == 0) {
325
                            request.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
28 ilm 326
 
144 ilm 327
                                @Override
328
                                public SQLSelect transformChecked(SQLSelect input) {
329
                                    Where w = new Where(input.getTable(createTableSource.getPrimaryTable().getName()).getField(splitField), "=", sqlRow.getID());
330
                                    Where w2 = new Where(input.getTable(createTableSource.getPrimaryTable().getName()).getField(splitField), "=", sqlRow.getTable().getUndefinedID());
331
                                    w2 = w2.or(new Where(input.getTable(createTableSource.getPrimaryTable().getName()).getField(splitField), "=", (Object) null));
332
                                    input.setWhere(w.or(w2));
333
                                    return input;
334
                                }
335
                            });
336
                        } else {
337
                            request.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
67 ilm 338
 
144 ilm 339
                                @Override
340
                                public SQLSelect transformChecked(SQLSelect input) {
341
                                    Where w = new Where(input.getTable(createTableSource.getPrimaryTable().getName()).getField(splitField), "=", sqlRow.getID());
342
                                    input.setWhere(w);
343
                                    return input;
344
                                }
345
                            });
346
                        }
347
                    }
348
                    if (where != null) {
349
                        if (new SQLFieldsSet(where.getFields()).getTables().contains(request.getPrimaryTable())) {
350
                            request.setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
67 ilm 351
 
144 ilm 352
                                @Override
149 ilm 353
                                public SQLSelect transformChecked(SQLSelect input) {
144 ilm 354
                                    input.setWhere(where);
355
                                    return input;
356
                                }
357
                            });
358
                            // request.setWhere(where);
359
                        }
360
                    }
156 ilm 361
                    // Evite de charger les listes completes à la création de la fenetre
362
                    request.setWhere(Where.FALSE);
28 ilm 363
 
144 ilm 364
                    if (elt.getTable().contains("ID_MOUVEMENT")) {
365
 
366
                        liste = new ListeGestCommEltPanel(elt, new IListe(createTableSource), "historique-" + title) {
367
 
368
                            protected void handleAction(JButton source, ActionEvent evt) {
369
 
370
                                if (elt.getTable().contains("ID_MOUVEMENT")) {
371
                                    SQLRow row = getListe().fetchSelectedRow();
372
                                    if (source == this.buttonModifier) {
373
                                        MouvementSQLElement.showSource(row.getInt("ID_MOUVEMENT"));
28 ilm 374
                                    } else {
144 ilm 375
                                        if (source == this.buttonEffacer) {
376
                                            PanelFrame f = new PanelFrame(new SuppressionEcrituresPanel(row.getInt("ID_MOUVEMENT")), "Suppresion d'une pièce");
377
                                            f.setLocationRelativeTo(null);
378
                                            f.setResizable(false);
379
                                            f.setVisible(true);
380
                                        } else {
381
                                            super.handleAction(source, evt);
382
                                        }
28 ilm 383
                                    }
144 ilm 384
                                } else {
385
                                    super.handleAction(source, evt);
18 ilm 386
                                }
387
                            }
144 ilm 388
                        };
18 ilm 389
 
177 ilm 390
                    } else
67 ilm 391
 
177 ilm 392
                    {
393
 
144 ilm 394
                        liste = new ListeAddPanel(elt, new IListe(createTableSource), "historique-" + title) {
395
                            @Override
396
                            protected void handleAction(JButton source, ActionEvent evt) {
397
                                if (source == this.buttonAjouter) {
398
                                    // toujours remplir la createFrame avec la ligne sélectionnée
399
                                    // car la frame écoute la sélection mais pas les modif, et se
400
                                    // reset
401
                                    // qd on la ferme
402
                                    // donc si on clic ajouter, on ferme, on modif la ligne, on clic
403
                                    // ajouter
404
                                    // on doit reremplir l'EditFrame
405
                                    int selectIndex = ListeHistoriquePanel.this.jListePanel.getSelectedIndex();
406
                                    SQLRowAccessor row = ListeHistoriquePanel.this.jListePanel.getModel().getRowAt(selectIndex);
407
                                    if (row != null && !row.isUndefined()) {
408
                                        SQLTable table = this.getCreateFrame().getSQLComponent().getElement().getTable();
409
                                        Set<SQLField> fields = table.getForeignKeys(ListeHistoriquePanel.this.jListePanel.getModel().getTable());
410
                                        if (fields != null && fields.size() > 0) {
411
                                            SQLRowValues rowVals = new SQLRowValues(table);
412
                                            rowVals.put(((SQLField) fields.toArray()[0]).getName(), row.getID());
413
                                            this.getCreateFrame().getSQLComponent().resetValue();
414
                                            this.getCreateFrame().getSQLComponent().select(rowVals);
415
                                        }
41 ilm 416
                                    }
144 ilm 417
                                    FrameUtil.show(this.getCreateFrame());
418
                                } else {
419
                                    super.handleAction(source, evt);
41 ilm 420
                                }
421
                            }
144 ilm 422
                        };
149 ilm 423
                        if (createTableSource.getPrimaryTable().getName().equals("DEMANDE_ACHAT_ELEMENT")) {
424
                            final ListeAddPanel listeDmd = (ListeAddPanel) liste;
425
                            PredicateRowAction actionDrop = new PredicateRowAction(new AbstractAction("Importer depuis Fichier Inventor") {
426
 
427
                                @Override
428
                                public void actionPerformed(ActionEvent e) {
429
                                    final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
430
                                    final FileDialog fd = new FileDialog(frame, "Import fichier inventor", FileDialog.LOAD);
431
                                    fd.setFilenameFilter(new FilenameFilter() {
432
                                        @Override
433
                                        public boolean accept(File dir, String name) {
434
                                            return name.endsWith(".xls");
435
                                        }
436
                                    });
437
                                    fd.setVisible(true);
438
                                    if (fd.getFile() != null) {
439
 
440
                                        ImportProductsToOrder importer = new ImportProductsToOrder();
441
                                        if (getSelectedRow() == null) {
442
                                            JOptionPane.showMessageDialog(frame, "Import impossible! Aucune affaire sélectionnée.");
443
                                        } else {
444
                                            int a = JOptionPane.showConfirmDialog(frame,
445
                                                    "Etes vous sûr de vouloir importer ces éléments dans l'affaire N°" + getSelectedRow().getString("NUMERO") + "?", "Import invertor",
446
                                                    JOptionPane.YES_NO_OPTION);
447
                                            if (a == JOptionPane.YES_OPTION) {
448
                                                importer.setRowAffaire(getSelectedRow());
449
                                                importer.setRowFamille(sqlRow);
450
                                                try {
451
                                                    importer.importFile(new File(fd.getDirectory(), fd.getFile()), createTableSource.getPrimaryTable().getDBRoot());
452
                                                    listeDmd.getListe().getModel().updateAll();
453
                                                } catch (IOException e1) {
454
                                                    ExceptionHandler.handle("Erreur lors de l'import du fichier!", e1);
455
                                                } catch (SQLException e1) {
456
                                                    ExceptionHandler.handle("Erreur lors de l'import du fichier!", e1);
457
                                                }
458
                                            }
459
                                        }
460
                                    }
461
                                }
462
                            }, true);
463
                            actionDrop.setPredicate(IListeEvent.createSelectionCountPredicate(0, Integer.MAX_VALUE));
464
                            liste.getListe().addIListeAction(actionDrop);
465
                        }
144 ilm 466
                    }
19 ilm 467
 
144 ilm 468
                    this.vectListePanel.add(liste);
18 ilm 469
 
144 ilm 470
                    setRenderer(liste);
471
                    if (elementSheet.get(liste.getElement()) != null) {
472
                        liste.getListe().addIListeActions(new MouseSheetXmlListeListener(elementSheet.get(liste.getElement())).getRowActions());
473
                    }
474
                    liste.getListe().getModel().setCellsEditable(false);
475
                    liste.setOpaque(false);
28 ilm 476
 
144 ilm 477
                    liste.setBorder(null);
478
                    liste.getListe().getModel().setHibernateDelay(-1);
28 ilm 479
 
144 ilm 480
                    if (listPanelTable.size() > 1) {
481
                        Font f = UIManager.getFont("TitledBorder.font");
482
                        f = f.deriveFont(Font.BOLD);
483
                        Border b = UIManager.getBorder("TitledBorder.border");
484
                        b = BorderFactory.createLineBorder(Color.BLACK);
485
                        liste.setBorder(BorderFactory.createTitledBorder(b, elt.getPluralName(), TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION, f));
486
                    }
487
                    if (sqlRow != null) {
488
                        String titleTab;
489
                        if (splitIndex == 0) {
490
                            titleTab = sqlRow.getString("NOM") + " et non classés";
491
                            tabbedPanel.add(tabbed, c2);
492
                        } else {
493
                            titleTab = sqlRow.getString("NOM");
494
                        }
495
                        tabbed.add(titleTab, liste);
28 ilm 496
 
144 ilm 497
                    } else {
498
                        tabbedPanel.add(liste, c2);
499
                    }
500
                }
18 ilm 501
            }
502
 
28 ilm 503
            tabbedPane.add(key, tabbedPanel);
18 ilm 504
        }
505
 
506
        // Left Panel
65 ilm 507
        this.undefinedLabel = undefinedLabel;
508
        this.jListePanel = new JListSQLTablePanel(req, undefinedLabel);
18 ilm 509
 
510
        // Right panel
511
        JPanel rightPanel = new JPanel();
512
        rightPanel.setLayout(new GridBagLayout());
513
        GridBagConstraints cRight = new DefaultGridBagConstraints();
514
        cRight.fill = GridBagConstraints.BOTH;
515
        cRight.weightx = 1;
516
        cRight.weighty = 1;
517
        rightPanel.add(tabbedPane, cRight);
518
 
519
        if (panelBottom != null) {
520
            cRight.fill = GridBagConstraints.HORIZONTAL;
521
            cRight.weightx = 1;
522
            cRight.weighty = 0;
523
            cRight.gridy++;
524
            panelBottom.setBorder(BorderFactory.createTitledBorder("Récapitulatif"));
525
            rightPanel.add(panelBottom, cRight);
526
        }
527
        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, this.jListePanel, rightPanel);
528
        split.setBorder(null);
529
        split.setDividerLocation(275);
530
        this.add(split, c);
531
 
532
        JButton buttonClose = new JButton("Fermer");
533
        c.gridy++;
534
        c.weightx = 0;
535
        c.weighty = 0;
536
        c.fill = GridBagConstraints.NONE;
537
        c.anchor = GridBagConstraints.EAST;
538
        this.add(buttonClose, c);
539
 
540
        buttonClose.addActionListener(new ActionListener() {
541
            public void actionPerformed(ActionEvent event) {
542
                ((JFrame) SwingUtilities.getRoot(ListeHistoriquePanel.this)).dispose();
543
            }
544
        });
545
 
546
        this.addAncestorListener(new AncestorListener() {
547
            @Override
548
            public void ancestorAdded(AncestorEvent event) {
177 ilm 549
                ListeHistoriquePanel.this.jListePanel.addListSelectionListener(ListeHistoriquePanel.this.listListener);
18 ilm 550
            }
551
 
552
            @Override
553
            public void ancestorMoved(AncestorEvent event) {
19 ilm 554
 
18 ilm 555
            }
556
 
557
            @Override
558
            public void ancestorRemoved(AncestorEvent event) {
177 ilm 559
                ListeHistoriquePanel.this.jListePanel.removeListSelectionListener(ListeHistoriquePanel.this.listListener);
18 ilm 560
            }
561
        });
80 ilm 562
 
18 ilm 563
    }
564
 
565
    public void selectIDinJList(int id) {
41 ilm 566
        this.jListePanel.selectID(id);
18 ilm 567
    }
568
 
132 ilm 569
    public void selectFirstIDinJList() {
570
        this.jListePanel.selectFirstID();
571
    }
572
 
18 ilm 573
    private void setRenderer(IListPanel liste) {
574
 
19 ilm 575
        SQLElement propositionItemElement = Configuration.getInstance().getDirectory().getElement("PROPOSITION_ELEMENT");
18 ilm 576
        SQLElement devisElement = Configuration.getInstance().getDirectory().getElement("DEVIS");
577
        JTable table = liste.getListe().getJTable();
578
        TableCellRenderer rend = null;
19 ilm 579
        if (liste.getElement().getClass() == devisElement.getClass()) {
18 ilm 580
            rend = new EtatDevisRenderer();
581
        } else {
19 ilm 582
                rend = new DeviseNiceTableCellRenderer();
18 ilm 583
        }
584
        for (int j = 0; j < table.getColumnCount(); j++) {
585
            if (rend instanceof EtatDevisRenderer) {
586
                table.getColumnModel().getColumn(j).setCellRenderer(rend);
587
            } else {
588
                if (table.getColumnClass(j) == Long.class || table.getColumnClass(j) == BigInteger.class) {
589
                    table.getColumnModel().getColumn(j).setCellRenderer(rend);
590
                }
591
            }
592
        }
593
    }
594
 
595
    public void addListSelectionListener(ListSelectionListener l) {
596
        this.jListePanel.addListSelectionListener(l);
177 ilm 597
        System.out.println("ListeHistoriquePanel.addListSelectionListener()" + this.jListePanel);
18 ilm 598
    }
599
 
600
    public void removeListSelectionListener(ListSelectionListener l) {
601
        this.jListePanel.removeListSelectionListener(l);
602
    }
603
 
604
    public SQLRowAccessor getSelectedRow() {
605
        return this.jListePanel.getModel().getRowAt(this.jListePanel.getSelectedIndex());
606
    }
607
 
608
    public void addWhere(String key, Where w) {
609
        this.whereList.put(key, w);
610
        this.listListener.valueChanged(null);
611
    }
612
 
613
    public void removeWhere(String key) {
614
        this.whereList.remove(key);
615
        this.listListener.valueChanged(null);
616
    }
617
 
618
    public void removeAllWhere() {
619
        for (String key : this.whereList.keySet()) {
620
            this.whereList.remove(key);
621
        }
622
        this.listListener.valueChanged(null);
623
    }
624
 
625
    public Map<String, Where> getWhere() {
626
        return this.whereList;
627
    }
628
 
629
    public List<Integer> getListId(String tableName) {
630
        IListe liste = getIListeFromTableName(tableName);
631
        List<Integer> listeIds = null;
632
        if (liste != null) {
177 ilm 633
            ITableModel m = liste.getModel();
634
            int size = m.getRowCount();
635
            listeIds = new ArrayList<>(size);
18 ilm 636
            for (int i = 0; i < size; i++) {
177 ilm 637
                try {
638
                    listeIds.add(m.idFromIndex(i));
639
                } catch (Exception e) {
640
                    e.printStackTrace();
641
                }
18 ilm 642
            }
177 ilm 643
 
18 ilm 644
        } else {
67 ilm 645
            listeIds = Collections.emptyList();
18 ilm 646
        }
647
        return listeIds;
648
    }
649
 
650
    public void removeAllTableListener() {
41 ilm 651
        this.jListePanel.removeAllTableListener();
18 ilm 652
        for (Integer i : this.mapListener.keySet()) {
177 ilm 653
            IListPanel panel = this.vectListePanel.get(i);
18 ilm 654
            List<TableModelListener> l = this.mapListener.get(i);
655
            for (TableModelListener tableModelListener : l) {
656
                final IListe liste = panel.getListe();
41 ilm 657
                if (liste != null) {
658
                    final TableModel tableModel = liste.getTableModel();
659
                    if (tableModel != null) {
660
                        tableModel.removeTableModelListener(tableModelListener);
661
                    }
662
                }
18 ilm 663
            }
664
 
665
        }
666
    }
667
 
668
    public void addListenerTable(TableModelListener listener, String tableName) {
669
        IListe liste = getIListeFromTableName(tableName);
670
        int index = getIndexFromTableName(tableName);
671
        if (liste != null) {
132 ilm 672
            liste.getModel().addTableModelListener(listener);
93 ilm 673
            List<TableModelListener> l = this.mapListener.get(index);
18 ilm 674
            if (l == null) {
675
                l = new ArrayList<TableModelListener>();
676
                this.mapListener.put(index, l);
677
            }
678
            l.add(listener);
679
        }
680
    }
681
 
682
    /**
683
     * Permet d'obtenir la IListe correspondant au nom d'une table
684
     *
685
     * @param tableName nom de la table
686
     * @return la Iliste associée, dans le cas échéant null
687
     */
688
 
28 ilm 689
    public IListe getIListeFromTableName(String tableName) {
67 ilm 690
        IListPanel liste = getIListePanelFromTableName(tableName);
691
        if (liste == null) {
692
            return null;
693
        } else {
694
            return liste.getListe();
695
        }
696
    }
697
 
698
    /**
699
     * Permet d'obtenir la IListe correspondant au nom d'une table
700
     *
701
     * @param tableName nom de la table
702
     * @return la Iliste associée, dans le cas échéant null
703
     */
704
 
705
    public IListPanel getIListePanelFromTableName(String tableName) {
706
        IListPanel liste = null;
80 ilm 707
        final int size = this.vectListePanel.size();
708
        for (int i = 0; i < size; i++) {
18 ilm 709
            IListPanel listeTmp = this.vectListePanel.get(i);
710
            // FIXME Null pointer Exception when client deleted
711
            if (listeTmp != null) {
712
                IListe list = listeTmp.getListe();
713
                if (list != null) {
714
                    final ITableModel model = list.getModel();
715
                    if (model != null) {
716
                        if (model.getTable().getName().equalsIgnoreCase(tableName)) {
67 ilm 717
                            liste = listeTmp;
18 ilm 718
                        }
719
                    }
720
                }
721
            }
722
        }
723
        return liste;
724
    }
725
 
726
    /**
144 ilm 727
     * Permet d'obtenir la IListe correspondant au nom d'une table
728
     *
729
     * @param tableName nom de la table
730
     * @return la Iliste associée, dans le cas échéant null
731
     */
732
 
733
    public List<IListPanel> getAllIListePanelFromTableName(String tableName) {
734
        List<IListPanel> liste = new ArrayList<IListPanel>();
735
        final int size = this.vectListePanel.size();
736
        for (int i = 0; i < size; i++) {
737
            IListPanel listeTmp = this.vectListePanel.get(i);
738
            // FIXME Null pointer Exception when client deleted
739
            if (listeTmp != null) {
740
                IListe list = listeTmp.getListe();
741
                if (list != null) {
742
                    final ITableModel model = list.getModel();
743
                    if (model != null) {
744
                        if (model.getTable().getName().equalsIgnoreCase(tableName)) {
745
                            liste.add(listeTmp);
746
                        }
747
                    }
748
                }
749
            }
750
        }
751
        return liste;
752
    }
753
 
754
    /**
18 ilm 755
     * Permet d'obtenir la position dans le vecteur correspondant au nom d'une table
756
     *
757
     * @param tableName nom de la table
758
     * @return -1 si la table n'est pas dans le vecteur
759
     */
760
    private int getIndexFromTableName(String tableName) {
80 ilm 761
        final int size = this.vectListePanel.size();
762
        for (int i = 0; i < size; i++) {
763
            final IListPanel listeTmp = this.vectListePanel.get(i);
764
            final String name = listeTmp.getListe().getModel().getTable().getName();
765
            if (name.equalsIgnoreCase(tableName)) {
18 ilm 766
                return i;
767
            }
768
        }
769
        return -1;
770
    }
771
 
19 ilm 772
    public IListe getListe(int index) {
773
        return this.vectListePanel.get(index).getListe();
774
    }
775
 
94 ilm 776
    public IListPanel getListePanel(int index) {
777
        return this.vectListePanel.get(index);
778
    }
779
 
18 ilm 780
    public void fireListesChanged() {
67 ilm 781
        final int size = this.vectListePanel.size();
782
        for (int i = 0; i < size; i++) {
783
            final IListPanel listeTmp = this.vectListePanel.get(i);
784
            final ITableModel model = listeTmp.getListe().getModel();
785
            model.fireTableDataChanged();
786
            model.fireTableStructureChanged();
18 ilm 787
        }
788
 
789
    }
67 ilm 790
 
18 ilm 791
}