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.0KB

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