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.

CompositeChainUI.java 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.vaadin.tests.components.composite;
  2. import java.util.Iterator;
  3. import com.vaadin.annotations.Widgetset;
  4. import com.vaadin.server.VaadinRequest;
  5. import com.vaadin.tests.components.AbstractTestUIWithLog;
  6. import com.vaadin.ui.Button;
  7. import com.vaadin.ui.Component;
  8. import com.vaadin.ui.Composite;
  9. import com.vaadin.ui.HasComponents;
  10. import com.vaadin.ui.HorizontalLayout;
  11. import com.vaadin.ui.Label;
  12. import com.vaadin.ui.VerticalLayout;
  13. @Widgetset("com.vaadin.DefaultWidgetSet")
  14. public class CompositeChainUI extends AbstractTestUIWithLog {
  15. private Label innermostComponent;
  16. private Composite innerComposite;
  17. private Composite outerComposite;
  18. private VerticalLayout container;
  19. private HorizontalLayout layout;
  20. @Override
  21. protected void setup(VaadinRequest request) {
  22. createComposite();
  23. layout = new HorizontalLayout(outerComposite);
  24. container = new VerticalLayout(layout);
  25. addComponent(container);
  26. Button updateCaption = new Button("Update caption");
  27. updateCaption.addClickListener(event -> innermostComponent
  28. .setCaption(innermostComponent.getCaption() + " - updated"));
  29. addComponent(updateCaption);
  30. Button replaceWithAnotherComposite = new Button(
  31. "Replace with another Composite", event -> {
  32. Composite oldOuter = outerComposite;
  33. createComposite();
  34. layout.replaceComponent(oldOuter, outerComposite);
  35. });
  36. addComponent(replaceWithAnotherComposite);
  37. logHierarchy();
  38. }
  39. private void createComposite() {
  40. innermostComponent = new Label("Label text");
  41. innermostComponent.setCaption("Label caption");
  42. innermostComponent.setId("innermost");
  43. innerComposite = new Composite(innermostComponent);
  44. outerComposite = new Composite(innerComposite);
  45. }
  46. private void logHierarchy() {
  47. String msg = "Hierarchy: ";
  48. if (container != null) {
  49. msg += getHierarchy(container);
  50. }
  51. log(msg);
  52. }
  53. private static String getHierarchy(Component component) {
  54. String msg = component.getClass().getSimpleName();
  55. if (component instanceof HasComponents) {
  56. Iterator<Component> it = ((HasComponents) component).iterator();
  57. if (it.hasNext()) {
  58. Component content = it.next();
  59. if (content != null) {
  60. msg += " -> " + getHierarchy(content);
  61. }
  62. }
  63. }
  64. return msg;
  65. }
  66. }