2 * Copyright 2000-2021 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.v7.client.widget.grid.selection;
18 import java.util.Collection;
19 import java.util.Collections;
21 import com.vaadin.client.data.DataSource.RowHandle;
22 import com.vaadin.v7.client.renderers.Renderer;
23 import com.vaadin.v7.client.widgets.Grid;
26 * Single-row selection model.
31 public class SelectionModelSingle<T> extends AbstractRowHandleSelectionModel<T>
32 implements SelectionModel.Single<T>, HasUserSelectionAllowed<T> {
35 private RowHandle<T> selectedRow;
37 /** Event handling for selection with space key */
38 private SpaceSelectHandler<T> spaceSelectHandler;
40 /** Event handling for selection by clicking cells */
41 private ClickSelectHandler<T> clickSelectHandler;
43 private boolean deselectAllowed = true;
44 private boolean userSelectionAllowed = true;
47 public boolean isSelected(T row) {
48 return selectedRow != null
49 && selectedRow.equals(grid.getDataSource().getHandle(row));
53 public Renderer<Boolean> getSelectionColumnRenderer() {
54 // No Selection column renderer for single selection
59 public void setGrid(Grid<T> grid) {
60 if (this.grid != null && grid != null) {
61 // Trying to replace grid
62 throw new IllegalStateException(
63 "Selection model is already attached to a grid. "
64 + "Remove the selection model first from "
65 + "the grid and then add it.");
69 if (this.grid != null) {
70 spaceSelectHandler = new SpaceSelectHandler<T>(grid);
71 clickSelectHandler = new ClickSelectHandler<T>(grid);
72 updateHandlerDeselectAllowed();
74 spaceSelectHandler.removeHandler();
75 clickSelectHandler.removeHandler();
76 spaceSelectHandler = null;
77 clickSelectHandler = null;
82 public boolean select(T row) {
85 throw new IllegalArgumentException("Row cannot be null");
88 T removed = getSelectedRow();
89 if (selectByHandle(grid.getDataSource().getHandle(row))) {
90 grid.fireEvent(new SelectionEvent<T>(grid, row, removed, false));
98 public boolean deselect(T row) {
101 throw new IllegalArgumentException("Row cannot be null");
104 if (isSelected(row)) {
105 deselectByHandle(selectedRow);
106 grid.fireEvent(new SelectionEvent<T>(grid, null, row, false));
114 public T getSelectedRow() {
115 return (selectedRow != null ? selectedRow.getRow() : null);
119 public void reset() {
120 if (selectedRow != null) {
121 deselect(getSelectedRow());
126 public Collection<T> getSelectedRows() {
127 if (getSelectedRow() != null) {
128 return Collections.singleton(getSelectedRow());
130 return Collections.emptySet();
134 protected boolean selectByHandle(RowHandle<T> handle) {
135 if (handle != null && !handle.equals(selectedRow)) {
136 deselectByHandle(selectedRow);
137 selectedRow = handle;
146 protected boolean deselectByHandle(RowHandle<T> handle) {
147 if (handle != null && handle.equals(selectedRow)) {
157 public void setDeselectAllowed(boolean deselectAllowed) {
158 this.deselectAllowed = deselectAllowed;
159 updateHandlerDeselectAllowed();
163 public boolean isDeselectAllowed() {
164 return deselectAllowed;
167 private void updateHandlerDeselectAllowed() {
168 if (spaceSelectHandler != null) {
169 spaceSelectHandler.setDeselectAllowed(deselectAllowed);
171 if (clickSelectHandler != null) {
172 clickSelectHandler.setDeselectAllowed(deselectAllowed);
177 public boolean isUserSelectionAllowed() {
178 return userSelectionAllowed;
182 public void setUserSelectionAllowed(boolean userSelectionAllowed) {
183 this.userSelectionAllowed = userSelectionAllowed;