Revision 41511 trunk/org.gvsig.desktop/org.gvsig.desktop.framework/org.gvsig.andami/src/main/java/org/gvsig/andami/impl/DefaultLocaleManager.java

View differences:

DefaultLocaleManager.java
28 28
import org.slf4j.Logger;
29 29
import org.slf4j.LoggerFactory;
30 30

  
31

  
32 31
public class DefaultLocaleManager implements LocaleManager {
33 32

  
34 33
    private static final Logger logger
35 34
            = LoggerFactory.getLogger(DefaultLocaleManager.class);
36 35

  
37 36
    private static final String LOCALE_CODE_VALENCIANO = "vl";
38
    
37

  
39 38
    private static final String I18N_EXTENSION = "org.gvsig.i18n.extension";
40 39
    private static final String INSTALLED_TRANSLATIONS_HOME_FOLDER = "i18n";
41 40
    private static final String VARIANT = "variant";
......
76 75
        Locale currentLocale = org.gvsig.i18n.Messages.getCurrentLocale();
77 76
        this.setCurrentLocale(currentLocale);
78 77
    }
79
    
78

  
80 79
    public Locale getCurrentLocale() {
81 80
        return org.gvsig.i18n.Messages.getCurrentLocale();
82 81
    }
......
85 84
        org.gvsig.i18n.Messages.setCurrentLocale(locale, this.getLocaleAlternatives(locale));
86 85

  
87 86
        String localeStr = locale.getLanguage();
