com.vaadin.v7.client.widget.grid.selection.SelectionModelMulti Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vaadin-compatibility-client Show documentation
Show all versions of vaadin-compatibility-client Show documentation
Vaadin 7 compatibility package for Vaadin 8
/*
* Copyright (C) 2000-2023 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See for the full
* license.
*/
package com.vaadin.v7.client.widget.grid.selection;
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.Set;
import com.vaadin.client.data.DataSource.RowHandle;
import com.vaadin.v7.client.renderers.Renderer;
import com.vaadin.v7.client.widgets.Grid;
/**
* Multi-row selection model.
*
* @author Vaadin Ltd
* @since 7.4
*/
public class SelectionModelMulti extends AbstractRowHandleSelectionModel
implements SelectionModel.Multi.Batched, HasUserSelectionAllowed {
private final LinkedHashSet> selectedRows;
private Renderer renderer;
private Grid grid;
private boolean batchStarted = false;
private final LinkedHashSet> selectionBatch = new LinkedHashSet>();
private final LinkedHashSet> deselectionBatch = new LinkedHashSet>();
/* Event handling for selection with space key */
private SpaceSelectHandler spaceSelectHandler;
private boolean userSelectionAllowed = true;
public SelectionModelMulti() {
grid = null;
renderer = null;
selectedRows = new LinkedHashSet>();
}
@Override
public boolean isSelected(T row) {
return isSelectedByHandle(grid.getDataSource().getHandle(row));
}
@Override
public Renderer getSelectionColumnRenderer() {
return renderer;
}
@Override
public void setGrid(Grid grid) {
if (this.grid != null && grid != null) {
// Trying to replace grid
throw new IllegalStateException(
"Selection model is already attached to a grid. "
+ "Remove the selection model first from "
+ "the grid and then add it.");
}
this.grid = grid;
if (this.grid != null) {
spaceSelectHandler = new SpaceSelectHandler(grid);
this.renderer = new MultiSelectionRenderer(grid);
} else {
spaceSelectHandler.removeHandler();
spaceSelectHandler = null;
this.renderer = null;
}
}
@Override
public boolean select(T... rows) {
if (rows == null) {
throw new IllegalArgumentException("Rows cannot be null");
}
return select(Arrays.asList(rows));
}
@Override
public boolean deselect(T... rows) {
if (rows == null) {
throw new IllegalArgumentException("Rows cannot be null");
}
return deselect(Arrays.asList(rows));
}
@Override
public boolean deselectAll() {
if (!selectedRows.isEmpty()) {
@SuppressWarnings("unchecked")
final LinkedHashSet> selectedRowsClone = (LinkedHashSet>) selectedRows
.clone();
SelectionEvent event = new SelectionEvent(grid, null,
getSelectedRows(), isBeingBatchSelected());
selectedRows.clear();
if (isBeingBatchSelected()) {
selectionBatch.clear();
deselectionBatch.clear();
deselectionBatch.addAll(selectedRowsClone);
}
grid.fireEvent(event);
return true;
}
return false;
}
@Override
public boolean select(Collection rows) {
if (rows == null) {
throw new IllegalArgumentException("Rows cannot be null");
}
Set added = new LinkedHashSet();
for (T row : rows) {
RowHandle handle = grid.getDataSource().getHandle(row);
if (selectByHandle(handle)) {
added.add(row);
}
}
if (!added.isEmpty()) {
grid.fireEvent(new SelectionEvent(grid, added, null,
isBeingBatchSelected()));
return true;
}
return false;
}
@Override
public boolean deselect(Collection rows) {
if (rows == null) {
throw new IllegalArgumentException("Rows cannot be null");
}
Set removed = new LinkedHashSet();
for (T row : rows) {
RowHandle handle = grid.getDataSource().getHandle(row);
if (deselectByHandle(handle)) {
removed.add(row);
}
}
if (!removed.isEmpty()) {
grid.fireEvent(new SelectionEvent(grid, null, removed,
isBeingBatchSelected()));
return true;
}
return false;
}
protected boolean isSelectedByHandle(RowHandle handle) {
return selectedRows.contains(handle);
}
@Override
protected boolean selectByHandle(RowHandle handle) {
if (selectedRows.add(handle)) {
handle.pin();
if (isBeingBatchSelected()) {
deselectionBatch.remove(handle);
selectionBatch.add(handle);
}
return true;
}
return false;
}
@Override
protected boolean deselectByHandle(RowHandle handle) {
if (selectedRows.remove(handle)) {
if (!isBeingBatchSelected()) {
handle.unpin();
} else {
selectionBatch.remove(handle);
deselectionBatch.add(handle);
}
return true;
}
return false;
}
@Override
public Collection getSelectedRows() {
Set selected = new LinkedHashSet();
for (RowHandle handle : selectedRows) {
selected.add(handle.getRow());
}
return Collections.unmodifiableSet(selected);
}
@Override
public void reset() {
deselectAll();
}
@Override
public void startBatchSelect() {
assert !isBeingBatchSelected() : "Batch has already been started";
batchStarted = true;
}
@Override
public void commitBatchSelect() {
assert isBeingBatchSelected() : "Batch was never started";
if (!isBeingBatchSelected()) {
return;
}
batchStarted = false;
final Collection added = getSelectedRowsBatch();
selectionBatch.clear();
final Collection removed = getDeselectedRowsBatch();
// unpin deselected rows
for (RowHandle handle : deselectionBatch) {
handle.unpin();
}
deselectionBatch.clear();
grid.fireEvent(new SelectionEvent(grid, added, removed,
isBeingBatchSelected()));
}
@Override
public boolean isBeingBatchSelected() {
return batchStarted;
}
@Override
public Collection getSelectedRowsBatch() {
return rowHandlesToRows(selectionBatch);
}
@Override
public Collection getDeselectedRowsBatch() {
return rowHandlesToRows(deselectionBatch);
}
private List rowHandlesToRows(Collection> rowHandles) {
List rows = new ArrayList(rowHandles.size());
for (RowHandle handle : rowHandles) {
rows.add(handle.getRow());
}
return rows;
}
@Override
public boolean isUserSelectionAllowed() {
return userSelectionAllowed;
}
@Override
public void setUserSelectionAllowed(boolean userSelectionAllowed) {
this.userSelectionAllowed = userSelectionAllowed;
}
}