Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

application-architecture.asciidoc 8.9KB

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