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.

HasItems.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import java.util.stream.Collectors;
  21. import java.util.stream.Stream;
  22. import com.vaadin.data.provider.BackEndDataProvider;
  23. import com.vaadin.data.provider.DataProvider;
  24. import com.vaadin.data.provider.ListDataProvider;
  25. import com.vaadin.ui.Component;
  26. /**
  27. * A component that displays a collection of items.
  28. *
  29. * @author Vaadin Ltd
  30. *
  31. * @since 8.0
  32. *
  33. * @param <T>
  34. * the type of the displayed item
  35. */
  36. public interface HasItems<T> extends Component {
  37. /**
  38. * Returns the source of data items used by this listing.
  39. *
  40. * @return the data provider, not null
  41. */
  42. public DataProvider<T, ?> getDataProvider();
  43. /**
  44. * Sets the data items of this component provided as a collection.
  45. * <p>
  46. * The provided items are wrapped into a {@link ListDataProvider} and this
  47. * instance is used as a data provider for the
  48. * {@link #setDataProvider(DataProvider)} method. It means that the items
  49. * collection can be accessed later on via
  50. * {@link ListDataProvider#getItems()}:
  51. *
  52. * <pre>
  53. * <code>
  54. * HasDataProvider&lt;String&gt; listing = new CheckBoxGroup&lt;&gt;();
  55. * listing.setItems(Arrays.asList("a","b"));
  56. * ...
  57. *
  58. * Collection<String> collection = ((ListDataProvider<String>)listing.getDataProvider()).getItems();
  59. * </code>
  60. * </pre>
  61. * <p>
  62. * The provided collection instance may be used as-is. Subsequent
  63. * modification of the collection might cause inconsistent data to be shown
  64. * in the component unless it is explicitly instructed to read the data
  65. * again.
  66. *
  67. * @param items
  68. * the data items to display, not null
  69. *
  70. */
  71. public void setItems(Collection<T> items);
  72. /**
  73. * Sets the data items of this listing.
  74. * <p>
  75. * The provided items are wrapped into a {@link ListDataProvider} and this
  76. * instance is used as a data provider for the
  77. * {@link #setDataProvider(DataProvider)} method. It means that the items
  78. * collection can be accessed later on via
  79. * {@link ListDataProvider#getItems()}:
  80. *
  81. * <pre>
  82. * <code>
  83. * HasDataProvider<String> listing = new CheckBoxGroup<>();
  84. * listing.setItems("a","b");
  85. * ...
  86. *
  87. * Collection<String> collection = ((ListDataProvider<String>)listing.getDataProvider()).getItems();
  88. * </code>
  89. * </pre>
  90. * <p>
  91. *
  92. * @see #setItems(Collection)
  93. *
  94. * @param items
  95. * the data items to display
  96. */
  97. public default void setItems(@SuppressWarnings("unchecked") T... items) {
  98. setItems(new ArrayList<>(Arrays.asList(items)));
  99. }
  100. /**
  101. * Sets the data items of this listing provided as a stream.
  102. * <p>
  103. * This is just a shorthand for {@link #setItems(Collection)}, that
  104. * <b>collects objects in the stream to a list</b>. Thus, using this method,
  105. * instead of its array and Collection variations, doesn't save any memory.
  106. * If you have a large data set to bind, using a lazy data provider is
  107. * recommended. See {@link BackEndDataProvider} for more info.
  108. * <p>
  109. * The provided items are wrapped into a {@link ListDataProvider} and this
  110. * instance is used as a data provider for the
  111. * {@link #setDataProvider(DataProvider)} method. It means that the items
  112. * collection can be accessed later on via
  113. * {@link ListDataProvider#getItems()}:
  114. *
  115. * <pre>
  116. * <code>
  117. * HasDataProvider&lt;String&gt; listing = new CheckBoxGroup<&gt;();
  118. * listing.setItems(Stream.of("a","b"));
  119. * ...
  120. *
  121. * Collection<String> collection = ((ListDataProvider&lt;String&gt;)listing.getDataProvider()).getItems();
  122. * </code>
  123. * </pre>
  124. * <p>
  125. *
  126. * @see #setItems(Collection)
  127. *
  128. * @param streamOfItems
  129. * the stream of data items to display, not {@code null}
  130. */
  131. public default void setItems(Stream<T> streamOfItems) {
  132. setItems(streamOfItems.collect(Collectors.toList()));
  133. }
  134. }