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.DefaultComboBoxModel;
|
---|
17 | import javax.swing.DefaultListCellRenderer;
|
---|
18 | import javax.swing.JComboBox;
|
---|
19 | import javax.swing.JLabel;
|
---|
20 | import javax.swing.JList;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.ListCellRenderer;
|
---|
23 | import javax.swing.SwingUtilities;
|
---|
24 | import javax.swing.event.ChangeEvent;
|
---|
25 | import javax.swing.event.ChangeListener;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
29 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
30 | import org.openstreetmap.josm.tools.GBC;
|
---|
31 | import org.openstreetmap.josm.tools.I18n;
|
---|
32 | import org.xml.sax.SAXException;
|
---|
33 |
|
---|
34 | public class LanguagePreference implements PreferenceSetting {
|
---|
35 | static private final Logger logger = Logger.getLogger(LanguagePreference.class.getName());
|
---|
36 |
|
---|
37 | public static class Factory implements PreferenceSettingFactory {
|
---|
38 | public PreferenceSetting createPreferenceSetting() {
|
---|
39 | return new LanguagePreference();
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | /** the combo box with the available locales */
|
---|
44 | private JComboBox langCombo;
|
---|
45 | /** the model for the combo box */
|
---|
46 | private LanguageComboBoxModel model;
|
---|
47 | /** true, if the available translations have been loaded; false otherwise */
|
---|
48 | private boolean translationsLoaded = false;
|
---|
49 |
|
---|
50 | public void addGui(final PreferenceDialog gui) {
|
---|
51 | model = new LanguageComboBoxModel();
|
---|
52 | langCombo = new JComboBox(model);
|
---|
53 | langCombo.setRenderer(new LanguageCellRenderer(langCombo.getRenderer()));
|
---|
54 |
|
---|
55 | LafPreference lafPreference = gui.getSetting(LafPreference.class);
|
---|
56 | final JPanel panel = lafPreference.panel;
|
---|
57 | panel.add(new JLabel(tr("Language")), GBC.std().insets(20, 0, 0, 0));
|
---|
58 | panel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
59 | panel.add(langCombo, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
60 | panel.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH));
|
---|
61 |
|
---|
62 | // this defers loading of available translations to the first time the tab
|
---|
63 | // with the available translations is selected by the user
|
---|
64 | //
|
---|
65 | gui.displaycontent.addChangeListener(
|
---|
66 | new ChangeListener() {
|
---|
67 | public void stateChanged(ChangeEvent e) {
|
---|
68 | int i = gui.displaycontent.getSelectedIndex();
|
---|
69 | String title = gui.displaycontent.getTitleAt(i);
|
---|
70 | if (title.equals(tr("Look and Feel"))) {
|
---|
71 | initiallyLoadAvailableTranslations();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | );
|
---|
76 | }
|
---|
77 |
|
---|
78 | public boolean ok() {
|
---|
79 | if (!translationsLoaded)
|
---|
80 | // keep the current language, don't update preferences
|
---|
81 | return true;
|
---|
82 | if(langCombo.getSelectedItem() == null)
|
---|
83 | return Main.pref.put("language", null);
|
---|
84 | else
|
---|
85 | return Main.pref.put("language",
|
---|
86 | ((Locale)langCombo.getSelectedItem()).toString());
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Load available translations if not loaded yet.
|
---|
91 | */
|
---|
92 | public void initiallyLoadAvailableTranslations() {
|
---|
93 | if (!translationsLoaded) {
|
---|
94 | reloadAvailableTranslations();
|
---|
95 | }
|
---|
96 | translationsLoaded = true;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Asynchronously loads available translations
|
---|
101 | *
|
---|
102 | */
|
---|
103 | protected void reloadAvailableTranslations() {
|
---|
104 | Main.worker.submit(new AvailableTranslationsLoader());
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Asynchronous task to lookup the available translations.
|
---|
109 | *
|
---|
110 | */
|
---|
111 | private class AvailableTranslationsLoader extends PleaseWaitRunnable {
|
---|
112 | public AvailableTranslationsLoader() {
|
---|
113 | super(tr("Looking up available translations..."));
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | protected void cancel() {
|
---|
118 | // can't cancel
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override
|
---|
122 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
123 | final List<Locale> locales = new ArrayList<Locale>(
|
---|
124 | Arrays.asList(I18n.getAvailableTranslations(getProgressMonitor()))
|
---|
125 | );
|
---|
126 | locales.add(0,Locale.ENGLISH);
|
---|
127 | Runnable r = new Runnable() {
|
---|
128 | public void run() {
|
---|
129 | model.setAvailableLocales(locales);
|
---|
130 | model.selectLanguage(Main.pref.get("language"));
|
---|
131 | }
|
---|
132 | };
|
---|
133 | try {
|
---|
134 | SwingUtilities.invokeAndWait(r);
|
---|
135 | } catch(InvocationTargetException e) {
|
---|
136 | throw new RuntimeException(e.getCause());
|
---|
137 | } catch(InterruptedException e) {
|
---|
138 | throw new RuntimeException(e);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override
|
---|
143 | protected void finish() {}
|
---|
144 | }
|
---|
145 |
|
---|
146 | private static class LanguageComboBoxModel extends DefaultComboBoxModel {
|
---|
147 | private final List<Locale> data = new ArrayList<Locale>();
|
---|
148 |
|
---|
149 | public LanguageComboBoxModel() {}
|
---|
150 |
|
---|
151 | public void setAvailableLocales(List<Locale> locales) {
|
---|
152 | data.clear();
|
---|
153 | if (locales != null) {
|
---|
154 | data.add(null); // the default locale
|
---|
155 | data.addAll(locales);
|
---|
156 | }
|
---|
157 | fireContentsChanged(this, 0, getSize());
|
---|
158 | }
|
---|
159 |
|
---|
160 | public void selectLanguage(String language) {
|
---|
161 | setSelectedItem(null);
|
---|
162 | if (language != null) {
|
---|
163 | for (Locale locale: data) {
|
---|
164 | if (locale == null) {
|
---|
165 | continue;
|
---|
166 | }
|
---|
167 | if (locale.toString().equals(language)) {
|
---|
168 | setSelectedItem(locale);
|
---|
169 | return;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | @Override
|
---|
176 | public Object getElementAt(int index) {
|
---|
177 | return data.get(index);
|
---|
178 | }
|
---|
179 |
|
---|
180 | @Override
|
---|
181 | public int getSize() {
|
---|
182 | return data.size();
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | static private class LanguageCellRenderer extends DefaultListCellRenderer {
|
---|
187 | private ListCellRenderer dispatch;
|
---|
188 | public LanguageCellRenderer(ListCellRenderer dispatch) {
|
---|
189 | this.dispatch = dispatch;
|
---|
190 | }
|
---|
191 | @Override
|
---|
192 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
|
---|
193 | boolean cellHasFocus) {
|
---|
194 | Locale l = (Locale) value;
|
---|
195 | return dispatch.getListCellRendererComponent(list,
|
---|
196 | l == null ? tr("Default (Auto determined)") : l.getDisplayName(),
|
---|
197 | index, isSelected, cellHasFocus);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|