« Wicket Javadocs in CHM formatGently asking the GWT Hosted Mode to NOT redirect the logs »

A simple, localized, Wicket country list model

03/02/09

Permalink 12:26:00 pm, by nogunner Email , 463 words   English (US)
Categories: Misc, Wicket

A simple, localized, Wicket country list model

A quick post just to share a country list model I did a few days ago. It's no big deal, but I think it's worth sharing to avoid my fellow wicketeers the trouble to do it.

So, it's a standard Wicket Model that uses a standard ISO-3166-1 (2-char long) code to store the country name. The idea is to keep simplicity in my domain object, so the country property is just a String that stores the ISO3166 code. As it's not easily possible to use this ISO3166 code directly in a Wicket DropDownChoice (I want to display a localized country full name, but store the ISO-3166 short code), I had to create a Country object, which is internally used. Note however that the CountryList class provides an adaptor that converts this internally-used Country object into the property string of my domain POJO, so it's transparent.

Example screenshot

The country list is localized, and comes from the ISO files I found at http://www.iso.org/iso/country_codes/iso_3166_code_lists.htm. Unfortunately, the ISO organization only provides english and french locales from the country name, so if you want more, you'll have to translate them yourselves and add the resource file to the jar.

Actually, using it is pretty straightforward, so I'll just show how to use it in an example.

The HTML is, as expected, the standard:

<select wicket:id="country">
 <option value="US">United States</option>
 <option value="FR">France</option>
</select> 

The java code instanciates a country list, and uses it as a model for the DropDownChoice element (although it can be used with any component that uses an IModel<List> ).

CountryList countrylist = new CountryList(); // (1)
DropDownChoice country = new DropDownChoice("country", 
		    countrylist.getAdaptor(new PropertyModel(this.obj, "country")), // (2)
		    countrylist, // (3)
		    countrylist.getRenderer()); // (4)
form.add(country);

in (1), we instantiate the CountryList class using the default constructor, which uses the Wicket Session locale. It is also possible to pass a Locale object to the constructor.

in (2), we use the adaptor provided by the CountryList class: this is a very basic IModel<Country> that takes a IModel<String> as its own model to set and get the correct Country element. This allows our POJO to just use a String property, instead of dealing with an annoying Country object.

in (3), this is actually the list of countries itself

in (4), this is the ChoiceRenderer that displays the full country names, instead of the short code.

Download

  • The full project (includes the jar binary and an example): Wountry-1.0.zip (100Kb)
    (NOTE: oooops, the file HomePage.java from the example was wrong, please download again if you got the first version of the archive)
  • The jar binary only: wountry-1.0.jar (10Kb)

4 comments

Comment from: Kamlesh [Visitor]
Hi - sorry to spam the blog, but just wanted to check if jsmooth is still in development?
03/05/09 @ 03:26
Comment from: john brightman [Visitor] · http://www.whoismark.com
****-
HI
looks very interesting!
bookmarked your blog.
good luck!
john brightman
05/24/09 @ 13:35
Comment from: Tauren [Visitor]
****-
Looks like something I could use! But the download links don't work. Any chance to fix that?
07/13/09 @ 09:07
Comment from: nogunner [Member] Email
sorry, the link was lost during a server migration.
It's fixed now.
07/13/09 @ 09:34

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
nogunner's blog

Pointless technical stuffs are the bomb diggity of life.

March 2010
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Search

XML Feeds

Web Monitoring

Be sure to check my LinkLogics web monitoring application if you happen to need external monitoring.
blog tool