Conditional Navigation in Vaadin?

Hello everyone,

we have been using the Vaadin Navigator API for a long time now. It’s nice, clean and simple, however we would like our views to be able to prevent navigating into them or away from them, depending on their current internal state. For example, if a user edits some text field and the new value is invalid, we don’t want to allow him to navigate away from the view and leave the text field in an invalid state (the click actions of our menu bar that trigger the actual navigation are not within the control of our views). Instead, if he tries to navigate away, we would like to display an error message (“hey, fix that before you leave!”). We would like to be able to implement a callback method “boolean canEnterView(View previous)” and “boolean canExitView(View next)” in each view that allows the view to specify if entering / exiting the view is currently allowed or not.

However, since there is no “View getView(String navigationState)” method in Vaadin’s Navigator class, and the corresponding collections that hold the providers are private (and not protected which prevents effective subclassing), we cannot write a wrapper Navigator (or subclass) that would respect these callback methods mentioned earlier.

Any suggestions on how to do this? I’m pretty sure that we are not the first people to encounter this problem.

Thanks,

Martin

EDIT: By coincidence, I’ve stumbled across the solution. It’s outlined here:
https://vaadin.com/wiki/-/wiki/Main/Access+control+for+views
. The key is that each ViewChangeListener can veto a view change. This is a little counter-intuitive because the usual semantics of a listener is that it merely reacts upon a change, but has no way to prevent it from happening. But we’ll do it that way since it seems to be the “Vaadin Style” to do it.

As you already figured out, ViewChangeListeners are where to put this kind of logic. And you are correct, the API could be a lot clearer… I’ll file an improvement ticket about it.

Thank you :slight_smile: Also, while you are at it, please at least link the tutorial from above in the Book of Vaadin chapter about the navigator - the listener is mentioned in the book, but not its particular role in deciding if a view transition should be allowed or not.

Done and done. Thank you for the improvements :slight_smile: