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.io.IOException;
|
---|
8 | import java.lang.reflect.InvocationTargetException;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Locale;
|
---|
13 | import java.util.logging.Logger;
|
---|
14 |
|
---|
15 | import javax.swing.Box;
|
---|
16 | import javax.swing.DefaultListCellRenderer;
|
---|
17 | import javax.swing.JComboBox;
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JList;
|
---|
20 | import javax.swing.JPanel;
|
---|
21 | import javax.swing.ListCellRenderer;
|
---|
22 | import javax.swing.SwingUtilities;
|
---|
23 | import javax.swing.event.ChangeEvent;
|
---|
24 | import javax.swing.event.ChangeListener;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
28 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
29 | import org.openstreetmap.josm.tools.GBC;
|
---|
30 | import org.openstreetmap.josm.tools.I18n;
|
---|
31 | import org.xml.sax.SAXException;
|
---|
32 |
|
---|
33 | public class LanguagePreference implements PreferenceSetting {
|
---|
34 | static private final Logger logger = Logger.getLogger(LanguagePreference.class.getName());
|
---|
35 |
|
---|
36 | public static class Factory implements PreferenceSettingFactory {
|
---|
37 | public PreferenceSetting createPreferenceSetting() {
|
---|
38 | return new LanguagePreference();
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * ComboBox with all available Translations
|
---|
44 | */
|
---|
45 | private JComboBox langCombo;
|
---|
46 | private boolean translationsLoaded = false;
|
---|
47 |
|
---|
48 | public void addGui(final PreferenceDialog gui) {
|
---|
49 | //langCombo = new JComboBox(I18n.getAvailableTranslations());
|
---|
50 | langCombo = new JComboBox(new Locale[0]);
|
---|
51 |
|
---|
52 | final ListCellRenderer oldRenderer = langCombo.getRenderer();
|
---|
53 | langCombo.setRenderer(new DefaultListCellRenderer() {
|
---|
54 | @Override
|
---|
55 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
|
---|
56 | boolean cellHasFocus) {
|
---|
57 | Locale l = (Locale) value;
|
---|
58 | return oldRenderer.getListCellRendererComponent(list,
|
---|
59 | l == null ? tr("Default (Auto determined)") : l.getDisplayName(),
|
---|
60 | index, isSelected, cellHasFocus);
|
---|
61 | }
|
---|
62 | });
|
---|
63 |
|
---|
64 | LafPreference lafPreference = gui.getSetting(LafPreference.class);
|
---|
65 | final JPanel panel = lafPreference.panel;
|
---|
66 | panel.add(new JLabel(tr("Language")), GBC.std().insets(20, 0, 0, 0));
|
---|
67 | panel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
68 | panel.add(langCombo, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
69 | panel.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH));
|
---|
70 |
|
---|
71 | // this defers loading of available translations to the first time the tab
|
---|
72 | // with the available translations is selected by the user
|
---|
73 | //
|
---|
74 | gui.displaycontent.addChangeListener(
|
---|
75 | new ChangeListener() {
|
---|
76 | public void stateChanged(ChangeEvent e) {
|
---|
77 | int i = gui.displaycontent.getSelectedIndex();
|
---|
78 | String title = gui.displaycontent.getTitleAt(i);
|
---|
79 | if (title.equals(tr("Look and Feel"))) {
|
---|
80 | initiallyLoadAvailableTranslations();
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | );
|
---|
85 | }
|
---|
86 |
|
---|
87 | public boolean ok() {
|
---|
88 | if(langCombo.getSelectedItem() == null)
|
---|
89 | return Main.pref.put("language", null);
|
---|
90 | else
|
---|
91 | return Main.pref.put("language",
|
---|
92 | ((Locale)langCombo.getSelectedItem()).toString());
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void initiallyLoadAvailableTranslations() {
|
---|
96 | if (!translationsLoaded) {
|
---|
97 | reloadAvailableTranslations();
|
---|
98 | }
|
---|
99 | translationsLoaded = true;
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected void reloadAvailableTranslations() {
|
---|
103 | Main.worker.submit(new AvailableTranslationsLoader());
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Asynchronous task to lookup the available translations.
|
---|
108 | *
|
---|
109 | */
|
---|
110 | private class AvailableTranslationsLoader extends PleaseWaitRunnable {
|
---|
111 | public AvailableTranslationsLoader() {
|
---|
112 | super(tr("Looking up available translations..."));
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | protected void cancel() {
|
---|
117 | // can't cancel
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
122 | final List<Locale> locales = new ArrayList<Locale>(
|
---|
123 | Arrays.asList(I18n.getAvailableTranslations(getProgressMonitor()))
|
---|
124 | );
|
---|
125 | locales.add(0,Locale.ENGLISH);
|
---|
126 | Runnable r = new Runnable() {
|
---|
127 | public void run() {
|
---|
128 | langCombo.removeAll();
|
---|
129 | langCombo.addItem(null); // the default enry
|
---|
130 | for (Locale locale : locales) {
|
---|
131 | langCombo.addItem(locale);
|
---|
132 | }
|
---|
133 | String ln = Main.pref.get("language");
|
---|
134 | langCombo.setSelectedIndex(0);
|
---|
135 | if (ln != null) {
|
---|
136 | for (int i = 1; i < langCombo.getItemCount(); ++i) {
|
---|
137 | if (((Locale) langCombo.getItemAt(i)).toString().equals(ln)) {
|
---|
138 | langCombo.setSelectedIndex(i);
|
---|
139 | break;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | };
|
---|
145 | try {
|
---|
146 | SwingUtilities.invokeAndWait(r);
|
---|
147 | } catch(InvocationTargetException e) {
|
---|
148 | throw new RuntimeException(e.getCause());
|
---|
149 | } catch(InterruptedException e) {
|
---|
150 | throw new RuntimeException(e);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | protected void finish() {}
|
---|
156 | }
|
---|
157 | }
|
---|