Navigator + ConfirmDialog

I have a problem with my navigator menu + a confirmation dialog (I use the ConfirmDialog addon for vaadin 7 beta 11 with some little customizations to get it run, but everything works fine)

I want to ask for changes made in the view before i leave to another view. As simple as it sounds, here is my code:


private boolean confirmed;
...
vcl = new ViewChangeListener() {
	public boolean beforeViewChange(ViewChangeEvent event) {
		// return true if 'Yes'
		// return false if 'No':
		return openConfirmDialog();
	}
	public void afterViewChange(ViewChangeEvent event) {
		Notification.show("View changed!");
	}
};
navigator.addViewChangeListener(vcl);


public boolean openConfirmDialog() {
	confirmed = false;
	ConfirmDialog.show(getUI(),
			"<b>Continue?</b>", 
			"Wanna continue?" , 
			"Yes", 
			"No",
			new ConfirmDialog.Listener() {
				public void onClose(ConfirmDialog dialog) {
					if(dialog.isConfirmed()) {
						confirmed = true;
					}
					else {
						dialog.close();
					}
				}
	});
	return confirmed;
}

The problem is, that beforeViewChange() obviously doesnt wait for the user to confirm an option (openConfirmDialog()) and always returns false. Is there a way to do what I need with Navigator + a confirmation. How do I achieve so that the boolean return in openConfirmDialog “waits” for the user input?

Thanks in advance

I think better design than “waiting” for the user input is to block the view change when you display the confirmation dialog, by returning false from beforeViewChange() method after you’ve shown the ConfirmationDialog. This will keep you in the current view while question is answered. Then, in the ConfirmationDialog.Listener implementation you will ask the Navigator to re-initiate the navigation again if user wants to change the view. You can get reference to the navigator from the ViewChangeEvent.

yes, thanks. thats something ive thought about afterwards, but how do i “re-initiate” the same navigation-event via the event, because the event previously already returned false?

One option is for example to create some conditional logic that sets “view changing allowed” property / flag to true. Then in your beforeViewChange you check if condition has been applied to bypass displaying the dialog, if not show the dialog.

  1. Check if there is unsaved modification → Display dialog
  2. If user answers that he wants to change view anyway, setup a flag to say that view change is allowed → renavigate
  3. Check if view changing is allowed despite save status

Another option would be to set navigator parameter that allowed navigation and during navigation event handling see if parameter is set and allow changing view, otherwise display dialog. This of course adds the visible parameter to URL which may not be nice.

There are probably many other options as well.