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) throws GigiApiException {
29 this.optional = optional;
32 public CountrySelector(String name, boolean optional, String state) throws GigiApiException {
34 selected = CountryCode.getCountryCode(state, CountryCodeType.CODE_2_CHARS);
38 public void update(HttpServletRequest r) throws GigiApiException {
39 String vS = r.getParameter(name);
41 if (vS == null || vS.equals("invalid")) {
45 throw new GigiApiException("Country code required.");
48 selected = CountryCode.getCountryCode(vS, CountryCodeType.CODE_2_CHARS);
53 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
54 vars.put("countryCode", new ArrayIterable<CountryCode>(all) {
57 public void apply(CountryCode t, Language l, Map<String, Object> vars) {
58 vars.put("cc", t.getCountryCode());
59 vars.put("display", t.getCountry());
60 if (selected != null && t.getCountryCode().equals(selected.getCountryCode())) {
61 vars.put("selected", "selected");
63 vars.put("selected", "");
67 vars.put("optional", optional);
68 vars.put("name", name);
69 t.output(out, l, vars);
72 public CountryCode getCountry() {