1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.selectOneCountry;
20
21 import java.util.ArrayList;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Locale;
26 import java.util.Set;
27 import java.util.TreeMap;
28
29 import javax.faces.component.UISelectItems;
30 import javax.faces.component.UIViewRoot;
31 import javax.faces.context.FacesContext;
32 import javax.faces.model.SelectItem;
33
34 import org.apache.myfaces.component.html.ext.HtmlSelectOneMenu;
35 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public abstract class AbstractSelectOneCountry extends HtmlSelectOneMenu {
56 public static final String COMPONENT_TYPE = "org.apache.myfaces.SelectOneCountry";
57 private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.SelectOneCountryRenderer";
58
59 public AbstractSelectOneCountry() {
60 setRendererType(DEFAULT_RENDERER_TYPE);
61 }
62
63
64
65
66
67
68 public abstract Integer getMaxLength();
69
70
71
72
73
74
75 public abstract String getEmptySelection();
76
77 private Set getFilterSet(){
78 List selectItems = RendererUtils.getSelectItemList( this );
79 Set set = new HashSet( selectItems.size() );
80
81 for (Iterator i = selectItems.iterator(); i.hasNext(); )
82 set.add( ((SelectItem)i.next()).getValue().toString().toUpperCase() );
83
84 return set;
85 }
86
87 protected List getCountriesChoicesAsSelectItemList(){
88
89
90 Set filterSet = getFilterSet();
91
92 String[] availableCountries = Locale.getISOCountries();
93
94 Locale currentLocale;
95
96 FacesContext facesContext = FacesContext.getCurrentInstance();
97 UIViewRoot viewRoot = facesContext.getViewRoot();
98 if( viewRoot != null )
99 currentLocale = viewRoot.getLocale();
100 else
101 currentLocale = facesContext.getApplication().getDefaultLocale();
102
103
104 TreeMap map = new TreeMap();
105
106
107 for(int i=0; i<availableCountries.length; i++){
108 String countryCode = availableCountries[i];
109 if( ! filterSet.isEmpty() && ! filterSet.contains(countryCode))
110 continue;
111 Locale tmp = new Locale(countryCode, countryCode);
112 map.put(tmp.getDisplayCountry(currentLocale), countryCode);
113 }
114
115 List countriesSelectItems = new ArrayList( map.size() );
116 if(getEmptySelection() != null)
117 countriesSelectItems.add(new SelectItem("", getEmptySelection()));
118
119 Integer maxLength = getMaxLength();
120 int maxDescriptionLength = maxLength==null ? Integer.MAX_VALUE : maxLength.intValue();
121 if( maxDescriptionLength < 5 )
122 maxDescriptionLength = 5;
123
124 for(Iterator i = map.keySet().iterator(); i.hasNext(); ){
125 String countryName = (String) i.next();
126 String countryCode = (String) map.get( countryName );
127 String label;
128 if( countryName.length() <= maxDescriptionLength )
129 label = countryName;
130 else
131 label = countryName.substring(0, maxDescriptionLength-3)+"...";
132
133 countriesSelectItems.add( new SelectItem(countryCode, label) );
134 }
135
136 return countriesSelectItems;
137 }
138
139 protected void validateValue(FacesContext context, Object value) {
140 UISelectItems selectItems = new UISelectItems();
141 selectItems.setTransient(true);
142 selectItems.setValue(getCountriesChoicesAsSelectItemList());
143 getChildren().add(selectItems);
144
145 super.validateValue(context,value);
146 }
147 }