X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Foutput%2FCountrySelector.java;fp=src%2Forg%2Fcacert%2Fgigi%2Foutput%2FCountrySelector.java;h=f35a8f299b486a06a7d8adb7457d96aa3f8a1bd6;hb=bead06ac89a5fbe282dab187c5d1babbb7dcdf66;hp=0000000000000000000000000000000000000000;hpb=be2db6c7661ba6d00aaf1395fb4b7cf6cf16cf9c;p=gigi.git diff --git a/src/org/cacert/gigi/output/CountrySelector.java b/src/org/cacert/gigi/output/CountrySelector.java new file mode 100644 index 00000000..f35a8f29 --- /dev/null +++ b/src/org/cacert/gigi/output/CountrySelector.java @@ -0,0 +1,76 @@ +package org.cacert.gigi.output; + +import java.io.PrintWriter; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.cacert.gigi.GigiApiException; +import org.cacert.gigi.dbObjects.CountryCode; +import org.cacert.gigi.dbObjects.CountryCode.CountryCodeType; +import org.cacert.gigi.localisation.Language; +import org.cacert.gigi.output.template.Outputable; +import org.cacert.gigi.output.template.Template; + +public class CountrySelector implements Outputable { + + private static final Template t = new Template(CountrySelector.class.getResource("CountrySelector.templ")); + + private CountryCode[] all = CountryCode.getCountryCodes(CountryCodeType.CODE_2_CHARS); + + private String name; + + private CountryCode selected; + + private boolean optional; + + public CountrySelector(String name, boolean optional) throws GigiApiException { + this.name = name; + this.optional = optional; + } + + public CountrySelector(String name, boolean optional, String state) throws GigiApiException { + this(name, optional); + selected = CountryCode.getCountryCode(state, CountryCodeType.CODE_2_CHARS); + + } + + public void update(HttpServletRequest r) throws GigiApiException { + String vS = r.getParameter(name); + selected = null; + if (vS == null || vS.equals("invalid")) { + if (optional) { + return; + } else { + throw new GigiApiException("Country code required."); + } + } + selected = CountryCode.getCountryCode(vS, CountryCodeType.CODE_2_CHARS); + + } + + @Override + public void output(PrintWriter out, Language l, Map vars) { + vars.put("countryCode", new ArrayIterable(all) { + + @Override + public void apply(CountryCode t, Language l, Map vars) { + vars.put("cc", t.getCountryCode()); + vars.put("display", t.getCountry()); + if (selected != null && t.getCountryCode().equals(selected.getCountryCode())) { + vars.put("selected", "selected"); + } else { + vars.put("selected", ""); + } + } + }); + vars.put("optional", optional); + vars.put("name", name); + t.output(out, l, vars); + } + + public CountryCode getCountry() { + return selected; + } + +}