final SimpleAttributeModifier sam = new SimpleAttributeModifier("class", "alt");
add(new ListView("usrs", new ArrayList(userRolesMap.keySet())) {
protected void populateItem(ListItem listItem) {
long userId = (Long) listItem.getModelObject();
final User user = userRolesMap.get(userId).get(0).getUser();
if(selectedUserId == userId) {
listItem.add(new SimpleAttributeModifier("class", "selected"));
} else if(listItem.getIndex() % 2 == 1) {
listItem.add(sam);
}
listItem.add(new Link("loginName") {
public void onClick() {
if(previous instanceof UserAllocatePage) { // prevent recursive stack buildup
previous = null;
}
setResponsePage(new UserAllocatePage(user.getId(), SpaceAllocatePage.this, spaceId));
}
}.add(new Label("loginName", new PropertyModel(user, "loginName"))));
listItem.add(new Label("name", new PropertyModel(user, "name")));
List<UserSpaceRole> usrs = userRolesMap.get(userId);
listItem.add(new RoleDeAllocatePanel("roleDeAllocatePanel", usrs));
}
});
add(new Link("createNewUser") {
@Override
public void onClick() {
UserFormPage page = new UserFormPage();
page.setPrevious(SpaceAllocatePage.this);
setResponsePage(page);
}
});
List<User> users = getJtrac().findUsersNotFullyAllocatedToSpace(spaceId);
DropDownChoice userChoice = new DropDownChoice("user", users, new IChoiceRenderer() {
public Object getDisplayValue(Object o) {
User u = (User) o;
return u.getName() + " (" + u.getLoginName() + ")";
}
public String getIdValue(Object o, int i) {
return ((User) o).getId() + "";
}
});
userChoice.setNullValid(true);
add(userChoice);
userChoice.add(new AjaxFormComponentUpdatingBehavior("onChange") {
protected void onUpdate(AjaxRequestTarget target) {
User u = (User) getFormComponent().getConvertedInput();
if (u == null) {
roleAllocatePanel.setChoices(new ArrayList<String>());
allocateButton.setEnabled(false);
} else {
User temp = getJtrac().loadUser(u.getId());
// populate choice, enable button etc
initRoleChoice(temp);
}
target.addComponent(roleAllocatePanel);
target.addComponent(allocateButton);
}
});
roleAllocatePanel = new RoleAllocatePanel("roleAllocatePanel");
roleAllocatePanel.setOutputMarkupId(true);
add(roleAllocatePanel);
allocateButton = new Button("allocate") {
@Override
public void onSubmit() {
List<String> roleKeys = roleAllocatePanel.getSelected();
if(user == null || roleKeys.size() == 0) {
return;
}
// avoid lazy init problem
User temp = getJtrac().loadUser(user.getId());
for(String roleKey : roleKeys) {
getJtrac().storeUserSpaceRole(temp, space, roleKey);
}
JtracSession.get().refreshPrincipalIfSameAs(temp);
setResponsePage(new SpaceAllocatePage(spaceId, previous, user.getId()));