Skip to content

Instantly share code, notes, and snippets.

@szpak
Created November 18, 2013 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szpak/b5c5ae36e7d170f3676c to your computer and use it in GitHub Desktop.
Save szpak/b5c5ae36e7d170f3676c to your computer and use it in GitHub Desktop.
Countries - drop down choice in Wicket
package org.appfuse.webapp.pages.components.country;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.appfuse.webapp.service.JavaLocaleCountryService;
import java.util.List;
import java.util.Locale;
public class CountriesModel extends LoadableDetachableModel<List<? extends Country>> {
@SpringBean
private JavaLocaleCountryService countryService;
private final Locale locale;
public CountriesModel(Locale locale) {
this.locale = locale;
Injector.get().inject(this);
}
@Override
protected List<? extends Country> load() {
return countryService.getAvailableCountriesInLocale(locale);
}
}
package org.appfuse.webapp.pages.components.country;
import java.io.Serializable;
public class Country implements Serializable {
private final String locale;
private final String name;
public Country(String locale, String name) {
this.locale = locale;
this.name = name;
}
public String getLocale() {
return locale;
}
public String getName() {
return name;
}
}
package org.appfuse.webapp.pages.components.country;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.model.PropertyModel;
import java.util.Locale;
public class CountryDropDownChoice extends DropDownChoice<Country> {
public CountryDropDownChoice(String id, PropertyModel<String> country, Locale locale) {
super(id, new EmbeddedCountryModel(country, locale), new CountriesModel(locale), new ChoiceRenderer<>("name", "locale"));
}
}
package org.appfuse.webapp.pages.components.country;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.appfuse.webapp.service.JavaLocaleCountryService;
import java.util.Locale;
public class EmbeddedCountryModel implements IModel<Country> {
private final IModel<String> nestedModel;
private final Locale locale;
@SpringBean
private JavaLocaleCountryService countryService;
public EmbeddedCountryModel(IModel<String> nestedModel, Locale locale) {
this.nestedModel = nestedModel;
this.locale = locale;
Injector.get().inject(this);
}
@Override
public Country getObject() {
if (nestedModel.getObject() != null) {
return countryService.getCountryInLocaleByCode(locale, nestedModel.getObject());
} else {
return null;
}
}
@Override
public void setObject(Country object) {
if (object != null) {
nestedModel.setObject(object.getLocale());
} else {
nestedModel.setObject(null);
}
}
@Override
public void detach() {
nestedModel.detach();
}
}
package org.appfuse.webapp.service;
import org.appfuse.webapp.pages.components.country.Country;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.*;
//TODO: Add @Cacheable decorator on this class
@Service("countryService")
public class JavaLocaleCountryService implements CountryService {
private final static Logger log = LoggerFactory.getLogger(JavaLocaleCountryService.class);
private CountryComparator countryComparator = new CountryComparator();
public List<Country> getAvailableCountriesInLocale(Locale locale) {
return new ArrayList<>(createSortedSet(locale));
}
public Country getCountryInLocaleByCode(Locale locale, String code) {
for (Country country : getAvailableCountriesInLocale(locale)/*countries*/) {
if (code.equals(country.getLocale())) {
return country;
}
}
return null;
}
private Set<Country> createSortedSet(Locale currentLocale) {
final String EMPTY = "";
Locale[] availableLocales = Locale.getAvailableLocales();
Set<Country> countrySet = new TreeSet<>(countryComparator);
for (Locale locale : availableLocales) {
String name = locale.getDisplayCountry(currentLocale);
String iso = locale.getCountry();
if (!EMPTY.equals(name) && !EMPTY.equals(iso)) {
countrySet.add(new Country(iso, name));
}
}
log.debug("Number of countries added: " + countrySet.size());
return countrySet;
}
private static class CountryComparator implements Comparator<Country> {
@Override
public int compare(Country country1, Country country2) {
return country1.getName().compareToIgnoreCase(country2.getName());
}
}
}
public abstract class UserEditPanel extends Panel {
(...)
public class AddressFragment extends Fragment {
public AddressFragment(String id, String markupId, IModel<Address> model) {
super(id, markupId, UserEditPanel.this, model);
}
@Override
protected void onInitialize() {
super.onInitialize();
//moved to onInitilize to prevent:
// "Make sure you are not calling Component#getString() inside your Component's constructor."
add(new TextField("address"));
add(new RequiredTextField("city").add(new RequiredBehavior()));
add(new RequiredTextField("province").add(new RequiredBehavior()));
add(new RequiredTextField("postalCode").add(new RequiredBehavior()));
CountryDropDownChoice countries = new CountryDropDownChoice("country",
new PropertyModel<String>(getDefaultModel(), "country"), getLocale());
add(countries.setRequired(true).add(new RequiredBehavior()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment