You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ViewChangeConfirmations.asciidoc 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. ---
  2. title: View Change Confirmations
  3. order: 80
  4. layout: page
  5. ---
  6. [[view-change-confirmations]]
  7. = View change confirmations
  8. The `Navigator` API provides ways to prevent the user from navigating away
  9. from a view in some cases, usually when the view has some unsaved
  10. changes. We'll make a simple example that does just that (and only
  11. that).
  12. We'll create our simple `SettingsView` later, because it has the actual
  13. meat of this example. Let's set up the basic stuff first, our UI and our
  14. `MainView`.
  15. UI:
  16. [source,java]
  17. ....
  18. import com.vaadin.navigator.Navigator;
  19. import com.vaadin.navigator.ViewChangeListener;
  20. import com.vaadin.server.VaadinRequest;
  21. import com.vaadin.ui.UI;
  22. public class NavigationtestUI extends UI {
  23. @Override
  24. public void init(VaadinRequest request) {
  25. // Create Navigator, make it control the ViewDisplay
  26. Navigator navigator = new Navigator(this, this);
  27. // no fragment for main view
  28. navigator.addView(MainView.NAME, new MainView(navigator));
  29. // #settings
  30. navigator.addView(SettingsView.NAME, new SettingsView(navigator));
  31. }
  32. }
  33. ....
  34. Minimalistic. The only thing to notice is that we pass the `Navigator` to
  35. the `SettingsView`, so that it can attach a listener and trigger
  36. navigation. More on that when we actually create the `SettingsView`.
  37. Let's do the `MainView`:
  38. [source,java]
  39. ....
  40. import com.vaadin.navigator.View;
  41. import com.vaadin.server.ExternalResource;
  42. import com.vaadin.ui.Link;
  43. import com.vaadin.ui.Panel;
  44. public class MainView extends Panel implements View {
  45. public static final String NAME = "";
  46. public MainView(final Navigator navigator) {
  47. Link lnk = new Link("Settings", new ExternalResource("#!"
  48. + SettingsView.NAME));
  49. setContent(lnk);
  50. }
  51. @Override
  52. public void enter(ViewChangeEvent event) {
  53. }
  54. }
  55. ....
  56. Yeah, really nothing to see here - we just create this so we can
  57. navigate back and forth when trying it out.
  58. Now let's do the SettingsView, which has some more things going on in
  59. order to make it fairly complete:
  60. [source,java]
  61. ....
  62. import java.util.Date;
  63. import com.vaadin.data.Property.ValueChangeEvent;
  64. import com.vaadin.data.Property.ValueChangeListener;
  65. import com.vaadin.data.util.ObjectProperty;
  66. import com.vaadin.navigator.Navigator;
  67. import com.vaadin.navigator.View;
  68. import com.vaadin.navigator.ViewChangeListener;
  69. import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
  70. import com.vaadin.ui.Button;
  71. import com.vaadin.ui.Button.ClickEvent;
  72. import com.vaadin.ui.DateField;
  73. import com.vaadin.ui.InlineDateField;
  74. import com.vaadin.ui.Layout;
  75. import com.vaadin.ui.Notification;
  76. import com.vaadin.ui.Notification.Type;
  77. import com.vaadin.ui.Panel;
  78. import com.vaadin.ui.VerticalLayout;
  79. import com.vaadin.ui.themes.Reindeer;
  80. public class SettingsView extends Panel implements View {
  81. public static String NAME = "settings";
  82. Navigator navigator;
  83. DateField date;
  84. Button apply;
  85. Button cancel;
  86. String pendingViewAndParameters = null;
  87. public SettingsView(final Navigator navigator) {
  88. this.navigator = navigator;
  89. Layout layout = new VerticalLayout();
  90. date = new InlineDateField("Birth date");
  91. date.setImmediate(true);
  92. layout.addComponent(date);
  93. // pretend we have a datasource:
  94. date.setPropertyDataSource(new ObjectProperty<Date>(new Date()));
  95. date.setBuffered(true);
  96. // show buttons when date is changed
  97. date.addValueChangeListener(new ValueChangeListener() {
  98. public void valueChange(ValueChangeEvent event) {
  99. hideOrShowButtons();
  100. pendingViewAndParameters = null;
  101. }
  102. });
  103. // commit the TextField changes when "Save" is clicked
  104. apply = new Button("Apply", new Button.ClickListener() {
  105. public void buttonClick(ClickEvent event) {
  106. date.commit();
  107. hideOrShowButtons();
  108. processPendingView();
  109. }
  110. });
  111. layout.addComponent(apply);
  112. // Discard the TextField changes when "Cancel" is clicked
  113. cancel = new Button("Cancel", new Button.ClickListener() {
  114. public void buttonClick(ClickEvent event) {
  115. date.discard();
  116. hideOrShowButtons();
  117. processPendingView();
  118. }
  119. });
  120. cancel.setStyleName(Reindeer.BUTTON_LINK);
  121. layout.addComponent(cancel);
  122. // attach a listener so that we'll get asked isViewChangeAllowed?
  123. navigator.addViewChangeListener(new ViewChangeListener() {
  124. public boolean beforeViewChange(ViewChangeEvent event) {
  125. if (event.getOldView() == SettingsView.this
  126. && date.isModified()) {
  127. // save the View where the user intended to go
  128. pendingViewAndParameters = event.getViewName();
  129. if (event.getParameters() != null) {
  130. pendingViewAndParameters += "/";
  131. pendingViewAndParameters += event
  132. .getParameters();
  133. }
  134. // Prompt the user to save or cancel if the name is changed
  135. Notification.show("Please apply or cancel your changes",
  136. Type.WARNING_MESSAGE);
  137. return false;
  138. } else {
  139. return true;
  140. }
  141. }
  142. public void afterViewChange(ViewChangeEvent event) {
  143. pendingViewAndParameters = null;
  144. }
  145. });
  146. setContent(layout);
  147. }
  148. // Hide or show buttons depending on whether date is modified or not
  149. private void hideOrShowButtons() {
  150. apply.setVisible(date.isModified());
  151. cancel.setVisible(date.isModified());
  152. }
  153. // if there is a pending view change, do it now
  154. private void processPendingView() {
  155. if (pendingViewAndParameters != null) {
  156. navigator.navigateTo(pendingViewAndParameters);
  157. pendingViewAndParameters = null;
  158. }
  159. }
  160. @Override
  161. public void enter(ViewChangeEvent event) {
  162. hideOrShowButtons();
  163. }
  164. }
  165. ....
  166. First we set up a `DateField` with buffering and a (dummy) datasource to
  167. make this work more as a real application would. With buffering on, the
  168. value (date in this case) can be changed, but it will not be written to
  169. the datasource before we `commit()`, which is what the Save -button does.
  170. The Cancel -button does `discard()` on the DateField, which returns the
  171. field to its unmodified state.
  172. The buttons do not need to be shown if nothing has changed, so we add a
  173. `ValueChangeListener` to the `DateField` for that purpose.
  174. But the main thing that we're trying to demonstrate here happens in the
  175. `ViewChangeListener` that we attach to the `Navigator`. There, if we're
  176. about to change _away_ from our settings _and_ the date is changed but
  177. _not_ saved, we'll make note of where the user wanted to go, but cancel
  178. that navigation and prompt the user to save or cancel the changes.
  179. When the user saves or cancels changes, we also check if the user
  180. previously tried to navigate away form the page, and sends him on his
  181. way if that is the case.
  182. That is basically all there is to this. You'll notice we try to
  183. carefully clear or set the 'pending view' and hide/show the buttons at
  184. the right places to make the user happy, other than that this is pretty
  185. straightforward.