]> source.dussan.org Git - vaadin-framework.git/blob
6a8fff912fcf2cd27fa611ea4c46123d558cec52
[vaadin-framework.git] /
1 /*
2  * Copyright 2000-2018 Vaadin Ltd.
3  *
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
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
14  * the License.
15  */
16 package com.vaadin.v7.client.widget.grid.selection;
17
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;
28
29 /**
30  * Generic class to perform selections when pressing space key.
31  *
32  * @author Vaadin Ltd
33  * @param <T>
34  *            row data type
35  * @since 7.4
36  */
37 public class SpaceSelectHandler<T> {
38
39     /**
40      * Handler for space key down events in Grid Body
41      */
42     private class SpaceKeyDownHandler implements BodyKeyDownHandler {
43         private HandlerRegistration scrollHandler = null;
44
45         @Override
46         public void onKeyDown(GridKeyDownEvent event) {
47             if (!grid.isUserSelectionAllowed()) {
48                 return;
49             }
50
51             if (event.getNativeKeyCode() != KeyCodes.KEY_SPACE || spaceDown) {
52                 return;
53             }
54
55             // Prevent space page scrolling
56             event.getNativeEvent().preventDefault();
57
58             spaceDown = true;
59             final int rowIndex = event.getFocusedCell().getRowIndex();
60
61             if (scrollHandler != null) {
62                 scrollHandler.removeHandler();
63                 scrollHandler = null;
64             }
65
66             scrollHandler = grid
67                     .addDataAvailableHandler(new DataAvailableHandler() {
68
69                         @Override
70                         public void onDataAvailable(
71                                 DataAvailableEvent dataAvailableEvent) {
72                             if (dataAvailableEvent.getAvailableRows()
73                                     .contains(rowIndex)) {
74                                 setSelected(grid, rowIndex);
75                                 scrollHandler.removeHandler();
76                                 scrollHandler = null;
77                             }
78                         }
79                     });
80             grid.scrollToRow(rowIndex, ScrollDestination.ANY);
81         }
82
83         protected void setSelected(Grid<T> grid, int rowIndex) {
84             T row = grid.getDataSource().getRow(rowIndex);
85
86             if (!grid.isSelected(row)) {
87                 grid.select(row);
88             } else if (deselectAllowed) {
89                 grid.deselect(row);
90             }
91         }
92     }
93
94     private boolean spaceDown = false;
95     private Grid<T> grid;
96     private HandlerRegistration spaceUpHandler;
97     private HandlerRegistration spaceDownHandler;
98     private boolean deselectAllowed = true;
99
100     /**
101      * Constructor for SpaceSelectHandler. This constructor will add all
102      * necessary handlers for selecting rows with space.
103      *
104      * @param grid
105      *            grid to attach to
106      */
107     public SpaceSelectHandler(Grid<T> grid) {
108         this.grid = grid;
109         spaceDownHandler = grid
110                 .addBodyKeyDownHandler(new SpaceKeyDownHandler());
111         spaceUpHandler = grid.addBodyKeyUpHandler(new BodyKeyUpHandler() {
112
113             @Override
114             public void onKeyUp(GridKeyUpEvent event) {
115                 if (event.getNativeKeyCode() == KeyCodes.KEY_SPACE) {
116                     spaceDown = false;
117                 }
118             }
119         });
120     }
121
122     /**
123      * Clean up function for removing all now obsolete handlers.
124      */
125     public void removeHandler() {
126         spaceDownHandler.removeHandler();
127         spaceUpHandler.removeHandler();
128     }
129
130     /**
131      * Sets whether pressing space for the currently selected row should
132      * deselect the row.
133      *
134      * @param deselectAllowed
135      *            <code>true</code> to allow deselecting the selected row;
136      *            otherwise <code>false</code>
137      */
138     public void setDeselectAllowed(boolean deselectAllowed) {
139         this.deselectAllowed = deselectAllowed;
140     }
141 }