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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. public 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 = createHierarchyMapper(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. * Create new {@code HierarchyMapper} for the given data provider.
  104. * May be overridden in subclasses.
  105. *
  106. * @param dataProvider the data provider
  107. * @param <F> Query type
  108. * @return new {@link HierarchyMapper}
  109. */
  110. protected <F> HierarchyMapper<T, F> createHierarchyMapper(HierarchicalDataProvider<T, F> dataProvider) {
  111. return new HierarchyMapper<>(dataProvider);
  112. }
  113. /**
  114. * Set the current hierarchical data provider for this communicator.
  115. *
  116. * @param dataProvider
  117. * the data provider to set, must extend
  118. * {@link HierarchicalDataProvider}, not <code>null</code>
  119. * @param initialFilter
  120. * the initial filter value to use, or <code>null</code> to not
  121. * use any initial filter value
  122. *
  123. * @param <F>
  124. * the filter type
  125. *
  126. * @return a consumer that accepts a new filter value to use
  127. */
  128. @Override
  129. public <F> SerializableConsumer<F> setDataProvider(
  130. DataProvider<T, F> dataProvider, F initialFilter) {
  131. if (dataProvider instanceof HierarchicalDataProvider) {
  132. return setDataProvider(
  133. (HierarchicalDataProvider<T, F>) dataProvider,
  134. initialFilter);
  135. }
  136. throw new IllegalArgumentException(
  137. "Only " + HierarchicalDataProvider.class.getName()
  138. + " and subtypes supported.");
  139. }
  140. /**
  141. * Collapses given item, removing all its subtrees. Calling this method will
  142. * have no effect if the row is already collapsed.
  143. *
  144. * @param item
  145. * the item to collapse
  146. */
  147. public void collapse(T item) {
  148. if (mapper.isExpanded(item)) {
  149. doCollapse(item, mapper.getIndexOf(item));
  150. }
  151. }
  152. /**
  153. * Collapses given item, removing all its subtrees. Calling this method will
  154. * have no effect if the row is already collapsed. The index is provided by
  155. * the client-side or calculated from a full data request.
  156. *
  157. * @see #collapse(Object)
  158. *
  159. * @param item
  160. * the item to collapse
  161. * @param index
  162. * the index of the item
  163. */
  164. public void doCollapse(T item, Optional<Integer> index) {
  165. if (mapper.isExpanded(item)) {
  166. Range removedRows = mapper.doCollapse(item, index);
  167. if (!reset && !removedRows.isEmpty()) {
  168. getClientRpc().removeRows(removedRows.getStart(),
  169. removedRows.length());
  170. }
  171. refresh(item);
  172. }
  173. }
  174. /**
  175. * Expands the given item. Calling this method will have no effect if the
  176. * row is already expanded.
  177. *
  178. * @param item
  179. * the item to expand
  180. */
  181. public void expand(T item) {
  182. if (!mapper.isExpanded(item) && mapper.hasChildren(item)) {
  183. doExpand(item, mapper.getIndexOf(item));
  184. }
  185. }
  186. /**
  187. * Expands the given item at given index. Calling this method will have no
  188. * effect if the row is already expanded. The index is provided by the
  189. * client-side or calculated from a full data request.
  190. *
  191. * @see #expand(Object)
  192. *
  193. * @param item
  194. * the item to expand
  195. * @param index
  196. * the index of the item
  197. */
  198. public void doExpand(T item, Optional<Integer> index) {
  199. if (!mapper.isExpanded(item)) {
  200. Range addedRows = mapper.doExpand(item, index);
  201. if (!reset && !addedRows.isEmpty()) {
  202. int start = addedRows.getStart();
  203. getClientRpc().insertRows(start, addedRows.length());
  204. Stream<T> children = mapper.fetchItems(item,
  205. Range.withLength(0, addedRows.length()));
  206. pushData(start, children.collect(Collectors.toList()));
  207. }
  208. refresh(item);
  209. }
  210. }
  211. /**
  212. * Returns whether given item has children.
  213. *
  214. * @param item
  215. * the item to test
  216. * @return {@code true} if item has children; {@code false} if not
  217. */
  218. public boolean hasChildren(T item) {
  219. return mapper.hasChildren(item);
  220. }
  221. /**
  222. * Returns whether given item is expanded.
  223. *
  224. * @param item
  225. * the item to test
  226. * @return {@code true} if item is expanded; {@code false} if not
  227. */
  228. public boolean isExpanded(T item) {
  229. return mapper.isExpanded(item);
  230. }
  231. /**
  232. * Sets the item collapse allowed provider for this
  233. * HierarchicalDataCommunicator. The provider should return {@code true} for
  234. * any item that the user can collapse.
  235. * <p>
  236. * <strong>Note:</strong> This callback will be accessed often when sending
  237. * data to the client. The callback should not do any costly operations.
  238. *
  239. * @param provider
  240. * the item collapse allowed provider, not {@code null}
  241. */
  242. public void setItemCollapseAllowedProvider(
  243. ItemCollapseAllowedProvider<T> provider) {
  244. Objects.requireNonNull(provider, "Provider can't be null");
  245. itemCollapseAllowedProvider = provider;
  246. // Update hierarchy mapper
  247. mapper.setItemCollapseAllowedProvider(provider);
  248. getActiveDataHandler().getActiveData().values().forEach(this::refresh);
  249. }
  250. /**
  251. * Returns parent index for the row or {@code null}.
  252. *
  253. * @param item
  254. * the item to find the parent of
  255. * @return the parent index or {@code null} for top-level items
  256. */
  257. public Integer getParentIndex(T item) {
  258. return mapper.getParentIndex(item);
  259. }
  260. /**
  261. * Gets the item collapse allowed provider.
  262. *
  263. * @return the item collapse allowed provider
  264. */
  265. public ItemCollapseAllowedProvider<T> getItemCollapseAllowedProvider() {
  266. return itemCollapseAllowedProvider;
  267. }
  268. @Override
  269. public int getDataProviderSize() {
  270. return mapper.getTreeSize();
  271. }
  272. @Override
  273. public void setBackEndSorting(List<QuerySortOrder> sortOrder) {
  274. if (mapper != null) {
  275. mapper.setBackEndSorting(sortOrder);
  276. }
  277. super.setBackEndSorting(sortOrder);
  278. }
  279. @Override
  280. public void setInMemorySorting(Comparator<T> comparator) {
  281. if (mapper != null) {
  282. mapper.setInMemorySorting(comparator);
  283. }
  284. super.setInMemorySorting(comparator);
  285. }
  286. @Override
  287. protected <F> void setFilter(F filter) {
  288. if (mapper != null) {
  289. mapper.setFilter(filter);
  290. }
  291. super.setFilter(filter);
  292. }
  293. /**
  294. * Returns active {@code HierarchyMapper}
  295. *
  296. * @return the mapper
  297. */
  298. protected HierarchyMapper<T, ?> getMapper() {
  299. return mapper;
  300. }
  301. }