0

MVVM listbox selection problem with MessageBox

asked 2012-06-04 09:45:12 +0800

lucamerolla gravatar image lucamerolla
66
Hello everyone, I'm porting some pages to the new MVVM pattern with the zk binding and I have found a problem regarding the selected item from a listbox. Basically I have a listbox populated withe my model entities and I have a toolbar with some button actions which are enabled only when an item is selected from a listbox. This works, however, if I add a MessageBox to the command method used by these toolbarbutton, the focus on the selected item get lost. In example
<toolbarbutton
	label="${labels.email.search.btn.details}"
	disabled="@load(empty vm.selected)"
	image="/images/buttons/details.png"
	onClick="@command('doViewDetails')" />
with the following associated command method:
@Command
	public void doViewDetails() {
		Executions.getCurrent().sendRedirect(
			"account/details-account.zul?username="
			+ getSelected().getUsername(), "_blank");
	}
works correctly. But if I have another button where I want a MessageBox to display a question first it doesn't work
<toolbarbutton image="/images/buttons/reset.png"
	disabled="@load(empty vm.selected)"
	label="${labels.account.search.reset}"
	onClick="@command('doReset')" />
@Command
@NotifyChange({ "accounts", "selected" })
public void doReset() {
	Messagebox.show("Do you want to reset the password for the account "
		+ selected.getUsername() + "?", new Messagebox.Button[] {
		Messagebox.Button.NO, Messagebox.Button.YES },
			new EventListener<Messagebox.ClickEvent>() {
			public void onEvent(Messagebox.ClickEvent event) {
				if (Messagebox.Button.YES.equals(event.getButton())) {
				try {
					accountBean.resetAccountPassword(selected.getUsername());
					Messagebox.show("Account password for "
						+ selected.getUsername()
						+ " has been reset successfully.",
						"Info", Messagebox.OK,
						Messagebox.INFORMATION);
				} catch (WebException e) {
					LOGGER.error("Error resetting the account password", e);
					Messagebox.show("Error while resetting the password for account "
					        + selected.getUsername() + "\n"
						+ e.getMessage(), "Error",
						Messagebox.OK, Messagebox.ERROR);
				}
			}
		}
	});
}
This doesn't work and I get a NullPointerException because "selected" is null. The fact is that the selected variable is null only insite the EventListener, so the NPE is happening at this line of code
accountBean.resetAccountPassword(selected.getUsername());
while the selected.getUsername() in the messagebox question is working fine. It seems that the focus on the selected item from the listbox is lost after I press YES or NO from the Messagebox question. How can I make sure that this behavior does not happen? Thanks in adavance, LM
delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2012-06-06 01:29:44 +0800

samchuang gravatar image samchuang
4084 4

updated 2012-06-06 01:30:13 +0800

hi

is't possible to provide a runnable sample code, it will help debug

If you can't provide runnable sample code, I am guessing:

1. in doReset method, when MVVM invoke it, it only shows Messagebox, finish command
2. when you click messagebox button, it execute Messagebox event, but now, not invoked from MVVM, MVVM command finished, so selected may be null or not

you could try to add local Final field in doReset

public void doReset() {
final Object selededItem = xxx;
...
}

not sure if it will work or not

link publish delete flag offensive edit

answered 2012-06-06 06:49:13 +0800

samchuang gravatar image samchuang
4084 4

after study MVVM doc, I found one link may helper, refer to Show Dialog

link publish delete flag offensive edit

answered 2012-06-06 08:48:28 +0800

lucamerolla gravatar image lucamerolla
66

Hi,

thanks for the link, I have used it to refactor the code and use a new modal window instead of using the Messagebox.
However, the problem was related to the fact that I was notifying changes to selected and accounts (list of entities).

Now for example I have changed doReset like this:

@Command @NotifyChange({ "messageBox", "messageBoxEvent" })
public void doReset()

And then the modal window, on the OK button, it fires this command:

@Command @NotifyChange({ "accounts", "selected", "messageBox", "messageBoxEvent" })
public void doConfirmOk() {
if ("reset".equalsIgnoreCase(messageBoxEvent)) {
			resetPassword();
...

In this way the selected item and the messagebox are working correctly.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2012-06-04 09:45:12 +0800

Seen: 505 times

Last updated: Jun 06 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More