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 8.0KB

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