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.

RandomLayoutStress.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 2000-2016 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.time.LocalDate;
  18. import java.util.Random;
  19. import com.vaadin.server.ExternalResource;
  20. import com.vaadin.tests.components.TestDateField;
  21. import com.vaadin.ui.AbstractComponent;
  22. import com.vaadin.ui.AbstractDateField;
  23. import com.vaadin.ui.Button;
  24. import com.vaadin.ui.CustomLayout;
  25. import com.vaadin.ui.GridLayout;
  26. import com.vaadin.ui.HorizontalLayout;
  27. import com.vaadin.ui.Label;
  28. import com.vaadin.ui.Layout;
  29. import com.vaadin.ui.LegacyWindow;
  30. import com.vaadin.ui.Link;
  31. import com.vaadin.ui.Panel;
  32. import com.vaadin.ui.TabSheet;
  33. import com.vaadin.ui.VerticalLayout;
  34. import com.vaadin.v7.ui.Select;
  35. import com.vaadin.v7.ui.TextField;
  36. /**
  37. * This example demonstrates layouts. Layouts are populated with sample Vaadin
  38. * UI components.
  39. *
  40. * @author Vaadin Ltd.
  41. *
  42. */
  43. public class RandomLayoutStress extends com.vaadin.server.LegacyApplication {
  44. private final Random seededRandom = new Random(1);
  45. // FIXME increasing these settings brings out interesting client-side issues
  46. // (DOM errors)
  47. // TODO increasing values "even more" crashes Hosted Mode, pumping Xmx/Xms
  48. // helps to some extent
  49. private static final int componentCountA = 50;
  50. private static final int componentCountB = 50;
  51. private static final int componentCountC = 200;
  52. private static final int componentCountD = 50;
  53. /**
  54. * Initialize Application. Demo components are added to main window.
  55. */
  56. @Override
  57. public void init() {
  58. final LegacyWindow mainWindow = new LegacyWindow("Layout demo");
  59. setMainWindow(mainWindow);
  60. // Create horizontal ordered layout
  61. VerticalLayout panelALayout = new VerticalLayout();
  62. panelALayout.setMargin(true);
  63. final Panel panelA = new Panel(
  64. "Panel containing horizontal ordered layout", panelALayout);
  65. HorizontalLayout layoutA = new HorizontalLayout();
  66. // Add 4 random components
  67. fillLayout(layoutA, componentCountA);
  68. // Add layout to panel
  69. panelALayout.addComponent(layoutA);
  70. // Create vertical ordered layout
  71. VerticalLayout panelBLayout = new VerticalLayout();
  72. panelBLayout.setMargin(true);
  73. final Panel panelB = new Panel(
  74. "Panel containing vertical ordered layout", panelBLayout);
  75. VerticalLayout layoutB = new VerticalLayout();
  76. // Add 4 random components
  77. fillLayout(layoutB, componentCountB);
  78. // Add layout to panel
  79. panelBLayout.addComponent(layoutB);
  80. // Create grid layout
  81. final int gridSize = (int) java.lang.Math.sqrt(componentCountC);
  82. VerticalLayout panelGLayout = new VerticalLayout();
  83. panelGLayout.setMargin(true);
  84. final Panel panelG = new Panel("Panel containing grid layout ("
  85. + gridSize + " x " + gridSize + ")", panelGLayout);
  86. GridLayout layoutG = new GridLayout(gridSize, gridSize);
  87. // Add 12 random components
  88. fillLayout(layoutG, componentCountC);
  89. // Add layout to panel
  90. panelGLayout.addComponent(layoutG);
  91. // Create TabSheet
  92. final TabSheet tabsheet = new TabSheet();
  93. tabsheet.setCaption(
  94. "Tabsheet, above layouts are added to this component");
  95. layoutA = new HorizontalLayout();
  96. // Add 4 random components
  97. fillLayout(layoutA, componentCountA);
  98. tabsheet.addTab(layoutA, "Horizontal ordered layout", null);
  99. layoutB = new VerticalLayout();
  100. // Add 4 random components
  101. fillLayout(layoutB, componentCountB);
  102. tabsheet.addTab(layoutB, "Vertical ordered layout", null);
  103. layoutG = new GridLayout(gridSize, gridSize);
  104. // Add 12 random components
  105. fillLayout(layoutG, componentCountC);
  106. tabsheet.addTab(layoutG, "Grid layout (4 x 2)", null);
  107. // Create custom layout
  108. VerticalLayout panelCLayout = new VerticalLayout();
  109. panelCLayout.setMargin(true);
  110. final Panel panelC = new Panel("Custom layout with style exampleStyle",
  111. panelCLayout);
  112. final CustomLayout layoutC = new CustomLayout("exampleStyle");
  113. // Add 4 random components
  114. fillLayout(layoutC, componentCountD);
  115. // Add layout to panel
  116. panelCLayout.addComponent(layoutC);
  117. // Add demo panels (layouts) to main window
  118. mainWindow.addComponent(panelA);
  119. mainWindow.addComponent(panelB);
  120. mainWindow.addComponent(panelG);
  121. mainWindow.addComponent(tabsheet);
  122. mainWindow.addComponent(panelC);
  123. }
  124. private AbstractComponent getRandomComponent(int caption) {
  125. AbstractComponent result = null;
  126. final int randint = seededRandom.nextInt(7);
  127. switch (randint) {
  128. case 0:
  129. // Label
  130. result = new Label();
  131. result.setCaption("Label component " + caption);
  132. break;
  133. case 1:
  134. // Button
  135. result = new Button();
  136. result.setCaption("Button component " + caption);
  137. break;
  138. case 2:
  139. // TextField
  140. result = new TextField();
  141. result.setCaption("TextField component " + caption);
  142. break;
  143. case 3:
  144. // Select
  145. result = new Select("Select " + caption);
  146. result.setCaption("Select component " + caption);
  147. ((Select) result).addItem("First item");
  148. ((Select) result).addItem("Second item");
  149. ((Select) result).addItem("Third item");
  150. break;
  151. case 4:
  152. // Link
  153. result = new Link("",
  154. new ExternalResource("http://www.vaadin.com"));
  155. result.setCaption("Link component " + caption);
  156. break;
  157. case 5:
  158. // Link
  159. VerticalLayout panelLayout = new VerticalLayout();
  160. panelLayout.setMargin(true);
  161. result = new Panel(panelLayout);
  162. result.setCaption("Panel component " + caption);
  163. panelLayout.addComponent(new Label(
  164. "Panel is a container for other components, by default it draws a frame around it's "
  165. + "extremities and may have a caption to clarify the nature of the contained components' purpose."
  166. + " Panel contains an layout where the actual contained components are added, "
  167. + "this layout may be switched on the fly."));
  168. ((Panel) result).setWidth("250px");
  169. break;
  170. case 6:
  171. // Datefield
  172. result = new TestDateField();
  173. ((AbstractDateField) result).setStyleName("calendar");
  174. ((AbstractDateField) result).setValue(LocalDate.now());
  175. result.setCaption("Calendar component " + caption);
  176. break;
  177. case 7:
  178. // Datefield
  179. result = new TestDateField();
  180. ((AbstractDateField) result).setValue(LocalDate.now());
  181. result.setCaption("Calendar component " + caption);
  182. break;
  183. }
  184. return result;
  185. }
  186. /**
  187. * Add demo components to given layout
  188. *
  189. * @param layout
  190. */
  191. private void fillLayout(Layout layout, int numberOfComponents) {
  192. for (int i = 0; i < numberOfComponents; i++) {
  193. layout.addComponent(getRandomComponent(i));
  194. }
  195. }
  196. }