Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

application-architecture.asciidoc 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. ---
  2. title: Building the UI
  3. order: 2
  4. layout: page
  5. ---
  6. [[application.architecture]]
  7. = Building the UI
  8. Vaadin 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("Hello!"));
  25. // Layout inside layout
  26. HorizontalLayout hor = new HorizontalLayout();
  27. hor.setSizeFull(); // Use all available space
  28. // Couple of horizontally laid out components
  29. Tree tree = new Tree("My Tree",
  30. TreeExample.createTreeContent());
  31. hor.addComponent(tree);
  32. Table table = new Table("My Table",
  33. TableExample.generateContent());
  34. table.setSizeFull();
  35. hor.addComponent(table);
  36. hor.setExpandRatio(table, 1); // Expand to fill
  37. content.addComponent(hor);
  38. content.setExpandRatio(hor, 1); // Expand to fill
  39. }
  40. }
  41. ----
  42. The component hierarchy could be illustrated with a tree as follows:
  43. ----
  44. UI
  45. `-- VerticalLayout
  46. |-- Label
  47. `-- HorizontalLayout
  48. |-- Tree
  49. `-- Table
  50. ----
  51. The result is shown in <<figure.application.architecture.example>>.
  52. [[figure.application.architecture.example]]
  53. .Simple hierarchical UI
  54. image::img/ui-architecture-hierarchical.png[width=70%, scaledwidth=90%]
  55. Instead of building the layout in Java, you can also use a declarative design,
  56. as described later in
  57. <<dummy/../../../framework/application/application-declarative#application.declarative,"Designing
  58. UIs Declaratively">>. The examples given for the declarative layouts give
  59. exactly the same UI layout as built from the components above.
  60. The built-in components are described in
  61. <<dummy/../../../framework/components/components-overview.asciidoc#components.overview,"User
  62. Interface Components">> and the layout components in
  63. <<dummy/../../../framework/layout/layout-overview.asciidoc#layout.overview,"Managing
  64. Layout">>.
  65. The example application described above just is, it does not do anything. User
  66. interaction is handled with event listeners, as described a bit later in
  67. <<dummy/../../../framework/application/application-events#application.events,"Handling
  68. Events with Listeners">>.
  69. [[application.architecture.architecture]]
  70. == Application Architecture
  71. Once your application grows beyond a dozen or so lines, which is usually quite
  72. soon, you need to start considering the application architecture more closely.
  73. You are free to use any object-oriented techniques available in Java to organize
  74. your code in methods, classes, packages, and libraries. An architecture defines
  75. how these modules communicate together and what sort of dependencies they have
  76. between them. It also defines the scope of the application. The scope of this
  77. book, however, only gives a possibility to mention some of the most common
  78. architectural patterns in Vaadin applications.
  79. The subsequent sections describe some basic application patterns. For more
  80. information about common architectures, see
  81. <<dummy/../../../framework/advanced/advanced-architecture#advanced.architecture,"Advanced
  82. Application Architectures">>, which discusses layered architectures, the
  83. Model-View-Presenter (MVP) pattern, and so forth.
  84. ifdef::web[]
  85. The
  86. <<dummy/../../../framework/advanced/advanced-global#advanced.global,"Accessing
  87. Session-Global Data">> discusses the problem of passing essentially global
  88. references around, a common problem which is also visited in
  89. <<application.architecture.accessing>>.
  90. endif::web[]
  91. [[application.architecture.composition]]
  92. == Compositing Components
  93. User interfaces typically contain many user interface components in a layout
  94. hierarchy. Vaadin provides many layout components for laying contained
  95. components vertically, horizontally, in a grid, and in many other ways. You can
  96. extend layout components to create composite components.
  97. [source, java]
  98. ----
  99. class MyView extends VerticalLayout {
  100. TextField entry = new TextField("Enter this");
  101. Label display = new Label("See this");
  102. Button click = new Button("Click This");
  103. public MyView() {
  104. addComponent(entry);
  105. addComponent(display);
  106. addComponent(click);
  107. // Configure it a bit
  108. setSizeFull();
  109. addStyleName("myview");
  110. }
  111. }
  112. // Use it
  113. Layout myview = new MyView();
  114. ----
  115. This composition pattern is especially supported for creating forms, as
  116. described in
  117. <<dummy/../../../framework/datamodel/datamodel-itembinding#datamodel.itembinding.formclass,"Binding
  118. Member Fields">>.
  119. While extending layouts is an easy way to make component composition, it is a
  120. good practice to encapsulate implementation details, such as the exact layout
  121. component used. Otherwise, the users of such a composite could begin to rely on
  122. such implementation details, which would make changes harder. For this purpose,
  123. Vaadin has a special [classname]#CustomComponent# wrapper, which hides the
  124. content representation.
  125. [source, java]
  126. ----
  127. class MyView extends CustomComponent {
  128. TextField entry = new TextField("Enter this");
  129. Label display = new Label("See this");
  130. Button click = new Button("Click This");
  131. public MyView() {
  132. Layout layout = new VerticalLayout();
  133. layout.addComponent(entry);
  134. layout.addComponent(display);
  135. layout.addComponent(click);
  136. setCompositionRoot(layout);
  137. setSizeFull();
  138. }
  139. }
  140. // Use it
  141. MyView myview = new MyView();
  142. ----
  143. For a more detailed description of the [classname]#CustomComponent#, see
  144. <<dummy/../../../framework/components/components-customcomponent#components.customcomponent,"Composition
  145. with CustomComponent">>.
  146. [[application.architecture.navigation]]
  147. == View Navigation
  148. While the most simple applications have just a single __view__ (or __screen__),
  149. perhaps most have many. Even in a single view, you often want to have sub-views,
  150. for example to display different content.
  151. <<figure.application.architecture.navigation>> illustrates a typical navigation
  152. between different top-level views of an application, and a main view with
  153. sub-views.
  154. [[figure.application.architecture.navigation]]
  155. .Navigation Between Views
  156. image::img/view-navigation-hi.png[]
  157. The [classname]#Navigator# described in
  158. <<dummy/../../../framework/advanced/advanced-navigator#advanced.navigator,"Navigating
  159. in an Application">> is a view manager that provides a flexible way to navigate
  160. between views and sub-views, while managing the URI fragment in the page URL to
  161. allow bookmarking, linking, and going back in browser history.
  162. Often Vaadin application views are part of something bigger. In such cases, you
  163. may need to integrate the Vaadin applications with the other website. You can
  164. use the embedding techniques described in
  165. <<dummy/../../../framework/advanced/advanced-embedding#advanced.embedding,"Embedding
  166. UIs in Web Pages">>.
  167. [[application.architecture.accessing]]
  168. == Accessing UI, Page, Session, and Service
  169. You can get the UI and the page to which a component is attached to with
  170. [methodname]#getUI()# and [methodname]#getPage()#.
  171. However, the values are [literal]#++null++# until the component is attached to
  172. the UI, and typically, when you need it in constructors, it is not. It is
  173. therefore preferable to access the current UI, page, session, and service
  174. objects from anywhere in the application using the static
  175. [methodname]#getCurrent()# methods in the respective [classname]#UI#,
  176. [classname]#Page#, [classname]#VaadinSession#, and [classname]#VaadinService#
  177. classes.
  178. [source, java]
  179. ----
  180. // Set the default locale of the UI
  181. UI.getCurrent().setLocale(new Locale("en"));
  182. // Set the page title (window or tab caption)
  183. Page.getCurrent().setTitle("My Page");
  184. // Set a session attribute
  185. VaadinSession.getCurrent().setAttribute("myattrib", "hello");
  186. // Access the HTTP service parameters
  187. File baseDir = VaadinService.getCurrent().getBaseDirectory();
  188. ----
  189. You can get the page and the session also from a [classname]#UI# with
  190. [methodname]#getPage()# and [methodname]#getSession()# and the service from
  191. [classname]#VaadinSession# with [methodname]#getService()#.
  192. The static methods use the built-in ThreadLocal support in the classes.
  193. ifdef::web[]
  194. The pattern is described in
  195. <<dummy/../../../framework/advanced/advanced-global#advanced.global.threadlocal,"ThreadLocal
  196. Pattern">>.
  197. endif::web[]