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.

GridKeyDownEvent.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.events;
  17. import com.google.gwt.dom.client.BrowserEvents;
  18. import com.google.gwt.event.dom.client.KeyCodes;
  19. import com.vaadin.v7.client.widget.grid.CellReference;
  20. import com.vaadin.v7.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyDownHandler;
  21. import com.vaadin.v7.client.widgets.Grid;
  22. import com.vaadin.v7.client.widgets.Grid.AbstractGridKeyEvent;
  23. import com.vaadin.v7.shared.ui.grid.GridConstants.Section;
  24. /**
  25. * Represents native key down event in Grid.
  26. *
  27. * @since 7.4
  28. * @author Vaadin Ltd
  29. */
  30. public class GridKeyDownEvent extends AbstractGridKeyEvent<GridKeyDownHandler> {
  31. public GridKeyDownEvent(Grid<?> grid, CellReference<?> targetCell) {
  32. super(grid, targetCell);
  33. }
  34. @Override
  35. protected void doDispatch(GridKeyDownHandler handler, Section section) {
  36. if ((section == Section.BODY && handler instanceof BodyKeyDownHandler)
  37. || (section == Section.HEADER
  38. && handler instanceof HeaderKeyDownHandler)
  39. || (section == Section.FOOTER
  40. && handler instanceof FooterKeyDownHandler)) {
  41. handler.onKeyDown(this);
  42. }
  43. }
  44. @Override
  45. protected String getBrowserEventType() {
  46. return BrowserEvents.KEYDOWN;
  47. }
  48. /**
  49. * Does the key code represent an arrow key?
  50. *
  51. * @param keyCode
  52. * the key code
  53. * @return if it is an arrow key code
  54. */
  55. public static boolean isArrow(int keyCode) {
  56. switch (keyCode) {
  57. case KeyCodes.KEY_DOWN:
  58. case KeyCodes.KEY_RIGHT:
  59. case KeyCodes.KEY_UP:
  60. case KeyCodes.KEY_LEFT:
  61. return true;
  62. default:
  63. return false;
  64. }
  65. }
  66. /**
  67. * Gets the native key code. These key codes are enumerated in the
  68. * {@link KeyCodes} class.
  69. *
  70. * @return the key code
  71. */
  72. public int getNativeKeyCode() {
  73. return getNativeEvent().getKeyCode();
  74. }
  75. /**
  76. * Is this a key down arrow?
  77. *
  78. * @return whether this is a down arrow key event
  79. */
  80. public boolean isDownArrow() {
  81. return getNativeKeyCode() == KeyCodes.KEY_DOWN;
  82. }
  83. /**
  84. * Is this a left arrow?
  85. *
  86. * @return whether this is a left arrow key event
  87. */
  88. public boolean isLeftArrow() {
  89. return getNativeKeyCode() == KeyCodes.KEY_LEFT;
  90. }
  91. /**
  92. * Is this a right arrow?
  93. *
  94. * @return whether this is a right arrow key event
  95. */
  96. public boolean isRightArrow() {
  97. return getNativeKeyCode() == KeyCodes.KEY_RIGHT;
  98. }
  99. /**
  100. * Is this a up arrow?
  101. *
  102. * @return whether this is a right arrow key event
  103. */
  104. public boolean isUpArrow() {
  105. return getNativeKeyCode() == KeyCodes.KEY_UP;
  106. }
  107. @Override
  108. public String toDebugString() {
  109. return super.toDebugString() + "[" + getNativeKeyCode() + "]";
  110. }
  111. }