88
        if ( localeStr.equalsIgnoreCase(LOCALE_CODE_VALENCIANO) ) {
87
        if (localeStr.equalsIgnoreCase(LOCALE_CODE_VALENCIANO)) {
89 88
            Locale.setDefault(new Locale("ca"));
90 89
        } else {
91 90
            Locale.setDefault(locale);
......
105 104
        // for compatibility, check for old user.region property
106 105
        region = getSystemProperty("user.region", null);
107 106

  
108
        if ( region != null ) {
107
        if (region != null) {
109 108
            // region can be of form country, country_variant, or _variant
110 109
            int i = region.indexOf('_');
111
            if ( i >= 0 ) {
110
            if (i >= 0) {
112 111
                country = region.substring(0, i);
113 112
                variant = region.substring(i + 1);
114 113
            } else {
......
123 122
    }
124 123

  
125 124
    public Set<Locale> getDefaultLocales() {
126
        if ( this.defaultLocales == null ) {
125
        if (this.defaultLocales == null) {
127 126
            PluginsManager pluginsManager = PluginsLocator.getManager();
128 127
            File localesFile = new File(pluginsManager.getApplicationI18nFolder(), "locale.config");
129 128

  
130 129
            PropertiesConfiguration config = null;
131
            if ( localesFile.canRead() ) {
130
            if (localesFile.canRead()) {
132 131
                try {
133 132
                    config = new PropertiesConfiguration(localesFile);
134 133
                } catch (Exception ex) {
......
136 135
                }
137 136
            }
138 137
            defaultLocales = new HashSet<Locale>();
139
            if ( config == null ) {
140
                for ( int i = 0; i < hardcoredLocales.length; i++ ) {
138
            if (config == null) {
139
                for (int i = 0; i < hardcoredLocales.length; i++) {
141 140
                    defaultLocales.add(hardcoredLocales[i]);
142 141
                }
143 142
            } else {
144 143
                List localeCodes = config.getList("locale");
145
                for ( Object localeCode : localeCodes ) {
144
                for (Object localeCode : localeCodes) {
146 145
                    Locale locale = LocaleUtils.toLocale((String) localeCode);
147 146
                    defaultLocales.add(locale);
148 147
                }
......
160 159
        } catch (Exception ex) {
161 160
            logger.warn("Can't open configuration '" + localesFile + "'.", ex);
162 161
        }
163
        if ( config == null ) {
162
        if (config == null) {
164 163
            return false;
165 164
        }
166 165
        List localeCodes = config.getList("locale");
167
        for ( Object localeCode : localeCodes ) {
166
        for (Object localeCode : localeCodes) {
168 167
            Locale locale = LocaleUtils.toLocale((String) localeCode);
169
            if ( !getInstalledLocales().contains(locale) ) {
168
            if (!getInstalledLocales().contains(locale)) {
170 169
                this.installedLocales.add(locale);
171 170
            }
172 171
        }
......
175 174
    }
176 175

  
177 176
    public Set<Locale> getInstalledLocales() {
178
        if ( installedLocales == null ) {
177
        if (installedLocales == null) {
179 178
            installedLocales = new TreeSet<Locale>(new Comparator<Locale>() {
180 179
                public int compare(Locale t0, Locale t1) {
181
                    if( t1 == null || t0 == null ) {
180
                    if (t1 == null || t0 == null) {
182 181
                        return 0;
183 182
                    }
184 183
                    return getLanguageDisplayName(t0).compareToIgnoreCase(getLanguageDisplayName(t1));
......
190 189
            // If the list of registered locales is not already persisted,
191 190
            // this should be the first time gvSIG is run with the I18nPlugin
192 191
            // so we will take the list of default locales
193
            if ( child == null ) {
192
            if (child == null) {
194 193
                installedLocales.addAll(getDefaultLocales());
195 194
                storeInstalledLocales();
196 195
            } else {
197 196
                XMLEntity localesEntity = getRegisteredLocalesPersistence();
198
                for ( int i = 0; i < localesEntity.getChildrenCount(); i++ ) {
197
                for (int i = 0; i < localesEntity.getChildrenCount(); i++) {
199 198
                    XMLEntity localeEntity = localesEntity.getChild(i);
200 199
                    String language = localeEntity.getStringProperty(LANGUAGE);
201 200
                    String country = localeEntity.getStringProperty(COUNTRY);
......
211 210
    public boolean installLocale(Locale locale) {
212 211
        // Add the locale to the list of installed ones, if it
213 212
        // is new, otherwise, update the texts.
214
        if ( !getInstalledLocales().contains(locale) ) {
213
        if (!getInstalledLocales().contains(locale)) {
215 214
            getInstalledLocales().add(locale);
216 215
            storeInstalledLocales();
217 216
        }
......
219 218
    }
220 219

  
221 220
    public boolean uninstallLocale(Locale locale) {
222
        if ( !getInstalledLocales().remove(locale) ) {
221
        if (!getInstalledLocales().remove(locale)) {
223 222
            return false;
224 223
        }
225 224
        storeInstalledLocales();
......
230 229

  
231 230
        String displayName;
232 231

  
233
        if ( "eu".equals(locale.getLanguage())
234
                && "vascuence".equals(locale.getDisplayLanguage()) ) {
232
        if ("eu".equals(locale.getLanguage())
233
                && "vascuence".equals(locale.getDisplayLanguage())) {
235 234
            // Correction for the Basque language display name,
236 235
            // show it in Basque, not in Spanish
237 236
            displayName = "Euskera";
238
        } else if ( LOCALE_CODE_VALENCIANO.equals(locale.getLanguage()) ) {
237
        } else if (LOCALE_CODE_VALENCIANO.equals(locale.getLanguage())) {
239 238
            // Patch for Valencian/Catalan
240 239
            displayName = "Valenci?";
241 240
        } else {
......
244 243

  
245 244
        return StringUtils.capitalize(displayName);
246 245
    }
247
    
246

  
248 247
    public Locale[] getLocaleAlternatives(Locale locale) {
249 248
        Locale alternatives[] = null;
250 249

  
251 250
        String localeStr = locale.getLanguage();
252
        if ( localeStr.equals("es") || 
253
             localeStr.equals("ca") ||
254
             localeStr.equals("gl") || 
255
             localeStr.equals("eu") ||
256
             localeStr.equals(LOCALE_CODE_VALENCIANO) ) {
251
        if (localeStr.equals("es")
252
                || localeStr.equals("ca")
253
                || localeStr.equals("gl")
254
                || localeStr.equals("eu")
255
                || localeStr.equals(LOCALE_CODE_VALENCIANO)) {
257 256
            alternatives = new Locale[2];
258 257
            alternatives[0] = new Locale("es");
259 258
            alternatives[1] = new Locale("en");
......
267 266
    }
268 267

  
269 268
    private void setCurrentLocaleUI(final Locale locale) {
270
        if ( !SwingUtilities.isEventDispatchThread() ) {
269
        if (!SwingUtilities.isEventDispatchThread()) {
271 270
            try {
272 271
                SwingUtilities.invokeAndWait(new Runnable() {
273 272
                    public void run() {
......
283 282
        } catch (Exception ex) {
284 283
            logger.warn("Problems setting locale to JComponent.", ex);
285 284
        }
286
        try {
287
            MDIFrame.getInstance().setLocale(locale);
288
        } catch (Exception ex) {
289
            logger.warn("Problems settings locale to MDIFrame.", ex);
285
        if (MDIFrame.isInitialized()) {
286
            try {
287
                MDIFrame.getInstance().setLocale(locale);
288
            } catch (Exception ex) {
289
                logger.warn("Problems settings locale to MDIFrame.", ex);
290
            }
290 291
        }
291 292
    }
292 293

  
......
295 296
                PluginsLocator.getManager().getApplicationHomeFolder(),
296 297
                INSTALLED_TRANSLATIONS_HOME_FOLDER
297 298
        );
298
        if ( !i18nFolder.exists() ) {
299
        if (!i18nFolder.exists()) {
299 300
            i18nFolder.mkdirs();
300 301
        }
301 302
        return i18nFolder;
......
307 308
    private XMLEntity getRegisteredLocalesPersistence() {
308 309
        XMLEntity entity = getI18nPersistence();
309 310
        XMLEntity child = null;
310
        for ( int i = entity.getChildrenCount() - 1; i >= 0; i-- ) {
311
        for (int i = entity.getChildrenCount() - 1; i >= 0; i--) {
311 312
            XMLEntity tmpchild = entity.getChild(i);
312
            if ( tmpchild.getName().equals(REGISTERED_LOCALES_PERSISTENCE) ) {
313
            if (tmpchild.getName().equals(REGISTERED_LOCALES_PERSISTENCE)) {
313 314
                child = tmpchild;
314 315
                break;
315 316
            }
......
324 325
        XMLEntity localesEntity = getRegisteredLocalesPersistence();
325 326

  
326 327
        // Remove the previous list of registered languages
327
        if ( localesEntity != null ) {
328
        if (localesEntity != null) {
328 329
            XMLEntity i18nPersistence = getI18nPersistence();
329
            for ( int i = i18nPersistence.getChildrenCount() - 1; i >= 0; i-- ) {
330
            for (int i = i18nPersistence.getChildrenCount() - 1; i >= 0; i--) {
330 331
                XMLEntity child = i18nPersistence.getChild(i);
331
                if ( child.getName().equals(REGISTERED_LOCALES_PERSISTENCE) ) {
332
                if (child.getName().equals(REGISTERED_LOCALES_PERSISTENCE)) {
332 333
                    i18nPersistence.removeChild(i);
333 334
                    break;
334 335
                }
......
341 342
        localesEntity.setName(REGISTERED_LOCALES_PERSISTENCE);
342 343

  
343 344
        Set<Locale> locales = getInstalledLocales();
344
        for ( Iterator iterator = locales.iterator(); iterator.hasNext(); ) {
345
        for (Iterator iterator = locales.iterator(); iterator.hasNext();) {
345 346
            Locale locale = (Locale) iterator.next();
346 347
            XMLEntity localeEntity = new XMLEntity();
347 348
            localeEntity.setName(locale.getDisplayName());

Also available in: Unified diff