OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
102 ilm 1
package org.openconcerto.modules.common.batchprocessing;
2
 
3
import java.awt.Component;
4
import java.awt.FlowLayout;
5
import java.awt.GridBagConstraints;
6
import java.awt.GridBagLayout;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.awt.event.ItemEvent;
10
import java.awt.event.ItemListener;
11
import java.util.ArrayList;
12
import java.util.Collections;
13
import java.util.Comparator;
14
import java.util.List;
15
import java.util.Set;
16
 
17
import javax.swing.DefaultListCellRenderer;
18
import javax.swing.JButton;
19
import javax.swing.JComboBox;
20
import javax.swing.JLabel;
21
import javax.swing.JList;
22
import javax.swing.JOptionPane;
23
import javax.swing.JPanel;
24
import javax.swing.SwingUtilities;
147 ilm 25
import javax.swing.SwingWorker;
102 ilm 26
 
181 ilm 27
import org.openconcerto.sql.element.SQLElementDirectory;
102 ilm 28
import org.openconcerto.sql.model.SQLField;
181 ilm 29
import org.openconcerto.sql.model.SQLRow;
30
import org.openconcerto.sql.model.SQLRowListRSH;
102 ilm 31
import org.openconcerto.sql.model.SQLRowValues;
181 ilm 32
import org.openconcerto.sql.model.SQLSelect;
33
import org.openconcerto.sql.model.SQLTable;
102 ilm 34
import org.openconcerto.sql.request.SQLFieldTranslator;
35
import org.openconcerto.ui.DefaultGridBagConstraints;
36
import org.openconcerto.ui.JLabelBold;
37
import org.openconcerto.ui.ReloadPanel;
38
import org.openconcerto.utils.ExceptionHandler;
39
 
