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

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