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.

HierarchicalDataCommunicator.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright 2000-2016 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.data.provider;
  17. import java.util.Comparator;
  18. import java.util.List;
  19. import java.util.Objects;
  20. import java.util.Optional;
  21. import java.util.stream.Collectors;
  22. import java.util.stream.Stream;
  23. import com.vaadin.data.TreeData;
  24. import com.vaadin.server.SerializableConsumer;
  25. import com.vaadin.shared.Range;
  26. import com.vaadin.shared.extension.datacommunicator.HierarchicalDataCommunicatorState;
  27. import com.vaadin.ui.ItemCollapseAllowedProvider;
  28. /**
  29. * Data communicator that handles requesting hierarchical data from
  30. * {@link HierarchicalDataProvider} and sending it to client side.
  31. *
  32. * @param <T>
  33. * the bean type
  34. * @author Vaadin Ltd
  35. * @since 8.1
  36. */
  37. public class HierarchicalDataCommunicator<T> extends DataCommunicator<T> {
  38. private HierarchyMapper<T, ?> mapper;
  39. /**
  40. * Collapse allowed provider used to allow/disallow collapsing nodes.
  41. */
  42. private ItemCollapseAllowedProvider<T> itemCollapseAllowedProvider = t -> true;
  43. /**
  44. * Construct a new hierarchical data communicator backed by a
  45. * {@link TreeDataProvider}.
  46. */
  47. public HierarchicalDataCommunicator() {
  48. super();
  49. setDataProvider(new TreeDataProvider<>(new TreeData<>()), null);
  50. }
  51. @Override
  52. protected HierarchicalDataCommunicatorState getState() {
  53. return (HierarchicalDataCommunicatorState) super.getState();
  54. }
  55. @Override
  56. protected HierarchicalDataCommunicatorState getState(boolean markAsDirty) {
  57. return (HierarchicalDataCommunicatorState) super.getState(markAsDirty);
  58. }
  59. @Override
  60. protected List<T> fetchItemsWithRange(int offset, int limit) {
  61. // Instead of adding logic to this class, delegate request to the
  62. // separate object handling hierarchies.
  63. return mapper.fetchItems(Range.withLength(offset, limit))
  64. .collect(Collectors.toList());
  65. }
  66. @Override
  67. public HierarchicalDataProvider<T, ?> getDataProvider() {
  68. return (HierarchicalDataProvider<T, ?>) super.getDataProvider();
  69. }
  70. /**
  71. * Set the current hierarchical data provider for this communicator.
  72. *
  73. * @param dataProvider
  74. * the data provider to set, not <code>null</code>
  75. * @param initialFilter
  76. * the initial filter value to use, or <code>null</code> to not
  77. * use any initial filter value
  78. *
  79. * @param <F>
  80. * the filter type
  81. *
  82. * @return a consumer that accepts a new filter value to use
  83. */
  84. public <F> SerializableConsumer<F> setDataProvider(
  85. HierarchicalDataProvider<T, F> dataProvider, F initialFilter) {
  86. SerializableConsumer<F> consumer = super.setDataProvider(dataProvider,
  87. initialFilter);
  88. // Remove old mapper
  89. if (mapper != null) {
  90. removeDataGenerator(mapper);
  91. }
  92. mapper = new HierarchyMapper<>(dataProvider);
  93. // Set up mapper for requests
  94. mapper.setBackEndSorting(getBackEndSorting());
  95. mapper.setInMemorySorting(getInMemorySorting());
  96. mapper.setFilter(getFilter());
  97. mapper.setItemCollapseAllowedProvider(getItemCollapseAllowedProvider());
  98. // Provide hierarchy data to json
  99. addDataGenerator(mapper);
  100. return consumer;
  101. }
  102. /**
  103. * Set the current hierarchical data provider for this communicator.
  104. *
  105. * @param dataProvider
  106. * the data provider to set, must extend
  107. * {@link HierarchicalDataProvider}, not <code>null</code>
  108. * @param initialFilter
  109. * the initial filter value to use, or <code>null</code> to not
  110. * use any initial filter value
  111. *
  112. * @param <F>
  113. * the filter type
  114. *
  115. * @return a consumer that accepts a new filter value to use
  116. */
  117. @Override
  118. public <F> SerializableConsumer<F> setDataProvider(
  119. DataProvider<T, F> dataProvider, F initialFilter) {
  120. if (dataProvider instanceof HierarchicalDataProvider) {
  121. return setDataProvider(
  122. (HierarchicalDataProvider<T, F>) dataProvider,
  123. initialFilter);
  124. }
  125. throw new IllegalArgumentException(
  126. "Only " + HierarchicalDataProvider.class.getName()
  127. + " and subtypes supported.");
  128. }
  129. /**
  130. * Collapses given item, removing all its subtrees. Calling this method will
  131. * have no effect if the row is already collapsed.
  132. *
  133. * @param item
  134. * the item to collapse
  135. */
  136. public void collapse(T item) {
  137. if (mapper.isExpanded(item)) {
  138. doCollapse(item, mapper.getIndexOf(item));
  139. }
  140. }
  141. /**
  142. * Collapses given item, removing all its subtrees. Calling this method will
  143. * have no effect if the row is already collapsed. The index is provided by
  144. * the client-side or calculated from a full data request.
  145. *
  146. * @see #collapse(Object)
  147. *
  148. * @param item
  149. * the item to collapse
  150. * @param index
  151. * the index of the item
  152. */
  153. public void doCollapse(T item, Optional<Integer> index) {
  154. if (mapper.isExpanded(item)) {
  155. Range removedRows = mapper.doCollapse(item, index);
  156. if (!reset && !removedRows.isEmpty()) {
  157. getClientRpc().removeRows(removedRows.getStart(),
  158. removedRows.length());
  159. }
  160. refresh(item);
  161. }
  162. }
  163. /**
  164. * Expands the given item. Calling this method will have no effect if the
  165. * row is already expanded.
  166. *
  167. * @param item
  168. * the item to expand
  169. */
  170. public void expand(T item) {
  171. if (!mapper.isExpanded(item) && mapper.hasChildren(item)) {
  172. doExpand(item, mapper.getIndexOf(item));
  173. }
  174. }
  175. /**
  176. * Expands the given item at given index. Calling this method will have no
  177. * effect if the row is already expanded. The index is provided by the
  178. * client-side or calculated from a full data request.
  179. *
  180. * @see #expand(Object)
  181. *
  182. * @param item
  183. * the item to expand
  184. * @param index
  185. * the index of the item
  186. */
  187. public void doExpand(T item, Optional<Integer> index) {
  188. if (!mapper.isExpanded(item)) {
  189. Range addedRows = mapper.doExpand(item, index);
  190. if (!reset && !addedRows.isEmpty()) {
  191. int start = addedRows.getStart();
  192. getClientRpc().insertRows(start, addedRows.length());
  193. Stream<T> children = mapper.fetchItems(item,
  194. Range.withLength(0, addedRows.length()));
  195. pushData(start, children.collect(Collectors.toList()));
  196. }
  197. refresh(item);
  198. }
  199. }
  200. /**
  201. * Returns whether given item has children.
  202. *
  203. * @param item
  204. * the item to test
  205. * @return {@code true} if item has children; {@code false} if not
  206. */
  207. public boolean hasChildren(T item) {
  208. return mapper.hasChildren(item);
  209. }
  210. /**
  211. * Returns whether given item is expanded.
  212. *
  213. * @param item
  214. * the item to test
  215. * @return {@code true} if item is expanded; {@code false} if not
  216. */
  217. public boolean isExpanded(T item) {
  218. return mapper.isExpanded(item);
  219. }
  220. /**
  221. * Sets the item collapse allowed provider for this
  222. * HierarchicalDataCommunicator. The provider should return {@code true} for
  223. * any item that the user can collapse.
  224. * <p>
  225. * <strong>Note:</strong> This callback will be accessed often when sending
  226. * data to the client. The callback should not do any costly operations.
  227. *
  228. * @param provider
  229. * the item collapse allowed provider, not {@code null}
  230. */
  231. public void setItemCollapseAllowedProvider(
  232. ItemCollapseAllowedProvider<T> provider) {
  233. Objects.requireNonNull(provider, "Provider can't be null");
  234. itemCollapseAllowedProvider = provider;
  235. // Update hierarchy mapper
  236. mapper.setItemCollapseAllowedProvider(provider);
  237. getActiveDataHandler().getActiveData().values().forEach(this::refresh);
  238. }
  239. /**
  240. * Returns parent index for the row or {@code null}.
  241. *
  242. * @param item
  243. * the item to find the parent of
  244. * @return the parent index or {@code null} for top-level items
  245. */
  246. public Integer getParentIndex(T item) {
  247. return mapper.getParentIndex(item);
  248. }
  249. /**
  250. * Gets the item collapse allowed provider.
  251. *
  252. * @return the item collapse allowed provider
  253. */
  254. public ItemCollapseAllowedProvider<T> getItemCollapseAllowedProvider() {
  255. return itemCollapseAllowedProvider;
  256. }
  257. @Override
  258. public int getDataProviderSize() {
  259. return mapper.getTreeSize();
  260. }
  261. @Override
  262. public void setBackEndSorting(List<QuerySortOrder> sortOrder) {
  263. if (mapper != null) {
  264. mapper.setBackEndSorting(sortOrder);
  265. }
  266. super.setBackEndSorting(sortOrder);
  267. }
  268. @Override
  269. public void setInMemorySorting(Comparator<T> comparator) {
  270. if (mapper != null) {
  271. mapper.setInMemorySorting(comparator);
  272. }
  273. super.setInMemorySorting(comparator);
  274. }
  275. @Override
  276. protected <F> void setFilter(F filter) {
  277. if (mapper != null) {
  278. mapper.setFilter(filter);
  279. }
  280. super.setFilter(filter);
  281. }
  282. }