OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

package org.openconcerto.modules.common.batchprocessing;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

import org.openconcerto.sql.element.SQLElementDirectory;
import org.openconcerto.sql.model.SQLField;
import org.openconcerto.sql.model.SQLRow;
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.request.SQLFieldTranslator;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.JLabelBold;
import org.openconcerto.ui.ReloadPanel;
import org.openconcerto.utils.ExceptionHandler;

public class BatchEditorPanel extends JPanel {

    public BatchEditorPanel(final SQLElementDirectory dir, final List<SQLRowValues> rows, FieldFilter filter) {
        SQLFieldTranslator translator = dir.getTranslator();
        Set<SQLField> fields = rows.get(0).getTable().getFields();
        List<BatchField> f = new ArrayList<BatchField>();
        for (SQLField sqlField : fields) {
            if (ForbiddenFieldName.isAllowed(sqlField.getName()) && translator.getLabelFor(sqlField) != null) {
                if (filter == null || !filter.isFiltered(sqlField)) {
                    f.add(new BatchField(dir, sqlField, null));
                }
            }
        }
        SQLTable tableTarif = rows.get(0).getTable().getTable("TARIF");
        SQLTable tableArticleTarif = rows.get(0).getTable().getTable("ARTICLE_TARIF");
        SQLSelect sel = new SQLSelect();
        sel.addSelectStar(tableTarif);
        List<SQLRow> rowTarif = SQLRowListRSH.execute(sel);
        for (SQLRow sqlRow : rowTarif) {
            f.add(new BatchField(dir, tableArticleTarif.getField("PV_HT"), sqlRow));
            if (tableArticleTarif.contains("POURCENT_REMISE")) {
                f.add(new BatchField(dir, tableArticleTarif.getField("POURCENT_REMISE"), sqlRow));
            }
        }

        Collections.sort(f, new Comparator<BatchField>() {

            @Override
            public int compare(BatchField o1, BatchField o2) {
                return o1.getComboName().compareToIgnoreCase(o2.getComboName());
            }
        });
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new DefaultGridBagConstraints();
        this.add(new JLabel("Champ"), c);

        final JComboBox<BatchField> combo = new JComboBox<BatchField>(f.toArray(new BatchField[1]));
        combo.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                String label = ((BatchField) value).getComboName();
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
            }
        });

        combo.setSelectedIndex(0);

        c.gridx++;
        c.weightx = 1;
        c.fill = GridBagConstraints.NONE;
        this.add(combo, c);
        c.gridx = 0;
        c.gridy++;
        c.gridwidth = 2;
        c.fill = GridBagConstraints.HORIZONTAL;
        this.add(new JLabelBold("Action à appliquer"), c);
        c.weightx = 1;
        c.weighty = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridy++;
        c.anchor = GridBagConstraints.NORTHWEST;
        final BatchDetailPanel comp = new BatchDetailPanel();
        comp.setField((BatchField) combo.getSelectedItem());
        this.add(comp, c);

        JPanel actions = new JPanel();
        actions.setLayout(new FlowLayout(FlowLayout.RIGHT));
        final JButton buttonProcess = new JButton("Lancer le traitement");

        final JButton buttonCancel = new JButton("Annuler");
        final ReloadPanel reload = new ReloadPanel();
        actions.add(reload);
        actions.add(buttonProcess);

        actions.add(buttonCancel);

        c.gridy++;
        c.weighty = 0;
        this.add(actions, c);

        combo.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                comp.setField((BatchField) combo.getSelectedItem());

            }
        });
        buttonProcess.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (!comp.getProcessor().checkParameters()) {
                    JOptionPane.showMessageDialog(BatchEditorPanel.this, "Paramètres non valides");
                    return;
                }

                buttonProcess.setEnabled(false);
                buttonCancel.setEnabled(false);
                comp.setEnabled(false);

                combo.setEnabled(false);
                reload.setMode(ReloadPanel.MODE_ROTATE);
                SwingWorker<Object, Object> w = new SwingWorker<Object, Object>() {

                    @Override
                    protected Object doInBackground() throws Exception {
                        try {
                            final BatchProcessor processor = comp.getProcessor();
                            processor.process(rows);
                        } catch (Exception e) {
                            ExceptionHandler.handle("Echec du traitement", e);
                        }
                        return null;
                    }

                    @Override
                    protected void done() {
                        reload.setMode(ReloadPanel.MODE_EMPTY);
                        JOptionPane.showMessageDialog(BatchEditorPanel.this, "Traitement terminé");
                        SwingUtilities.getWindowAncestor(BatchEditorPanel.this).dispose();
                    }

                };
                w.execute();
            }
        });
        buttonCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.getWindowAncestor(BatchEditorPanel.this).dispose();
            }
        });
    }
}