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 13KB

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