source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/LanguagePreference.java@ 2535

Last change on this file since 2535 was 2535, checked in by Gubaer, 14 years ago

fixed #3693: Preferences dialog take very long to show up
loading of available translations now deferred. List is loaded asynchronously when it is actually needed

File size: 5.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.io.IOException;
8import java.lang.reflect.InvocationTargetException;
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.List;
12import java.util.Locale;
13import java.util.logging.Logger;
14
15import javax.swing.Box;
16import javax.swing.DefaultListCellRenderer;
17import javax.swing.JComboBox;
18import javax.swing.JLabel;
19import javax.swing.JList;
20import javax.swing.JPanel;
21import javax.swing.ListCellRenderer;
22import javax.swing.SwingUtilities;
23import javax.swing.event.ChangeEvent;
24import javax.swing.event.ChangeListener;
25
26import org.openstreetmap.josm.Main;
27import org.openstreetmap.josm.gui.PleaseWaitRunnable;
28import org.openstreetmap.josm.io.OsmTransferException;
29import org.openstreetmap.josm.tools.GBC;
30import org.openstreetmap.josm.tools.I18n;
31import org.xml.sax.SAXException;
32
33public 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}
Note: See TracBrowser for help on using the repository browser.