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.4KB

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