View Javadoc

1   package org.appfuse.webapp.action;
2   
3   import org.appfuse.model.LabelValue;
4   
5   import java.text.Collator;
6   import java.util.ArrayList;
7   import java.util.Collections;
8   import java.util.Comparator;
9   import java.util.LinkedHashMap;
10  import java.util.List;
11  import java.util.Locale;
12  import java.util.Map;
13  
14  public class CountryModel {
15      private Map<String, String> availableCountries;
16  
17      /**
18       * Build a List of LabelValues for all the available countries. Uses
19       * the two letter uppercase ISO name of the country as the value and the
20       * localized country name as the label.
21       *
22       * @param locale The Locale used to localize the country names.
23       *
24       * @return List of LabelValues for all available countries.
25       */
26      @SuppressWarnings("unchecked")
27      public Map<String, String> getCountries(Locale locale) {
28          if (availableCountries == null) {
29              final String EMPTY = "";
30              final Locale[] available = Locale.getAvailableLocales();
31      
32              List<LabelValue> countries = new ArrayList<LabelValue>();
33              countries.add(new LabelValue("",""));
34  
35              for (Locale anAvailable : available) {
36                  final String iso = anAvailable.getCountry();
37                  final String name = anAvailable.getDisplayCountry(locale);
38  
39                  if (!EMPTY.equals(iso) && !EMPTY.equals(name)) {
40                      LabelValue country = new LabelValue(name, iso);
41  
42                      if (!countries.contains(country)) {
43                          countries.add(new LabelValue(name, iso));
44                      }
45                  }
46              }
47  
48              Collections.sort(countries, new LabelValueComparator(locale));
49              
50              Map<String, String> options = new LinkedHashMap<String, String>();
51              // loop through and convert list to a JSF-Friendly Map for a <select>
52              for (Object country : countries) {
53                  LabelValue option = (LabelValue) country;
54                  if (!options.containsValue(option.getValue())) {
55                      options.put(option.getLabel(), option.getValue());
56                  }
57              }
58              this.availableCountries = options;
59          }
60  
61          return availableCountries;
62      }
63  
64      /**
65       * Class to compare LabelValues using their labels with
66       * locale-sensitive behaviour.
67       */
68      public class LabelValueComparator implements Comparator {
69          private Comparator<Object> c;
70  
71          /**
72           * Creates a new LabelValueComparator object.
73           *
74           * @param locale The Locale used for localized String comparison.
75           */
76          public LabelValueComparator(Locale locale) {
77              c = Collator.getInstance(locale);
78          }
79  
80          /**
81           * Compares the localized labels of two LabelValues.
82           *
83           * @param o1 The first LabelValue to compare.
84           * @param o2 The second LabelValue to compare.
85           *
86           * @return The value returned by comparing the localized labels.
87           */
88          public final int compare(Object o1, Object o2) {
89              LabelValue lhs = (LabelValue) o1;
90              LabelValue rhs = (LabelValue) o2;
91  
92              return c.compare(lhs.getLabel(), rhs.getLabel());
93          }
94      }
95  }