Back: Tables Next: MoreInformation
There are times when you may want to implement your own Fields (net.sourceforge.wicketwebbeans.fields.Field). net.sourceforge.wicketwebbeans.fields.AbstractField is the base class for the Field interface. It is recommended that you use the AbstractField base class, or an extension of it, if your field will participate in a BeanForm.
By convention, all Fields must define a constructor with the signature:
public SomeField(String id, IModel model, ElementMetaData metaData, boolean viewOnly)
In this example, we're going to enhance the net.sourceforge.wicketwebbeans.examples.customfields.Address bean to add a Country property. The Country property will be a non-Java enumeration. While Java enums are statically defined at compile-time, there are cases where you need to derive the values at runtime - let's say from a database. To make things simpler for you, WWB defines an interface called net.sourceforge.wicketwebbeans.model.NonJavaEnum. It also has a base class for this interface called net.sourceforge.wicketwebbeans.model.BaseNonJavaEnum. So let's look at our net.sourceforge.wicketwebbeans.examples.customfields.Country enumeration:
01 package net.sourceforge.wicketwebbeans.examples.customfields;
|
Java2html |
You would normally query the database for countries in the static values() method. However, we don't have a database, so we just hard coded them. The idea is that you would retrieve these values at runtime. The static values() and valueOf() methods are similar to those found on a Java language enum.
Next we need to implement the Field. WWB has a base Field class net.sourceforge.wicketwebbeans.fields.EnumField that can be used for Java enums and NonJavaEnums. Here's our net.sourceforge.wicketwebbeans.examples.customfields.CountryField that extends EnumField:
01 package net.sourceforge.wicketwebbeans.examples.customfields;
|
Java2html |
Next, we need to register the CountryField type so that whenever WWB sees a Country bean, it will know how to handle it. In this example, this is done in net.sourceforge.wicketwebbeans.examples.customfields.CustomFieldPage:
01 package net.sourceforge.wicketwebbeans.examples.customfields;
|
Java2html |
For simplicity, we add the mapping to ComponentRegistry directly in our Page code. However, you would normally create your instance or sub-class of ComponentRegistry outside of the Page code so that it can be used from multiple pages.
The end result looks like this:
Back: Tables Next: MoreInformation