{
form = new Form("form");
add(form);
// create a repeater that will display the list of contacts.
RefreshingView refreshingView = new RefreshingView("simple")
{
protected Iterator getItemModels()
{
// for simplicity we only show the first 10 contacts
Iterator contacts = DatabaseLocator.getDatabase().find(0, 10, "firstName", true)
.iterator();
// the iterator returns contact objects, but we need it to
// return models, we use this handy adapter class to perform
// on-the-fly conversion.
return new ModelIteratorAdapter(contacts)
{
protected IModel model(Object object)
{
return new DetachableContactModel((Contact)object);
}
};
}
protected void populateItem(final Item item)
{
// populate the row of the repeater
IModel contact = item.getModel();
item.add(new ActionPanel("actions", contact));
// FIXME use CompoundPropertyModel!
item.add(new TextField("id", new PropertyModel(contact, "id")));
item.add(new TextField("firstName", new PropertyModel(contact, "firstName")));
item.add(new TextField("lastName", new PropertyModel(contact, "lastName")));
item.add(new TextField("homePhone", new PropertyModel(contact, "homePhone")));
item.add(new TextField("cellPhone", new PropertyModel(contact, "cellPhone")));
}
protected Item newItem(String id, int index, IModel model)
{
// this item sets markup class attribute to either 'odd' or
// 'even' for decoration
return new OddEvenItem(id, index, model);
}
};
// because we are in a form we need to preserve state of the component
// hierarchy (because it might contain things like form errors that
// would be lost if the hierarchy for each item was recreated every
// request by default), so we use an item reuse strategy.
refreshingView.setItemReuseStrategy(ReuseIfModelsEqualStrategy.getInstance());
form.add(refreshingView);
}