aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <teemusa@vaadin.com>2014-08-27 10:38:51 +0300
committerVaadin Code Review <review@vaadin.com>2014-09-24 09:42:31 +0000
commit1deac7127365049ed206dc7106661fa34495f981 (patch)
tree51990177182fe3fe8add687e1cdc056404cbf8d6
parent16f651275a417b2051ead1f06ec30ddfc1d251c9 (diff)
downloadvaadin-framework-1deac7127365049ed206dc7106661fa34495f981.tar.gz
vaadin-framework-1deac7127365049ed206dc7106661fa34495f981.zip
Fix GridKeyEvents to provide correct API for different types (#13334)
Change-Id: Id8ee1b8aab40273b2c0da4d85d8d07b419af49d7
-rw-r--r--client/src/com/vaadin/client/ui/grid/Grid.java4
-rw-r--r--client/src/com/vaadin/client/ui/grid/events/GridKeyDownEvent.java70
-rw-r--r--client/src/com/vaadin/client/ui/grid/events/GridKeyPressEvent.java22
-rw-r--r--client/src/com/vaadin/client/ui/grid/events/GridKeyUpEvent.java70
-rw-r--r--uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridClientKeyEventsTest.java27
-rw-r--r--uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java46
6 files changed, 210 insertions, 29 deletions
diff --git a/client/src/com/vaadin/client/ui/grid/Grid.java b/client/src/com/vaadin/client/ui/grid/Grid.java
index 03508fa1bb..195ddb4398 100644
--- a/client/src/com/vaadin/client/ui/grid/Grid.java
+++ b/client/src/com/vaadin/client/ui/grid/Grid.java
@@ -34,8 +34,8 @@ import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.dom.client.TableCellElement;
import com.google.gwt.dom.client.TableRowElement;
import com.google.gwt.dom.client.Touch;
-import com.google.gwt.event.dom.client.KeyCodeEvent;
import com.google.gwt.event.dom.client.KeyCodes;
+import com.google.gwt.event.dom.client.KeyEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.touch.client.Point;
import com.google.gwt.user.client.DOM;
@@ -125,7 +125,7 @@ public class Grid<T> extends ResizeComposite implements
HasSelectionChangeHandlers<T>, SubPartAware, DeferredWorker {
public static abstract class AbstractGridKeyEvent<HANDLER extends AbstractGridKeyEventHandler>
- extends KeyCodeEvent<HANDLER> {
+ extends KeyEvent<HANDLER> {
/**
* Enum describing different section of Grid.
diff --git a/client/src/com/vaadin/client/ui/grid/events/GridKeyDownEvent.java b/client/src/com/vaadin/client/ui/grid/events/GridKeyDownEvent.java
index 81ff0e0a19..dd6469d349 100644
--- a/client/src/com/vaadin/client/ui/grid/events/GridKeyDownEvent.java
+++ b/client/src/com/vaadin/client/ui/grid/events/GridKeyDownEvent.java
@@ -16,6 +16,7 @@
package com.vaadin.client.ui.grid.events;
import com.google.gwt.dom.client.BrowserEvents;
+import com.google.gwt.event.dom.client.KeyCodes;
import com.vaadin.client.ui.grid.Grid;
import com.vaadin.client.ui.grid.Grid.AbstractGridKeyEvent;
import com.vaadin.client.ui.grid.events.AbstractGridKeyEventHandler.GridKeyDownHandler;
@@ -46,4 +47,73 @@ public class GridKeyDownEvent extends AbstractGridKeyEvent<GridKeyDownHandler> {
return BrowserEvents.KEYDOWN;
}
+ /**
+ * Does the key code represent an arrow key?
+ *
+ * @param keyCode
+ * the key code
+ * @return if it is an arrow key code
+ */
+ public static boolean isArrow(int keyCode) {
+ switch (keyCode) {
+ case KeyCodes.KEY_DOWN:
+ case KeyCodes.KEY_RIGHT:
+ case KeyCodes.KEY_UP:
+ case KeyCodes.KEY_LEFT:
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ /**
+ * Gets the native key code. These key codes are enumerated in the
+ * {@link KeyCodes} class.
+ *
+ * @return the key code
+ */
+ public int getNativeKeyCode() {
+ return getNativeEvent().getKeyCode();
+ }
+
+ /**
+ * Is this a key down arrow?
+ *
+ * @return whether this is a down arrow key event
+ */
+ public boolean isDownArrow() {
+ return getNativeKeyCode() == KeyCodes.KEY_DOWN;
+ }
+
+ /**
+ * Is this a left arrow?
+ *
+ * @return whether this is a left arrow key event
+ */
+ public boolean isLeftArrow() {
+ return getNativeKeyCode() == KeyCodes.KEY_LEFT;
+ }
+
+ /**
+ * Is this a right arrow?
+ *
+ * @return whether this is a right arrow key event
+ */
+ public boolean isRightArrow() {
+ return getNativeKeyCode() == KeyCodes.KEY_RIGHT;
+ }
+
+ /**
+ * Is this a up arrow?
+ *
+ * @return whether this is a right arrow key event
+ */
+ public boolean isUpArrow() {
+ return getNativeKeyCode() == KeyCodes.KEY_UP;
+ }
+
+ @Override
+ public String toDebugString() {
+ return super.toDebugString() + "[" + getNativeKeyCode() + "]";
+ }
}
diff --git a/client/src/com/vaadin/client/ui/grid/events/GridKeyPressEvent.java b/client/src/com/vaadin/client/ui/grid/events/GridKeyPressEvent.java
index 9033344597..7b0edc413c 100644
--- a/client/src/com/vaadin/client/ui/grid/events/GridKeyPressEvent.java
+++ b/client/src/com/vaadin/client/ui/grid/events/GridKeyPressEvent.java
@@ -47,4 +47,26 @@ public class GridKeyPressEvent extends
return BrowserEvents.KEYPRESS;
}
+ /**
+ * Gets the char code for this event.
+ *
+ * @return the char code
+ */
+ public char getCharCode() {
+ return (char) getUnicodeCharCode();
+ }
+
+ /**
+ * Gets the Unicode char code (code point) for this event.
+ *
+ * @return the Unicode char code
+ */
+ public int getUnicodeCharCode() {
+ return getNativeEvent().getCharCode();
+ }
+
+ @Override
+ public String toDebugString() {
+ return super.toDebugString() + "[" + getCharCode() + "]";
+ }
} \ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/events/GridKeyUpEvent.java b/client/src/com/vaadin/client/ui/grid/events/GridKeyUpEvent.java
index 623f3d5ed8..4e177932eb 100644
--- a/client/src/com/vaadin/client/ui/grid/events/GridKeyUpEvent.java
+++ b/client/src/com/vaadin/client/ui/grid/events/GridKeyUpEvent.java
@@ -16,6 +16,7 @@
package com.vaadin.client.ui.grid.events;
import com.google.gwt.dom.client.BrowserEvents;
+import com.google.gwt.event.dom.client.KeyCodes;
import com.vaadin.client.ui.grid.Grid;
import com.vaadin.client.ui.grid.Grid.AbstractGridKeyEvent;
import com.vaadin.client.ui.grid.events.AbstractGridKeyEventHandler.GridKeyUpHandler;
@@ -46,4 +47,73 @@ public class GridKeyUpEvent extends AbstractGridKeyEvent<GridKeyUpHandler> {
return BrowserEvents.KEYUP;
}
+ /**
+ * Does the key code represent an arrow key?
+ *
+ * @param keyCode
+ * the key code
+ * @return if it is an arrow key code
+ */
+ public static boolean isArrow(int keyCode) {
+ switch (keyCode) {
+ case KeyCodes.KEY_DOWN:
+ case KeyCodes.KEY_RIGHT:
+ case KeyCodes.KEY_UP:
+ case KeyCodes.KEY_LEFT:
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ /**
+ * Gets the native key code. These key codes are enumerated in the
+ * {@link KeyCodes} class.
+ *
+ * @return the key code
+ */
+ public int getNativeKeyCode() {
+ return getNativeEvent().getKeyCode();
+ }
+
+ /**
+ * Is this a key down arrow?
+ *
+ * @return whether this is a down arrow key event
+ */
+ public boolean isDownArrow() {
+ return getNativeKeyCode() == KeyCodes.KEY_DOWN;
+ }
+
+ /**
+ * Is this a left arrow?
+ *
+ * @return whether this is a left arrow key event
+ */
+ public boolean isLeftArrow() {
+ return getNativeKeyCode() == KeyCodes.KEY_LEFT;
+ }
+
+ /**
+ * Is this a right arrow?
+ *
+ * @return whether this is a right arrow key event
+ */
+ public boolean isRightArrow() {
+ return getNativeKeyCode() == KeyCodes.KEY_RIGHT;
+ }
+
+ /**
+ * Is this a up arrow?
+ *
+ * @return whether this is a right arrow key event
+ */
+ public boolean isUpArrow() {
+ return getNativeKeyCode() == KeyCodes.KEY_UP;
+ }
+
+ @Override
+ public String toDebugString() {
+ return super.toDebugString() + "[" + getNativeKeyCode() + "]";
+ }
}
diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridClientKeyEventsTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridClientKeyEventsTest.java
index 47bd9f6cb7..ed6e6586dc 100644
--- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridClientKeyEventsTest.java
+++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridClientKeyEventsTest.java
@@ -32,8 +32,7 @@ import com.vaadin.tests.components.grid.basicfeatures.GridBasicClientFeaturesTes
public class GridClientKeyEventsTest extends GridBasicClientFeaturesTest {
- private List<String> eventOrder = Arrays.asList("keydown", "keyup",
- "keypress");
+ private List<String> eventOrder = Arrays.asList("Down", "Up", "Press");
@Test
public void testBodyKeyEvents() throws IOException {
@@ -41,11 +40,13 @@ public class GridClientKeyEventsTest extends GridBasicClientFeaturesTest {
getGridElement().getCell(2, 2).click();
- new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
+ new Actions(getDriver()).sendKeys("a").perform();
for (int i = 0; i < 3; ++i) {
- assertEquals("Body key event handler was not called.", "(2, 2) "
- + eventOrder.get(i) + " 13",
+ assertEquals("Body key event handler was not called.",
+ "(2, 2) event: GridKey" + eventOrder.get(i) + "Event:["
+ + (eventOrder.get(i).equals("Press") ? "a" : 65)
+ + "]",
findElements(By.className("v-label")).get(i * 3).getText());
assertTrue("Header key event handler got called unexpectedly.",
@@ -64,11 +65,13 @@ public class GridClientKeyEventsTest extends GridBasicClientFeaturesTest {
getGridElement().getHeaderCell(0, 2).click();
- new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
+ new Actions(getDriver()).sendKeys("a").perform();
for (int i = 0; i < 3; ++i) {
- assertEquals("Header key event handler was not called.", "(0, 2) "
- + eventOrder.get(i) + " 13",
+ assertEquals("Header key event handler was not called.",
+ "(0, 2) event: GridKey" + eventOrder.get(i) + "Event:["
+ + (eventOrder.get(i).equals("Press") ? "a" : 65)
+ + "]",
findElements(By.className("v-label")).get(i * 3 + 1)
.getText());
@@ -88,11 +91,13 @@ public class GridClientKeyEventsTest extends GridBasicClientFeaturesTest {
selectMenuPath("Component", "Footer", "Append row");
getGridElement().getFooterCell(0, 2).click();
- new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
+ new Actions(getDriver()).sendKeys("a").perform();
for (int i = 0; i < 3; ++i) {
- assertEquals("Footer key event handler was not called.", "(0, 2) "
- + eventOrder.get(i) + " 13",
+ assertEquals("Footer key event handler was not called.",
+ "(0, 2) event: GridKey" + eventOrder.get(i) + "Event:["
+ + (eventOrder.get(i).equals("Press") ? "a" : 65)
+ + "]",
findElements(By.className("v-label")).get(i * 3 + 2)
.getText());
diff --git a/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java b/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java
index d5d276c7db..e967dd8a47 100644
--- a/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java
+++ b/uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java
@@ -36,7 +36,6 @@ import com.vaadin.client.ui.grid.Cell;
import com.vaadin.client.ui.grid.EditorRowHandler;
import com.vaadin.client.ui.grid.FlyweightCell;
import com.vaadin.client.ui.grid.Grid;
-import com.vaadin.client.ui.grid.Grid.AbstractGridKeyEvent;
import com.vaadin.client.ui.grid.Grid.SelectionMode;
import com.vaadin.client.ui.grid.GridColumn;
import com.vaadin.client.ui.grid.GridFooter;
@@ -841,7 +840,9 @@ public class GridBasicClientFeaturesWidget extends
@Override
public void onKeyDown(GridKeyDownEvent event) {
- updateLabel(label, event);
+ Cell active = event.getActiveCell();
+ updateLabel(label, event.toDebugString(), active.getRow(),
+ active.getColumn());
}
});
@@ -850,7 +851,9 @@ public class GridBasicClientFeaturesWidget extends
@Override
public void onKeyDown(GridKeyDownEvent event) {
- updateLabel(label, event);
+ Cell active = event.getActiveCell();
+ updateLabel(label, event.toDebugString(), active.getRow(),
+ active.getColumn());
}
});
@@ -859,7 +862,9 @@ public class GridBasicClientFeaturesWidget extends
@Override
public void onKeyDown(GridKeyDownEvent event) {
- updateLabel(label, event);
+ Cell active = event.getActiveCell();
+ updateLabel(label, event.toDebugString(), active.getRow(),
+ active.getColumn());
}
});
@@ -869,7 +874,9 @@ public class GridBasicClientFeaturesWidget extends
@Override
public void onKeyUp(GridKeyUpEvent event) {
- updateLabel(label, event);
+ Cell active = event.getActiveCell();
+ updateLabel(label, event.toDebugString(), active.getRow(),
+ active.getColumn());
}
});
@@ -878,7 +885,9 @@ public class GridBasicClientFeaturesWidget extends
@Override
public void onKeyUp(GridKeyUpEvent event) {
- updateLabel(label, event);
+ Cell active = event.getActiveCell();
+ updateLabel(label, event.toDebugString(), active.getRow(),
+ active.getColumn());
}
});
@@ -887,7 +896,9 @@ public class GridBasicClientFeaturesWidget extends
@Override
public void onKeyUp(GridKeyUpEvent event) {
- updateLabel(label, event);
+ Cell active = event.getActiveCell();
+ updateLabel(label, event.toDebugString(), active.getRow(),
+ active.getColumn());
}
});
@@ -897,7 +908,9 @@ public class GridBasicClientFeaturesWidget extends
@Override
public void onKeyPress(GridKeyPressEvent event) {
- updateLabel(label, event);
+ Cell active = event.getActiveCell();
+ updateLabel(label, event.toDebugString(), active.getRow(),
+ active.getColumn());
}
});
@@ -906,7 +919,9 @@ public class GridBasicClientFeaturesWidget extends
@Override
public void onKeyPress(GridKeyPressEvent event) {
- updateLabel(label, event);
+ Cell active = event.getActiveCell();
+ updateLabel(label, event.toDebugString(), active.getRow(),
+ active.getColumn());
}
});
@@ -915,17 +930,16 @@ public class GridBasicClientFeaturesWidget extends
@Override
public void onKeyPress(GridKeyPressEvent event) {
- updateLabel(label, event);
+ Cell active = event.getActiveCell();
+ updateLabel(label, event.toDebugString(), active.getRow(),
+ active.getColumn());
}
});
}
- private void updateLabel(VLabel label, AbstractGridKeyEvent<?> event) {
- String type = event.getNativeEvent().getType();
- Cell active = event.getActiveCell();
- String coords = "(" + active.getRow() + ", " + active.getColumn() + ")";
- String keyCode = "" + event.getNativeKeyCode();
- label.setText(coords + " " + type + " " + keyCode);
+ private void updateLabel(VLabel label, String output, int row, int col) {
+ String coords = "(" + row + ", " + col + ")";
+ label.setText(coords + " " + output);
}
}