OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.translation.menu;
2
 
3
import java.awt.GridBagConstraints;
4
import java.awt.GridBagLayout;
181 ilm 5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
74 ilm 7
import java.util.Locale;
8
 
9
import javax.swing.JLabel;
10
import javax.swing.JPanel;
11
import javax.swing.JSeparator;
12
import javax.swing.JTextField;
13
import javax.swing.SwingConstants;
86 ilm 14
import javax.swing.event.DocumentEvent;
15
import javax.swing.event.DocumentListener;
74 ilm 16
 
17
import org.openconcerto.modules.extensionbuilder.Extension;
18
import org.openconcerto.modules.extensionbuilder.translation.LocaleSelector;
19
import org.openconcerto.ui.DefaultGridBagConstraints;
20
import org.openconcerto.ui.group.Item;
21
import org.openconcerto.ui.group.LayoutHints;
22
 
23
public class MenuTranslationItemEditor extends JPanel {
24
 
25
    final Extension extension;
26
    private JTextField textId;
27
    private JTextField textTranslation1;
28
    private JTextField textTranslation2;
181 ilm 29
    final LocaleSelector comboLang1;
30
    final LocaleSelector comboLang2;
74 ilm 31
 
32
    public MenuTranslationItemEditor(final Item item, final Extension extension) {
33
        this.extension = extension;
34
        this.setLayout(new GridBagLayout());
35
        GridBagConstraints c = new DefaultGridBagConstraints();
36
 
37
        c.weightx = 0;
38
        c.fill = GridBagConstraints.HORIZONTAL;
39
        this.add(new JLabel("Identifiant", SwingConstants.RIGHT), c);
40
        c.gridx++;
41
        c.fill = GridBagConstraints.HORIZONTAL;
42
        c.weightx = 1;
43
        c.gridwidth = 3;
181 ilm 44
        this.textId = new JTextField();
45
        this.add(this.textId, c);
74 ilm 46
 
47
        // Language selector
48
        c.gridx = 0;
49
        c.gridy++;
50
        c.weightx = 0;
51
        c.gridwidth = 1;
52
        c.fill = GridBagConstraints.HORIZONTAL;
53
        this.add(new JLabel("Langue et pays", SwingConstants.RIGHT), c);
54
        final String[] isoLanguages = Locale.getISOLanguages();
55
        System.out.println(isoLanguages.length);
56
 
181 ilm 57
        comboLang1 = new LocaleSelector();
74 ilm 58
 
59
        c.weightx = 1;
60
        c.gridx++;
61
        c.fill = GridBagConstraints.NONE;
62
        this.add(comboLang1, c);
63
        c.weightx = 0;
64
        c.gridx++;
65
        c.gridheight = 2;
66
        c.fill = GridBagConstraints.BOTH;
67
        final JSeparator sep = new JSeparator(JSeparator.VERTICAL);
68
        this.add(sep, c);
69
 
70
        c.gridheight = 1;
71
        c.weightx = 1;
72
        c.gridx++;
73
        c.fill = GridBagConstraints.NONE;
181 ilm 74
        comboLang2 = new LocaleSelector();
74 ilm 75
 
76
        comboLang2.setLocale(Locale.ENGLISH);
77
        this.add(comboLang2, c);
78
        // Traduction
79
        c.gridx = 0;
80
        c.gridy++;
81
 
82
        c.gridwidth = 1;
83
 
84
        c.weightx = 0;
85
        c.fill = GridBagConstraints.HORIZONTAL;
86
        this.add(new JLabel("Traduction", SwingConstants.RIGHT), c);
87
        c.gridx++;
88
 
89
        c.weightx = 1;
90
 
181 ilm 91
        this.textTranslation1 = new JTextField(20);
74 ilm 92
 
181 ilm 93
        this.add(this.textTranslation1, c);
74 ilm 94
 
95
        c.gridx += 2;
96
        c.fill = GridBagConstraints.HORIZONTAL;
97
 
98
        c.weightx = 1;
181 ilm 99
        this.textTranslation2 = new JTextField(20);
100
        this.add(this.textTranslation2, c);
74 ilm 101
 
102
        c.gridy++;
103
        c.weighty = 1;
104
        this.add(new JPanel(), c);
105
 
106
        initUIFrom(item);
107
 
181 ilm 108
        this.textTranslation1.getDocument().addDocumentListener(new DocumentListener() {
86 ilm 109
 
110
            @Override
111
            public void removeUpdate(DocumentEvent e) {
112
                changedUpdate(e);
113
 
114
            }
115
 
116
            @Override
117
            public void insertUpdate(DocumentEvent e) {
118
                changedUpdate(e);
119
            }
120
 
121
            @Override
122
            public void changedUpdate(DocumentEvent e) {
181 ilm 123
                extension.setMenuTranslation(item.getId(), MenuTranslationItemEditor.this.textTranslation1.getText(), comboLang1.getLocale());
86 ilm 124
                extension.setChanged();
125
            }
126
        });
181 ilm 127
        this.textTranslation2.getDocument().addDocumentListener(new DocumentListener() {
86 ilm 128
 
129
            @Override
130
            public void removeUpdate(DocumentEvent e) {
131
                changedUpdate(e);
132
 
133
            }
134
 
135
            @Override
136
            public void insertUpdate(DocumentEvent e) {
137
                changedUpdate(e);
138
            }
139
 
140
            @Override
141
            public void changedUpdate(DocumentEvent e) {
181 ilm 142
                extension.setMenuTranslation(item.getId(), MenuTranslationItemEditor.this.textTranslation2.getText(), comboLang2.getLocale());
86 ilm 143
                extension.setChanged();
144
            }
145
        });
181 ilm 146
        comboLang1.addActionListener(new ActionListener() {
86 ilm 147
 
181 ilm 148
            @Override
149
            public void actionPerformed(ActionEvent e) {
150
                MenuTranslation t = extension.getMenuTranslation(textId.getText(), comboLang1.getLocale().toString());
151
                if (t != null) {
152
                    textTranslation1.setText(t.getLabel());
153
                } else {
154
                    textTranslation1.setText("");
155
                }
156
            }
157
        });
158
        comboLang2.addActionListener(new ActionListener() {
159
 
160
            @Override
161
            public void actionPerformed(ActionEvent e) {
162
                MenuTranslation t = extension.getMenuTranslation(textId.getText(), comboLang2.getLocale().toString());
163
                if (t != null) {
164
                    textTranslation2.setText(t.getLabel());
165
                } else {
166
                    textTranslation2.setText("");
167
                }
168
            }
169
        });
170
 
74 ilm 171
    }
172
 
173
    private void initUIFrom(Item item) {
174
 
175
        final LayoutHints localHint = item.getLocalHint();
176
        System.out.println("ItemEditor.initUIFrom:" + item + " " + localHint);
181 ilm 177
        this.textId.setEnabled(false);
178
        if (this.textId != null) {
179
            this.textId.setText(item.getId());
74 ilm 180
        }
181 ilm 181
        MenuTranslation t = extension.getMenuTranslation(textId.getText(), comboLang1.getLocale().toString());
182
        if (t != null) {
183
            textTranslation1.setText(t.getLabel());
184
        } else {
185
            textTranslation1.setText("");
186
        }
187
        t = extension.getMenuTranslation(textId.getText(), comboLang2.getLocale().toString());
188
        if (t != null) {
189
            textTranslation2.setText(t.getLabel());
190
        } else {
191
            textTranslation2.setText("");
192
        }
74 ilm 193
    }
194
}