Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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", assocModelnoAdminModel));

		// 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);

...