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.

TreeGridExpandCollapseRecursively.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.vaadin.tests.components.treegrid;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collection;
  5. import java.util.List;
  6. import com.vaadin.annotations.Widgetset;
  7. import com.vaadin.server.VaadinRequest;
  8. import com.vaadin.tests.components.AbstractTestUI;
  9. import com.vaadin.ui.Button;
  10. import com.vaadin.ui.HorizontalLayout;
  11. import com.vaadin.ui.RadioButtonGroup;
  12. import com.vaadin.ui.TreeGrid;
  13. @Widgetset("com.vaadin.DefaultWidgetSet")
  14. public class TreeGridExpandCollapseRecursively extends AbstractTestUI {
  15. private static class Directory {
  16. private String name;
  17. private Directory parent;
  18. private List<Directory> subDirectories = new ArrayList<>();
  19. public Directory(String name, Directory parent) {
  20. this.name = name;
  21. this.parent = parent;
  22. }
  23. public String getName() {
  24. return name;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. public Directory getParent() {
  30. return parent;
  31. }
  32. public void setParent(Directory parent) {
  33. this.parent = parent;
  34. }
  35. public List<Directory> getSubDirectories() {
  36. return subDirectories;
  37. }
  38. public void setSubDirectories(List<Directory> subDirectories) {
  39. this.subDirectories = subDirectories;
  40. }
  41. }
  42. private static final int DEPTH = 4;
  43. private static final int CHILDREN = 5;
  44. @Override
  45. protected void setup(VaadinRequest request) {
  46. Collection<Directory> roots = generateDirectoryStructure(DEPTH);
  47. TreeGrid<Directory> grid = new TreeGrid<>();
  48. grid.addColumn(item -> "Item" + item.getName());
  49. grid.setItems(roots, Directory::getSubDirectories);
  50. RadioButtonGroup<Integer> depthSelector = new RadioButtonGroup<>(
  51. "Depth", Arrays.asList(0, 1, 2, 3));
  52. depthSelector.addStyleName("horizontal");
  53. depthSelector.setValue(3);
  54. HorizontalLayout buttons = new HorizontalLayout();
  55. buttons.addComponent(new Button("Expand recursively",
  56. e -> grid.expandRecursively(roots, depthSelector.getValue())));
  57. buttons.addComponent(new Button("Collapse recursively", e -> grid
  58. .collapseRecursively(roots, depthSelector.getValue())));
  59. addComponents(depthSelector, buttons, grid);
  60. }
  61. private Collection<Directory> generateDirectoryStructure(int depth) {
  62. return generateDirectories(depth, null, CHILDREN);
  63. }
  64. private Collection<Directory> generateDirectories(int depth,
  65. Directory parent, int childCount) {
  66. Collection<Directory> dirs = new ArrayList<>();
  67. if (depth >= 0) {
  68. for (int i = 0; i < childCount; i++) {
  69. String name = parent != null ? parent.getName() + "-" + i
  70. : "-" + i;
  71. Directory dir = new Directory(name, parent);
  72. if (parent != null) {
  73. parent.getSubDirectories().add(dir);
  74. }
  75. dirs.add(dir);
  76. generateDirectories(depth - 1, dir, childCount);
  77. }
  78. }
  79. return dirs;
  80. }
  81. }