KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > action > CountryModel


1 package org.appfuse.webapp.action;
2
3 import java.text.Collator JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collections JavaDoc;
6 import java.util.Comparator JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.LinkedHashMap JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Locale JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.appfuse.model.LabelValue;
14
15 public class CountryModel {
16     private Map JavaDoc availableCountries;
17
18     /**
19      * Build a List of LabelValues for all the available countries. Uses
20      * the two letter uppercase ISO name of the country as the value and the
21      * localized country name as the label.
22      *
23      * @param locale The Locale used to localize the country names.
24      *
25      * @return List of LabelValues for all available countries.
26      */

27     public Map JavaDoc getCountries(Locale JavaDoc locale) {
28         if (availableCountries == null) {
29             final String JavaDoc EMPTY = "";
30             final Locale JavaDoc[] available = Locale.getAvailableLocales();
31     
32             List JavaDoc countries = new ArrayList JavaDoc();
33             countries.add(new LabelValue("",""));
34     
35             for (int i = 0; i < available.length; i++) {
36                 final String JavaDoc iso = available[i].getCountry();
37                 final String JavaDoc name = available[i].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 JavaDoc options = new LinkedHashMap JavaDoc();
51             // loop through and convert list to a JSF-Friendly Map for a <select>
52
for (Iterator JavaDoc it = countries.iterator(); it.hasNext();) {
53                 LabelValue option = (LabelValue) it.next();
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 JavaDoc {
69         private Comparator JavaDoc 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 JavaDoc 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 JavaDoc o1, Object JavaDoc o2) {
89             LabelValue lhs = (LabelValue) o1;
90             LabelValue rhs = (LabelValue) o2;
91
92             return c.compare(lhs.getLabel(), rhs.getLabel());
93         }
94     }
95 }
96
Popular Tags