OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 74 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 74 Rev 181
Line 34... Line 34...
34
                langs.add(language);
34
                langs.add(language);
35
            }
35
            }
36
        }
36
        }
37
 
37
 
38
        Collections.sort(langs);
38
        Collections.sort(langs);
39
        comboLang = new JComboBox(langs);
39
        this.comboLang = new JComboBox(langs);
40
        this.add(comboLang);
40
        this.add(this.comboLang);
41
        comboCountry = new JComboBox();
41
        this.comboCountry = new JComboBox();
42
        this.add(comboCountry);
42
        this.add(this.comboCountry);
43
 
43
 
44
        try {
44
        try {
45
            this.setLocale(Locale.getDefault());
45
            this.setLocale(Locale.getDefault());
46
        } catch (Exception e) {
46
        } catch (Exception e) {
47
            System.err.println("LocaleSelector warning: unable to set current language");
47
            System.err.println("LocaleSelector warning: unable to set current language");
48
        }
48
        }
49
        comboLang.addActionListener(new ActionListener() {
49
        this.comboLang.addActionListener(new ActionListener() {
50
 
50
 
51
            @Override
51
            @Override
52
            public void actionPerformed(ActionEvent e) {
52
            public void actionPerformed(ActionEvent e) {
53
                if (interactive) {
53
                if (LocaleSelector.this.interactive) {
54
                    lang = comboLang.getSelectedItem().toString();
54
                    LocaleSelector.this.lang = LocaleSelector.this.comboLang.getSelectedItem().toString();
55
                    updateCountryFromLang();
55
                    updateCountryFromLang();
56
                    country = comboCountry.getSelectedItem().toString();
56
                    LocaleSelector.this.country = LocaleSelector.this.comboCountry.getSelectedItem().toString();
57
                    fireActionPerformed();
57
                    fireActionPerformed();
58
                }
58
                }
59
            }
59
            }
60
 
60
 
61
        });
61
        });
62
        comboCountry.addActionListener(new ActionListener() {
62
        this.comboCountry.addActionListener(new ActionListener() {
63
 
63
 
64
            @Override
64
            @Override
65
            public void actionPerformed(ActionEvent e) {
65
            public void actionPerformed(ActionEvent e) {
66
                if (interactive) {
66
                if (LocaleSelector.this.interactive) {
67
                    country = comboCountry.getSelectedItem().toString();
67
                    LocaleSelector.this.country = LocaleSelector.this.comboCountry.getSelectedItem().toString();
68
                    fireActionPerformed();
68
                    fireActionPerformed();
69
                }
69
                }
70
            }
70
            }
71
 
71
 
72
        });
72
        });
73
        final int minWidth = comboLang.getPreferredSize().width * 2;
73
        final int minWidth = this.comboLang.getPreferredSize().width * 2;
74
        final int minHeight = comboLang.getPreferredSize().height;
74
        final int minHeight = this.comboLang.getPreferredSize().height;
75
        comboLang.setMinimumSize(new Dimension(minWidth, minHeight));
75
        this.comboLang.setMinimumSize(new Dimension(minWidth, minHeight));
76
        comboLang.setPreferredSize(new Dimension(minWidth, minHeight));
76
        this.comboLang.setPreferredSize(new Dimension(minWidth, minHeight));
77
        comboCountry.setMinimumSize(new Dimension(minWidth, minHeight));
77
        this.comboCountry.setMinimumSize(new Dimension(minWidth, minHeight));
78
        comboCountry.setPreferredSize(new Dimension(minWidth, minHeight));
78
        this.comboCountry.setPreferredSize(new Dimension(minWidth, minHeight));
79
    }
79
    }
80
 
80
 
81
    private void updateCountryFromLang() {
81
    private void updateCountryFromLang() {
82
 
82
 
83
        Vector<String> countries = new Vector<String>();
83
        Vector<String> countries = new Vector<String>();
84
        Locale[] l = Locale.getAvailableLocales();
84
        Locale[] l = Locale.getAvailableLocales();
85
        for (int i = 0; i < l.length; i++) {
85
        for (int i = 0; i < l.length; i++) {
86
            Locale lo = (Locale) l[i];
86
            Locale lo = (Locale) l[i];
87
            if (lo.getLanguage().equals(lang)) {
87
            if (lo.getLanguage().equals(this.lang)) {
88
                countries.add(lo.getCountry());
88
                countries.add(lo.getCountry());
89
            }
89
            }
90
        }
90
        }
91
        Collections.sort(countries);
91
        Collections.sort(countries);
92
        if (countries.isEmpty()) {
92
        if (countries.isEmpty()) {
93
            countries.add("");
93
            countries.add("");
94
        }
94
        }
95
        comboCountry.setModel(new DefaultComboBoxModel(countries));
95
        this.comboCountry.setModel(new DefaultComboBoxModel(countries));
96
    }
96
    }
97
 
97
 
98
    public Locale getLocale() {
98
    public Locale getLocale() {
99
        Locale[] l = Locale.getAvailableLocales();
99
        Locale[] l = Locale.getAvailableLocales();
100
        if (country != null) {
100
        if (this.country != null) {
101
 
101
 
102
            for (int i = 0; i < l.length; i++) {
102
            for (int i = 0; i < l.length; i++) {
103
                Locale lo = (Locale) l[i];
103
                Locale lo = (Locale) l[i];
104
                if (lo.getLanguage().equals(lang) && lo.getCountry().equals(country)) {
104
                if (lo.getLanguage().equals(this.lang) && lo.getCountry().equals(this.country)) {
105
                    return lo;
105
                    return lo;
106
                }
106
                }
107
            }
107
            }
108
        }
108
        }
109
        for (int i = 0; i < l.length; i++) {
109
        for (int i = 0; i < l.length; i++) {
110
            Locale lo = (Locale) l[i];
110
            Locale lo = (Locale) l[i];
111
            if (lo.getLanguage().equals(lang)) {
111
            if (lo.getLanguage().equals(this.lang)) {
112
                return lo;
112
                return lo;
113
            }
113
            }
114
        }
114
        }
115
        return null;
115
        return null;
116
    }
116
    }
Line 124... Line 124...
124
        }
124
        }
125
        this.interactive = false;
125
        this.interactive = false;
126
        this.lang = l.getLanguage();
126
        this.lang = l.getLanguage();
127
        this.country = l.getCountry();
127
        this.country = l.getCountry();
128
        System.err.println("LocaleSelector.setLocale() " + this.lang + " " + this.country);
128
        System.err.println("LocaleSelector.setLocale() " + this.lang + " " + this.country);
129
        this.comboLang.setSelectedItem(lang);
129
        this.comboLang.setSelectedItem(this.lang);
130
        updateCountryFromLang();
130
        updateCountryFromLang();
131
        this.comboCountry.setSelectedItem(country);
131
        this.comboCountry.setSelectedItem(this.country);
132
        this.interactive = true;
132
        this.interactive = true;
133
    }
133
    }
134
 
134
 
135
    public void addActionListener(ActionListener actionListener) {
135
    public void addActionListener(ActionListener actionListener) {
136
        this.listeners.add(actionListener);
136
        this.listeners.add(actionListener);