1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.util.Locale;
|
---|
8 |
|
---|
9 | import javax.swing.Box;
|
---|
10 | import javax.swing.DefaultListCellRenderer;
|
---|
11 | import javax.swing.JComboBox;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JList;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 | import javax.swing.ListCellRenderer;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.tools.GBC;
|
---|
19 | import org.openstreetmap.josm.tools.I18n;
|
---|
20 |
|
---|
21 | public class LanguagePreference implements PreferenceSetting {
|
---|
22 |
|
---|
23 | public static class Factory implements PreferenceSettingFactory {
|
---|
24 | public PreferenceSetting createPreferenceSetting() {
|
---|
25 | return new LanguagePreference();
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * ComboBox with all available Translations
|
---|
31 | */
|
---|
32 | private JComboBox langCombo;
|
---|
33 | private final Locale AUTO_LANGUAGE = null;
|
---|
34 |
|
---|
35 | public void addGui(PreferenceDialog gui) {
|
---|
36 | langCombo = new JComboBox(I18n.getAvailableTranslations());
|
---|
37 | langCombo.insertItemAt(AUTO_LANGUAGE, 0); // Default
|
---|
38 | langCombo.insertItemAt(Locale.ENGLISH, 1); // Built-in language
|
---|
39 | String ln = Main.pref.get("language");
|
---|
40 | langCombo.setSelectedIndex(0);
|
---|
41 |
|
---|
42 | if (ln != null) {
|
---|
43 | for (int i = 1; i < langCombo.getItemCount(); ++i) {
|
---|
44 | if (((Locale) langCombo.getItemAt(i)).toString().equals(ln)) {
|
---|
45 | langCombo.setSelectedIndex(i);
|
---|
46 | break;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | final ListCellRenderer oldRenderer = langCombo.getRenderer();
|
---|
52 | langCombo.setRenderer(new DefaultListCellRenderer() {
|
---|
53 | @Override
|
---|
54 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
|
---|
55 | boolean cellHasFocus) {
|
---|
56 | Locale l = (Locale) value;
|
---|
57 | return oldRenderer.getListCellRendererComponent(list,
|
---|
58 | l == AUTO_LANGUAGE ? tr("Default (Auto determined)") : l.getDisplayName(),
|
---|
59 | index, isSelected, cellHasFocus);
|
---|
60 | }
|
---|
61 | });
|
---|
62 |
|
---|
63 | LafPreference lafPreference = gui.getSetting(LafPreference.class);
|
---|
64 | JPanel panel = lafPreference.panel;
|
---|
65 | panel.add(new JLabel(tr("Language")), GBC.std().insets(20, 0, 0, 0));
|
---|
66 | panel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
67 | panel.add(langCombo, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
68 | panel.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH));
|
---|
69 | }
|
---|
70 |
|
---|
71 | public boolean ok() {
|
---|
72 | if(langCombo.getSelectedItem() == null)
|
---|
73 | return Main.pref.put("language", null);
|
---|
74 | else
|
---|
75 | return Main.pref.put("language",
|
---|
76 | ((Locale)langCombo.getSelectedItem()).toString());
|
---|
77 | }
|
---|
78 | }
|
---|