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

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