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 7.1KB

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