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.

TestForPreconfiguredComponents.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.vaadin.tests;
  2. import com.vaadin.ui.Button;
  3. import com.vaadin.ui.CheckBox;
  4. import com.vaadin.ui.Component;
  5. import com.vaadin.ui.CustomComponent;
  6. import com.vaadin.ui.HorizontalLayout;
  7. import com.vaadin.ui.Label;
  8. import com.vaadin.ui.Panel;
  9. import com.vaadin.ui.VerticalLayout;
  10. import com.vaadin.v7.ui.AbstractSelect;
  11. import com.vaadin.v7.ui.NativeSelect;
  12. import com.vaadin.v7.ui.OptionGroup;
  13. import com.vaadin.v7.ui.Tree;
  14. import com.vaadin.v7.ui.TwinColSelect;
  15. /**
  16. * @author Vaadin Ltd.
  17. */
  18. public class TestForPreconfiguredComponents extends CustomComponent {
  19. private static final String[] firstnames = { "John", "Mary", "Joe", "Sarah",
  20. "Jeff", "Jane", "Peter", "Marc", "Josie", "Linus" };
  21. private static final String[] lastnames = { "Torvalds", "Smith", "Jones",
  22. "Beck", "Sheridan", "Picard", "Hill", "Fielding", "Einstein" };
  23. private final VerticalLayout main = new VerticalLayout();
  24. public TestForPreconfiguredComponents() {
  25. setCompositionRoot(main);
  26. createNewView();
  27. }
  28. public void createNewView() {
  29. main.removeAllComponents();
  30. main.addComponent(new Label(
  31. "In Toolkit 5 we introduce new components. Previously we"
  32. + " usually used setStyle or some other methods on possibly "
  33. + "multiple steps to configure component for ones needs. These new "
  34. + "server side components are mostly just classes that in constructor "
  35. + "set base class to state that programmer wants."));
  36. main.addComponent(new Button("commit"));
  37. Panel test = createTestBench(new CheckBox());
  38. test.setCaption("CheckBox (configured from button)");
  39. main.addComponent(test);
  40. AbstractSelect s = new TwinColSelect();
  41. fillSelect(s, 20);
  42. test = createTestBench(s);
  43. test.setCaption("TwinColSelect (configured from select)");
  44. main.addComponent(test);
  45. s = new NativeSelect();
  46. fillSelect(s, 20);
  47. test = createTestBench(s);
  48. test.setCaption("Native (configured from select)");
  49. main.addComponent(test);
  50. s = new OptionGroup();
  51. fillSelect(s, 20);
  52. test = createTestBench(s);
  53. test.setCaption("OptionGroup (configured from select)");
  54. main.addComponent(test);
  55. s = new OptionGroup();
  56. fillSelect(s, 20);
  57. s.setMultiSelect(true);
  58. test = createTestBench(s);
  59. test.setCaption(
  60. "OptionGroup + multiselect manually (configured from select)");
  61. main.addComponent(test);
  62. final Button b = new Button("refresh view", event -> createNewView());
  63. main.addComponent(b);
  64. }
  65. public static void fillSelect(AbstractSelect s, int items) {
  66. for (int i = 0; i < items; i++) {
  67. final String name = firstnames[(int) (Math.random()
  68. * (firstnames.length - 1))] + " "
  69. + lastnames[(int) (Math.random() * (lastnames.length - 1))];
  70. s.addItem(name);
  71. }
  72. }
  73. public Tree createTestTree() {
  74. Tree t = new Tree("Tree");
  75. final String[] names = new String[100];
  76. for (int i = 0; i < names.length; i++) {
  77. names[i] = firstnames[(int) (Math.random()
  78. * (firstnames.length - 1))] + " "
  79. + lastnames[(int) (Math.random() * (lastnames.length - 1))];
  80. }
  81. // Create tree
  82. t = new Tree("Organization Structure");
  83. for (int i = 0; i < 100; i++) {
  84. t.addItem(names[i]);
  85. final String parent = names[(int) (Math.random()
  86. * (names.length - 1))];
  87. if (t.containsId(parent)) {
  88. t.setParent(names[i], parent);
  89. }
  90. }
  91. // Forbid childless people to have children (makes them leaves)
  92. for (int i = 0; i < 100; i++) {
  93. if (!t.hasChildren(names[i])) {
  94. t.setChildrenAllowed(names[i], false);
  95. }
  96. }
  97. return t;
  98. }
  99. public Panel createTestBench(Component t) {
  100. final HorizontalLayout ol = new HorizontalLayout();
  101. ol.addComponent(t);
  102. final HorizontalLayout ol2 = new HorizontalLayout();
  103. final VerticalLayout statusLayout = new VerticalLayout();
  104. final Panel status = new Panel("Events", statusLayout);
  105. final Button clear = new Button("clear event log");
  106. clear.addClickListener(event -> {
  107. statusLayout.removeAllComponents();
  108. statusLayout.addComponent(ol2);
  109. });
  110. ol2.addComponent(clear);
  111. final Button commit = new Button("commit changes");
  112. ol2.addComponent(commit);
  113. statusLayout.addComponent(ol2);
  114. status.setHeight("300px");
  115. status.setWidth("400px");
  116. ol.addComponent(status);
  117. t.addListener(event -> {
  118. statusLayout.addComponent(new Label(event.getClass().getName()));
  119. // TODO should not use LegacyField.toString()
  120. statusLayout.addComponent(
  121. new Label("selected: " + event.getSource().toString()));
  122. });
  123. return new Panel(ol);
  124. }
  125. }