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.

layout-customlayout.asciidoc 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ---
  2. title: Custom Layouts
  3. order: 14
  4. layout: page
  5. ---
  6. [[layout.customlayout]]
  7. = Custom Layouts
  8. ifdef::web[]
  9. [.sampler]
  10. image:{live-demo-image}[alt="Live Demo", link="http://demo.vaadin.com/sampler/#ui/layout/custom-layout"]
  11. endif::web[]
  12. While it is possible to create almost any typical layout with the standard
  13. layout components, it is sometimes best to separate the layout completely from
  14. code. With the [classname]#CustomLayout# component, you can write your layout as
  15. a template in HTML that provides locations of any contained components. The
  16. layout template is included in a theme. This separation allows the layout to be
  17. designed separately from code, for example using WYSIWYG web designer tools such
  18. as Adobe Dreamweaver.
  19. A template is a HTML file located under [filename]#layouts# folder under a theme
  20. folder under the [filename]#/VAADIN/themes/# folder, for example,
  21. [filename]#/VAADIN/themes/__themename/layouts/mylayout.html__#.
  22. (Notice that the root path [filename]#/VAADIN/themes/# for themes is
  23. fixed.) A template can also be provided dynamically from an
  24. [classname]#InputStream#, as explained below. A template includes
  25. [literal]#++<div>++# elements with a [parameter]#data-location# or
  26. [parameter]#location# attribute that defines the location identifier. All
  27. custom layout HTML-files must be saved using UTF-8 character encoding.
  28. [subs="normal"]
  29. ----
  30. &lt;table width="100%" height="100%"&gt;
  31. &lt;tr height="100%"&gt;
  32. &lt;td&gt;
  33. &lt;table align="center"&gt;
  34. &lt;tr&gt;
  35. &lt;td align="right"&gt;User&amp;nbsp;name:&lt;/td&gt;
  36. &lt;td&gt;**&lt;div data-location="username"&gt;&lt;/div&gt;**&lt;/td&gt;
  37. &lt;/tr&gt;
  38. &lt;tr&gt;
  39. &lt;td align="right"&gt;Password:&lt;/td&gt;
  40. &lt;td&gt;**&lt;div data-location="password"&gt;&lt;/div&gt;**&lt;/td&gt;
  41. &lt;/tr&gt;
  42. &lt;/table&gt;
  43. &lt;/td&gt;
  44. &lt;/tr&gt;
  45. &lt;tr&gt;
  46. &lt;td align="right" colspan="2"&gt;
  47. **&lt;div data-location="okbutton"&gt;**&lt;/div&gt;
  48. &lt;/td&gt;
  49. &lt;/tr&gt;
  50. &lt;/table&gt;
  51. ----
  52. The client-side engine of Vaadin will replace contents of the location elements
  53. with the components. The components are bound to the location elements by the
  54. location identifier given to [methodname]#addComponent()#, as shown in the
  55. example below.
  56. [source, java]
  57. ----
  58. Panel loginPanel = new Panel("Login");
  59. CustomLayout content = new CustomLayout("layoutname");
  60. content.setSizeUndefined();
  61. loginPanel.setContent(content);
  62. loginPanel.setSizeUndefined();
  63. // No captions for fields is they are provided in the template
  64. content.addComponent(new TextField(), "username");
  65. content.addComponent(new TextField(), "password");
  66. content.addComponent(new Button("Login"), "okbutton");
  67. ----
  68. The resulting layout is shown below in <<figure.layout.customlayout>>.
  69. [[figure.layout.customlayout]]
  70. .Example of a Custom Layout Component
  71. image::img/customlayout-example1.png[width=40%, scaledwidth=70%]
  72. You can use [methodname]#addComponent()# also to replace an existing component
  73. in the location given in the second parameter.
  74. In addition to a static template file, you can provide a template dynamically
  75. with the [classname]#CustomLayout# constructor that accepts an
  76. [classname]#InputStream# as the template source. For example:
  77. [source, java]
  78. ----
  79. new CustomLayout(new ByteArrayInputStream("<b>Template</b>".getBytes()));
  80. ----
  81. or
  82. [source, java]
  83. ----
  84. new CustomLayout(new FileInputStream(file));
  85. ----