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.6KB

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