2 * Copyright 2000-2018 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 com.google.gwt.event.shared.HandlerRegistration;
19 import com.vaadin.v7.client.widget.grid.events.BodyClickHandler;
20 import com.vaadin.v7.client.widget.grid.events.GridClickEvent;
21 import com.vaadin.v7.client.widgets.Grid;
24 * Generic class to perform selections when clicking on cells in body of Grid.
29 public class ClickSelectHandler<T> {
32 private HandlerRegistration clickHandler;
33 private boolean deselectAllowed = true;
35 private class RowClickHandler implements BodyClickHandler {
38 public void onClick(GridClickEvent event) {
39 if (!grid.isUserSelectionAllowed()) {
43 T row = (T) event.getTargetCell().getRow();
44 if (!grid.isSelected(row)) {
46 } else if (deselectAllowed) {
53 * Constructor for ClickSelectHandler. This constructor will add all
54 * necessary handlers for selecting rows by clicking cells.
59 public ClickSelectHandler(Grid<T> grid) {
61 clickHandler = grid.addBodyClickHandler(new RowClickHandler());
65 * Clean up function for removing all now obsolete handlers.
67 public void removeHandler() {
68 clickHandler.removeHandler();
72 * Sets whether clicking the currently selected row should deselect the row.
74 * @param deselectAllowed
75 * <code>true</code> to allow deselecting the selected row;
76 * otherwise <code>false</code>
78 public void setDeselectAllowed(boolean deselectAllowed) {
79 this.deselectAllowed = deselectAllowed;