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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright 2000-2018 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. May be
  104. * overridden in subclasses.
  105. *
  106. * @param dataProvider
  107. * the data provider
  108. * @param <F>
  109. * Query type
  110. * @return new {@link HierarchyMapper}
  111. */
  112. protected <F> HierarchyMapper<T, F> createHierarchyMapper(
  113. HierarchicalDataProvider<T, F> dataProvider) {
  114. return new HierarchyMapper<>(dataProvider);
  115. }
  116. /**
  117. * Set the current hierarchical data provider for this communicator.
  118. *
  119. * @param dataProvider
  120. * the data provider to set, must extend
  121. * {@link HierarchicalDataProvider}, not <code>null</code>
  122. * @param initialFilter
  123. * the initial filter value to use, or <code>null</code> to not
  124. * use any initial filter value
  125. *
  126. * @param <F>
  127. * the filter type
  128. *
  129. * @return a consumer that accepts a new filter value to use
  130. */
  131. @Override
  132. public <F> SerializableConsumer<F> setDataProvider(
  133. DataProvider<T, F> dataProvider, F initialFilter) {
  134. if (dataProvider instanceof HierarchicalDataProvider) {
  135. return setDataProvider(
  136. (HierarchicalDataProvider<T, F>) dataProvider,
  137. initialFilter);
  138. }
  139. throw new IllegalArgumentException(
  140. "Only " + HierarchicalDataProvider.class.getName()
  141. + " and subtypes supported.");
  142. }
  143. /**
  144. * Collapses the given item and removes its sub-hierarchy. Calling this
  145. * method will have no effect if the row is already collapsed.
  146. *
  147. * @param item
  148. * the item to collapse
  149. */
  150. public void collapse(T item) {
  151. collapse(item, true);
  152. }
  153. /**
  154. * Collapses the given item and removes its sub-hierarchy. Calling this
  155. * method will have no effect if the row is already collapsed.
  156. * {@code syncAndRefresh} indicates whether the changes should be
  157. * synchronised to the client and the data provider be notified.
  158. *
  159. * @param item
  160. * the item to collapse
  161. * @param syncAndRefresh
  162. * {@code true} if the changes should be synchronised to the
  163. * client and the data provider should be notified of the
  164. * changes, {@code false} otherwise.
  165. */
  166. public void collapse(T item, boolean syncAndRefresh) {
  167. Integer index = syncAndRefresh ? mapper.getIndexOf(item).orElse(null)
  168. : null;
  169. doCollapse(item, index, syncAndRefresh);
  170. }
  171. /**
  172. * Collapses the given item and removes its sub-hierarchy. Calling this
  173. * method will have no effect if the row is already collapsed.
  174. *
  175. * @param item
  176. * the item to collapse
  177. * @param index
  178. * the index of the item
  179. */
  180. public void collapse(T item, Integer index) {
  181. doCollapse(item, index, true);
  182. }
  183. /**
  184. * Collapses given item and removes its sub-hierarchy. Calling this method
  185. * will have no effect if the row is already collapsed. The index is
  186. * provided by the client-side or calculated from a full data request.
  187. *
  188. *
  189. * @param item
  190. * the item to collapse
  191. * @param index
  192. * the index of the item
  193. * @deprecated Use {@link #collapse(Object, Integer)} instead.
  194. */
  195. @Deprecated
  196. public void doCollapse(T item, Optional<Integer> index) {
  197. doCollapse(item, index.orElse(null), true);
  198. }
  199. /**
  200. * Collapses the given item and removes its sub-hierarchy. Calling this
  201. * method will have no effect if the row is already collapsed. The index is
  202. * provided by the client-side or calculated from a full data request.
  203. * {@code syncAndRefresh} indicates whether the changes should be
  204. * synchronised to the client and the data provider be notified.
  205. *
  206. * @param item
  207. * the item to collapse
  208. * @param index
  209. * the index of the item
  210. * @param syncAndRefresh
  211. * {@code true} if the changes should be synchronised to the
  212. * client and the data provider should be notified of the
  213. * changes, {@code false} otherwise.
  214. */
  215. private void doCollapse(T item, Integer index, boolean syncAndRefresh) {
  216. Range removedRows = mapper.collapse(item, index);
  217. if (syncAndRefresh) {
  218. if (!reset && !removedRows.isEmpty()) {
  219. getClientRpc().removeRows(removedRows.getStart(),
  220. removedRows.length());
  221. }
  222. refresh(item);
  223. }
  224. }
  225. /**
  226. * Expands the given item. Calling this method will have no effect if the
  227. * item is already expanded or if it has no children.
  228. *
  229. * @param item
  230. * the item to expand
  231. */
  232. public void expand(T item) {
  233. expand(item, true);
  234. }
  235. /**
  236. * Expands the given item. Calling this method will have no effect if the
  237. * item is already expanded or if it has no children. {@code syncAndRefresh}
  238. * indicates whether the changes should be synchronised to the client and
  239. * the data provider be notified.
  240. *
  241. * @param item
  242. * the item to expand
  243. * @param syncAndRefresh
  244. * {@code true} if the changes should be synchronised to the
  245. * client and the data provider should be notified of the
  246. * changes, {@code
  247. * false} otherwise.
  248. */
  249. public void expand(T item, boolean syncAndRefresh) {
  250. Integer index = syncAndRefresh ? mapper.getIndexOf(item).orElse(null)
  251. : null;
  252. doExpand(item, index, syncAndRefresh);
  253. }
  254. /**
  255. * Expands the given item at the given index. Calling this method will have
  256. * no effect if the item is already expanded.
  257. *
  258. * @param item
  259. * the item to expand
  260. * @param index
  261. * the index of the item
  262. */
  263. public void expand(T item, Integer index) {
  264. doExpand(item, index, true);
  265. }
  266. /**
  267. * Expands the given item. Calling this method will have no effect if the
  268. * item is already expanded or if it has no children. The index is provided
  269. * by the client-side or calculated from a full data request.
  270. * {@code syncAndRefresh} indicates whether the changes should be
  271. * synchronised to the client and the data provider be notified.
  272. *
  273. * @param item
  274. * the item to expand
  275. * @param index
  276. * the index of the item
  277. * @param syncAndRefresh
  278. * {@code true} if the changes should be synchronised to the
  279. * client and the data provider should be notified of the
  280. * changes, {@code false} otherwise.
  281. */
  282. private void doExpand(T item, Integer index, boolean syncAndRefresh) {
  283. Range addedRows = mapper.expand(item, index);
  284. if (syncAndRefresh) {
  285. if (!reset && !addedRows.isEmpty()) {
  286. getClientRpc().insertRows(addedRows.getStart(),
  287. addedRows.length());
  288. Stream<T> children = mapper.fetchItems(item,
  289. Range.withLength(0, addedRows.length()));
  290. pushData(addedRows.getStart(),
  291. children.collect(Collectors.toList()));
  292. }
  293. refresh(item);
  294. }
  295. }
  296. /**
  297. * Expands the given item at given index. Calling this method will have no
  298. * effect if the row is already expanded. The index is provided by the
  299. * client-side or calculated from a full data request.
  300. *
  301. * @param item
  302. * the item to expand
  303. * @param index
  304. * the index of the item
  305. * @see #expand(Object)
  306. * @deprecated use {@link #expand(Object, Integer)} instead
  307. */
  308. @Deprecated
  309. public void doExpand(T item, Optional<Integer> index) {
  310. expand(item, index.orElse(null));
  311. }
  312. /**
  313. * Returns whether given item has children.
  314. *
  315. * @param item
  316. * the item to test
  317. * @return {@code true} if item has children; {@code false} if not
  318. */
  319. public boolean hasChildren(T item) {
  320. return mapper.hasChildren(item);
  321. }
  322. /**
  323. * Returns whether given item is expanded.
  324. *
  325. * @param item
  326. * the item to test
  327. * @return {@code true} if item is expanded; {@code false} if not
  328. */
  329. public boolean isExpanded(T item) {
  330. return mapper.isExpanded(item);
  331. }
  332. /**
  333. * Sets the item collapse allowed provider for this
  334. * HierarchicalDataCommunicator. The provider should return {@code true} for
  335. * any item that the user can collapse.
  336. * <p>
  337. * <strong>Note:</strong> This callback will be accessed often when sending
  338. * data to the client. The callback should not do any costly operations.
  339. *
  340. * @param provider
  341. * the item collapse allowed provider, not {@code null}
  342. */
  343. public void setItemCollapseAllowedProvider(
  344. ItemCollapseAllowedProvider<T> provider) {
  345. Objects.requireNonNull(provider, "Provider can't be null");
  346. itemCollapseAllowedProvider = provider;
  347. // Update hierarchy mapper
  348. mapper.setItemCollapseAllowedProvider(provider);
  349. getActiveDataHandler().getActiveData().values().forEach(this::refresh);
  350. }
  351. /**
  352. * Returns parent index for the row or a negative value.
  353. *
  354. * @param item
  355. * the item to find the parent of
  356. * @return the parent index or a negative value for top-level items
  357. */
  358. public Integer getParentIndex(T item) {
  359. return mapper.getParentIndex(item);
  360. }
  361. /**
  362. * Gets the item collapse allowed provider.
  363. *
  364. * @return the item collapse allowed provider
  365. */
  366. public ItemCollapseAllowedProvider<T> getItemCollapseAllowedProvider() {
  367. return itemCollapseAllowedProvider;
  368. }
  369. @Override
  370. public int getDataProviderSize() {
  371. return mapper.getTreeSize();
  372. }
  373. @Override
  374. public void setBackEndSorting(List<QuerySortOrder> sortOrder,
  375. boolean immediateReset) {
  376. if (mapper != null) {
  377. mapper.setBackEndSorting(sortOrder);
  378. }
  379. super.setBackEndSorting(sortOrder, immediateReset);
  380. }
  381. @Override
  382. public void setBackEndSorting(List<QuerySortOrder> sortOrder) {
  383. setBackEndSorting(sortOrder, true);
  384. }
  385. @Override
  386. public void setInMemorySorting(Comparator<T> comparator,
  387. boolean immediateReset) {
  388. if (mapper != null) {
  389. mapper.setInMemorySorting(comparator);
  390. }
  391. super.setInMemorySorting(comparator, immediateReset);
  392. }
  393. @Override
  394. public void setInMemorySorting(Comparator<T> comparator) {
  395. setInMemorySorting(comparator, true);
  396. }
  397. @Override
  398. protected <F> void setFilter(F filter) {
  399. if (mapper != null) {
  400. mapper.setFilter(filter);
  401. }
  402. super.setFilter(filter);
  403. }
  404. /**
  405. * Returns the {@code HierarchyMapper} used by this data communicator.
  406. *
  407. * @return the hierarchy mapper used by this data communicator
  408. */
  409. protected HierarchyMapper<T, ?> getHierarchyMapper() {
  410. return mapper;
  411. }
  412. }