Monday, April 19, 2010

Select Drop-down List. Where is PropertySelection?

Functionally SELECTS work pretty much the same, except you now you need to build the whole list for Tapestry to retrieve all at once. Under Tapestry 3 I was already building a List of Vectors in my Model for drop downs that gets used by both the Swing (desktop gui) application and the Tapestry3 web app. That meant that I was making a list, the Tapestry3 PropertySelector object was reading that list one option at a time and building it's own separate copy of the list.

This was done through an IPropertySelectionModel interface. Now instead the list goes all at once directly to Tapestry and it keeps track of items and the current selection for you.

For drop-down selects my model which previously implemented IPropertySelectionModel was converted to extend AbstractSelectModel.

So previously in our template (html) file under Tapestry 3 we would have something like this:

<select jwcid="@PropertySelection"
value="ognl:shiptoData.state"
model="ognl:stateSelectModel"/>

It only looks a little simpler for Tapestry 5:

<select t:type="Select"
value="shiptoData.state"
model="StateSelectModel"/>

The class declaration changes from implementing an interface to extending an abstract class definition:

Tapestry3:
public class StateSelectModel implements IPropertySelectionModel {

Tapestry5:
public class StateSelectModel extends AbstractSelectModel {


Additional code for Tapestry 5:
Add a private property for the entire list if you don't already have one (make sure you include the OptionModel interface in your delcaration):

private List optionModelList;

Then add the following two functions to your class:

public List<OptionModel> getOptions() {
optionModelList = new ArrayList<OptionModel>();
for (int i=1; i <= getOptionCount(); i++) {
optionModelList.add(new OptionModelImpl(getLabel(i), getValue(i)));
}
return optionModelList;
}

public List<OptionGroupModel> getOptionGroups() {
return null;
}

That's all there is to it. Your SELECT drop-down will work. Note that the get functions used in that getOptions() function are ones already implemented for the IPropertySelectionModel interface in Tapestry 3.

But even if you are doing this from scratch you should be able to easily provide the return values for the "label" and "value" which are both String type.

The getOptionGroups() function is just returning a null. This is a simple drop down. To do groups is slightly more complicated. You would have to have a loop like the one in getOptions, but then another loop inside of that which built an OptionModel list.

Now if you look closely at this simple example you'll notice that the servlet ends up doing the same extra step as in Tapestry 3. I'm making a list (you can assume that anyway), then the function is reading it one option at a time and making a copy of that list which is then offerered to Tapestry Select component through the getOptions() function.

The reason for this is there is another class that I am getting the values from and that class is the same one that is being used for the JComboBox in the swing application.

No comments:

Post a Comment