Changeset 2553 in josm
- Timestamp:
- 2009-11-30T12:55:02+01:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/preferences/LanguagePreference.java
r2535 r2553 14 14 15 15 import javax.swing.Box; 16 import javax.swing.DefaultComboBoxModel; 16 17 import javax.swing.DefaultListCellRenderer; 17 18 import javax.swing.JComboBox; … … 40 41 } 41 42 42 /** 43 * ComboBox with all available Translations 44 */ 43 /** the combo box with the available locales */ 45 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 */ 46 48 private boolean translationsLoaded = false; 47 49 48 50 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 }); 51 model = new LanguageComboBoxModel(); 52 langCombo = new JComboBox(model); 53 langCombo.setRenderer(new LanguageCellRenderer(langCombo.getRenderer())); 63 54 64 55 LafPreference lafPreference = gui.getSetting(LafPreference.class); … … 86 77 87 78 public boolean ok() { 79 if (!translationsLoaded) 80 // keep the current language, don't update preferences 81 return true; 88 82 if(langCombo.getSelectedItem() == null) 89 83 return Main.pref.put("language", null); … … 93 87 } 94 88 89 /** 90 * Load available translations if not loaded yet. 91 */ 95 92 public void initiallyLoadAvailableTranslations() { 96 93 if (!translationsLoaded) { … … 100 97 } 101 98 99 /** 100 * Asynchronously loads available translations 101 * 102 */ 102 103 protected void reloadAvailableTranslations() { 103 104 Main.worker.submit(new AvailableTranslationsLoader()); … … 126 127 Runnable r = new Runnable() { 127 128 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 } 129 model.setAvailableLocales(locales); 130 model.selectLanguage(Main.pref.get("language")); 143 131 } 144 132 }; … … 155 143 protected void finish() {} 156 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 } 157 200 }
Note:
See TracChangeset
for help on using the changeset viewer.