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.

GridMultiSelect.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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.ui.components.grid;
  17. import java.util.Optional;
  18. import java.util.Set;
  19. import com.vaadin.event.selection.MultiSelectionListener;
  20. import com.vaadin.event.selection.SelectionListener;
  21. import com.vaadin.shared.Registration;
  22. import com.vaadin.ui.Grid;
  23. import com.vaadin.ui.MultiSelect;
  24. import com.vaadin.ui.components.grid.MultiSelectionModel.SelectAllCheckBoxVisibility;
  25. /**
  26. * Wrapper class to wrap Grid into a MultiSelect. This class also provides
  27. * useful access to API of MultiSelectionModel.
  28. *
  29. * @param <T>
  30. * the bean type of grid
  31. * @since 8.6.0
  32. */
  33. public class GridMultiSelect<T> implements MultiSelect<T> {
  34. private MultiSelectionModel<T> model;
  35. /**
  36. * Constructs a MultiSelect wrapper for given Grid.
  37. *
  38. * @param grid
  39. * the grid to wrap
  40. */
  41. public GridMultiSelect(Grid<T> grid) {
  42. GridSelectionModel<T> selectionModel = grid.getSelectionModel();
  43. if (!(selectionModel instanceof MultiSelectionModel)) {
  44. throw new IllegalStateException(
  45. "Grid is not in multiselect mode, it needs to be explicitly set to such with setSelectionModel(MultiSelectionModel) before being able to use multiselection features.");
  46. }
  47. model = (MultiSelectionModel<T>) selectionModel;
  48. }
  49. /* API for MultiSelectionModelImpl */
  50. /**
  51. * Get first selected data item.
  52. *
  53. * @return the first selected item.
  54. */
  55. public Optional<T> getFirstSelectedItem() {
  56. return model.getFirstSelectedItem();
  57. }
  58. /**
  59. * Selects all available the items.
  60. */
  61. public void selectAll() {
  62. model.selectAll();
  63. }
  64. /**
  65. * Selects the given item. Depending on the implementation, may cause other
  66. * items to be deselected. If the item is already selected, does nothing.
  67. *
  68. * @param item
  69. * the item to select, not null
  70. */
  71. public void deselect(T item) {
  72. model.deselect(item);
  73. }
  74. /**
  75. * Selects the given item. If another item was already selected, that item
  76. * is deselected.
  77. *
  78. * @param item
  79. * the item to select
  80. */
  81. public void select(T item) {
  82. model.select(item);
  83. }
  84. /**
  85. * Deselects all currently selected items, if any.
  86. */
  87. public void deselectAll() {
  88. model.deselectAll();
  89. }
  90. /**
  91. * Adds the given items to the set of currently selected items.
  92. * <p>
  93. * By default this does not clear any previous selection. To do that, use
  94. * {@link #deselectAll()}.
  95. * <p>
  96. * If the all the items were already selected, this is a NO-OP.
  97. * <p>
  98. * This is a short-hand for {@link #updateSelection(Set, Set)} with nothing
  99. * to deselect.
  100. *
  101. * @param items
  102. * to add to selection, not {@code null}
  103. */
  104. public void selectItems(T... items) {
  105. model.selectItems(items);
  106. }
  107. /**
  108. * Removes the given items from the set of currently selected items.
  109. * <p>
  110. * If the none of the items were selected, this is a NO-OP.
  111. * <p>
  112. * This is a short-hand for {@link #updateSelection(Set, Set)} with nothing
  113. * to select.
  114. *
  115. * @param items
  116. * to remove from selection, not {@code null}
  117. */
  118. public void deselectItems(T... items) {
  119. model.deselectItems(items);
  120. }
  121. /**
  122. * Sets the select all checkbox visibility mode.
  123. * <p>
  124. * The default value is {@link SelectAllCheckBoxVisibility#DEFAULT}, which
  125. * means that the checkbox is only visible if the grid's data provider is
  126. * in- memory.
  127. *
  128. * @param visibility
  129. * the visiblity mode to use
  130. * @see SelectAllCheckBoxVisibility
  131. */
  132. public void setSelectAllCheckBoxVisibility(
  133. SelectAllCheckBoxVisibility visibility) {
  134. model.setSelectAllCheckBoxVisibility(visibility);
  135. }
  136. /**
  137. * Gets the current mode for the select all checkbox visibility.
  138. *
  139. * @return the select all checkbox visibility mode
  140. * @see SelectAllCheckBoxVisibility
  141. * @see #isSelectAllCheckBoxVisible()
  142. */
  143. public SelectAllCheckBoxVisibility getSelectAllCheckBoxVisibility() {
  144. return model.getSelectAllCheckBoxVisibility();
  145. }
  146. /**
  147. * Returns whether the select all checkbox will be visible with the current
  148. * setting of
  149. * {@link #setSelectAllCheckBoxVisibility(SelectAllCheckBoxVisibility)}.
  150. *
  151. * @return {@code true} if the checkbox will be visible with the current
  152. * settings
  153. * @see SelectAllCheckBoxVisibility
  154. * @see #setSelectAllCheckBoxVisibility(SelectAllCheckBoxVisibility)
  155. */
  156. public boolean isSelectAllCheckBoxVisible() {
  157. return model.isSelectAllCheckBoxVisible();
  158. }
  159. /**
  160. * Sets whether the user is allowed to change the selection.
  161. * <p>
  162. * The check is done only for the client side actions. It doesn't affect
  163. * selection requests sent from the server side.
  164. *
  165. * @param allowed
  166. * <code>true</code> if the user is allowed to change the
  167. * selection, <code>false</code> otherwise
  168. */
  169. public void setUserSelectionAllowed(boolean allowed) {
  170. model.setUserSelectionAllowed(allowed);
  171. }
  172. /**
  173. * Returns whether all items are selected or not.
  174. * <p>
  175. * This is only {@code true} if user has selected all rows with the select
  176. * all checkbox on client side, or if {@link #selectAll()} has been used
  177. * from server side.
  178. *
  179. * @return {@code true} if all selected, {@code false} if not
  180. * @since 8.12.0
  181. */
  182. public boolean isAllSelected() {
  183. return model.isAllSelected();
  184. }
  185. /**
  186. * Checks if the user is allowed to change the selection.
  187. * <p>
  188. * The check is done only for the client side actions. It doesn't affect
  189. * selection requests sent from the server side.
  190. *
  191. * @return <code>true</code> if the user is allowed to change the selection,
  192. * <code>false</code> otherwise
  193. */
  194. public boolean isUserSelectionAllowed() {
  195. return model.isUserSelectionAllowed();
  196. }
  197. /**
  198. * Adds a generic listener to this selection model, accepting both single
  199. * and multiselection events.
  200. *
  201. * @param listener
  202. * the listener to add
  203. * @return a registration handle for removing the listener
  204. */
  205. public Registration addSelectionListener(SelectionListener<T> listener) {
  206. return model.addSelectionListener(listener);
  207. }
  208. /**
  209. * Adds a selection listener that will be called when the selection is
  210. * changed either by the user or programmatically.
  211. *
  212. * @param listener
  213. * the value change listener, not {@code null}
  214. * @return a registration for the listener
  215. */
  216. public Registration addMultiSelectionListener(
  217. MultiSelectionListener<T> listener) {
  218. return model.addMultiSelectionListener(listener);
  219. }
  220. /* MultiSelect implementation */
  221. @Override
  222. public void setValue(Set<T> value) {
  223. model.asMultiSelect().setValue(value);
  224. }
  225. @Override
  226. public Set<T> getValue() {
  227. return model.asMultiSelect().getValue();
  228. }
  229. @Override
  230. public Registration addValueChangeListener(
  231. ValueChangeListener<Set<T>> listener) {
  232. return model.asMultiSelect().addValueChangeListener(listener);
  233. }
  234. @Override
  235. public void setRequiredIndicatorVisible(boolean requiredIndicatorVisible) {
  236. model.asMultiSelect()
  237. .setRequiredIndicatorVisible(requiredIndicatorVisible);
  238. }
  239. @Override
  240. public boolean isRequiredIndicatorVisible() {
  241. return model.asMultiSelect().isRequiredIndicatorVisible();
  242. }
  243. @Override
  244. public void setReadOnly(boolean readOnly) {
  245. model.asMultiSelect().setReadOnly(readOnly);
  246. }
  247. @Override
  248. public boolean isReadOnly() {
  249. return model.asMultiSelect().isReadOnly();
  250. }
  251. @Override
  252. public void updateSelection(Set<T> addedItems, Set<T> removedItems) {
  253. model.asMultiSelect().updateSelection(addedItems, removedItems);
  254. }
  255. @Override
  256. public Set<T> getSelectedItems() {
  257. return model.asMultiSelect().getSelectedItems();
  258. }
  259. @Override
  260. public Registration addSelectionListener(
  261. MultiSelectionListener<T> listener) {
  262. return model.asMultiSelect().addSelectionListener(listener);
  263. }
  264. }