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 |
|
|
|
27 |
import org.openconcerto.sql.Configuration;
|
|
|
28 |
import org.openconcerto.sql.PropsConfiguration;
|
|
|
29 |
import org.openconcerto.sql.model.SQLField;
|
|
|
30 |
import org.openconcerto.sql.model.SQLRowValues;
|
|
|
31 |
import org.openconcerto.sql.request.SQLFieldTranslator;
|
|
|
32 |
import org.openconcerto.ui.DefaultGridBagConstraints;
|
|
|
33 |
import org.openconcerto.ui.JLabelBold;
|
|
|
34 |
import org.openconcerto.ui.ReloadPanel;
|
|
|
35 |
import org.openconcerto.utils.ExceptionHandler;
|
|
|
36 |
|
|
|
37 |
public class BatchEditorPanel extends JPanel {
|
|
|
38 |
|
|
|
39 |
public BatchEditorPanel(final List<SQLRowValues> rows, FieldFilter filter) {
|
|
|
40 |
Configuration conf = PropsConfiguration.getInstance();
|
|
|
41 |
final SQLFieldTranslator translator = conf.getTranslator();
|
|
|
42 |
Set<SQLField> fields = rows.get(0).getTable().getFields();
|
|
|
43 |
List<SQLField> f = new ArrayList<SQLField>();
|
|
|
44 |
for (SQLField sqlField : fields) {
|
|
|
45 |
if (ForbiddenFieldName.isAllowed(sqlField.getName()) && translator.getLabelFor(sqlField) != null) {
|
147 |
ilm |
46 |
if (filter == null || !filter.isFiltered(sqlField)) {
|
102 |
ilm |
47 |
f.add(sqlField);
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
Collections.sort(f, new Comparator<SQLField>() {
|
|
|
53 |
|
|
|
54 |
@Override
|
|
|
55 |
public int compare(SQLField o1, SQLField o2) {
|
|
|
56 |
return translator.getLabelFor(o1).compareToIgnoreCase(translator.getLabelFor(o2));
|
|
|
57 |
}
|
|
|
58 |
});
|
|
|
59 |
this.setLayout(new GridBagLayout());
|
|
|
60 |
GridBagConstraints c = new DefaultGridBagConstraints();
|
|
|
61 |
this.add(new JLabel("Champ"), c);
|
|
|
62 |
|
147 |
ilm |
63 |
final JComboBox<SQLField> combo = new JComboBox<SQLField>(f.toArray(new SQLField[1]));
|
102 |
ilm |
64 |
combo.setRenderer(new DefaultListCellRenderer() {
|
|
|
65 |
@Override
|
|
|
66 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
147 |
ilm |
67 |
String label = translator.getLabelFor(((SQLField) value));
|
|
|
68 |
return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
|
102 |
ilm |
69 |
}
|
|
|
70 |
});
|
|
|
71 |
|
|
|
72 |
combo.setSelectedIndex(0);
|
|
|
73 |
|
|
|
74 |
c.gridx++;
|
|
|
75 |
c.weightx = 1;
|
|
|
76 |
c.fill = GridBagConstraints.NONE;
|
|
|
77 |
this.add(combo, c);
|
|
|
78 |
c.gridx = 0;
|
|
|
79 |
c.gridy++;
|
|
|
80 |
c.gridwidth = 2;
|
|
|
81 |
c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
82 |
this.add(new JLabelBold("Action à appliquer"), c);
|
|
|
83 |
c.weightx = 1;
|
|
|
84 |
c.weighty = 1;
|
|
|
85 |
c.fill = GridBagConstraints.HORIZONTAL;
|
|
|
86 |
c.gridy++;
|
|
|
87 |
c.anchor = GridBagConstraints.NORTHWEST;
|
|
|
88 |
final BatchDetailPanel comp = new BatchDetailPanel();
|
|
|
89 |
comp.setField((SQLField) combo.getSelectedItem());
|
|
|
90 |
this.add(comp, c);
|
|
|
91 |
|
|
|
92 |
JPanel actions = new JPanel();
|
|
|
93 |
actions.setLayout(new FlowLayout(FlowLayout.RIGHT));
|
|
|
94 |
final JButton buttonProcess = new JButton("Lancer le traitement");
|
|
|
95 |
|
|
|
96 |
final JButton buttonCancel = new JButton("Annuler");
|
|
|
97 |
final ReloadPanel reload = new ReloadPanel();
|
|
|
98 |
actions.add(reload);
|
|
|
99 |
actions.add(buttonProcess);
|
|
|
100 |
|
|
|
101 |
actions.add(buttonCancel);
|
|
|
102 |
|
|
|
103 |
c.gridy++;
|
|
|
104 |
c.weighty = 0;
|
|
|
105 |
this.add(actions, c);
|
|
|
106 |
|
|
|
107 |
combo.addItemListener(new ItemListener() {
|
|
|
108 |
|
|
|
109 |
@Override
|
|
|
110 |
public void itemStateChanged(ItemEvent e) {
|
|
|
111 |
comp.setField((SQLField) combo.getSelectedItem());
|
|
|
112 |
|
|
|
113 |
}
|
|
|
114 |
});
|
|
|
115 |
buttonProcess.addActionListener(new ActionListener() {
|
|
|
116 |
|
|
|
117 |
@Override
|
|
|
118 |
public void actionPerformed(ActionEvent e) {
|
|
|
119 |
if (!comp.getProcessor().checkParameters()) {
|
|
|
120 |
JOptionPane.showMessageDialog(BatchEditorPanel.this, "Paramètres non valides");
|
|
|
121 |
return;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
buttonProcess.setEnabled(false);
|
|
|
125 |
buttonCancel.setEnabled(false);
|
|
|
126 |
comp.setEnabled(false);
|
|
|
127 |
|
|
|
128 |
combo.setEnabled(false);
|
|
|
129 |
reload.setMode(ReloadPanel.MODE_ROTATE);
|
147 |
ilm |
130 |
SwingWorker<Object, Object> w = new SwingWorker<Object, Object>() {
|
102 |
ilm |
131 |
|
|
|
132 |
@Override
|
|
|
133 |
protected Object doInBackground() throws Exception {
|
|
|
134 |
try {
|
|
|
135 |
final BatchProcessor processor = comp.getProcessor();
|
|
|
136 |
processor.process(rows);
|
|
|
137 |
} catch (Exception e) {
|
|
|
138 |
ExceptionHandler.handle("Echec du traitement", e);
|
|
|
139 |
}
|
|
|
140 |
return null;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
@Override
|
|
|
144 |
protected void done() {
|
|
|
145 |
reload.setMode(ReloadPanel.MODE_EMPTY);
|
|
|
146 |
JOptionPane.showMessageDialog(BatchEditorPanel.this, "Traitement terminé");
|
|
|
147 |
SwingUtilities.getWindowAncestor(BatchEditorPanel.this).dispose();
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
};
|
|
|
151 |
w.execute();
|
|
|
152 |
}
|
|
|
153 |
});
|
|
|
154 |
buttonCancel.addActionListener(new ActionListener() {
|
|
|
155 |
|
|
|
156 |
@Override
|
|
|
157 |
public void actionPerformed(ActionEvent e) {
|
|
|
158 |
SwingUtilities.getWindowAncestor(BatchEditorPanel.this).dispose();
|
|
|
159 |
}
|
|
|
160 |
});
|
|
|
161 |
}
|
|
|
162 |
}
|