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.

application-architecture.asciidoc 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. ---
  2. title: Building the UI
  3. order: 2
  4. layout: page
  5. ---
  6. [[application.architecture]]
  7. = Building the UI
  8. *_This section has not yet been updated to Vaadin Framework 8_*
  9. Vaadin Framework user interfaces are built hierarchically from components, so that the
  10. leaf components are contained within layout components and other component
  11. containers. Building the hierarchy starts from the top (or bottom - whichever
  12. way you like to think about it), from the [classname]#UI# class of the
  13. application. You normally set a layout component as the content of the UI and
  14. fill it with other components.
  15. [source, java]
  16. ----
  17. public class MyHierarchicalUI extends UI {
  18. @Override
  19. protected void init(VaadinRequest request) {
  20. // The root of the component hierarchy
  21. VerticalLayout content = new VerticalLayout();
  22. content.setSizeFull(); // Use entire window
  23. setContent(content); // Attach to the UI
  24. // Add some component
  25. content.addComponent(new Label("<b>Hello!</b> - How are you?",
  26. ContentMode.HTML));
  27. Grid<Person> grid = new Grid<>();
  28. grid.setCaption("My Grid");
  29. grid.setItems(GridExample.generateContent());
  30. grid.setSizeFull();
  31. content.addComponent(grid);
  32. content.setExpandRatio(grid, 1); // Expand to fill
  33. }
  34. }
  35. ----
  36. The component hierarchy is illustrated in <<figure.application.architecture.schematic>>.
  37. [[figure.application.architecture.schematic]]
  38. .Schematic diagram of the UI
  39. image::img/ui-schematic-hi.png[width=80%, scaledwidth=100%]
  40. The actual UI is shown in <<figure.application.architecture.example>>.
  41. [[figure.application.architecture.example]]
  42. .Simple hierarchical UI
  43. image::img/ui-architecture-hierarchical.png[width=70%, scaledwidth=90%]
  44. Instead of building the layout in Java, you can also use a declarative design, as described later in <<dummy/../../../framework/application/application-declarative#application.declarative,"Designing UIs Declaratively">>.
  45. The examples given for the declarative layouts give exactly the same UI layout as built from the components above.
  46. The easiest way to create declarative designs is to use Vaadin Designer.
  47. The built-in components are described in
  48. <<dummy/../../../framework/components/components-overview.asciidoc#components.overview,"User
  49. Interface Components">> and the layout components in
  50. <<dummy/../../../framework/layout/layout-overview.asciidoc#layout.overview,"Managing
  51. Layout">>.
  52. The example application described above just is, it does not do anything. User
  53. interaction is handled with event listeners, as described a bit later in
  54. <<dummy/../../../framework/application/application-events#application.events,"Handling
  55. Events with Listeners">>.
  56. [[application.architecture.architecture]]
  57. == Application Architecture
  58. Once your application grows beyond a dozen or so lines, which is usually quite
  59. soon, you need to start considering the application architecture more closely.
  60. You are free to use any object-oriented techniques available in Java to organize
  61. your code in methods, classes, packages, and libraries. An architecture defines
  62. how these modules communicate together and what sort of dependencies they have
  63. between them. It also defines the scope of the application. The scope of this
  64. book, however, only gives a possibility to mention some of the most common
  65. architectural patterns in Vaadin applications.
  66. The subsequent sections describe some basic application patterns. For more
  67. information about common architectures, see
  68. <<dummy/../../../framework/advanced/advanced-architecture#advanced.architecture,"Advanced
  69. Application Architectures">>, which discusses layered architectures, the
  70. Model-View-Presenter (MVP) pattern, and so forth.
  71. [[application.architecture.composition]]
  72. == Compositing Components
  73. User interfaces typically contain many user interface components in a layout
  74. hierarchy. Vaadin provides many layout components for laying contained
  75. components vertically, horizontally, in a grid, and in many other ways. You can
  76. extend layout components to create composite components.
  77. [source, java]
  78. ----
  79. class MyView extends VerticalLayout {
  80. TextField entry = new TextField("Enter this");
  81. Label display = new Label("See this");
  82. Button click = new Button("Click This");
  83. public MyView() {
  84. addComponent(entry);
  85. addComponent(display);
  86. addComponent(click);
  87. setSizeFull();
  88. addStyleName("myview");
  89. }
  90. }
  91. // Create an instance of MyView
  92. Layout myview = new MyView();
  93. ----
  94. While extending layouts is an easy way to make component composition, it is a
  95. good practice to encapsulate implementation details, such as the exact layout
  96. component used. Otherwise, the users of such a composite could begin to rely on
  97. such implementation details, which would make changes harder. For this purpose,
  98. Vaadin has a special [classname]#CustomComponent# wrapper, which hides the
  99. content representation.
  100. [source, java]
  101. ----
  102. class MyView extends CustomComponent {
  103. TextField entry = new TextField("Enter this");
  104. Label display = new Label("See this");
  105. Button click = new Button("Click This");
  106. public MyView() {
  107. Layout layout = new VerticalLayout();
  108. layout.addComponent(entry);
  109. layout.addComponent(display);
  110. layout.addComponent(click);
  111. setCompositionRoot(layout);
  112. setSizeFull();
  113. }
  114. }
  115. // Create an instance of MyView
  116. MyView myview = new MyView();
  117. ----
  118. For a more detailed description of the [classname]#CustomComponent#, see
  119. <<dummy/../../../framework/components/components-customcomponent#components.customcomponent,"Composition
  120. with CustomComponent">>.
  121. [[application.architecture.navigation]]
  122. == View Navigation
  123. While the simplest applications have just one __view__ (or __screen__), most of them often require several.
  124. Even in a single view, you often want to have sub-views,
  125. for example to display different content.
  126. <<figure.application.architecture.navigation>> illustrates a typical navigation
  127. between different top-level views of an application, and a main view with
  128. sub-views.
  129. [[figure.application.architecture.navigation]]
  130. .Navigation Between Views
  131. image::img/view-navigation-hi.png[width=80%, scaledwidth=100%]
  132. The [classname]#Navigator# described in <<dummy/../../../framework/advanced/advanced-navigator#advanced.navigator,"Navigating in an Application">> is a view manager that provides a flexible way to navigate between views and sub-views, while managing the URI fragment in the page URL to allow bookmarking, linking, and going back in the browser history.
  133. Often Vaadin application views are part of something bigger.
  134. In such cases, you may need to integrate the Vaadin applications with the other website.
  135. You can use the embedding techniques described in <<dummy/../../../framework/advanced/advanced-embedding#advanced.embedding,"Embedding UIs in Web Pages">>.
  136. [[application.architecture.accessing]]
  137. == Accessing UI, Page, Session, and Service
  138. You can get the UI and the page to which a component is attached to with
  139. [methodname]#getUI()# and [methodname]#getPage()#.
  140. However, the values are [literal]#++null++# until the component is attached to
  141. the UI, and typically, when you need it in constructors, it is not. It is
  142. therefore preferable to access the current UI, page, session, and service
  143. objects from anywhere in the application using the static
  144. [methodname]#getCurrent()# methods in the respective [classname]#UI#,
  145. [classname]#Page#, [classname]#VaadinSession#, and [classname]#VaadinService#
  146. classes.
  147. [source, java]
  148. ----
  149. // Set the default locale of the UI
  150. UI.getCurrent().setLocale(new Locale("en"));
  151. // Set the page title (window or tab caption)
  152. Page.getCurrent().setTitle("My Page");
  153. // Set a session attribute
  154. VaadinSession.getCurrent().setAttribute("myattrib", "hello");
  155. // Access the HTTP service parameters
  156. File baseDir = VaadinService.getCurrent().getBaseDirectory();
  157. ----
  158. You can get the page and the session also from a [classname]#UI# with
  159. [methodname]#getPage()# and [methodname]#getSession()# and the service from
  160. [classname]#VaadinSession# with [methodname]#getService()#.
  161. The static methods use the built-in ThreadLocal support in the classes.
  162. ifdef::web[]
  163. The pattern is described in <<dummy/../../../framework/advanced/advanced-global#advanced.global.threadlocal,"ThreadLocal Pattern">>.
  164. endif::web[]