OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.translation;
2
 
3
import java.awt.Dimension;
4
import java.awt.FlowLayout;
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.List;
10
import java.util.Locale;
11
import java.util.Vector;
12
 
13
import javax.swing.DefaultComboBoxModel;
14
import javax.swing.JComboBox;
15
import javax.swing.JPanel;
16
import javax.swing.SwingUtilities;
17
 
18
public class LocaleSelector extends JPanel {
19
 
20
    private String lang, country;
21
    private JComboBox comboLang;
22
    private JComboBox comboCountry;
23
    private List<ActionListener> listeners = new ArrayList<ActionListener>();
24
    private boolean interactive = true;
25
 
26
    public LocaleSelector() {
27
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
28
        final Vector<String> langs = new Vector<String>();
29
        final Locale[] l = Locale.getAvailableLocales();
30
 
31
        for (int i = 0; i < l.length; i++) {
32
            final String language = ((Locale) l[i]).getLanguage();
33
            if (!langs.contains(language)) {
34
                langs.add(language);
35
            }
36
        }
37
 
38
        Collections.sort(langs);
181 ilm 39
        this.comboLang = new JComboBox(langs);
40
        this.add(this.comboLang);
41
        this.comboCountry = new JComboBox();
42
        this.add(this.comboCountry);
74 ilm 43
 
44
        try {
45
            this.setLocale(Locale.getDefault());
46
        } catch (Exception e) {
47
            System.err.println("LocaleSelector warning: unable to set current language");
48
        }
181 ilm 49
        this.comboLang.addActionListener(new ActionListener() {
74 ilm 50
 
51
            @Override
52
            public void actionPerformed(ActionEvent e) {
181 ilm 53
                if (LocaleSelector.this.interactive) {
54
                    LocaleSelector.this.lang = LocaleSelector.this.comboLang.getSelectedItem().toString();
74 ilm 55
                    updateCountryFromLang();
181 ilm 56
                    LocaleSelector.this.country = LocaleSelector.this.comboCountry.getSelectedItem().toString();
74 ilm 57
                    fireActionPerformed();
58
                }
59
            }
60
 
61
        });
181 ilm 62
        this.comboCountry.addActionListener(new ActionListener() {
74 ilm 63
 
64
            @Override
65
            public void actionPerformed(ActionEvent e) {
181 ilm 66
                if (LocaleSelector.this.interactive) {
67
                    LocaleSelector.this.country = LocaleSelector.this.comboCountry.getSelectedItem().toString();
74 ilm 68
                    fireActionPerformed();
69
                }
70
            }
71
 
72
        });
181 ilm 73
        final int minWidth = this.comboLang.getPreferredSize().width * 2;
74
        final int minHeight = this.comboLang.getPreferredSize().height;
75
        this.comboLang.setMinimumSize(new Dimension(minWidth, minHeight));
76
        this.comboLang.setPreferredSize(new Dimension(minWidth, minHeight));
77
        this.comboCountry.setMinimumSize(new Dimension(minWidth, minHeight));
78
        this.comboCountry.setPreferredSize(new Dimension(minWidth, minHeight));
74 ilm 79
    }
80
 
81
    private void updateCountryFromLang() {
82
 
83
        Vector<String> countries = new Vector<String>();
84
        Locale[] l = Locale.getAvailableLocales();
85
        for (int i = 0; i < l.length; i++) {
86
            Locale lo = (Locale) l[i];
181 ilm 87
            if (lo.getLanguage().equals(this.lang)) {
74 ilm 88
                countries.add(lo.getCountry());
89
            }
90
        }
91
        Collections.sort(countries);
92
        if (countries.isEmpty()) {
93
            countries.add("");
94
        }
181 ilm 95
        this.comboCountry.setModel(new DefaultComboBoxModel(countries));
74 ilm 96
    }
97
 
98
    public Locale getLocale() {
99
        Locale[] l = Locale.getAvailableLocales();
181 ilm 100
        if (this.country != null) {
74 ilm 101
 
102
            for (int i = 0; i < l.length; i++) {
103
                Locale lo = (Locale) l[i];
181 ilm 104
                if (lo.getLanguage().equals(this.lang) && lo.getCountry().equals(this.country)) {
74 ilm 105
                    return lo;
106
                }
107
            }
108
        }
109
        for (int i = 0; i < l.length; i++) {
110
            Locale lo = (Locale) l[i];
181 ilm 111
            if (lo.getLanguage().equals(this.lang)) {
74 ilm 112
                return lo;
113
            }
114
        }
115
        return null;
116
    }
117
 
118
    public void setLocale(Locale l) {
119
        if (!SwingUtilities.isEventDispatchThread()) {
120
            throw new IllegalStateException("Must be called in EDT");
121
        }
122
        if (l.getLanguage().equals(this.lang) && l.getCountry().equals(this.country)) {
123
            return;
124
        }
125
        this.interactive = false;
126
        this.lang = l.getLanguage();
127
        this.country = l.getCountry();
128
        System.err.println("LocaleSelector.setLocale() " + this.lang + " " + this.country);
181 ilm 129
        this.comboLang.setSelectedItem(this.lang);
74 ilm 130
        updateCountryFromLang();
181 ilm 131
        this.comboCountry.setSelectedItem(this.country);
74 ilm 132
        this.interactive = true;
133
    }
134
 
135
    public void addActionListener(ActionListener actionListener) {
136
        this.listeners.add(actionListener);
137
    }
138
 
139
    public void fireActionPerformed() {
140
        System.err.println("LocaleSelector.fireActionPerformed():" + this.lang + " " + this.country);
141
        System.err.println("LocaleSelector.fireActionPerformed(); locale: " + this.getLocale());
142
        for (ActionListener listener : this.listeners) {
143
            listener.actionPerformed(new ActionEvent(this, this.hashCode(), ""));
144
        }
145
    }
146
 
147
}