KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > taglib > CountryTag


1 package org.appfuse.webapp.taglib;
2
3 import java.io.IOException JavaDoc;
4
5 import java.text.Collator JavaDoc;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.Comparator JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Locale JavaDoc;
13
14 import javax.servlet.jsp.JspException JavaDoc;
15 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
16
17 import org.appfuse.model.LabelValue;
18
19 import org.displaytag.tags.el.ExpressionEvaluator;
20
21 /**
22  * Tag for creating multiple <select> options for displaying a list of
23  * country names.
24  *
25  * <p>
26  * <b>NOTE</b> - This tag requires a Java2 (JDK 1.2 or later) platform.
27  * </p>
28  *
29  * @author Jens Fischer, Matt Raible
30  * @version $Revision: 1.4.2.1 $ $Date: 2006-06-10 08:00:48 -0600 (Sat, 10 Jun 2006) $
31  *
32  * @jsp.tag name="country" bodycontent="empty"
33  */

34 public class CountryTag extends TagSupport JavaDoc {
35     private static final long serialVersionUID = 3905528206810167095L;
36     private String JavaDoc name;
37     private String JavaDoc prompt;
38     private String JavaDoc scope;
39     private String JavaDoc selected;
40
41     /**
42      * @param name The name to set.
43      *
44      * @jsp.attribute required="false" rtexprvalue="true"
45      */

46     public void setName(String JavaDoc name) {
47         this.name = name;
48     }
49
50     /**
51      * @param prompt The prompt to set.
52      * @jsp.attribute required="false" rtexprvalue="true"
53      */

54     public void setPrompt(String JavaDoc prompt) {
55         this.prompt = prompt;
56     }
57
58     /**
59      * @param selected The selected option.
60      * @jsp.attribute required="false" rtexprvalue="true"
61      */

62     public void setDefault(String JavaDoc selected) {
63         this.selected = selected;
64     }
65
66     /**
67      * Property used to simply stuff the list of countries into a
68      * specified scope.
69      *
70      * @param scope
71      *
72      * @jsp.attribute required="false" rtexprvalue="true"
73      */

74     public void setToScope(String JavaDoc scope) {
75         this.scope = scope;
76     }
77
78     /**
79      * Process the start of this tag.
80      *
81      * @return int status
82      *
83      * @exception JspException if a JSP exception has occurred
84      *
85      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
86      */

87     public int doStartTag() throws JspException JavaDoc {
88         ExpressionEvaluator eval = new ExpressionEvaluator(this, pageContext);
89
90         if (selected != null) {
91             selected = eval.evalString("default", selected);
92         }
93
94         Locale JavaDoc userLocale = pageContext.getRequest().getLocale();
95         List JavaDoc 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 JavaDoc("Attribute 'scope' must be: page, request, session or application");
108             }
109         } else {
110             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc io) {
134                 throw new JspException JavaDoc(io);
135             }
136         }
137
138         return super.doStartTag();
139     }
140
141     /**
142      * Release aquired resources to enable tag reusage.
143      *
144      * @see javax.servlet.jsp.tagext.Tag#release()
145      */

146     public void release() {
147         super.release();
148     }
149
150     /**
151      * Build a List of LabelValues for all the available countries. Uses
152      * the two letter uppercase ISO name of the country as the value and the
153      * localized country name as the label.
154      *
155      * @param locale The Locale used to localize the country names.
156      *
157      * @return List of LabelValues for all available countries.
158      */

159     protected List JavaDoc buildCountryList(Locale JavaDoc locale) {
160         final String JavaDoc EMPTY = "";
161         final Locale JavaDoc[] available = Locale.getAvailableLocales();
162
163         List JavaDoc countries = new ArrayList JavaDoc();
164
165         for (int i = 0; i < available.length; i++) {
166             final String JavaDoc iso = available[i].getCountry();
167             final String JavaDoc 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     /**
184      * Class to compare LabelValues using their labels with
185      * locale-sensitive behaviour.
186      */

187     public class LabelValueComparator implements Comparator JavaDoc {
188         private Comparator JavaDoc c;
189
190         /**
191          * Creates a new LabelValueComparator object.
192          *
193          * @param locale The Locale used for localized String comparison.
194          */

195         public LabelValueComparator(Locale JavaDoc locale) {
196             c = Collator.getInstance(locale);
197         }
198
199         /**
200          * Compares the localized labels of two LabelValues.
201          *
202          * @param o1 The first LabelValue to compare.
203          * @param o2 The second LabelValue to compare.
204          *
205          * @return The value returned by comparing the localized labels.
206          */

207         public final int compare(Object JavaDoc o1, Object JavaDoc 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