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.

MovingComponentsWhileOldParentInvisible.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.vaadin.tests.layouts;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.List;
  5. import com.vaadin.data.Property.ValueChangeEvent;
  6. import com.vaadin.data.Property.ValueChangeListener;
  7. import com.vaadin.tests.components.TestBase;
  8. import com.vaadin.ui.AbsoluteLayout;
  9. import com.vaadin.ui.Accordion;
  10. import com.vaadin.ui.Button;
  11. import com.vaadin.ui.Button.ClickEvent;
  12. import com.vaadin.ui.ComboBox;
  13. import com.vaadin.ui.ComponentContainer;
  14. import com.vaadin.ui.CssLayout;
  15. import com.vaadin.ui.FormLayout;
  16. import com.vaadin.ui.GridLayout;
  17. import com.vaadin.ui.HasComponents;
  18. import com.vaadin.ui.HorizontalLayout;
  19. import com.vaadin.ui.HorizontalSplitPanel;
  20. import com.vaadin.ui.Label;
  21. import com.vaadin.ui.Panel;
  22. import com.vaadin.ui.SingleComponentContainer;
  23. import com.vaadin.ui.TabSheet;
  24. import com.vaadin.ui.VerticalLayout;
  25. import com.vaadin.ui.VerticalSplitPanel;
  26. public class MovingComponentsWhileOldParentInvisible extends TestBase {
  27. private HasComponents cc = new AbsoluteLayout(); // initial dummy
  28. // contents
  29. private Label lab;
  30. @Override
  31. protected void setup() {
  32. lab = new Label("Label inside the component container");
  33. lab.setWidth(null);
  34. ComboBox componentContainerSelect = new ComboBox("Container") {
  35. {
  36. setPageLength(0);
  37. }
  38. };
  39. componentContainerSelect.setId("componentContainerSelect");
  40. componentContainerSelect.setWidth("300px");
  41. componentContainerSelect.setImmediate(true);
  42. componentContainerSelect.setNullSelectionAllowed(false);
  43. // componentContainer.addContainerProperty(CAPTION, String.class, "");
  44. // componentContainer.addContainerProperty(CLASS, Class.class, "");
  45. for (Class<? extends HasComponents> cls : getComponentContainers()) {
  46. componentContainerSelect.addItem(cls);
  47. }
  48. componentContainerSelect.addListener(new ValueChangeListener() {
  49. @Override
  50. @SuppressWarnings("unchecked")
  51. public void valueChange(ValueChangeEvent event) {
  52. HasComponents oldCC = cc;
  53. cc = createComponentContainer((Class<? extends HasComponents>) event
  54. .getProperty().getValue());
  55. addToCC(lab);
  56. replaceComponent(oldCC, cc);
  57. }
  58. });
  59. componentContainerSelect.setValue(componentContainerSelect.getItemIds()
  60. .iterator().next());
  61. Button but1 = new Button("Move in and out of component container",
  62. new Button.ClickListener() {
  63. @Override
  64. public void buttonClick(ClickEvent event) {
  65. cc.setVisible(!cc.isVisible());
  66. if (!cc.isVisible()) {
  67. getLayout().addComponent(lab);
  68. lab.setValue(lab.getValue().replace("inside",
  69. "outside"));
  70. } else {
  71. addToCC(lab);
  72. lab.setValue(lab.getValue().replace("outside",
  73. "inside"));
  74. }
  75. }
  76. });
  77. addComponent(componentContainerSelect);
  78. addComponent(cc);
  79. addComponent(but1);
  80. }
  81. protected void addToCC(Label lab2) {
  82. if (cc instanceof ComponentContainer) {
  83. ((ComponentContainer) cc).addComponent(lab);
  84. } else if (cc instanceof SingleComponentContainer) {
  85. ((SingleComponentContainer) cc).setContent(lab);
  86. } else {
  87. throw new RuntimeException("Don't know how to add to "
  88. + cc.getClass().getName());
  89. }
  90. }
  91. private Collection<Class<? extends HasComponents>> getComponentContainers() {
  92. List<Class<? extends HasComponents>> list = new ArrayList<Class<? extends HasComponents>>();
  93. list.add(AbsoluteLayout.class);
  94. list.add(Accordion.class);
  95. list.add(CssLayout.class);
  96. list.add(FormLayout.class);
  97. list.add(GridLayout.class);
  98. list.add(HorizontalLayout.class);
  99. list.add(HorizontalSplitPanel.class);
  100. list.add(Panel.class);
  101. list.add(TabSheet.class);
  102. list.add(VerticalLayout.class);
  103. list.add(VerticalSplitPanel.class);
  104. return list;
  105. }
  106. protected HasComponents createComponentContainer(
  107. Class<? extends HasComponents> value) {
  108. try {
  109. HasComponents cc = value.newInstance();
  110. cc.setWidth("300px");
  111. cc.setHeight("300px");
  112. return cc;
  113. } catch (InstantiationException e) {
  114. // TODO Auto-generated catch block
  115. e.printStackTrace();
  116. } catch (IllegalAccessException e) {
  117. // TODO Auto-generated catch block
  118. e.printStackTrace();
  119. }
  120. return null;
  121. }
  122. @Override
  123. protected String getDescription() {
  124. return "Client side layouts can easily have a bug where its internal data structures gets messed up when child components from it are moved forth and back when it is invisible (registered, but renders are ignored until becomes visible again). Things are especially easy to mess up when the layout uses wrapper widget over each component (like VOrderedLayout and VGridLayout does). This tests Vertical (Ordered), Grid and CssLayout.";
  125. }
  126. @Override
  127. protected Integer getTicketNumber() {
  128. return 5372;
  129. }
  130. }