1 package org.appfuse.webapp.taglib; 2 3 import java.io.IOException ; 4 5 import java.text.Collator ; 6 7 import java.util.ArrayList ; 8 import java.util.Collections ; 9 import java.util.Comparator ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Locale ; 13 14 import javax.servlet.jsp.JspException ; 15 import javax.servlet.jsp.tagext.TagSupport ; 16 17 import org.appfuse.model.LabelValue; 18 19 import org.displaytag.tags.el.ExpressionEvaluator; 20 21 34 public class CountryTag extends TagSupport { 35 private static final long serialVersionUID = 3905528206810167095L; 36 private String name; 37 private String prompt; 38 private String scope; 39 private String selected; 40 41 46 public void setName(String name) { 47 this.name = name; 48 } 49 50 54 public void setPrompt(String prompt) { 55 this.prompt = prompt; 56 } 57 58 62 public void setDefault(String selected) { 63 this.selected = selected; 64 } 65 66 74 public void setToScope(String scope) { 75 this.scope = scope; 76 } 77 78 87 public int doStartTag() throws JspException { 88 ExpressionEvaluator eval = new ExpressionEvaluator(this, pageContext); 89 90 if (selected != null) { 91 selected = eval.evalString("default", selected); 92 } 93 94 Locale userLocale = pageContext.getRequest().getLocale(); 95 List countries = this.buildCountryList(userLocale); 96 97 if (scope != null) { 98 if (scope.equals("page")) { 99 pageContext.setAttribute(name, countries); 100 } else if (scope.equals("request")) { 101 pageContext.getRequest().setAttribute(name, countries); 102 } else if (scope.equals("session")) { 103 pageContext.getSession().setAttribute(name, countries); 104 } else if (scope.equals("application")) { 105 pageContext.getServletContext().setAttribute(name, countries); 106 } else { 107 throw new JspException ("Attribute 'scope' must be: page, request, session or application"); 108 } 109 } else { 110 StringBuffer sb = new StringBuffer (); 111 sb.append("<select name=\"" + name + "\" id=\"" + name + "\" class=\"select\">\n"); 112 113 if (prompt != null) { 114 sb.append(" <option value=\"\" selected=\"selected\">"); 115 sb.append(eval.evalString("prompt", prompt) + "</option>\n"); 116 } 117 118 for (Iterator i = countries.iterator(); i.hasNext();) { 119 LabelValue country = (LabelValue) i.next(); 120 sb.append(" <option value=\"" + country.getValue() + "\""); 121 122 if ((selected != null) && selected.equals(country.getValue())) { 123 sb.append(" selected=\"selected\""); 124 } 125 126 sb.append(">" + country.getLabel() + "</option>\n"); 127 } 128 129 sb.append("</select>"); 130 131 try { 132 pageContext.getOut().write(sb.toString()); 133 } catch (IOException io) { 134 throw new JspException (io); 135 } 136 } 137 138 return super.doStartTag(); 139 } 140 141 146 public void release() { 147 super.release(); 148 } 149 150 159 protected List buildCountryList(Locale locale) { 160 final String EMPTY = ""; 161 final Locale [] available = Locale.getAvailableLocales(); 162 163 List countries = new ArrayList (); 164 165 for (int i = 0; i < available.length; i++) { 166 final String iso = available[i].getCountry(); 167 final String name = available[i].getDisplayCountry(locale); 168 169 if (!EMPTY.equals(iso) && !EMPTY.equals(name)) { 170 LabelValue country = new LabelValue(name, iso); 171 172 if (!countries.contains(country)) { 173 countries.add(new LabelValue(name, iso)); 174 } 175 } 176 } 177 178 Collections.sort(countries, new LabelValueComparator(locale)); 179 180 return countries; 181 } 182 183 187 public class LabelValueComparator implements Comparator { 188 private Comparator c; 189 190 195 public LabelValueComparator(Locale locale) { 196 c = Collator.getInstance(locale); 197 } 198 199 207 public final int compare(Object o1, Object o2) { 208 LabelValue lhs = (LabelValue) o1; 209 LabelValue rhs = (LabelValue) o2; 210 211 return c.compare(lhs.getLabel(), rhs.getLabel()); 212 } 213 } 214 } 215 | Popular Tags |