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.

components-customcomponent.asciidoc 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ---
  2. title: Composition with Composite and CustomComponent
  3. order: 33
  4. layout: page
  5. ---
  6. [[components.customcomponent]]
  7. = Composition with Composite and CustomComponent
  8. The ease of making new user interface components is one of the core features of
  9. Vaadin. Typically, you simply combine existing built-in components to produce
  10. composite components. In many applications, such composite components make up
  11. the majority of the user interface.
  12. As described earlier in
  13. <<../application/application-architecture#application.architecture.composition,"Compositing
  14. Components">>, you have two basic ways to create a composite - either by
  15. extending a layout component or by using a composition component (a
  16. [classname]#Composite# or a [classname]#CustomComponent#), which typically
  17. wraps around a layout component.
  18. The benefit of wrapping a layout composite is mainly encapsulation - hiding the
  19. implementation details of the composition. Otherwise, a user of the composite
  20. could rely on implementation details, which would create an unwanted dependency.
  21. To create a composite, you need to inherit [classname]#Composite# or
  22. [classname]#CustomComponent# and set the __composition root__ component in the
  23. constructor. The composition root is typically a layout component that contains
  24. other components.
  25. The difference between the two composition classes is that a
  26. [classname]#CustomComponent# introduces another layer in the DOM on the
  27. browser, which can be used e.g. for styling but does require its own size
  28. setting etc. On the other hand, a [classname]#Composite# is a more light-weight
  29. version that has no visual representation in the browser, and is effectively
  30. replaced by its contents.
  31. For example:
  32. [source, java]
  33. ----
  34. class MyComposite extends CustomComponent {
  35. public MyComposite(String message) {
  36. // A layout structure used for composition
  37. Panel panel = new Panel("My Custom Component");
  38. VerticalLayout panelContent = new VerticalLayout();
  39. panel.setContent(panelContent);
  40. // Compose from multiple components
  41. Label label = new Label(message);
  42. panelContent.addComponent(label);
  43. panelContent.addComponent(new Button("Ok"));
  44. // The composition root MUST be set
  45. setCompositionRoot(panel);
  46. // Set the size as undefined at all levels
  47. panelContent.setSizeUndefined();
  48. panel.setSizeUndefined();
  49. // this is not needed for a Composite
  50. setSizeUndefined();
  51. }
  52. }
  53. ----
  54. Take note of the sizing when trying to make a customcomponent that shrinks to
  55. fit the contained components. You have to set the size as undefined at all
  56. levels. In the case of [classname]#CustomComponent#, the sizing of the composite
  57. component and the composition root are separate, whereas [classname]#Composite#
  58. delegates its size management to its composition root.
  59. You can use the component as follows:
  60. [source, java]
  61. ----
  62. MyComposite mycomposite = new MyComposite("Hello");
  63. ----
  64. The rendered component is shown in <<figure.components.customcomponent>>.
  65. [[figure.components.customcomponent]]
  66. .A custom composite component
  67. image::img/customcomponent-example1.png[width=25%, scaledwidth=40%]
  68. You can also inherit any other components, such as layouts, to attain similar
  69. composition.
  70. ((("Google Web Toolkit")))
  71. Even further, you can create entirely new low-level components, by integrating
  72. pure client-side components or by extending the client-side functionality of
  73. built-in components. Development of new components is covered in
  74. <<../gwt/gwt-overview.asciidoc#gwt.overview,"Integrating
  75. with the Server-Side">>.