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-declarative.asciidoc 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. ---
  2. title: Designing UIs Declaratively
  3. order: 3
  4. layout: page
  5. ---
  6. [[application.declarative]]
  7. = Designing UIs Declaratively
  8. Declarative definition of composites and even entire UIs makes it easy for
  9. developers and especially graphical designers to work on visual designs without
  10. any coding. Designs can be modified even while the application is running, as
  11. can be the associated themes. A design is a representation of a component
  12. hierarcy, which can be accessed from Java code to implement dynamic UI logic, as
  13. well as data binding.
  14. For example, considering the following layout in Java:
  15. [source, java]
  16. ----
  17. VerticalLayout vertical = new VerticalLayout ();
  18. vertical.addComponent(new TextField("Name"));
  19. vertical.addComponent(new TextField("Street address"));
  20. vertical.addComponent(new TextField("Postal code"));
  21. layout.addComponent(vertical);
  22. ----
  23. You could define it declaratively with the following equivalent design:
  24. [source, html]
  25. ----
  26. <vaadin-vertical-layout>
  27. <vaadin-text-field caption="Name"/>
  28. <vaadin-text-field caption="Street address"/>
  29. <vaadin-text-field caption="Postal code"/>
  30. </vaadin-vertical-layout>
  31. ----
  32. Declarative designs can be crafted by hand, but are most conveniently created
  33. with the Vaadin Designer.
  34. In the following, we first go through the syntax of the declarative design
  35. files, and then see how to use them in applications by binding them to data and
  36. handling user interaction events.
  37. [[application.declarative.syntax]]
  38. == Declarative Syntax
  39. A design is an HTML document with custom elements for representing components
  40. and their configuration. A design has a single root component inside the HTML
  41. body element. Enclosing [literal]#++<html>++#, [literal]#++<head>++#, and
  42. [literal]#++<body>++# are optional, but necessary if you need to make namespace
  43. definitions for custom components. Other regular HTML elements may not be used
  44. in the file, except inside components that specifically accept HTML content.
  45. In a design, each nested element corresponds to a Vaadin component in a
  46. component tree. Components can have explicitly given IDs to enable binding them
  47. to variables in the Java code, as well as optional attributes.
  48. [source, html]
  49. ----
  50. <!DOCTYPE html>
  51. <html>
  52. <body>
  53. <vaadin-vertical-layout size-full>
  54. <!-- Label with HTML content -->
  55. <vaadin-label><b>Hello!</b> -
  56. How are you?</vaadin-label>
  57. <vaadin-grid _id="mygrid" caption="My Grid"
  58. size-full :expand/>
  59. </vaadin-vertical-layout>
  60. </body>
  61. </html>
  62. ----
  63. The DOCTYPE is not required, neither is the [literal]#++<html>++#, or
  64. [literal]#++<body>++# elements. Nevertheless, there may only be one design root
  65. element.
  66. The above design defines the same UI layout as done earlier with Java code, and
  67. illustrated in
  68. <<application-architecture#figure.application.architecture.example,"Simple
  69. Hierarchical UI">>.
  70. [[application.declarative.elements]]
  71. == Component Elements
  72. HTML elements of the declarative syntax are directly mapped to Vaadin components according to their Java class names.
  73. The tag of a component element has a namespace prefix separated by a dash.
  74. Vaadin core components, which are defined in the [package]#com.vaadin.ui# package, have [literal]#++vaadin-++# prefix.
  75. The rest of an element tag is determined from the Java class name of the component, by making it lower-case, while adding a dash (`-`) before every previously upper-case letter as a word separator.
  76. For example, [classname]#ComboBox# component has declarative element tag [vaadinelement]#vaadin-combo-box#.
  77. [[application.declarative.elements.prefix]]
  78. === Component Prefix to Package Mapping
  79. You can use any components in a design: components extending Vaadin components,
  80. composite components, and add-on components. To do so, you need to define a
  81. mapping from an element prefix to the Java package of the component. The prefix
  82. is used as a sort of a namespace.
  83. The mappings are defined in `<meta name="package-mapping" ...>`
  84. elements in the HTML head. A [parameter]#content# attribute defines a mapping,
  85. in notation with a prefix separated from the corresponding Java package name
  86. with a colon, such as `my:com.example.myapp`.
  87. For example, consider that you have the following composite class
  88. [classname]#com.example.myapp.ExampleComponent#:
  89. [source, java]
  90. ----
  91. package com.example.myapp;
  92. public class ExampleComponent extends CustomComponent {
  93. public ExampleComponent() {
  94. setCompositionRoot(new Label("I am an example."));
  95. }
  96. }
  97. ----
  98. You would make the package prefix mapping and then use the component as follows:
  99. [subs="normal"]
  100. ----
  101. &lt;!DOCTYPE html&gt;
  102. &lt;html&gt;
  103. &lt;head&gt;
  104. **&lt;meta name="package-mapping"
  105. content="my:com.example.myapp" /&gt;**
  106. &lt;/head&gt;
  107. &lt;body&gt;
  108. &lt;vaadin-vertical-layout&gt;
  109. &lt;vaadin-label&gt;&lt;b&gt;Hello!&lt;/b&gt; -
  110. How are you?&lt;/vaadin-label&gt;
  111. &lt;!-- Use it here --&gt;
  112. **&lt;my-example-component/&gt;**
  113. &lt;/vaadin-vertical-layout&gt;
  114. &lt;/body&gt;
  115. &lt;/html&gt;
  116. ----
  117. [[application.declarative.elements.inline]]
  118. === Inline Content and Data
  119. The element content can be used for certain default attributes, such as a button
  120. caption. For example:
  121. [source, html]
  122. ----
  123. <vaadin-button><b>OK</b></vaadin-button>
  124. ----
  125. Some components, such as selection components, allow defining inline data within
  126. the element. For example:
  127. [source, html]
  128. ----
  129. <vaadin-native-select>
  130. <option>Mercury</option>
  131. <option>Venus</option>
  132. <option selected>Earth</option>
  133. </vaadin-native-select>
  134. ----
  135. The declarative syntax of each component type is described in the JavaDoc API
  136. documentation of Vaadin.
  137. [[application.declarative.attributes]]
  138. == Component Attributes
  139. [[application.declarative.attributes.mapping]]
  140. === Attribute-to-Property Mapping
  141. Component properties are directly mapped to the attributes of the HTML elements
  142. according to the names of the properties. Attributes are written in lower-case
  143. letters and dash is used for word separation instead of upper-case letters in
  144. the Java methods, so that [literal]#++placeholder++# attribute is equivalent to
  145. [methodname]#setPlaceholder()#.
  146. For example, the __caption__ property, which you can set with
  147. [methodname]#setCaption()#, is represented as [literal]#++caption++# attribute.
  148. You can find the component properties by the setter methods in the
  149. link:https://vaadin.com/api/[JavaDoc API documentation] of the component
  150. classes.
  151. [source, html]
  152. ----
  153. <vaadin-text-field caption="Name" placeholder="Enter Name"/>
  154. ----
  155. [[application.declarative.attributes.parameters]]
  156. === Attribute Values
  157. Attribute parameters must be enclosed in quotes and the value given as a string
  158. must be convertible to the type of the property (string, integer, boolean, or
  159. enumeration). Object types are not supported.
  160. Some attribute names are given by a shorthand. For example,
  161. [parameter]#alternateText# property of the [classname]#Image# component, which
  162. you would set with [methodname]#setAlternateText()#, is given as the
  163. [literal]#++alt++# attribute.
  164. Boolean values must be either `true` or `false`.
  165. The value can be omitted, in which case `true` is assumed.
  166. For example, the [literal]#++enabled++# attribute is boolean and has default value "`true`", so `enabled="true"` and `enabled` and equivalent.
  167. [source, html]
  168. ----
  169. <vaadin-button enabled="false">OK</vaadin-button>
  170. ----
  171. [[application.declarative.attributes.parent]]
  172. === Parent Component Settings
  173. Certain settings, such as a component's alignment in a layout, are not done in
  174. the component itself, but in the layout. Attributes prefixed with colon (
  175. [literal]#++:++#) are passed to the containing component, with the component as
  176. a target parameter. For example, [literal]#++:expand="1"++# given for a
  177. component [parameter]#c# is equivalent to calling [methodname]#setExpandRatio(c,
  178. 1)# for the containing layout.
  179. [subs="normal"]
  180. ----
  181. &lt;vaadin-vertical-layout size-full&gt;
  182. &lt;!-- Align right in the containing layout --&gt;
  183. &lt;vaadin-label width-auto **:right**&gt;Hello!&lt;/vaadin-label&gt;
  184. &lt;!-- Expands to take up all remaining vertical space --&gt;
  185. &lt;vaadin-horizontal-layout size-full **:expand**&gt;
  186. &lt;!-- Automatic width - shrinks horizontally --&gt;
  187. &lt;vaadin-radio-button-group width-auto height-full/&gt;
  188. &lt;!-- Expands horizontally to take remaining space --&gt;
  189. &lt;vaadin-grid size-full **:expand**/&gt;
  190. &lt;/vaadin-horizontal-layout&gt;
  191. &lt;/vaadin-vertical-layout&gt;
  192. ----
  193. [[application.declarative.identifiers]]
  194. == Component Identifiers
  195. Components can be identified by either an identifier or a caption. There are two
  196. types of identifiers: page-global and local. This allows accessing them from
  197. Java code and binding them to components, as described later in
  198. <<application.declarative.composite>>.
  199. The [literal]#++id++# attribute can be used to define a page-global identifier,
  200. which must be unique within the page. Another design or UI shown simultaneously
  201. in the same page may not have components sharing the same ID. Using global
  202. identifiers is therefore not recommended, except in special cases where
  203. uniqueness is ensured.
  204. The [literal]#++_id++# attribute defines a local identifier used only within the
  205. design. This is the recommended way to identifying components.
  206. [source, html]
  207. ----
  208. <vaadin-grid _id="mygrid" caption="My Grid"/>
  209. ----
  210. [[application.declarative.composite]]
  211. == Using Designs in Code
  212. The main use of declarative designs is in building application views, sub-views,
  213. dialogs, and forms through composition. The two main tasks are filling the
  214. designs with application data and handling user interaction events.
  215. [[application.declarative.composite.designroot]]
  216. === Binding to a Design Root
  217. You can bind any component container as the root component of a design with the
  218. [classname]#@DesignRoot# annotation. The class must match or extend the class of
  219. the root element in the design.
  220. The member variables are automatically initialized from the design according to
  221. the component identifiers (see <<application.declarative.identifiers>>), which
  222. must match the variable names.
  223. For example, the following class could be used to bind the design given earlier.
  224. [source, java]
  225. ----
  226. @DesignRoot
  227. public class MyViewDesign extends VerticalLayout {
  228. RadioButtonGroup<String> myRadioButtonGroup;
  229. Grid<String> myGrid;
  230. public MyViewDesign() {
  231. Design.read("MyDeclarativeUI.html", this);
  232. // Show some (example) data
  233. myCheckBoxGroup.setItems("Venus", "Earth", "Mars");
  234. myGrid.setItems(
  235. GridExample.generateContent());
  236. // Some interaction
  237. myCheckBoxGroup.addValueChangeListener(event ->
  238. Notification.show("Selected " +
  239. event.getValue());
  240. }
  241. }
  242. ----
  243. The design root class must match or extend the root element class of the design.
  244. For example, earlier we had [literal]#++<vaadin-vertical-layout>++# element in the
  245. HTML file, which can be bound to a class extending [classname]#VerticalLayout#.
  246. [[application.declarative.composite.using]]
  247. === Using a Design
  248. You can create and use a declaratively defined component just like any other component.
  249. For example, to use the previously defined design root component as the content
  250. of the entire UI:
  251. [source, java]
  252. ----
  253. public class DeclarativeViewUI extends UI {
  254. @Override
  255. protected void init(VaadinRequest request) {
  256. setContent(new MyViewDesign());
  257. }
  258. }
  259. ----
  260. [[application.declarative.composite.viewnavigation]]
  261. === Designs in View Navigation
  262. To use a design in view navigation, as described in
  263. <<../advanced/advanced-navigator#advanced.navigator,"Navigating
  264. in an Application">>, you just need to implement the [interfacename]#View#
  265. interface.
  266. [source, java]
  267. ----
  268. @DesignRoot
  269. public class MainView extends VerticalLayout
  270. implements View {
  271. public MainView() {
  272. Design.read(this);
  273. ...
  274. }
  275. ...
  276. }
  277. ...
  278. // Use the view by precreating it
  279. navigator.addView(MAINVIEW, new MainView());
  280. ----
  281. See
  282. <<../advanced/advanced-navigator#advanced.navigator.pathparam,"Handling
  283. Path Parameters">> for a complete example.