OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.translation.field;
2
 
3
import java.awt.GridBagConstraints;
4
import java.awt.GridBagLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.util.ArrayList;
8
import java.util.Collections;
9
import java.util.HashMap;
10
import java.util.HashSet;
11
import java.util.List;
12
import java.util.Locale;
13
import java.util.Map;
14
import java.util.Set;
15
 
16
import javax.swing.JComponent;
17
import javax.swing.JLabel;
18
import javax.swing.JPanel;
19
import javax.swing.JSeparator;
20
import javax.swing.JTextField;
21
import javax.swing.SwingConstants;
139 ilm 22
import javax.swing.event.DocumentEvent;
74 ilm 23
 
24
import org.openconcerto.modules.extensionbuilder.Extension;
25
import org.openconcerto.modules.extensionbuilder.translation.LocaleSelector;
26
import org.openconcerto.ui.DefaultGridBagConstraints;
27
import org.openconcerto.ui.JLabelBold;
28
 
29
public class TableTranslationEditorPanel extends JPanel {
30
    private final List<JComponent> hideableComponents = new ArrayList<JComponent>();
31
    final JTextField textSingular1 = new JTextField();
32
    final JTextField textSingular2 = new JTextField();
33
    final JTextField textPlural1 = new JTextField();
34
    final JTextField textPlural2 = new JTextField();
35
    private Map<String, JTextField> map1 = new HashMap<String, JTextField>();
36
    private Map<String, JTextField> map2 = new HashMap<String, JTextField>();
139 ilm 37
    private Map<JTextField, String> fieldsMap = new HashMap<JTextField, String>();
74 ilm 38
 
39
    TableTranslationEditorPanel(final Extension extension, final String tableName) {
40
        this.setLayout(new GridBagLayout());
41
        GridBagConstraints c = new DefaultGridBagConstraints();
42
        c.gridwidth = 2;
43
        c.fill = GridBagConstraints.NONE;
44
        this.add(new JLabelBold("Table " + tableName), c);
45
        c.gridy++;
46
        c.gridwidth = 1;
47
 
48
        // Languages
49
        c.weightx = 0;
50
        c.fill = GridBagConstraints.HORIZONTAL;
51
        this.add(new JLabel("Langue et pays", SwingConstants.RIGHT), c);
52
        final String[] isoLanguages = Locale.getISOLanguages();
53
        System.out.println(isoLanguages.length);
54
 
55
        final LocaleSelector comboLang1 = new LocaleSelector();
56
 
57
        c.weightx = 1;
58
        c.gridx++;
59
        c.fill = GridBagConstraints.NONE;
60
        this.add(comboLang1, c);
61
        c.weightx = 0;
62
        c.gridx++;
63
        c.gridheight = 3;
64
        c.fill = GridBagConstraints.BOTH;
65
        final JSeparator sep = new JSeparator(JSeparator.VERTICAL);
66
        this.add(sep, c);
181 ilm 67
        this.hideableComponents.add(sep);
74 ilm 68
        c.gridheight = 1;
69
        c.weightx = 1;
70
        c.gridx++;
71
        c.fill = GridBagConstraints.NONE;
72
        final LocaleSelector comboLang2 = new LocaleSelector();
181 ilm 73
        this.hideableComponents.add(comboLang2);
74 ilm 74
        comboLang2.setLocale(Locale.ENGLISH);
75
        this.add(comboLang2, c);
76
        // Singular
77
        c.gridx = 0;
78
        c.gridy++;
79
        c.weightx = 0;
80
        c.fill = GridBagConstraints.HORIZONTAL;
81
        this.add(new JLabel("Singulier", SwingConstants.RIGHT), c);
82
        c.gridx++;
83
 
181 ilm 84
        this.add(this.textSingular1, c);
74 ilm 85
        c.gridx += 2;
181 ilm 86
        this.hideableComponents.add(this.textSingular2);
87
        this.add(this.textSingular2, c);
74 ilm 88
 
89
        // Plural
90
        c.gridx = 0;
91
        c.gridy++;
92
        c.weightx = 0;
93
        this.add(new JLabel("Pluriel", SwingConstants.RIGHT), c);
94
        c.gridx++;
181 ilm 95
        this.add(this.textPlural1, c);
74 ilm 96
        c.gridx += 2;
181 ilm 97
        this.hideableComponents.add(this.textPlural2);
98
        this.add(this.textPlural2, c);
74 ilm 99
 
100
        // Fields
101
        c.gridx = 0;
102
        c.gridy++;
103
        c.gridwidth = 2;
104
        c.fill = GridBagConstraints.NONE;
105
        this.add(new JLabelBold("Champs de la table"), c);
106
        c.gridwidth = 1;
107
 
108
        List<String> fieldsName = getFieldsName(extension, tableName);
109
        c.gridy++;
110
        c.gridheight = fieldsName.size();
111
        c.gridx = 2;
112
        c.fill = GridBagConstraints.BOTH;
113
        final JSeparator sep2 = new JSeparator(JSeparator.VERTICAL);
181 ilm 114
        this.hideableComponents.add(sep2);
74 ilm 115
        this.add(sep2, c);
116
 
117
        c.fill = GridBagConstraints.HORIZONTAL;
118
        c.gridheight = 1;
119
        for (String fName : fieldsName) {
120
            c.gridx = 0;
121
            c.weightx = 0;
122
            this.add(new JLabel(fName, SwingConstants.RIGHT), c);
123
            c.gridx++;
124
            final JTextField t1 = new JTextField();
181 ilm 125
            this.map1.put(fName, t1);
126
            this.fieldsMap.put(t1, fName);
74 ilm 127
            this.add(t1, c);
128
            c.gridx += 2;
129
            final JTextField t2 = new JTextField();
181 ilm 130
            this.hideableComponents.add(t2);
131
            this.fieldsMap.put(t2, fName);
132
            this.map2.put(fName, t2);
74 ilm 133
            this.add(t2, c);
134
            c.gridy++;
135
        }
136
        c.gridx = 0;
137
        c.gridy++;
138
        c.weighty = 1;
139
        c.weightx = 0;
140
        this.add(new JPanel(), c);
141
        // TODO: ajouter la checkbox pour masquer
142
        // TODO: repercuter les modifs dans les traductions
143
 
144
        updateUIFrom(extension, tableName, comboLang1.getLocale(), comboLang2.getLocale());
145
        comboLang1.addActionListener(new ActionListener() {
146
 
147
            @Override
148
            public void actionPerformed(ActionEvent e) {
149
                updateUIFrom(extension, tableName, comboLang1.getLocale(), null);
150
            }
151
        });
152
        comboLang2.addActionListener(new ActionListener() {
153
 
154
            @Override
155
            public void actionPerformed(ActionEvent e) {
156
                updateUIFrom(extension, tableName, null, comboLang2.getLocale());
157
            }
158
        });
139 ilm 159
 
160
        this.textSingular1.getDocument().addDocumentListener(new SimpleDocumentListener() {
161
 
162
            @Override
163
            public void changedUpdate(DocumentEvent e, String text) {
164
                extension.setTableSingularTranslation(tableName, comboLang1.getLocale(), text);
165
            }
166
 
167
        });
168
        this.textSingular2.getDocument().addDocumentListener(new SimpleDocumentListener() {
169
 
170
            @Override
171
            public void changedUpdate(DocumentEvent e, String text) {
172
                extension.setTableSingularTranslation(tableName, comboLang2.getLocale(), text);
173
            }
174
 
175
        });
176
        this.textPlural1.getDocument().addDocumentListener(new SimpleDocumentListener() {
177
 
178
            @Override
179
            public void changedUpdate(DocumentEvent e, String text) {
180
                extension.setTablePluralTranslation(tableName, comboLang1.getLocale(), text);
181
            }
182
 
183
        });
184
        this.textPlural2.getDocument().addDocumentListener(new SimpleDocumentListener() {
185
 
186
            @Override
187
            public void changedUpdate(DocumentEvent e, String text) {
188
                extension.setTablePluralTranslation(tableName, comboLang2.getLocale(), text);
189
            }
190
 
191
        });
181 ilm 192
        for (final JTextField textField : this.map1.values()) {
139 ilm 193
            textField.getDocument().addDocumentListener(new SimpleDocumentListener() {
194
 
195
                @Override
196
                public void changedUpdate(DocumentEvent e, String text) {
181 ilm 197
                    extension.setFieldTranslation(tableName, TableTranslationEditorPanel.this.fieldsMap.get(textField), comboLang1.getLocale(), text);
139 ilm 198
                }
199
 
200
            });
201
        }
181 ilm 202
        for (final JTextField textField : this.map2.values()) {
139 ilm 203
            textField.getDocument().addDocumentListener(new SimpleDocumentListener() {
204
 
205
                @Override
206
                public void changedUpdate(DocumentEvent e, String text) {
181 ilm 207
                    extension.setFieldTranslation(tableName, TableTranslationEditorPanel.this.fieldsMap.get(textField), comboLang2.getLocale(), text);
139 ilm 208
                }
209
 
210
            });
211
        }
212
 
74 ilm 213
    }
214
 
215
    private void updateUIFrom(Extension extension, String tableName, Object l1, Object l2) {
216
        if (l1 != null) {
217
            final String lang = l1.toString();
181 ilm 218
            updateUI(extension, tableName, lang, this.textSingular1, this.textPlural1, this.map1);
74 ilm 219
        }
220
        if (l2 != null) {
221
            final String lang = l2.toString();
181 ilm 222
            updateUI(extension, tableName, lang, this.textSingular2, this.textPlural2, this.map2);
74 ilm 223
        }
224
 
225
    }
226
 
227
    private void updateUI(Extension extension, String tableName, final String lang, JTextField textSingular, JTextField textPlural, Map<String, JTextField> map) {
228
        final TableTranslation tTrans = extension.getTableTranslation(lang, tableName);
229
        if (tTrans != null && tTrans.getSingular() != null) {
230
            textSingular.setText(tTrans.getSingular());
231
        } else {
232
            textSingular.setText("");
233
        }
234
        if (tTrans != null && tTrans.getPlural() != null) {
235
            textPlural.setText(tTrans.getPlural());
236
        } else {
237
            textPlural.setText("");
238
        }
239
        for (String fName : map.keySet()) {
240
            String t = extension.getFieldTranslation(lang, tableName, fName);
241
            if (t != null) {
242
                map.get(fName).setText(t);
243
            } else {
244
                map.get(fName).setText("");
245
            }
246
        }
247
    }
248
 
249
    private List<String> getFieldsName(Extension extension, String tableName) {
250
        final Set<String> l = new HashSet<String>();
251
        l.addAll(extension.getAllKnownFieldName(tableName));
252
 
253
        // + champs traduit dans l'extension
254
        List<String> lT = extension.getTranslatedFieldOfTable(tableName);
255
        l.addAll(lT);
256
 
257
        l.remove("ARCHIVE");
258
        l.remove("ORDRE");
259
        l.remove("ID_USER_COMMON_CREATE");
260
        l.remove("ID_USER_COMMON_MODIFY");
261
        // Sort
262
        final ArrayList<String> result = new ArrayList<String>();
263
        result.addAll(l);
264
        Collections.sort(result);
265
        return result;
266
    }
267
}