/* * Copyright 2000-2016 Vaadin Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.vaadin.ui.components.grid; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; import java.util.Set; import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.Stream; import com.vaadin.data.provider.DataCommunicator; import com.vaadin.data.provider.DataProvider; import com.vaadin.data.provider.Query; import com.vaadin.event.selection.MultiSelectionEvent; import com.vaadin.event.selection.MultiSelectionListener; import com.vaadin.shared.Registration; import com.vaadin.shared.data.selection.GridMultiSelectServerRpc; import com.vaadin.shared.ui.grid.MultiSelectionModelState; import com.vaadin.ui.MultiSelect; /** * Multiselection model for grid. *
* Shows a column of checkboxes as the first column of grid. Each checkbox * triggers the selection for that row. *
* Implementation detail: The Grid selection is updated immediately after user
* selection on client side, without waiting for the server response.
*
* @author Vaadin Ltd.
* @since 8.0
*
* @param
* Default value is {@link #DEFAULT}, which means that the select all is
* only visible if an in-memory data provider is used
* {@link DataProvider#isInMemory()}.
*/
public enum SelectAllCheckBoxVisibility {
/**
* Shows the select all checkbox, regardless of data provider used.
*
* For a lazy data provider, selecting all will result in to all rows
* being fetched from backend to application memory!
*/
VISIBLE,
/**
* Never shows the select all checkbox, regardless of data provider
* used.
*/
HIDDEN,
/**
* By default select all checkbox depends on the grid's dataprovider.
*
* The default value is {@link SelectAllCheckBoxVisibility#DEFAULT}, which
* means that the checkbox is only visible if the grid's data provider is
* in- memory.
*
* @param selectAllCheckBoxVisibility
* the visiblity mode to use
* @see SelectAllCheckBoxVisibility
*/
public void setSelectAllCheckBoxVisibility(
SelectAllCheckBoxVisibility selectAllCheckBoxVisibility) {
if (this.selectAllCheckBoxVisibility != selectAllCheckBoxVisibility) {
this.selectAllCheckBoxVisibility = selectAllCheckBoxVisibility;
markAsDirty();
}
}
/**
* Gets the current mode for the select all checkbox visibility.
*
* @return the select all checkbox visibility mode
* @see SelectAllCheckBoxVisibility
* @see #isSelectAllCheckBoxVisible()
*/
public SelectAllCheckBoxVisibility getSelectAllCheckBoxVisibility() {
return selectAllCheckBoxVisibility;
}
/**
* Returns whether the select all checkbox will be visible with the current
* setting of
* {@link #setSelectAllCheckBoxVisibility(SelectAllCheckBoxVisibility)}.
*
* @return {@code true} if the checkbox will be visible with the current
* settings
* @see SelectAllCheckBoxVisibility
* @see #setSelectAllCheckBoxVisibility(SelectAllCheckBoxVisibility)
*/
public boolean isSelectAllCheckBoxVisible() {
updateCanSelectAll();
return getState(false).selectAllCheckBoxVisible;
}
/**
* Returns whether all items are selected or not.
*
* This is only {@code true} if user has selected all rows with the select
* all checkbox on client side, or if {@link #selectAll()} has been used
* from server side.
*
* @return {@code true} if all selected, {@code false} if not
*/
public boolean isAllSelected() {
return getState(false).allSelected;
}
@Override
public boolean isSelected(T item) {
return isAllSelected()
|| selectionContainsId(getGrid().getDataProvider().getId(item));
}
/**
* Returns if the given id belongs to one of the selected items.
*
* @param id
* the id to check for
* @return {@code true} if id is selected, {@code false} if not
*/
protected boolean selectionContainsId(Object id) {
DataProvider
* This is updated as a part of {@link #beforeClientResponse(boolean)},
* since the data provider for grid can be changed on the fly.
*
* @see SelectAllCheckBoxVisibility
*/
protected void updateCanSelectAll() {
switch (selectAllCheckBoxVisibility) {
case VISIBLE:
getState(false).selectAllCheckBoxVisible = true;
break;
case HIDDEN:
getState(false).selectAllCheckBoxVisible = false;
break;
case DEFAULT:
getState(false).selectAllCheckBoxVisible = getGrid()
.getDataProvider().isInMemory();
break;
default:
break;
}
}
@Override
public Registration addMultiSelectionListener(
MultiSelectionListener
* All selection updates should go through this method, since it handles
* incorrect parameters, removing duplicates, notifying data communicator
* and and firing events.
*
* @param addedItems
* the items added to selection, not {@code} null
* @param removedItems
* the items removed from selection, not {@code} null
* @param userOriginated
* {@code true} if this was used originated, {@code false} if not
*/
protected void updateSelection(Set
*
*
* @see DataProvider#isInMemory()}.
*/
DEFAULT;
}
private class GridMultiSelectServerRpcImpl
implements GridMultiSelectServerRpc {
@Override
public void select(String key) {
MultiSelectionModelImpl.this.updateSelection(
new LinkedHashSet<>(Arrays.asList(getData(key))),
Collections.emptySet(), true);
}
@Override
public void deselect(String key) {
if (getState(false).allSelected) {
// updated right away on client side
getState(false).allSelected = false;
getUI().getConnectorTracker()
.getDiffState(MultiSelectionModelImpl.this)
.put("allSelected", false);
}
MultiSelectionModelImpl.this.updateSelection(Collections.emptySet(),
new LinkedHashSet<>(Arrays.asList(getData(key))), true);
}
@Override
public void selectAll() {
onSelectAll(true);
}
@Override
public void deselectAll() {
onDeselectAll(true);
}
}
private List