[1065] | 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 |
|
---|
[1165] | 9 | import javax.swing.Box;
|
---|
[1065] | 10 | import javax.swing.DefaultListCellRenderer;
|
---|
| 11 | import javax.swing.JComboBox;
|
---|
| 12 | import javax.swing.JLabel;
|
---|
| 13 | import javax.swing.JList;
|
---|
[1165] | 14 | import javax.swing.JPanel;
|
---|
[1065] | 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 {
|
---|
[1742] | 22 |
|
---|
| 23 | public static class Factory implements PreferenceSettingFactory {
|
---|
| 24 | public PreferenceSetting createPreferenceSetting() {
|
---|
| 25 | return new LanguagePreference();
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[1169] | 29 | /**
|
---|
| 30 | * ComboBox with all available Translations
|
---|
| 31 | */
|
---|
| 32 | private JComboBox langCombo;
|
---|
| 33 | private final Locale AUTO_LANGUAGE = null;
|
---|
[1065] | 34 |
|
---|
[1169] | 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);
|
---|
[1065] | 41 |
|
---|
[1169] | 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 | }
|
---|
[1065] | 50 |
|
---|
[1169] | 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 | });
|
---|
[1065] | 62 |
|
---|
[1742] | 63 | LafPreference lafPreference = gui.getSetting(LafPreference.class);
|
---|
| 64 | JPanel panel = lafPreference.panel;
|
---|
[1169] | 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 |
|
---|
[1180] | 71 | public boolean ok() {
|
---|
[1169] | 72 | if(langCombo.getSelectedItem() == null)
|
---|
[1180] | 73 | return Main.pref.put("language", null);
|
---|
[1169] | 74 | else
|
---|
[1180] | 75 | return Main.pref.put("language",
|
---|
| 76 | ((Locale)langCombo.getSelectedItem()).toString());
|
---|
[1169] | 77 | }
|
---|
[1065] | 78 | }
|
---|