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

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