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.

TestForGridLayoutChildComponentRendering.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import com.vaadin.server.ExternalResource;
  20. import com.vaadin.ui.Button;
  21. import com.vaadin.ui.Button.ClickEvent;
  22. import com.vaadin.ui.Component;
  23. import com.vaadin.ui.CustomComponent;
  24. import com.vaadin.ui.GridLayout;
  25. import com.vaadin.ui.Label;
  26. import com.vaadin.ui.Link;
  27. import com.vaadin.ui.Select;
  28. /**
  29. *
  30. * This Component contains some simple test to see that component updates its
  31. * contents propertly.
  32. *
  33. * @author Vaadin Ltd.
  34. */
  35. public class TestForGridLayoutChildComponentRendering extends CustomComponent {
  36. private final GridLayout main = new GridLayout(2, 3);
  37. public TestForGridLayoutChildComponentRendering() {
  38. setCompositionRoot(main);
  39. createNewView();
  40. }
  41. public void createNewView() {
  42. main.removeAllComponents();
  43. main.addComponent(new Label("SDFGFHFHGJGFDSDSSSGFDD"));
  44. final Link l = new Link();
  45. l.setCaption("Siirry Vaadiniin");
  46. l.setResource(new ExternalResource("http://www.vaadin.com/"));
  47. l.setTargetHeight(200);
  48. l.setTargetWidth(500);
  49. l.setTargetBorder(Link.TARGET_BORDER_MINIMAL);
  50. main.addComponent(l);
  51. final Select se = new Select("Tästä valitaan");
  52. se.setCaption("Whattaa select");
  53. se.addItem("valinta1");
  54. se.addItem("Valinta 2");
  55. main.addComponent(se, 0, 1, 1, 1);
  56. Button b = new Button("refresh view", new Button.ClickListener() {
  57. @Override
  58. public void buttonClick(ClickEvent event) {
  59. createNewView();
  60. }
  61. });
  62. main.addComponent(b);
  63. b = new Button("reorder view", new Button.ClickListener() {
  64. @Override
  65. public void buttonClick(ClickEvent event) {
  66. randomReorder();
  67. }
  68. });
  69. main.addComponent(b);
  70. b = new Button("remove randomly one component",
  71. new Button.ClickListener() {
  72. @Override
  73. public void buttonClick(ClickEvent event) {
  74. removeRandomComponent();
  75. }
  76. });
  77. main.addComponent(b);
  78. }
  79. public void randomReorder() {
  80. final Iterator<Component> it = main.getComponentIterator();
  81. final ArrayList<Component> components = new ArrayList<Component>();
  82. while (it.hasNext()) {
  83. components.add(it.next());
  84. }
  85. main.removeAllComponents();
  86. final int size = components.size();
  87. final int colspanIndex = ((int) (Math.random() * size) / 2) * 2 + 2;
  88. for (int i = components.size(); i > 0; i--) {
  89. final int index = (int) (Math.random() * i);
  90. if (i == colspanIndex) {
  91. main.addComponent(components.get(index), 0, (size - i) / 2, 1,
  92. (size - i) / 2);
  93. } else {
  94. main.addComponent(components.get(index));
  95. }
  96. components.remove(index);
  97. }
  98. }
  99. public void removeRandomComponent() {
  100. final Iterator<Component> it = main.getComponentIterator();
  101. final ArrayList<Component> components = new ArrayList<Component>();
  102. while (it.hasNext()) {
  103. components.add(it.next());
  104. }
  105. final int size = components.size();
  106. final int index = (int) (Math.random() * size);
  107. main.removeComponent(components.get(index));
  108. }
  109. }