1 package org.cacert.gigi.output;
3 import java.io.PrintWriter;
6 import javax.servlet.http.HttpServletRequest;
8 import org.cacert.gigi.GigiApiException;
9 import org.cacert.gigi.dbObjects.CountryCode;
10 import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType;
11 import org.cacert.gigi.localisation.Language;
12 import org.cacert.gigi.output.template.Outputable;
13 import org.cacert.gigi.output.template.Template;
15 public class CountrySelector implements Outputable {
17 private static final Template t = new Template(CountrySelector.class.getResource("CountrySelector.templ"));
19 private CountryCode[] all = CountryCode.getCountryCodes(CountryCodeType.CODE_2_CHARS);
23 private CountryCode selected;
25 private boolean optional;
27 public CountrySelector(String name, boolean optional) {
29 this.optional = optional;
32 public CountrySelector(String name, boolean optional, CountryCode state) {
34 selected = state == null ? null : state.convertToCountryCodeType(CountryCodeType.CODE_2_CHARS);
37 public void update(HttpServletRequest r) throws GigiApiException {
38 String vS = r.getParameter(name);
42 if (vS == null || vS.equals("invalid")) {
46 throw new GigiApiException("Country code required.");
50 selected = CountryCode.getCountryCode(vS, CountryCodeType.CODE_2_CHARS);
54 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
55 vars.put("countryCode", new ArrayIterable<CountryCode>(all) {
58 public void apply(CountryCode t, Language l, Map<String, Object> vars) {
59 vars.put("cc", t.getCountryCode());
60 vars.put("display", t.getCountry());
61 if (selected != null && t.getCountryCode().equals(selected.getCountryCode())) {
62 vars.put("selected", "selected");
64 vars.put("selected", "");
70 vars.put("optional", optional);
71 vars.put("name", name);
73 t.output(out, l, vars);
76 public CountryCode getCountry() {