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.

LayoutDemo.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 com.vaadin.server.ClassResource;
  18. import com.vaadin.shared.ui.ContentMode;
  19. import com.vaadin.ui.Component;
  20. import com.vaadin.ui.Embedded;
  21. import com.vaadin.ui.GridLayout;
  22. import com.vaadin.ui.HorizontalLayout;
  23. import com.vaadin.ui.Label;
  24. import com.vaadin.ui.Layout;
  25. import com.vaadin.ui.LegacyWindow;
  26. import com.vaadin.ui.Panel;
  27. import com.vaadin.ui.TabSheet;
  28. import com.vaadin.ui.VerticalLayout;
  29. /**
  30. * This example demonstrates layouts. Layouts are populated with sample Vaadin
  31. * UI components.
  32. *
  33. * @author Vaadin Ltd.
  34. * @since 4.0.0
  35. *
  36. */
  37. public class LayoutDemo extends com.vaadin.server.LegacyApplication {
  38. /**
  39. * Initialize Application. Demo components are added to main window.
  40. */
  41. @Override
  42. public void init() {
  43. final LegacyWindow mainWindow = new LegacyWindow("Layout demo");
  44. setMainWindow(mainWindow);
  45. //
  46. // Create horizontal ordered layout
  47. //
  48. final HorizontalLayout layoutA = new HorizontalLayout();
  49. // Add 4 panels
  50. fillLayout(layoutA, 4);
  51. //
  52. // Create vertical ordered layout
  53. //
  54. final VerticalLayout layoutB = new VerticalLayout();
  55. // Add 4 panels
  56. fillLayout(layoutB, 4);
  57. //
  58. // Create grid layout
  59. //
  60. final GridLayout layoutG = new GridLayout(4, 4);
  61. // Add 16 panels components
  62. fillLayout(layoutG, 16);
  63. //
  64. // Create grid layout
  65. //
  66. final GridLayout layoutG2 = new GridLayout(4, 4);
  67. // Add 4 panels with absolute coordinates (diagonally)
  68. layoutG2.addComponent(getExampleComponent("x=0, y=0"), 0, 0);
  69. layoutG2.addComponent(getExampleComponent("x=1, y=1"), 1, 1);
  70. layoutG2.addComponent(getExampleComponent("x=2, y=2"), 2, 2);
  71. layoutG2.addComponent(getExampleComponent("x=3, y=3"), 3, 3);
  72. // Add 4 pictures with absolute coordinates (diagonally)
  73. layoutG2.addComponent(getExamplePicture("x=3, y=0"), 3, 0);
  74. layoutG2.addComponent(getExamplePicture("x=2, y=1"), 2, 1);
  75. layoutG2.addComponent(getExamplePicture("x=1, y=2"), 1, 2);
  76. layoutG2.addComponent(getExamplePicture("x=0, y=3"), 0, 3);
  77. //
  78. // Create TabSheet
  79. //
  80. final TabSheet tabsheet = new TabSheet();
  81. tabsheet.setCaption(
  82. "Tabsheet, above layouts are added to this component");
  83. tabsheet.addTab(layoutA, "Horizontal ordered layout", null);
  84. tabsheet.addTab(layoutB, "Vertical ordered layout", null);
  85. tabsheet.addTab(layoutG, "First grid layout", null);
  86. tabsheet.addTab(layoutG2, "Second grid layout", null);
  87. //
  88. // Add demo layouts to main window
  89. //
  90. mainWindow.addComponent(new Label(
  91. "<h3>Horizontal ordered layout</h3>Added four components.",
  92. ContentMode.HTML));
  93. mainWindow.addComponent(layoutA);
  94. mainWindow.addComponent(new Label(
  95. "<br /><h3>Vertical ordered layout</h3>Added four components.",
  96. ContentMode.HTML));
  97. mainWindow.addComponent(layoutB);
  98. mainWindow.addComponent(new Label(
  99. "<br /><h3>Grid Layout (4 x 4)</h3>Added 16 components.",
  100. ContentMode.HTML));
  101. mainWindow.addComponent(layoutG);
  102. mainWindow.addComponent(new Label(
  103. "<br /><h3>Grid Layout (4 x 4)</h3>"
  104. + "Added four panels and four embedded components "
  105. + "diagonally with absolute coordinates.",
  106. ContentMode.HTML));
  107. mainWindow.addComponent(layoutG2);
  108. mainWindow.addComponent(
  109. new Label("<br /><h3>TabSheet</h3>Added above layouts as tabs.",
  110. ContentMode.HTML));
  111. mainWindow.addComponent(tabsheet);
  112. }
  113. private Component getExamplePicture(String caption) {
  114. // loads image from package com.vaadin.demo
  115. final ClassResource cr = new ClassResource("m-bullet-blue.gif");
  116. final Embedded em = new Embedded("Embedded " + caption, cr);
  117. em.setWidth("170px");
  118. return em;
  119. }
  120. private Component getExampleComponent(String caption) {
  121. VerticalLayout layout = new VerticalLayout();
  122. layout.setMargin(true);
  123. final Panel panel = new Panel(layout);
  124. panel.setCaption("Panel component " + caption);
  125. layout.addComponent(new Label(
  126. "Panel is a container for other components, by default it draws a frame around it's "
  127. + "extremities and may have a caption to clarify the nature of the contained components' purpose."
  128. + " Panel contains an layout where the actual contained components are added, "
  129. + "this layout may be switched on the fly.",
  130. ContentMode.HTML));
  131. panel.setWidth("222px");
  132. return panel;
  133. }
  134. /**
  135. * Add multiple demo component to given layout.
  136. *
  137. * @param layout
  138. * where components are added
  139. * @param numberOfComponents
  140. * to add
  141. */
  142. private void fillLayout(Layout layout, int numberOfComponents) {
  143. for (int i = 1; i <= numberOfComponents; i++) {
  144. layout.addComponent(getExampleComponent(Integer.toString(i)));
  145. }
  146. }
  147. }