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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2000-2014 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.AbstractSelect;
  18. import com.vaadin.ui.Button;
  19. import com.vaadin.ui.Button.ClickEvent;
  20. import com.vaadin.ui.Button.ClickListener;
  21. import com.vaadin.ui.CheckBox;
  22. import com.vaadin.ui.Component;
  23. import com.vaadin.ui.CustomComponent;
  24. import com.vaadin.ui.HorizontalLayout;
  25. import com.vaadin.ui.Label;
  26. import com.vaadin.ui.NativeSelect;
  27. import com.vaadin.ui.OptionGroup;
  28. import com.vaadin.ui.Panel;
  29. import com.vaadin.ui.Tree;
  30. import com.vaadin.ui.TwinColSelect;
  31. import com.vaadin.ui.VerticalLayout;
  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("OptionGroup + multiselect manually (configured from select)");
  78. main.addComponent(test);
  79. final Button b = new Button("refresh view", new Button.ClickListener() {
  80. @Override
  81. public void buttonClick(ClickEvent event) {
  82. createNewView();
  83. }
  84. });
  85. main.addComponent(b);
  86. }
  87. public static void fillSelect(AbstractSelect s, int items) {
  88. for (int i = 0; i < items; i++) {
  89. final String name = firstnames[(int) (Math.random() * (firstnames.length - 1))]
  90. + " "
  91. + lastnames[(int) (Math.random() * (lastnames.length - 1))];
  92. s.addItem(name);
  93. }
  94. }
  95. public Tree createTestTree() {
  96. Tree t = new Tree("Tree");
  97. final String[] names = new String[100];
  98. for (int i = 0; i < names.length; i++) {
  99. names[i] = firstnames[(int) (Math.random() * (firstnames.length - 1))]
  100. + " "
  101. + lastnames[(int) (Math.random() * (lastnames.length - 1))];
  102. }
  103. // Create tree
  104. t = new Tree("Organization Structure");
  105. for (int i = 0; i < 100; i++) {
  106. t.addItem(names[i]);
  107. final String parent = names[(int) (Math.random() * (names.length - 1))];
  108. if (t.containsId(parent)) {
  109. t.setParent(names[i], parent);
  110. }
  111. }
  112. // Forbid childless people to have children (makes them leaves)
  113. for (int i = 0; i < 100; i++) {
  114. if (!t.hasChildren(names[i])) {
  115. t.setChildrenAllowed(names[i], false);
  116. }
  117. }
  118. return t;
  119. }
  120. public Panel createTestBench(Component t) {
  121. final HorizontalLayout ol = new HorizontalLayout();
  122. ol.addComponent(t);
  123. final HorizontalLayout ol2 = new HorizontalLayout();
  124. final VerticalLayout statusLayout = new VerticalLayout();
  125. final Panel status = new Panel("Events", statusLayout);
  126. final Button clear = new Button("clear event log");
  127. clear.addClickListener(new ClickListener() {
  128. @Override
  129. public void buttonClick(ClickEvent event) {
  130. statusLayout.removeAllComponents();
  131. statusLayout.addComponent(ol2);
  132. }
  133. });
  134. ol2.addComponent(clear);
  135. final Button commit = new Button("commit changes");
  136. ol2.addComponent(commit);
  137. statusLayout.addComponent(ol2);
  138. status.setHeight("300px");
  139. status.setWidth("400px");
  140. ol.addComponent(status);
  141. t.addListener(new Listener() {
  142. @Override
  143. public void componentEvent(Event event) {
  144. statusLayout
  145. .addComponent(new Label(event.getClass().getName()));
  146. // TODO should not use Field.toString()
  147. statusLayout.addComponent(new Label("selected: "
  148. + event.getSource().toString()));
  149. }
  150. });
  151. return new Panel(ol);
  152. }
  153. }