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

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