summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/nls/model/LocalePartInfos.java
blob: 68f9097f8950f4c552d7e10529561cc9fed38751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.core.nls.model;

import com.google.common.collect.Sets;

import org.eclipse.wb.internal.core.nls.ui.FlagImagesRepository;

import org.eclipse.swt.graphics.Image;

import java.util.Arrays;
import java.util.Locale;
import java.util.Set;

/**
 * Utilities for {@link LocaleInfo}.
 * 
 * @author scheglov_ke
 * @coverage core.nls
 */
public final class LocalePartInfos {
  private static LocalePartInfo m_languages[];
  private static LocalePartInfo m_countries[];

  ////////////////////////////////////////////////////////////////////////////
  //
  // Access
  //
  ////////////////////////////////////////////////////////////////////////////
  public static LocalePartInfo[] getLanguages() {
    initLanguagesAndCountries();
    return m_languages;
  }

  public static LocalePartInfo[] getCountries() {
    initLanguagesAndCountries();
    return m_countries;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Find items
  //
  ////////////////////////////////////////////////////////////////////////////
  public static int indexByName(LocalePartInfo[] parts, String name) {
    for (int i = 0; i < parts.length; i++) {
      LocalePartInfo part = parts[i];
      if (part.getName().equals(name)) {
        return i;
      }
    }
    return -1;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Initialization
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Prepare arrays of all available languages and countries with titles and flags.
   */
  private static void initLanguagesAndCountries() {
    if (m_languages == null) {
      // languages
      {
        Set<LocalePartInfo> languagesSet = Sets.newHashSet();
        // fill
        for (String language : Locale.getISOLanguages()) {
          Locale locale = new Locale(language);
          Image flagImage = FlagImagesRepository.getFlagImage(locale);
          languagesSet.add(new LocalePartInfo(locale.getLanguage(),
              locale.getDisplayLanguage(),
              flagImage));
        }
        // remember as array
        m_languages = languagesSet.toArray(new LocalePartInfo[languagesSet.size()]);
        Arrays.sort(m_languages);
      }
      // countries
      {
        Set<LocalePartInfo> countriesSet = Sets.newHashSet();
        countriesSet.add(new LocalePartInfo("", "(none)", FlagImagesRepository.getEmptyFlagImage()));
        // fill
        for (String country : Locale.getISOCountries()) {
          Locale locale = new Locale("", country);
          Image flagImage = FlagImagesRepository.getFlagImage(locale);
          countriesSet.add(new LocalePartInfo(locale.getCountry(),
              locale.getDisplayCountry(),
              flagImage));
        }
        // remember as array
        m_countries = countriesSet.toArray(new LocalePartInfo[countriesSet.size()]);
        Arrays.sort(m_countries);
      }
    }
  }
}