On 9/20/07, dtoffe <[EMAIL PROTECTED]> wrote:
>
>
> For most cases, the PropertyChangeEvents are ok. But now I'm facing a
> particular problem and I guess I'm trying to use the wrong tool for the
> task.
> Let's say I have a bean with two properties and I want to show them as
> dropdownchoices, but they are related, think for example of the CarMake,
> CarModel, or Country, State pairs, where the first value determines a
> subset
> of possibilities for the second value.
> I would like to select the first value, and have the second
> dropdownchoice to get filtered by the possible values. How should I face
> such a situation ? I guess having the second dropdownchoice based on an
> enum is not correct, since I only want a (non necessarily contiguous)
> subset
> of values to be allowed.
>
> Thanks for your help !
>
> Daniel
I've created a new example in the
wicket.contrib.webbeans.examples.dependentfields package that illustrates
this. You'll need to get the latest WWB code from SVN (rev 28) to run the
example. The example is based on the Wicket Ajax example with a Car Make and
Model (two dependent drop-down fields). Basically, you need to implement a
custom field for the Car's Model (an enum), because it is dependent on the
Make enum:
public class ModelField extends EnumField
{
private ElementMetaData makeProp;
public ModelField(String id, IModel model, ElementMetaData metaData,
boolean viewOnly)
{
// Init with an empty set of values. We can't build the list until
later.
super(id, model, metaData, viewOnly, Collections.EMPTY_LIST);
// Retrieve the parameter that defines which Make property we need.
makeProp = getDependentProperty(metaData, "makeProp", Make.class);
setValuesModel( new ValuesModel() );
}
private final class ValuesModel extends AbstractReadOnlyModel
{
@Override
public Object getObject(Component component)
{
// Retrieve the value of the dependent property.
Make make = (Make)getDependentPropertyBean(makeProp);
if (make != null) {
// Build a list of models based on the make.
List<Model> values = new ArrayList<Model>();
for (Model modelChoice : Model.values()) {
if (modelChoice.getMake().equals(make)) {
values.add(modelChoice);
}
}
return values;
}
return Collections.EMPTY_LIST;
}
}
}
And in beanprops, you would say:
Car {
cols: 1;
props: make, model{ makeProp: make };
}
On the "model" property, I specified the "makeProp" parameter (defined by
ModelField) to specify which Make property in your bean that it should be
dependent on. And viola, changing the Car's Make causes the list of choices
for the Car's Model to change - in real-time via Ajax. The advantage here is
that this field can be reused over and over wherever you may have a
Make/Model pair.