diff options
author | Martin Vysny <martin@vysny.me> | 2019-05-16 08:27:33 +0200 |
---|---|---|
committer | Sun Zhe <31067185+ZheSun88@users.noreply.github.com> | 2019-05-16 09:27:33 +0300 |
commit | 07fe51a15eaac560c58eabc969940660c192c134 (patch) | |
tree | e0bac8f0b8e5fe29b38deb676b1ab4e4174dd07d /client/src | |
parent | 6bffdc53c207c177c2cedf1c1432c696bd6c4a7a (diff) | |
download | vaadin-framework-07fe51a15eaac560c58eabc969940660c192c134.tar.gz vaadin-framework-07fe51a15eaac560c58eabc969940660c192c134.zip |
Grid editor: TAB now skips non-editable columns (#11573)
* Grid editor: TAB now skips non-editable columns
Pressing TAB would shift the focus to non-editable cells when the Grid was in edit mode.
This patch makes DefaultEditorEventHandler to skip such columns.
Closes #10970
* Add tests
Diffstat (limited to 'client/src')
-rw-r--r-- | client/src/main/java/com/vaadin/client/widget/grid/DefaultEditorEventHandler.java | 78 |
1 files changed, 70 insertions, 8 deletions
diff --git a/client/src/main/java/com/vaadin/client/widget/grid/DefaultEditorEventHandler.java b/client/src/main/java/com/vaadin/client/widget/grid/DefaultEditorEventHandler.java index ad99719aa8..426d7c3016 100644 --- a/client/src/main/java/com/vaadin/client/widget/grid/DefaultEditorEventHandler.java +++ b/client/src/main/java/com/vaadin/client/widget/grid/DefaultEditorEventHandler.java @@ -23,9 +23,12 @@ import com.google.gwt.user.client.Event; import com.google.gwt.user.client.ui.Widget; import com.vaadin.client.WidgetUtil; import com.vaadin.client.ui.FocusUtil; +import com.vaadin.client.widgets.Grid; import com.vaadin.client.widgets.Grid.Editor; import com.vaadin.client.widgets.Grid.EditorDomEvent; +import java.util.List; + /** * The default handler for Grid editor events. Offers several overridable * protected methods for easier customization. @@ -164,20 +167,26 @@ public class DefaultEditorEventHandler<T> implements Editor.EventHandler<T> { int columnCount = event.getGrid().getVisibleColumns().size(); - int colIndex = event.getFocusedColumnIndex() + colDelta; + int colIndex = colDelta > 0 + ? findNextEditableColumnIndex(event.getGrid(), + event.getFocusedColumnIndex() + colDelta) + : findPrevEditableColumnIndex(event.getGrid(), + event.getFocusedColumnIndex() + colDelta); int rowIndex = event.getRowIndex(); // Handle row change with horizontal move when column goes out // of range. - if (rowDelta == 0) { - if (colIndex >= columnCount + if (rowDelta == 0 && colIndex < 0) { + if (colDelta > 0 && rowIndex < event.getGrid().getDataSource().size() - 1) { rowDelta = 1; - colIndex = 0; - } else if (colIndex < 0 && rowIndex > 0) { + colIndex = findNextEditableColumnIndex(event.getGrid(), + 0); + } else if (colDelta < 0 && rowIndex > 0) { rowDelta = -1; - colIndex = columnCount - 1; + colIndex = findPrevEditableColumnIndex(event.getGrid(), + columnCount - 1); } } @@ -191,6 +200,52 @@ public class DefaultEditorEventHandler<T> implements Editor.EventHandler<T> { } /** + * Finds index of the first editable column, starting at the specified + * index. + * + * @param grid + * the current grid, not null. + * @param startingWith + * start with this column. Index into the + * {@link Grid#getVisibleColumns()}. + * @return the index of the nearest visible column; may return the + * <code>startingWith</code> itself. Returns -1 if there is no such + * column. + */ + private int findNextEditableColumnIndex(Grid<T> grid, int startingWith) { + final List<Grid.Column<?, T>> columns = grid.getVisibleColumns(); + for (int i = startingWith; i < columns.size(); i++) { + if (columns.get(i).isEditable()) { + return i; + } + } + return -1; + } + + /** + * Finds index of the last editable column, searching backwards starting at + * the specified index. + * + * @param grid + * the current grid, not null. + * @param startingWith + * start with this column. Index into the + * {@link Grid#getVisibleColumns()}. + * @return the index of the nearest visible column; may return the + * <code>startingWith</code> itself. Returns -1 if there is no such + * column. + */ + private int findPrevEditableColumnIndex(Grid<T> grid, int startingWith) { + final List<Grid.Column<?, T>> columns = grid.getVisibleColumns(); + for (int i = startingWith; i >= 0; i--) { + if (columns.get(i).isEditable()) { + return i; + } + } + return -1; + } + + /** * Moves the editor to another column if the received event is a move event. * By default the editor is moved on a keydown event with keycode * {@link #KEYCODE_MOVE_HORIZONTAL}. This moves the editor left or right if @@ -218,8 +273,15 @@ public class DefaultEditorEventHandler<T> implements Editor.EventHandler<T> { // Prevent tab out of Grid Editor event.getDomEvent().preventDefault(); - editRow(event, event.getRowIndex(), event.getFocusedColumnIndex() - + (e.getShiftKey() ? -1 : +1)); + final int newColIndex = e.getShiftKey() + ? findPrevEditableColumnIndex(event.getGrid(), + event.getFocusedColumnIndex() - 1) + : findNextEditableColumnIndex(event.getGrid(), + event.getFocusedColumnIndex() + 1); + + if (newColIndex >= 0) { + editRow(event, event.getRowIndex(), newColIndex); + } return true; } else if (e.getType().equals(BrowserEvents.KEYDOWN) |