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.

TreeGridBasicFeatures.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.vaadin.tests.components.treegrid;
  2. import java.util.Arrays;
  3. import java.util.LinkedHashMap;
  4. import java.util.List;
  5. import com.vaadin.annotations.Theme;
  6. import com.vaadin.annotations.Widgetset;
  7. import com.vaadin.data.HierarchyData;
  8. import com.vaadin.data.provider.DataProvider;
  9. import com.vaadin.data.provider.InMemoryHierarchicalDataProvider;
  10. import com.vaadin.tests.components.AbstractComponentTest;
  11. import com.vaadin.ui.TreeGrid;
  12. @Theme("valo")
  13. @Widgetset("com.vaadin.DefaultWidgetSet")
  14. public class TreeGridBasicFeatures extends AbstractComponentTest<TreeGrid> {
  15. private TreeGrid<HierarchicalTestBean> grid;
  16. private InMemoryHierarchicalDataProvider<HierarchicalTestBean> inMemoryDataProvider;
  17. private LazyHierarchicalDataProvider lazyDataProvider;
  18. @Override
  19. public TreeGrid getComponent() {
  20. return grid;
  21. }
  22. @Override
  23. protected Class<TreeGrid> getTestClass() {
  24. return TreeGrid.class;
  25. }
  26. @Override
  27. protected void initializeComponents() {
  28. initializeDataProviders();
  29. grid = new TreeGrid<>();
  30. grid.setSizeFull();
  31. grid.addColumn(HierarchicalTestBean::toString).setCaption("String")
  32. .setId("string");
  33. grid.addColumn(HierarchicalTestBean::getDepth).setCaption("Depth")
  34. .setId("depth");
  35. grid.addColumn(HierarchicalTestBean::getIndex)
  36. .setCaption("Index on this depth").setId("index");
  37. grid.setHierarchyColumn("string");
  38. grid.setDataProvider(new LazyHierarchicalDataProvider(3, 2));
  39. grid.setId("testComponent");
  40. addTestComponent(grid);
  41. }
  42. @Override
  43. protected void createActions() {
  44. super.createActions();
  45. createDataProviderSelect();
  46. createHierarchyColumnSelect();
  47. }
  48. private void initializeDataProviders() {
  49. HierarchyData<HierarchicalTestBean> data = new HierarchyData<>();
  50. List<Integer> ints = Arrays.asList(0, 1, 2);
  51. ints.stream().forEach(index -> {
  52. HierarchicalTestBean bean = new HierarchicalTestBean(null, 0,
  53. index);
  54. data.addItem(null, bean);
  55. ints.stream().forEach(childIndex -> {
  56. HierarchicalTestBean childBean = new HierarchicalTestBean(
  57. bean.getId(), 1, childIndex);
  58. data.addItem(bean, childBean);
  59. ints.stream()
  60. .forEach(grandChildIndex -> data.addItem(childBean,
  61. new HierarchicalTestBean(childBean.getId(), 2,
  62. grandChildIndex)));
  63. });
  64. });
  65. inMemoryDataProvider = new InMemoryHierarchicalDataProvider<>(data);
  66. lazyDataProvider = new LazyHierarchicalDataProvider(3, 2);
  67. }
  68. @SuppressWarnings("unchecked")
  69. private void createDataProviderSelect() {
  70. @SuppressWarnings("rawtypes")
  71. LinkedHashMap<String, DataProvider> options = new LinkedHashMap<>();
  72. options.put("LazyHierarchicalDataProvider", lazyDataProvider);
  73. options.put("InMemoryHierarchicalDataProvider", inMemoryDataProvider);
  74. createSelectAction("Set data provider", CATEGORY_FEATURES, options,
  75. "LazyHierarchicalDataProvider",
  76. (treeGrid, value, data) -> treeGrid.setDataProvider(value));
  77. }
  78. private void createHierarchyColumnSelect() {
  79. LinkedHashMap<String, String> options = new LinkedHashMap<>();
  80. grid.getColumns().stream()
  81. .forEach(column -> options.put(column.getId(), column.getId()));
  82. createSelectAction("Set hierarchy column", CATEGORY_FEATURES, options,
  83. grid.getColumns().get(0).getId(),
  84. (treeGrid, value, data) -> treeGrid.setHierarchyColumn(value));
  85. }
  86. static class HierarchicalTestBean {
  87. private final String id;
  88. private final int depth;
  89. private final int index;
  90. public HierarchicalTestBean(String parentId, int depth, int index) {
  91. id = (parentId == null ? "" : parentId) + "/" + depth + "/" + index;
  92. this.depth = depth;
  93. this.index = index;
  94. }
  95. public int getDepth() {
  96. return depth;
  97. }
  98. public int getIndex() {
  99. return index;
  100. }
  101. public String getId() {
  102. return id;
  103. }
  104. @Override
  105. public String toString() {
  106. return depth + " | " + index;
  107. }
  108. @Override
  109. public int hashCode() {
  110. final int prime = 31;
  111. int result = 1;
  112. result = prime * result + ((id == null) ? 0 : id.hashCode());
  113. return result;
  114. }
  115. @Override
  116. public boolean equals(Object obj) {
  117. if (this == obj) {
  118. return true;
  119. }
  120. if (obj == null) {
  121. return false;
  122. }
  123. if (getClass() != obj.getClass()) {
  124. return false;
  125. }
  126. HierarchicalTestBean other = (HierarchicalTestBean) obj;
  127. if (id == null) {
  128. if (other.id != null) {
  129. return false;
  130. }
  131. } else if (!id.equals(other.id)) {
  132. return false;
  133. }
  134. return true;
  135. }
  136. }
  137. }