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.

TestForTrees.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2000-2013 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.event.Action;
  18. import com.vaadin.event.Action.Handler;
  19. import com.vaadin.ui.AbstractOrderedLayout;
  20. import com.vaadin.ui.Button;
  21. import com.vaadin.ui.Button.ClickEvent;
  22. import com.vaadin.ui.Button.ClickListener;
  23. import com.vaadin.ui.Component;
  24. import com.vaadin.ui.CustomComponent;
  25. import com.vaadin.ui.HorizontalLayout;
  26. import com.vaadin.ui.Label;
  27. import com.vaadin.ui.Panel;
  28. import com.vaadin.ui.Tree;
  29. import com.vaadin.ui.VerticalLayout;
  30. /**
  31. * Some test cases for trees. Events panel logs events that happen server side.
  32. *
  33. * @author Vaadin Ltd.
  34. */
  35. public class TestForTrees extends CustomComponent implements Handler {
  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. private final Action[] actions = new Action[] { new Action("edit"),
  43. new Action("delete") };
  44. private VerticalLayout al;
  45. private Tree contextTree;
  46. public TestForTrees() {
  47. setCompositionRoot(main);
  48. createNewView();
  49. }
  50. public void createNewView() {
  51. main.removeAllComponents();
  52. main.addComponent(new Label(
  53. "Some test cases for trees. Events panel logs events that happen server side."));
  54. main.addComponent(new Button("commit"));
  55. Tree t;
  56. t = createTestTree();
  57. t.setCaption("Default settings");
  58. main.addComponent(createTestBench(t));
  59. t = createTestTree();
  60. t.setCaption("Multiselect settings");
  61. t.setMultiSelect(true);
  62. main.addComponent(createTestBench(t));
  63. t = createTestTree();
  64. t.setCaption("Multiselect and immediate");
  65. t.setImmediate(true);
  66. t.setMultiSelect(true);
  67. main.addComponent(createTestBench(t));
  68. t = createTestTree();
  69. t.setCaption("immediate");
  70. t.setImmediate(true);
  71. main.addComponent(createTestBench(t));
  72. t = createTestTree();
  73. t.setCaption("with actions");
  74. t.setImmediate(true);
  75. t.addActionHandler(this);
  76. final AbstractOrderedLayout ol = (AbstractOrderedLayout) createTestBench(t);
  77. al = new VerticalLayout();
  78. al.setMargin(true);
  79. ol.addComponent(new Panel("action log", al));
  80. main.addComponent(ol);
  81. contextTree = t;
  82. final Button b = new Button("refresh view", new Button.ClickListener() {
  83. @Override
  84. public void buttonClick(ClickEvent event) {
  85. createNewView();
  86. }
  87. });
  88. main.addComponent(b);
  89. }
  90. public Tree createTestTree() {
  91. Tree t = new Tree("Tree");
  92. final String[] names = new String[100];
  93. for (int i = 0; i < names.length; i++) {
  94. names[i] = firstnames[(int) (Math.random() * (firstnames.length - 1))]
  95. + " "
  96. + lastnames[(int) (Math.random() * (lastnames.length - 1))];
  97. }
  98. // Create tree
  99. t = new Tree("Organization Structure");
  100. for (int i = 0; i < 100; i++) {
  101. t.addItem(names[i]);
  102. final String parent = names[(int) (Math.random() * (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 Component createTestBench(Tree t) {
  116. final HorizontalLayout ol = new HorizontalLayout();
  117. ol.addComponent(t);
  118. final VerticalLayout statusLayout = new VerticalLayout();
  119. statusLayout.setMargin(true);
  120. final Panel status = new Panel("Events", statusLayout);
  121. final Button clear = new Button("c");
  122. clear.addClickListener(new ClickListener() {
  123. @Override
  124. public void buttonClick(ClickEvent event) {
  125. statusLayout.removeAllComponents();
  126. statusLayout.addComponent(clear);
  127. }
  128. });
  129. statusLayout.addComponent(clear);
  130. status.setHeight("300px");
  131. status.setWidth("400px");
  132. ol.addComponent(status);
  133. t.addListener(new Listener() {
  134. @Override
  135. public void componentEvent(Event event) {
  136. statusLayout
  137. .addComponent(new Label(event.getClass().getName()));
  138. // TODO should not use Field.toString()
  139. statusLayout.addComponent(new Label("selected: "
  140. + event.getSource().toString()));
  141. }
  142. });
  143. return ol;
  144. }
  145. @Override
  146. public Action[] getActions(Object target, Object sender) {
  147. return actions;
  148. }
  149. @Override
  150. public void handleAction(Action action, Object sender, Object target) {
  151. if (action == actions[1]) {
  152. al.addComponent(new Label("Delete selected on " + target));
  153. contextTree.removeItem(target);
  154. } else {
  155. al.addComponent(new Label("Edit selected on " + target));
  156. }
  157. }
  158. }