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

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