1 package org.appfuse.webapp.action; 2 3 import java.text.Collator ; 4 import java.util.ArrayList ; 5 import java.util.Collections ; 6 import java.util.Comparator ; 7 import java.util.Iterator ; 8 import java.util.LinkedHashMap ; 9 import java.util.List ; 10 import java.util.Locale ; 11 import java.util.Map ; 12 13 import org.appfuse.model.LabelValue; 14 15 public class CountryModel { 16 private Map availableCountries; 17 18 27 public Map getCountries(Locale locale) { 28 if (availableCountries == null) { 29 final String EMPTY = ""; 30 final Locale [] available = Locale.getAvailableLocales(); 31 32 List countries = new ArrayList (); 33 countries.add(new LabelValue("","")); 34 35 for (int i = 0; i < available.length; i++) { 36 final String iso = available[i].getCountry(); 37 final String 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 options = new LinkedHashMap (); 51 for (Iterator 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 68 public class LabelValueComparator implements Comparator { 69 private Comparator c; 70 71 76 public LabelValueComparator(Locale locale) { 77 c = Collator.getInstance(locale); 78 } 79 80 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 } 96 | Popular Tags |