40
public class BatchEditorPanel extends JPanel {
41
 
181 ilm 42
    public BatchEditorPanel(final SQLElementDirectory dir, final List<SQLRowValues> rows, FieldFilter filter) {
43
        SQLFieldTranslator translator = dir.getTranslator();
102 ilm 44
        Set<SQLField> fields = rows.get(0).getTable().getFields();
181 ilm 45
        List<BatchField> f = new ArrayList<BatchField>();
102 ilm 46
        for (SQLField sqlField : fields) {
47
            if (ForbiddenFieldName.isAllowed(sqlField.getName()) && translator.getLabelFor(sqlField) != null) {
147 ilm 48
                if (filter == null || !filter.isFiltered(sqlField)) {
181 ilm 49
                    f.add(new BatchField(dir, sqlField, null));
102 ilm 50
                }
51
            }
52
        }
181 ilm 53
        SQLTable tableTarif = rows.get(0).getTable().getTable("TARIF");
54
        SQLTable tableArticleTarif = rows.get(0).getTable().getTable("ARTICLE_TARIF");
55
        SQLSelect sel = new SQLSelect();
56
        sel.addSelectStar(tableTarif);
57
        List<SQLRow> rowTarif = SQLRowListRSH.execute(sel);
58
        for (SQLRow sqlRow : rowTarif) {
59
            f.add(new BatchField(dir, tableArticleTarif.getField("PV_HT"), sqlRow));
60
            if (tableArticleTarif.contains("POURCENT_REMISE")) {
61
                f.add(new BatchField(dir, tableArticleTarif.getField("POURCENT_REMISE"), sqlRow));
62
            }
63
        }
102 ilm 64
 
181 ilm 65
        Collections.sort(f, new Comparator<BatchField>() {
102 ilm 66
 
67
            @Override
181 ilm 68
            public int compare(BatchField o1, BatchField o2) {
69
                return o1.getComboName().compareToIgnoreCase(o2.getComboName());
102 ilm 70
            }
71
        });
72
        this.setLayout(new GridBagLayout());
73
        GridBagConstraints c = new DefaultGridBagConstraints();
74
        this.add(new JLabel("Champ"), c);
75
 
181 ilm 76
        final JComboBox<BatchField> combo = new JComboBox<BatchField>(f.toArray(new BatchField[1]));
102 ilm 77
        combo.setRenderer(new DefaultListCellRenderer() {
78
            @Override
79
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
181 ilm 80
                String label = ((BatchField) value).getComboName();
147 ilm 81
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
102 ilm 82
            }
83
        });
84
 
85
        combo.setSelectedIndex(0);
86
 
87
        c.gridx++;
88
        c.weightx = 1;
89
        c.fill = GridBagConstraints.NONE;
90
        this.add(combo, c);
91
        c.gridx = 0;
92
        c.gridy++;
93
        c.gridwidth = 2;
94
        c.fill = GridBagConstraints.HORIZONTAL;
95
        this.add(new JLabelBold("Action à appliquer"), c);
96
        c.weightx = 1;
97
        c.weighty = 1;
98
        c.fill = GridBagConstraints.HORIZONTAL;
99
        c.gridy++;
100
        c.anchor = GridBagConstraints.NORTHWEST;
101
        final BatchDetailPanel comp = new BatchDetailPanel();
181 ilm 102
        comp.setField((BatchField) combo.getSelectedItem());
102 ilm 103
        this.add(comp, c);
104
 
105
        JPanel actions = new JPanel();
106
        actions.setLayout(new FlowLayout(FlowLayout.RIGHT));
107
        final JButton buttonProcess = new JButton("Lancer le traitement");
108
 
109
        final JButton buttonCancel = new JButton("Annuler");
110
        final ReloadPanel reload = new ReloadPanel();
111
        actions.add(reload);
112
        actions.add(buttonProcess);
113
 
114
        actions.add(buttonCancel);
115
 
116
        c.gridy++;
117
        c.weighty = 0;
118
        this.add(actions, c);
119
 
120
        combo.addItemListener(new ItemListener() {
121
 
122
            @Override
123
            public void itemStateChanged(ItemEvent e) {
181 ilm 124
                comp.setField((BatchField) combo.getSelectedItem());
102 ilm 125
 
126
            }
127
        });
128
        buttonProcess.addActionListener(new ActionListener() {
129
 
130
            @Override
131
            public void actionPerformed(ActionEvent e) {
132
                if (!comp.getProcessor().checkParameters()) {
133
                    JOptionPane.showMessageDialog(BatchEditorPanel.this, "Paramètres non valides");
134
                    return;
135
                }
136
 
137
                buttonProcess.setEnabled(false);
138
                buttonCancel.setEnabled(false);
139
                comp.setEnabled(false);
140
 
141
                combo.setEnabled(false);
142
                reload.setMode(ReloadPanel.MODE_ROTATE);
147 ilm 143
                SwingWorker<Object, Object> w = new SwingWorker<Object, Object>() {
102 ilm 144
 
145
                    @Override
146
                    protected Object doInBackground() throws Exception {
147
                        try {
148
                            final BatchProcessor processor = comp.getProcessor();
149
                            processor.process(rows);
150
                        } catch (Exception e) {
151
                            ExceptionHandler.handle("Echec du traitement", e);
152
                        }
153
                        return null;
154
                    }
155
 
156
                    @Override
157
                    protected void done() {
158
                        reload.setMode(ReloadPanel.MODE_EMPTY);
159
                        JOptionPane.showMessageDialog(BatchEditorPanel.this, "Traitement terminé");
160
                        SwingUtilities.getWindowAncestor(BatchEditorPanel.this).dispose();
161
                    }
162
 
163
                };
164
                w.execute();
165
            }
166
        });
167
        buttonCancel.addActionListener(new ActionListener() {
168
 
169
            @Override
170
            public void actionPerformed(ActionEvent e) {
171
                SwingUtilities.getWindowAncestor(BatchEditorPanel.this).dispose();
172
            }
173
        });
174
    }
175
}