OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | Details | 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);
39
        comboLang = new JComboBox(langs);
40
        this.add(comboLang);
41
        comboCountry = new JComboBox();
42
        this.add(comboCountry);
43
 
44
        try {
45
            this.setLocale(Locale.getDefault());
46
        } catch (Exception e) {
47
            System.err.println("LocaleSelector warning: unable to set current language");
48
        }
49
        comboLang.addActionListener(new ActionListener() {
50
 
51
            @Override
52
            public void actionPerformed(ActionEvent e) {
53
                if (interactive) {
54
                    lang = comboLang.getSelectedItem().toString();
55
                    updateCountryFromLang();
56
                    country = comboCountry.getSelectedItem().toString();
57
                    fireActionPerformed();
58
                }
59
            }
60
 
61
        });
62
        comboCountry.addActionListener(new ActionListener() {
63
 
64
            @Override
65
            public void actionPerformed(ActionEvent e) {
66
                if (interactive) {
67
                    country = comboCountry.getSelectedItem().toString();
68
                    fireActionPerformed();
69
                }
70
            }
71
 
72
        });
73
        final int minWidth = comboLang.getPreferredSize().width * 2;
74
        final int minHeight = comboLang.getPreferredSize().height;
75
        comboLang.setMinimumSize(new Dimension(minWidth, minHeight));
76
        comboLang.setPreferredSize(new Dimension(minWidth, minHeight));
77
        comboCountry.setMinimumSize(new Dimension(minWidth, minHeight));
78
        comboCountry.setPreferredSize(new Dimension(minWidth, minHeight));
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];
87
            if (lo.getLanguage().equals(lang)) {
88
                countries.add(lo.getCountry());
89
            }
90
        }
91
        Collections.sort(countries);
92
        if (countries.isEmpty()) {
93
            countries.add("");
94
        }
95
        comboCountry.setModel(new DefaultComboBoxModel(countries));
96
    }
97
 
98
    public Locale getLocale() {
99
        Locale[] l = Locale.getAvailableLocales();
100
        if (country != null) {
101
 
102
            for (int i = 0; i < l.length; i++) {
103
                Locale lo = (Locale) l[i];
104
                if (lo.getLanguage().equals(lang) && lo.getCountry().equals(country)) {
105
                    return lo;
106
                }
107
            }
108
        }
109
        for (int i = 0; i < l.length; i++) {
110
            Locale lo = (Locale) l[i];
111
            if (lo.getLanguage().equals(lang)) {
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);
129
        this.comboLang.setSelectedItem(lang);
130
        updateCountryFromLang();
131
        this.comboCountry.setSelectedItem(country);
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
}