Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package com.vaadin.tests;
  2. import com.vaadin.event.Action;
  3. import com.vaadin.event.Action.Handler;
  4. import com.vaadin.ui.AbstractOrderedLayout;
  5. import com.vaadin.ui.Button;
  6. import com.vaadin.ui.Component;
  7. import com.vaadin.ui.CustomComponent;
  8. import com.vaadin.ui.HorizontalLayout;
  9. import com.vaadin.ui.Label;
  10. import com.vaadin.ui.Panel;
  11. import com.vaadin.ui.VerticalLayout;
  12. import com.vaadin.v7.ui.Tree;
  13. /**
  14. * Some test cases for trees. Events panel logs events that happen server side.
  15. *
  16. * @author Vaadin Ltd.
  17. */
  18. public class TestForTrees extends CustomComponent implements Handler {
  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. private final Action[] actions = { new Action("edit"),
  25. new Action("delete") };
  26. private VerticalLayout al;
  27. private Tree contextTree;
  28. public TestForTrees() {
  29. setCompositionRoot(main);
  30. createNewView();
  31. }
  32. public void createNewView() {
  33. main.removeAllComponents();
  34. main.addComponent(new Label(
  35. "Some test cases for trees. Events panel logs events that happen server side."));
  36. main.addComponent(new Button("commit"));
  37. Tree t;
  38. t = createTestTree();
  39. t.setCaption("Default settings");
  40. main.addComponent(createTestBench(t));
  41. t = createTestTree();
  42. t.setCaption("Multiselect settings");
  43. t.setMultiSelect(true);
  44. main.addComponent(createTestBench(t));
  45. t = createTestTree();
  46. t.setCaption("Multiselect and immediate");
  47. t.setImmediate(true);
  48. t.setMultiSelect(true);
  49. main.addComponent(createTestBench(t));
  50. t = createTestTree();
  51. t.setCaption("immediate");
  52. t.setImmediate(true);
  53. main.addComponent(createTestBench(t));
  54. t = createTestTree();
  55. t.setCaption("with actions");
  56. t.setImmediate(true);
  57. t.addActionHandler(this);
  58. final AbstractOrderedLayout ol = (AbstractOrderedLayout) createTestBench(
  59. t);
  60. al = new VerticalLayout();
  61. al.setMargin(true);
  62. ol.addComponent(new Panel("action log", al));
  63. main.addComponent(ol);
  64. contextTree = t;
  65. final Button b = new Button("refresh view", event -> createNewView());
  66. main.addComponent(b);
  67. }
  68. public Tree createTestTree() {
  69. Tree t = new Tree("Tree");
  70. final String[] names = new String[100];
  71. for (int i = 0; i < names.length; i++) {
  72. names[i] = firstnames[(int) (Math.random()
  73. * (firstnames.length - 1))] + " "
  74. + lastnames[(int) (Math.random() * (lastnames.length - 1))];
  75. }
  76. // Create tree
  77. t = new Tree("Organization Structure");
  78. for (int i = 0; i < 100; i++) {
  79. t.addItem(names[i]);
  80. final String parent = names[(int) (Math.random()
  81. * (names.length - 1))];
  82. if (t.containsId(parent)) {
  83. t.setParent(names[i], parent);
  84. }
  85. }
  86. // Forbid childless people to have children (makes them leaves)
  87. for (int i = 0; i < 100; i++) {
  88. if (!t.hasChildren(names[i])) {
  89. t.setChildrenAllowed(names[i], false);
  90. }
  91. }
  92. return t;
  93. }
  94. public Component createTestBench(Tree t) {
  95. final HorizontalLayout ol = new HorizontalLayout();
  96. ol.addComponent(t);
  97. final VerticalLayout statusLayout = new VerticalLayout();
  98. statusLayout.setMargin(true);
  99. final Panel status = new Panel("Events", statusLayout);
  100. final Button clear = new Button("c");
  101. clear.addClickListener(event -> {
  102. statusLayout.removeAllComponents();
  103. statusLayout.addComponent(clear);
  104. });
  105. statusLayout.addComponent(clear);
  106. status.setHeight("300px");
  107. status.setWidth("400px");
  108. ol.addComponent(status);
  109. t.addListener((Listener) event -> {
  110. statusLayout.addComponent(new Label(event.getClass().getName()));
  111. // TODO should not use LegacyField.toString()
  112. statusLayout.addComponent(
  113. new Label("selected: " + event.getSource().toString()));
  114. });
  115. return ol;
  116. }
  117. @Override
  118. public Action[] getActions(Object target, Object sender) {
  119. return actions;
  120. }
  121. @Override
  122. public void handleAction(Action action, Object sender, Object target) {
  123. if (action == actions[1]) {
  124. al.addComponent(new Label("Delete selected on " + target));
  125. contextTree.removeItem(target);
  126. } else {
  127. al.addComponent(new Label("Edit selected on " + target));
  128. }
  129. }
  130. }