You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SpaceSelectHandler.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2000-2016 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.client.widget.grid.selection;
  17. import com.google.gwt.event.dom.client.KeyCodes;
  18. import com.google.gwt.event.shared.HandlerRegistration;
  19. import com.vaadin.client.widget.grid.events.BodyKeyDownHandler;
  20. import com.vaadin.client.widget.grid.events.GridKeyDownEvent;
  21. import com.vaadin.client.widgets.Grid;
  22. import com.vaadin.shared.ui.grid.ScrollDestination;
  23. /**
  24. * Generic class to perform selections when pressing space key.
  25. *
  26. * @author Vaadin Ltd
  27. * @param <T>
  28. * row data type
  29. * @since 7.4
  30. */
  31. public class SpaceSelectHandler<T> {
  32. /**
  33. * Handler for space key down events in Grid Body
  34. */
  35. private class SpaceKeyDownHandler implements BodyKeyDownHandler {
  36. private HandlerRegistration scrollHandler = null;
  37. @Override
  38. public void onKeyDown(GridKeyDownEvent event) {
  39. if (event.getNativeKeyCode() != KeyCodes.KEY_SPACE || spaceDown) {
  40. return;
  41. }
  42. // Prevent space page scrolling
  43. event.getNativeEvent().preventDefault();
  44. spaceDown = true;
  45. final int rowIndex = event.getFocusedCell().getRowIndex();
  46. if (scrollHandler != null) {
  47. scrollHandler.removeHandler();
  48. scrollHandler = null;
  49. }
  50. scrollHandler = grid
  51. .addDataAvailableHandler(dataAvailableEvent -> {
  52. if (dataAvailableEvent.getAvailableRows()
  53. .contains(rowIndex)) {
  54. setSelected(grid, rowIndex);
  55. scrollHandler.removeHandler();
  56. scrollHandler = null;
  57. }
  58. });
  59. grid.scrollToRow(rowIndex, ScrollDestination.ANY);
  60. }
  61. protected void setSelected(Grid<T> grid, int rowIndex) {
  62. T row = grid.getDataSource().getRow(rowIndex);
  63. if (!grid.isSelected(row)) {
  64. grid.select(row);
  65. } else if (deselectAllowed) {
  66. grid.deselect(row);
  67. }
  68. }
  69. }
  70. private boolean spaceDown = false;
  71. private Grid<T> grid;
  72. private HandlerRegistration spaceUpHandler;
  73. private HandlerRegistration spaceDownHandler;
  74. private boolean deselectAllowed = true;
  75. /**
  76. * Constructor for SpaceSelectHandler. This constructor will add all
  77. * necessary handlers for selecting rows with space.
  78. *
  79. * @param grid
  80. * grid to attach to
  81. */
  82. public SpaceSelectHandler(Grid<T> grid) {
  83. this.grid = grid;
  84. spaceDownHandler = grid
  85. .addBodyKeyDownHandler(new SpaceKeyDownHandler());
  86. spaceUpHandler = grid.addBodyKeyUpHandler(event -> {
  87. if (event.getNativeKeyCode() == KeyCodes.KEY_SPACE) {
  88. spaceDown = false;
  89. }
  90. });
  91. }
  92. /**
  93. * Clean up function for removing all now obsolete handlers.
  94. */
  95. public void removeHandler() {
  96. spaceDownHandler.removeHandler();
  97. spaceUpHandler.removeHandler();
  98. }
  99. /**
  100. * Sets whether pressing space for the currently selected row should
  101. * deselect the row.
  102. *
  103. * @param deselectAllowed
  104. * <code>true</code> to allow deselecting the selected row;
  105. * otherwise <code>false</code>
  106. */
  107. public void setDeselectAllowed(boolean deselectAllowed) {
  108. this.deselectAllowed = deselectAllowed;
  109. }
  110. }