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

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