RadioGroups contain Radio objects. The model for the RadioGroup is the object property, while the child Radio objects represent the set of values the property can be set to. Let's look at a trivial example, starting with a POJO:
Panel |
---|
Code Block |
public class MyObject {
private Integer myInt;
public void setMyInt(Integer myInt) ...
public Integer getMyInt() ...
}
|
Next, the relevant snippet from the Form class:
Panelcode |
---|
MyObject bean=new MyObject(); RadioGroup myInt = new RadioGroup("group", new PropertyModel(bean, "myInt")); myInt.add(new Radio("1", new Model(1))); myInt.add(new Radio("2", new Model(2))); myInt.add(new Radio("3", new Model(3))); myInt.add(new Radio("4", new Model(4))); add(myInt); |
And finally, the markup:
Panelcode |
---|
<span wicket:id="group"> <input wicket:id="1" type="radio"/> <input wicket:id="2" type="radio"/> <input wicket:id="3" type="radio"/> <input wicket:id="4" type="radio"/> </span> |
And that's all there is to using a RadioGroup with Radio instances.
Also note that the Radio components do not need to be direct children of the radio group, they can be anywhere in the component hierarchy below the RadioGroup component.
Using RadioGroup within table rows
Sometimes it is useful to have a RadioGroup that encompasses each row. This can be accomplished by using a wicket encloser. In your web page (example uses a data view, but any view will do):
Code Block |
---|
final Form profileForm = new Form("profile-form");
final DataView dataView = new DataView("profile-rows", myIDataProvider) {
private static final long serialVersionUID = 1L;
/**
* {@inheritDoc}
*/
@Override
protected void populateItem(final Item item) {
Person person = (Person) item.getModelObject();
item.add(new Label("first-name", person.getFirstName()));
item.add(new Label("last-name", person.getLastName()));
// add the radio group to select system or department (organizational) administrators
final RadioGroup adminRadioGroup = new RadioGroup("radio-admin", new Model());
item.add(adminRadioGroup);
adminRadioGroup.add(new Radio("radio-sys-admin", new Model()));
adminRadioGroup.add(new Radio("radio-dept-admin", new Model()));
adminRadioGroup.add(new Radio("radio-no-admin", new Model()));
}
};
profileForm.add(dataView);
profileForm.add(new Button("button-save", new Model()));
add(profileForm);
|
And finally, the markup:
Code Block |
---|
<form wicket:id="profile-form">
<table>
<thead>
<tr>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Sys. Admin
</th>
<th>
Dept. Admin
</th>
</tr>
</thead>
<tbody>
<tr wicket:id="profile-rows">
<td><span wicket:id="first-name">[First Name]</span></td>
<td><span wicket:id="last-name">[Last Name]</span></td>
<wicket:container wicket:id="radio-admin">
<td><input wicket:id="radio-sys-admin" type="radio" /></td>
<td><input wicket:id="radio-dept-admin" type="radio" /></td>
<td><input wicket:id="radio-no-admin" type="radio" /></td>
</wicket:container>
</tr>
</tbody>
</table>
<input wicket:id="button-save" value="Save" type="submit" />
</form>
|
Default selections
Using the previous example we can make a slight alteration that will add a default selection based upon the currently iterated item (person) in the DataView. Setting the default selection is accomplished by setting the same model used for the individual Radio component as we do on the RadioGroup. For Example:
Code Block |
---|
final Form profileForm = new Form("profile-form"); final DataView dataView = new DataView("profile-rows", myIDataProvider) { private static final long serialVersionUID = 1L; private static final Long ROLE_SYS_ADMIN = 1L; private static final Long ROLE_DEPT_ADMIN = 2L; private static final Long ROLE_NO_ADMIN = 3L; /** * {@inheritDoc} */ @Override protected void populateItem(final Item item) { Person person = (Person) item.getModelObject(); item.add(new Label("first-name", person.getFirstName())); item.add(new Label("last-name", person.getLastName())); // add the radio group to select system or department (organizational) administrators final RadioGroup adminRadioGroup = new RadioGroup("radio-admin"); item.add(adminRadioGroup); // create our three models with corresponding object values (PersonRoleXrefId = persistent model that holds person/role id) final Model sysAdminModel = new Model(new PersonRoleXrefId(person.getId(), ROLE_SYS_ADMIN)); final Model deptAdminModel = new Model(new PersonRoleXrefId(person.getId(), ROLE_DEPT_ADMIN)); final Model noAdminModel = new Model(new PersonRoleXrefId(person.getId(), ROLE_NO_ADMIN )); // add the models to the radios and the radios to the radio group adminRadioGroup.add(new Radio("radio-sys-admin", sysAdminModel)); adminRadioGroup.add(new Radio("radio-dept-admin", deptAdminModel)); adminRadioGroup.add(new Radio("radio-non-admin", noAdminModel)); // set current default role selection based upon the currently iterated item // ( hasRole(...) is example method that determines if the person has the specified role or not) if (hasRole(person, ROLE_SYS_ADMIN)) { adminRadioGroup.setModel(sysAdminModel); } else if (hasRole(person, ROLE_SYS_ADMIN)) { adminRadioGroup.setModel(deptAdminModel); } else { adminRadioGroup.setModel(noAdminModel); } } }; profileForm.add(dataView); profileForm.add(new Button("button-save", new Model()) { private static final long serialVersionUID = 1L; /** * {@inheritDoc} */ @Override public final void onSubmit() { // see section for "Processing more than one RadioGroup simultaneously" for explanation saveRoleAssignments(dataView); } }); add(profileForm); |
Processing more than one RadioGroup simultaneously
Sometimes it is useful to be able to process numerous RadioGroups at any given time. This can be accomplished by visiting the children of the DataView (expanding on the previous example).
Code Block |
---|
/**
* Saves the data views items that have changed. Each item represents a person with a corresponding child RadioGroup that contains the person/role ID reference.
*
* @param dataView
* the data view in which the items/selections will be extracted from
*/
private final void saveRoleAssignments(final DataView dataView) {
final List<PersonRoleXrefId> saveXrefs = new ArrayList<PersonRoleXrefId>();
for (final Iterator<?> it = dataView.getItems(); it.hasNext();) {
final Item item = (Item) it.next();
final Person person = (Person) item.getModelObject();
item.visitChildren(new Component.IVisitor() {
/**
* {@inheritDoc}
*/
@Override
public final Object component(final Component component) {
if (component instanceof RadioGroup) {
final PersonRoleXrefId prId = (PersonRoleXrefId) component.getModelObject();
// compare with existing person's assigned roles and only add changed associations
if (!hasRole(person, prId.getRoleId(), false)) {
saveXrefs.add(prId);
}
}
// continue visiting children to locate other RadioGroup components
return CONTINUE_TRAVERSAL;
}
});
}
if (!saveXrefs.isEmpty()) {
// save person/role assignments using a IDataProvider assigned to the DataView
((MyIDataProvider) dataView.getDataProvider()).saveRoleAssignments(saveXrefs);
}
}
|