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.

advanced-navigator.asciidoc 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. ---
  2. title: Navigating in an Application
  3. order: 9
  4. layout: page
  5. ---
  6. [[advanced.navigator]]
  7. = Navigating in an Application
  8. Plain Vaadin applications do not have normal web page navigation as they usually
  9. run on a single page, as all Ajax applications do. Quite commonly, however,
  10. applications have different views between which the user should be able to
  11. navigate. The [classname]#Navigator# in Vaadin can be used for most cases of
  12. navigation. Views managed by the navigator automatically get a distinct URI
  13. fragment, which can be used to be able to bookmark the views and their states
  14. and to go back and forward in the browser history.
  15. [[advanced.navigator.navigating]]
  16. == Setting Up for Navigation
  17. The [classname]#Navigator# class manages a collection of __views__ that
  18. implement the [interfacename]#View# interface. The views can be either
  19. registered beforehand or acquired from a __view provider__. When registering,
  20. the views must have a name identifier and be added to a navigator with
  21. [methodname]#addView()#. You can register new views at any point. Once
  22. registered, you can navigate to them with [methodname]#navigateTo()#.
  23. [classname]#Navigator# manages navigation in a component container, which can be
  24. either a [interfacename]#ComponentContainer# (most layouts) or a
  25. [interfacename]#SingleComponentContainer# ( [classname]#UI#, [classname]#Panel#,
  26. or [classname]#Window#). The component container is managed through a
  27. [interfacename]#ViewDisplay#. Two view displays are defined:
  28. [classname]#ComponentContainerViewDisplay# and
  29. [classname]#SingleComponentContainerViewDisplay#, for the respective component
  30. container types. Normally, you can let the navigator create the view display
  31. internally, as we do in the example below, but you can also create it yourself
  32. to customize it.
  33. Let us consider the following UI with two views: start and main. Here, we define
  34. their names with enums to be typesafe. We manage the navigation with the UI
  35. class itself, which is a [interfacename]#SingleComponentContainer#.
  36. [source, java]
  37. ----
  38. public class NavigatorUI extends UI {
  39. Navigator navigator;
  40. protected static final String MAINVIEW = "main";
  41. @Override
  42. protected void init(VaadinRequest request) {
  43. getPage().setTitle("Navigation Example");
  44. // Create a navigator to control the views
  45. navigator = new Navigator(this, this);
  46. // Create and register the views
  47. navigator.addView("", new StartView());
  48. navigator.addView(MAINVIEW, new MainView());
  49. }
  50. }
  51. ----
  52. The [classname]#Navigator# automatically sets the URI fragment of the
  53. application URL. It also registers a [interfacename]#URIFragmentChangedListener#
  54. in the page
  55. ifdef::web[]
  56. (see <<dummy/../../../framework/advanced/advanced-urifu#advanced.urifu,"Managing
  57. URI
  58. Fragments">>)
  59. endif::web[]
  60. to show the view identified by the URI fragment if entered or navigated to in
  61. the browser. This also enables browser navigation history in the application.
  62. [[advanced.navigator.navigating.viewprovider]]
  63. === View Providers
  64. You can create new views dynamically using a __view provider__ that implements
  65. the [interfacename]#ViewProvider# interface. A provider is registered in
  66. [classname]#Navigator# with [methodname]#addProvider()#.
  67. The [methodname]#ClassBasedViewProvider# is a view provider that can dynamically
  68. create new instances of a specified view class based on the view name.
  69. The [methodname]#StaticViewProvider# returns an existing view instance based on
  70. the view name. The [methodname]#addView()# in [classname]#Navigator# is actually
  71. just a shorthand for creating a static view provider for each registered view.
  72. [[advanced.navigator.navigating.viewchangelistener]]
  73. === View Change Listeners
  74. You can handle view changes also by implementing a
  75. [interfacename]#ViewChangeListener# and adding it to a [classname]#Navigator#.
  76. When a view change occurs, a listener receives a [classname]#ViewChangeEvent#
  77. object, which has references to the old and the activated view, the name of the
  78. activated view, as well as the fragment parameters.
  79. [[advanced.navigator.view]]
  80. == Implementing a View
  81. Views can be any objects that implement the [interfacename]#View# interface.
  82. When the [methodname]#navigateTo()# is called for the navigator, or the
  83. application is opened with the URI fragment associated with the view, the
  84. navigator switches to the view and calls its [methodname]#enter()# method.
  85. To continue with the example, consider the following simple start view that just
  86. lets the user to navigate to the main view. It only pops up a notification when
  87. the user navigates to it and displays the navigation button.
  88. [source, java]
  89. ----
  90. /** A start view for navigating to the main view */
  91. public class StartView extends VerticalLayout implements View {
  92. public StartView() {
  93. setSizeFull();
  94. Button button = new Button("Go to Main View",
  95. new Button.ClickListener() {
  96. @Override
  97. public void buttonClick(ClickEvent event) {
  98. navigator.navigateTo(MAINVIEW);
  99. }
  100. });
  101. addComponent(button);
  102. setComponentAlignment(button, Alignment.MIDDLE_CENTER);
  103. }
  104. @Override
  105. public void enter(ViewChangeEvent event) {
  106. Notification.show("Welcome to the Animal Farm");
  107. }
  108. }
  109. ----
  110. You can initialize the view content in the constructor, as was done in the
  111. example above, or in the [methodname]#enter()# method. The advantage with the
  112. latter method is that the view is attached to the view container as well as to
  113. the UI at that time, which is not the case in the constructor.
  114. [[advanced.navigator.urifragment]]
  115. == Handling URI Fragment Path
  116. URI fragment part of a URL is the part after a hash [literal]#++#++# character.
  117. Is used for within-UI URLs, because it is the only part of the URL that can be
  118. changed with JavaScript from within a page without reloading the page. The URLs
  119. with URI fragments can be used for hyperlinking and bookmarking, as well as
  120. browser history, just like any other URLs. In addition, an exclamation mark
  121. [literal]#++#!++# after the hash marks that the page is a stateful AJAX page,
  122. which can be crawled by search engines. Crawling requires that the application
  123. also responds to special URLs to get the searchable content. URI fragments are
  124. managed by [classname]#Page#, which provides a low-level API.
  125. URI fragments can be used with [classname]#Navigator# in two ways: for
  126. navigating to a view and to a state within a view. The URI fragment accepted by
  127. [methodname]#navigateTo()# can have the view name at the root, followed by
  128. fragment parameters after a slash (" [literal]#++/++#"). These parameters are
  129. passed to the [methodname]#enter()# method in the [interfacename]#View#.
  130. In the following example, we implement within-view navigation. Here we use the
  131. following declarative design for the view:
  132. [source, html]
  133. ----
  134. <vaadin-vertical-layout size-full>
  135. <vaadin-horizontal-layout size-full :expand>
  136. <vaadin-panel caption="List of Equals" height-full width-auto>
  137. <vaadin-vertical-layout _id="menuContent" width-auto margin/>
  138. </vaadin-panel>
  139. <vaadin-panel _id="equalPanel" caption="An Equal" size-full :expand/>
  140. </vaadin-horizontal-layout>
  141. <vaadin-button _id="logout">Logout</vaadin-button>
  142. </vaadin-vertical-layout>
  143. ----
  144. The view's logic code would be as follows:
  145. [source, java]
  146. ----
  147. /** Main view with a menu (with declarative layout design) */
  148. @DesignRoot
  149. public class MainView extends VerticalLayout implements View {
  150. // Menu navigation button listener
  151. class ButtonListener implements Button.ClickListener {
  152. String menuitem;
  153. public ButtonListener(String menuitem) {
  154. this.menuitem = menuitem;
  155. }
  156. @Override
  157. public void buttonClick(ClickEvent event) {
  158. // Navigate to a specific state
  159. navigator.navigateTo(MAINVIEW + "/" + menuitem);
  160. }
  161. }
  162. VerticalLayout menuContent;
  163. Panel equalPanel;
  164. Button logout;
  165. public MainView() {
  166. Design.read(this);
  167. menuContent.addComponent(new Button("Pig",
  168. new ButtonListener("pig")));
  169. menuContent.addComponent(new Button("Cat",
  170. new ButtonListener("cat")));
  171. menuContent.addComponent(new Button("Dog",
  172. new ButtonListener("dog")));
  173. menuContent.addComponent(new Button("Reindeer",
  174. new ButtonListener("reindeer")));
  175. menuContent.addComponent(new Button("Penguin",
  176. new ButtonListener("penguin")));
  177. menuContent.addComponent(new Button("Sheep",
  178. new ButtonListener("sheep")));
  179. // Allow going back to the start
  180. logout.addClickListener(event -> // Java 8
  181. navigator.navigateTo(""));
  182. }
  183. @DesignRoot
  184. class AnimalViewer extends VerticalLayout {
  185. Label watching;
  186. Embedded pic;
  187. Label back;
  188. public AnimalViewer(String animal) {
  189. Design.read(this);
  190. watching.setValue("You are currently watching a " +
  191. animal);
  192. pic.setSource(new ThemeResource(
  193. "img/" + animal + "-128px.png"));
  194. back.setValue("and " + animal +
  195. " is watching you back");
  196. }
  197. }
  198. @Override
  199. public void enter(ViewChangeEvent event) {
  200. if (event.getParameters() == null
  201. || event.getParameters().isEmpty()) {
  202. equalPanel.setContent(
  203. new Label("Nothing to see here, " +
  204. "just pass along."));
  205. return;
  206. } else
  207. equalPanel.setContent(new AnimalViewer(
  208. event.getParameters()));
  209. }
  210. }
  211. ----
  212. The animal sub-view would have the following declarative design:
  213. [source, html]
  214. ----
  215. <vaadin-vertical-layout size-full>
  216. <vaadin-label _id="watching" size-auto :middle :center/>
  217. <vaadin-embedded _id="pic" :middle :center :expand/>
  218. <vaadin-label _id="back" size-auto :middle :center/>
  219. </vaadin-vertical-layout>
  220. ----
  221. The main view is shown in <<figure.advanced.navigator.mainview>>. At this point,
  222. the URL would be [literal]#++http://localhost:8080/myapp#!main/reindeer++#.
  223. [[figure.advanced.navigator.mainview]]
  224. .Navigator Main View
  225. image::img/navigator-mainview.png[]