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.dom.client.KeyCodes;
19 import com.google.gwt.event.shared.HandlerRegistration;
20 import com.vaadin.v7.client.widget.grid.DataAvailableEvent;
21 import com.vaadin.v7.client.widget.grid.DataAvailableHandler;
22 import com.vaadin.v7.client.widget.grid.events.BodyKeyDownHandler;
23 import com.vaadin.v7.client.widget.grid.events.BodyKeyUpHandler;
24 import com.vaadin.v7.client.widget.grid.events.GridKeyDownEvent;
25 import com.vaadin.v7.client.widget.grid.events.GridKeyUpEvent;
26 import com.vaadin.v7.client.widgets.Grid;
27 import com.vaadin.v7.shared.ui.grid.ScrollDestination;
30 * Generic class to perform selections when pressing space key.
37 public class SpaceSelectHandler<T> {
40 * Handler for space key down events in Grid Body
42 private class SpaceKeyDownHandler implements BodyKeyDownHandler {
43 private HandlerRegistration scrollHandler = null;
46 public void onKeyDown(GridKeyDownEvent event) {
47 if (!grid.isUserSelectionAllowed()) {
51 if (event.getNativeKeyCode() != KeyCodes.KEY_SPACE || spaceDown) {
55 // Prevent space page scrolling
56 event.getNativeEvent().preventDefault();
59 final int rowIndex = event.getFocusedCell().getRowIndex();
61 if (scrollHandler != null) {
62 scrollHandler.removeHandler();
67 .addDataAvailableHandler(new DataAvailableHandler() {
70 public void onDataAvailable(
71 DataAvailableEvent dataAvailableEvent) {
72 if (dataAvailableEvent.getAvailableRows()
73 .contains(rowIndex)) {
74 setSelected(grid, rowIndex);
75 scrollHandler.removeHandler();
80 grid.scrollToRow(rowIndex, ScrollDestination.ANY);
83 protected void setSelected(Grid<T> grid, int rowIndex) {
84 T row = grid.getDataSource().getRow(rowIndex);
86 if (!grid.isSelected(row)) {
88 } else if (deselectAllowed) {
94 private boolean spaceDown = false;
96 private HandlerRegistration spaceUpHandler;
97 private HandlerRegistration spaceDownHandler;
98 private boolean deselectAllowed = true;
101 * Constructor for SpaceSelectHandler. This constructor will add all
102 * necessary handlers for selecting rows with space.
107 public SpaceSelectHandler(Grid<T> grid) {
109 spaceDownHandler = grid
110 .addBodyKeyDownHandler(new SpaceKeyDownHandler());
111 spaceUpHandler = grid.addBodyKeyUpHandler(new BodyKeyUpHandler() {
114 public void onKeyUp(GridKeyUpEvent event) {
115 if (event.getNativeKeyCode() == KeyCodes.KEY_SPACE) {
123 * Clean up function for removing all now obsolete handlers.
125 public void removeHandler() {
126 spaceDownHandler.removeHandler();
127 spaceUpHandler.removeHandler();
131 * Sets whether pressing space for the currently selected row should
134 * @param deselectAllowed
135 * <code>true</code> to allow deselecting the selected row;
136 * otherwise <code>false</code>
138 public void setDeselectAllowed(boolean deselectAllowed) {
139 this.deselectAllowed = deselectAllowed;