summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/nls/ui/FlagImagesRepository.java
blob: 854ba174be51a4f5f97a6df9aeff1fcd972a3cc9 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
 * 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.ui;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import org.eclipse.swt.graphics.Image;
import org.eclipse.wb.internal.core.DesignerPlugin;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Repository of flags for locale/country.
 *
 * @author scheglov_ke
 * @coverage core.nls.ui
 */
public class FlagImagesRepository {
  private static Map<String, Image> m_countriesFlags = Maps.newHashMap();
  private static Locale[] m_locales;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Initialization
  //
  ////////////////////////////////////////////////////////////////////////////
  private static void init() {
    if (m_locales == null) {
      // prepare sorted Locale's
      {
        List<Locale> locales = Lists.newArrayList();
        Collections.addAll(locales, Locale.getAvailableLocales());
        Collections.sort(locales, new Comparator<Locale>() {
          @Override
        public int compare(Locale o1, Locale o2) {
            return o1.toString().compareTo(o2.toString());
          }
        });
        m_locales = locales.toArray(new Locale[locales.size()]);
      }
    }
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Access
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * @return the array of {@link Locale}'s sorted by title.
   */
  public static Locale[] getSortedLocales() {
    init();
    return m_locales;
  }

  /**
   * @return the {@link Image} of flag for default {@link Locale}.
   */
  public static Image getEmptyFlagImage() {
    return DesignerPlugin.getImage("nls/flags/flag_empty.png");
  }

  /**
   * @return the {@link Image} of flag for given {@link Locale}.
   */
  public static Image getFlagImage(Locale locale) {
    init();
    String localeCountry = locale.getCountry();
    String localeLanguage = locale.getLanguage();

// BEGIN ADT MODIFICATIONS
    return getFlagImage(localeCountry, localeLanguage);
  }

  public static Image getFlagImage(String localeCountry, String localeLanguage) {
        init();
// END ADT MODIFICATIONS

    // if locale has no assosiated country set, try to find the locale with the same language but with the country set
    if (localeCountry.length() == 0) {
      // special cases
      if (localeLanguage.equals("ar")) {
        localeCountry = "AE";
      } else if (localeLanguage.equals("zh")) {
        localeCountry = "CN";
      } else if (localeLanguage.equals("en")) {
        localeCountry = "US";
      } else {
        // try to guess
        String localeCountryCandidate = "";
        for (int i = 0; i < m_locales.length; i++) {
          Locale lookupLocale = m_locales[i];
          String lookupLanguage = lookupLocale.getLanguage();
          if (lookupLanguage.equals(localeLanguage)) {
            if (lookupLocale.getCountry().length() != 0) {
              localeCountryCandidate = lookupLocale.getCountry();
              if (localeCountryCandidate.equalsIgnoreCase(lookupLanguage)) {
                localeCountry = localeCountryCandidate;
                break;
              }
            }
          }
        }
        if (localeCountry.length() == 0) {
          localeCountry = localeCountryCandidate;
        }
      }
    }
    //
    Image flagImage = m_countriesFlags.get(localeCountry);
    if (flagImage == null) {
      try {
        String flagFileName = null;
        if (localeCountry.equalsIgnoreCase("YU")) {
          localeCountry = "CS"; // use Serbia and Montenegro
        }
        if (Strings.isNullOrEmpty(localeCountry)) {
          return null;
        }
        flagFileName = localeCountry.toLowerCase() + ".png";
        flagImage = DesignerPlugin.getImage("nls/flags/" + flagFileName);
        m_countriesFlags.put(localeCountry, flagImage);
      } catch (Throwable e) {
        return null;
      }
    }
    return flagImage;
  }
}