com.vaadin.client.widget.grid.selection.SelectionModelMulti Maven / Gradle / Ivy
/*
* Copyright 2000-2014 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.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.Set;
import com.vaadin.client.data.DataSource.RowHandle;
import com.vaadin.client.renderers.Renderer;
import com.vaadin.client.widgets.Grid;
/**
* Multi-row selection model.
*
* @author Vaadin Ltd
* @since 7.4
*/
public class SelectionModelMulti extends AbstractRowHandleSelectionModel
implements SelectionModel.Multi.Batched {
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;
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.size() > 0) {
@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.size() > 0) {
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.size() > 0) {
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 ArrayList rowHandlesToRows(Collection> rowHandles) {
ArrayList rows = new ArrayList(rowHandles.size());
for (RowHandle handle : rowHandles) {
rows.add(handle.getRow());
}
return rows;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy