View change confirmations

The Navigator API provides ways to prevent the user from navigating away from a view in some cases, usually when the view has some unsaved changes. We’ll make a simple example that does just that (and only that).

We’ll create our simple SettingsView later, because it has the actual meat of this example. Let’s set up the basic stuff first, our UI and our MainView.

UI:

Java

  1. import com.vaadin.navigator.Navigator;
  2. import com.vaadin.navigator.ViewChangeListener;
  3. import com.vaadin.server.VaadinRequest;
  4. import com.vaadin.ui.UI;
  5. public class NavigationtestUI extends UI {
  6. @Override
  7. public void init(VaadinRequest request) {
  8. // Create Navigator, make it control the ViewDisplay
  9. Navigator navigator = new Navigator(this, this);
  10. // no fragment for main view
  11. navigator.addView(MainView.NAME, new MainView(navigator));
  12. // #settings
  13. navigator.addView(SettingsView.NAME, new SettingsView(navigator));
  14. }
  15. }

Minimalistic. The only thing to notice is that we pass the Navigator to the SettingsView, so that it can attach a listener and trigger navigation. More on that when we actually create the SettingsView.

Let’s do the MainView:

Java

  1. import com.vaadin.navigator.View;
  2. import com.vaadin.server.ExternalResource;
  3. import com.vaadin.ui.Link;
  4. import com.vaadin.ui.Panel;
  5. public class MainView extends Panel implements View {
  6. public static final String NAME = "";
  7. public MainView(final Navigator navigator) {
  8. Link lnk = new Link("Settings", new ExternalResource("#!"
  9. + SettingsView.NAME));
  10. setContent(lnk);
  11. }
  12. @Override
  13. public void enter(ViewChangeEvent event) {
  14. }
  15. }

Yeah, really nothing to see here - we just create this so we can navigate back and forth when trying it out.

Now let’s do the SettingsView, which has some more things going on in order to make it fairly complete:

Java

  1. import java.util.Date;
  2. import com.vaadin.data.Property.ValueChangeEvent;
  3. import com.vaadin.data.Property.ValueChangeListener;
  4. import com.vaadin.data.util.ObjectProperty;
  5. import com.vaadin.navigator.Navigator;
  6. import com.vaadin.navigator.View;
  7. import com.vaadin.navigator.ViewChangeListener;
  8. import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
  9. import com.vaadin.ui.Button;
  10. import com.vaadin.ui.Button.ClickEvent;
  11. import com.vaadin.ui.DateField;
  12. import com.vaadin.ui.InlineDateField;
  13. import com.vaadin.ui.Layout;
  14. import com.vaadin.ui.Notification;
  15. import com.vaadin.ui.Notification.Type;
  16. import com.vaadin.ui.Panel;
  17. import com.vaadin.ui.VerticalLayout;
  18. import com.vaadin.ui.themes.Reindeer;
  19. public class SettingsView extends Panel implements View {
  20. public static String NAME = "settings";
  21. Navigator navigator;
  22. DateField date;
  23. Button apply;
  24. Button cancel;
  25. String pendingViewAndParameters = null;
  26. public SettingsView(final Navigator navigator) {
  27. this.navigator = navigator;
  28. Layout layout = new VerticalLayout();
  29. date = new InlineDateField("Birth date");
  30. date.setImmediate(true);
  31. layout.addComponent(date);
  32. // pretend we have a datasource:
  33. date.setPropertyDataSource(new ObjectProperty<Date>(new Date()));
  34. date.setBuffered(true);
  35. // show buttons when date is changed
  36. date.addValueChangeListener(new ValueChangeListener() {
  37. public void valueChange(ValueChangeEvent event) {
  38. hideOrShowButtons();
  39. pendingViewAndParameters = null;
  40. }
  41. });
  42. // commit the TextField changes when "Save" is clicked
  43. apply = new Button("Apply", new Button.ClickListener() {
  44. public void buttonClick(ClickEvent event) {
  45. date.commit();
  46. hideOrShowButtons();
  47. processPendingView();
  48. }
  49. });
  50. layout.addComponent(apply);
  51. // Discard the TextField changes when "Cancel" is clicked
  52. cancel = new Button("Cancel", new Button.ClickListener() {
  53. public void buttonClick(ClickEvent event) {
  54. date.discard();
  55. hideOrShowButtons();
  56. processPendingView();
  57. }
  58. });
  59. cancel.setStyleName(Reindeer.BUTTON_LINK);
  60. layout.addComponent(cancel);
  61. // attach a listener so that we'll get asked isViewChangeAllowed?
  62. navigator.addViewChangeListener(new ViewChangeListener() {
  63. public boolean beforeViewChange(ViewChangeEvent event) {
  64. if (event.getOldView() == SettingsView.this
  65. && date.isModified()) {
  66. // save the View where the user intended to go
  67. pendingViewAndParameters = event.getViewName();
  68. if (event.getParameters() != null) {
  69. pendingViewAndParameters += "/";
  70. pendingViewAndParameters += event
  71. .getParameters();
  72. }
  73. // Prompt the user to save or cancel if the name is changed
  74. Notification.show("Please apply or cancel your changes",
  75. Type.WARNING_MESSAGE);
  76. return false;
  77. } else {
  78. return true;
  79. }
  80. }
  81. public void afterViewChange(ViewChangeEvent event) {
  82. pendingViewAndParameters = null;
  83. }
  84. });
  85. setContent(layout);
  86. }
  87. // Hide or show buttons depending on whether date is modified or not
  88. private void hideOrShowButtons() {
  89. apply.setVisible(date.isModified());
  90. cancel.setVisible(date.isModified());
  91. }
  92. // if there is a pending view change, do it now
  93. private void processPendingView() {
  94. if (pendingViewAndParameters != null) {
  95. navigator.navigateTo(pendingViewAndParameters);
  96. pendingViewAndParameters = null;
  97. }
  98. }
  99. @Override
  100. public void enter(ViewChangeEvent event) {
  101. hideOrShowButtons();
  102. }
  103. }

First we set up a DateField with buffering and a (dummy) datasource to make this work more as a real application would. With buffering on, the value (date in this case) can be changed, but it will not be written to the datasource before we commit(), which is what the Save -button does. The Cancel -button does discard() on the DateField, which returns the field to its unmodified state.

The buttons do not need to be shown if nothing has changed, so we add a ValueChangeListener to the DateField for that purpose.

But the main thing that we’re trying to demonstrate here happens in the ViewChangeListener that we attach to the Navigator. There, if we’re about to change away from our settings and the date is changed but not saved, we’ll make note of where the user wanted to go, but cancel that navigation and prompt the user to save or cancel the changes.

When the user saves or cancels changes, we also check if the user previously tried to navigate away form the page, and sends him on his way if that is the case.

That is basically all there is to this. You’ll notice we try to carefully clear or set the ‘pending view’ and hide/show the buttons at the right places to make the user happy, other than that this is pretty straightforward.