]> source.dussan.org Git - vaadin-framework.git/commitdiff
Implement KeyEvents for Grid (#13334)
authorTeemu Suo-Anttila <teemusa@vaadin.com>
Wed, 13 Aug 2014 13:39:40 +0000 (16:39 +0300)
committerTeemu Suo-Anttila <teemusa@vaadin.com>
Wed, 20 Aug 2014 12:30:26 +0000 (15:30 +0300)
Change-Id: Ida7a358aa23a4c96232a01bed0b4810fb91dd832

18 files changed:
client/src/com/vaadin/client/ui/grid/FlyweightCell.java
client/src/com/vaadin/client/ui/grid/Grid.java
client/src/com/vaadin/client/ui/grid/Row.java
client/src/com/vaadin/client/ui/grid/keyevents/AbstractGridKeyEventHandler.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyDownHandler.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyPressHandler.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyUpHandler.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyDownHandler.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyPressHandler.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyUpHandler.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/keyevents/GridKeyDownEvent.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/keyevents/GridKeyPressEvent.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/keyevents/GridKeyUpEvent.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyDownHandler.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyPressHandler.java [new file with mode: 0644]
client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyUpHandler.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridClientKeyEventsTest.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java

index dcc543de9c09efa2215b9b55e6e5eeff563cc9ca..30cc4fc79e9144981914e9b5be5e2704b7ee41f3 100644 (file)
@@ -17,7 +17,6 @@ package com.vaadin.client.ui.grid;
 
 import java.util.List;
 
-import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.Style.Display;
 import com.google.gwt.dom.client.Style.Unit;
 import com.google.gwt.dom.client.TableCellElement;
@@ -70,7 +69,7 @@ public class FlyweightCell {
      * Returns the element of the cell. Can be either a <code>TD</code> element
      * or a <code>TH</code> element.
      */
-    public Element getElement() {
+    public TableCellElement getElement() {
         assertSetup();
         return element;
     }
index 531e27098c44ecd289052942137b3e74b488854c..a2bb0d3f098dcbbdbf15408fa454bd801d491824 100644 (file)
@@ -33,7 +33,9 @@ import com.google.gwt.dom.client.Element;
 import com.google.gwt.dom.client.EventTarget;
 import com.google.gwt.dom.client.NativeEvent;
 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.shared.HandlerRegistration;
 import com.google.gwt.touch.client.Point;
@@ -50,6 +52,22 @@ import com.vaadin.client.ui.SubPartAware;
 import com.vaadin.client.ui.grid.GridFooter.FooterRow;
 import com.vaadin.client.ui.grid.GridHeader.HeaderRow;
 import com.vaadin.client.ui.grid.GridStaticSection.StaticCell;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyDownHandler;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyPressHandler;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyUpHandler;
+import com.vaadin.client.ui.grid.keyevents.BodyKeyDownHandler;
+import com.vaadin.client.ui.grid.keyevents.BodyKeyPressHandler;
+import com.vaadin.client.ui.grid.keyevents.BodyKeyUpHandler;
+import com.vaadin.client.ui.grid.keyevents.FooterKeyDownHandler;
+import com.vaadin.client.ui.grid.keyevents.FooterKeyPressHandler;
+import com.vaadin.client.ui.grid.keyevents.FooterKeyUpHandler;
+import com.vaadin.client.ui.grid.keyevents.GridKeyDownEvent;
+import com.vaadin.client.ui.grid.keyevents.GridKeyPressEvent;
+import com.vaadin.client.ui.grid.keyevents.GridKeyUpEvent;
+import com.vaadin.client.ui.grid.keyevents.HeaderKeyDownHandler;
+import com.vaadin.client.ui.grid.keyevents.HeaderKeyPressHandler;
+import com.vaadin.client.ui.grid.keyevents.HeaderKeyUpHandler;
 import com.vaadin.client.ui.grid.renderers.ComplexRenderer;
 import com.vaadin.client.ui.grid.renderers.WidgetRenderer;
 import com.vaadin.client.ui.grid.selection.HasSelectionChangeHandlers;
@@ -105,6 +123,68 @@ import com.vaadin.shared.ui.grid.SortDirection;
 public class Grid<T> extends Composite implements
         HasSelectionChangeHandlers<T>, SubPartAware {
 
+    public static abstract class AbstractGridKeyEvent<T, HANDLER extends AbstractGridKeyEventHandler>
+            extends KeyCodeEvent<HANDLER> {
+
+        /**
+         * Enum describing different section of Grid.
+         */
+        public enum GridSection {
+            HEADER, BODY, FOOTER
+        }
+
+        private Grid<T> grid;
+        protected Cell activeCell;
+        protected GridSection activeSection;
+        private final Type<HANDLER> associatedType = new Type<HANDLER>(
+                getBrowserEventType(), this);
+
+        public AbstractGridKeyEvent(Grid<T> grid) {
+            this.grid = grid;
+        }
+
+        protected abstract String getBrowserEventType();
+
+        /**
+         * Gets the Grid instance for this event.
+         * 
+         * @return grid
+         */
+        public Grid<T> getGrid() {
+            return grid;
+        }
+
+        /**
+         * Gets the active cell for this event.
+         * 
+         * @return active cell
+         */
+        public Cell getActiveCell() {
+            return activeCell;
+        }
+
+        @Override
+        protected void dispatch(HANDLER handler) {
+            activeCell = grid.activeCellHandler.getActiveCell();
+            activeSection = GridSection.FOOTER;
+            final RowContainer container = grid.activeCellHandler.container;
+            if (container == grid.escalator.getHeader()) {
+                activeSection = GridSection.HEADER;
+            } else if (container == grid.escalator.getBody()) {
+                activeSection = GridSection.BODY;
+            }
+        }
+
+        @Override
+        public Type<HANDLER> getAssociatedType() {
+            return associatedType;
+        }
+    }
+
+    private GridKeyDownEvent<T> keyDown = new GridKeyDownEvent<T>(this);
+    private GridKeyUpEvent<T> keyUp = new GridKeyUpEvent<T>(this);
+    private GridKeyPressEvent<T> keyPress = new GridKeyPressEvent<T>(this);
+
     private class ActiveCellHandler {
 
         private RowContainer container = escalator.getBody();
@@ -113,13 +193,17 @@ public class Grid<T> extends Composite implements
         private int lastActiveBodyRow = 0;
         private int lastActiveHeaderRow = 0;
         private int lastActiveFooterRow = 0;
-        private Element cellWithActiveStyle = null;
-        private Element rowWithActiveStyle = null;
+        private TableCellElement cellWithActiveStyle = null;
+        private TableRowElement rowWithActiveStyle = null;
 
         public ActiveCellHandler() {
             sinkEvents(getNavigationEvents());
         }
 
+        private Cell getActiveCell() {
+            return new Cell(activeRow, activeColumn, cellWithActiveStyle);
+        }
+
         /**
          * Sets style names for given cell when needed.
          */
@@ -594,7 +678,7 @@ public class Grid<T> extends Composite implements
      */
     private SelectionModel<T> selectionModel;
 
-    private final ActiveCellHandler activeCellHandler;
+    protected final ActiveCellHandler activeCellHandler;
 
     private final LazySorter lazySorter = new LazySorter();
 
@@ -1199,8 +1283,10 @@ public class Grid<T> extends Composite implements
             }
         });
 
-        // Sink header events
+        // Sink header events and key events
         sinkEvents(getHeader().getConsumedEvents());
+        sinkEvents(Arrays.asList(BrowserEvents.KEYDOWN, BrowserEvents.KEYUP,
+                BrowserEvents.KEYPRESS));
     }
 
     @Override
@@ -2356,6 +2442,66 @@ public class Grid<T> extends Composite implements
         return addHandler(handler, DataAvailableEvent.TYPE);
     }
 
+    /**
+     * Register a KeyDown handler to this Grid. If the handler is a
+     * HeaderKeyDownHandler, it will be fired only when a header cell is active.
+     * The same goes for body and footer with their respective handlers.
+     * 
+     * @param handler
+     *            the key handler to register
+     * @return the registration for the event
+     */
+    public <HANDLER extends GridKeyDownHandler<T>> HandlerRegistration addKeyDownHandler(
+            HANDLER handler) {
+        if (handler instanceof BodyKeyDownHandler
+                || handler instanceof HeaderKeyDownHandler
+                || handler instanceof FooterKeyDownHandler) {
+            return addHandler(handler, keyDown.getAssociatedType());
+        }
+        throw new IllegalArgumentException(
+                "Handler not a valid extension of GridKeyDownHandler");
+    }
+
+    /**
+     * Register a KeyUp handler to this Grid. If the handler is a
+     * HeaderKeyUpHandler, it will be fired only when a header cell is active.
+     * The same goes for body and footer with their respective handlers.
+     * 
+     * @param handler
+     *            the key handler to register
+     * @return the registration for the event
+     */
+    public <HANDLER extends GridKeyUpHandler<T>> HandlerRegistration addKeyUpHandler(
+            HANDLER handler) {
+        if (handler instanceof BodyKeyUpHandler
+                || handler instanceof HeaderKeyUpHandler
+                || handler instanceof FooterKeyUpHandler) {
+            return addHandler(handler, keyUp.getAssociatedType());
+        }
+        throw new IllegalArgumentException(
+                "Handler not a valid extension of GridKeyUpHandler");
+    }
+
+    /**
+     * Register a KeyPress handler to this Grid. If the handler is a
+     * HeaderKeyPressHandler, it will be fired only when a header cell is
+     * active. The same goes for body and footer with their respective handlers.
+     * 
+     * @param handler
+     *            the key handler to register
+     * @return the registration for the event
+     */
+    public <HANDLER extends GridKeyPressHandler<T>> HandlerRegistration addKeyPressHandler(
+            HANDLER handler) {
+        if (handler instanceof BodyKeyPressHandler
+                || handler instanceof HeaderKeyPressHandler
+                || handler instanceof FooterKeyPressHandler) {
+            return addHandler(handler, keyPress.getAssociatedType());
+        }
+        throw new IllegalArgumentException(
+                "Handler not a valid extension of GridKeyPressHandler");
+    }
+
     /**
      * Apply sorting to data source.
      */
index a5317e52c496f254a9820935a801385c30338e86..6419a985749dc4917ae413a9e50fa85da5a52ff1 100644 (file)
@@ -16,7 +16,7 @@
 
 package com.vaadin.client.ui.grid;
 
-import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.TableRowElement;
 
 /**
  * A representation of a row in an {@link Escalator}.
@@ -44,5 +44,5 @@ public interface Row {
      * 
      * @return the root element of the row
      */
-    public Element getElement();
+    public TableRowElement getElement();
 }
\ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/AbstractGridKeyEventHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/AbstractGridKeyEventHandler.java
new file mode 100644 (file)
index 0000000..28a8592
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.google.gwt.event.shared.EventHandler;
+
+/**
+ * Base interface of all handlers for {@link AbstractGridKeyEvent}s.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public abstract interface AbstractGridKeyEventHandler extends EventHandler {
+
+    public abstract interface GridKeyDownHandler<T> extends
+            AbstractGridKeyEventHandler {
+        public void onKeyDown(GridKeyDownEvent<T> event);
+    }
+
+    public abstract interface GridKeyUpHandler<T> extends
+            AbstractGridKeyEventHandler {
+        public void onKeyUp(GridKeyUpEvent<T> event);
+    }
+
+    public abstract interface GridKeyPressHandler<T> extends
+            AbstractGridKeyEventHandler {
+        public void onKeyPress(GridKeyPressEvent<T> event);
+    }
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyDownHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyDownHandler.java
new file mode 100644 (file)
index 0000000..a3b76ea
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyDownHandler;
+
+/**
+ * Handler for {@link GridKeyDownEvent}s that happen when active cell is in the
+ * body of the Grid.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface BodyKeyDownHandler<T> extends GridKeyDownHandler<T> {
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyPressHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyPressHandler.java
new file mode 100644 (file)
index 0000000..5548994
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyPressHandler;
+
+/**
+ * Handler for {@link GridKeyPressEvent}s that happen when active cell is in the
+ * body of the Grid.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface BodyKeyPressHandler<T> extends GridKeyPressHandler<T> {
+}
\ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyUpHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyUpHandler.java
new file mode 100644 (file)
index 0000000..33b4fc8
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyUpHandler;
+
+/**
+ * Handler for {@link GridKeyUpEvent}s that happen when active cell is in the
+ * body of the Grid.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface BodyKeyUpHandler<T> extends GridKeyUpHandler<T> {
+}
\ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyDownHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyDownHandler.java
new file mode 100644 (file)
index 0000000..e90f52e
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyDownHandler;
+
+/**
+ * Handler for {@link GridKeyDownEvent}s that happen when active cell is in the
+ * footer of the Grid.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface FooterKeyDownHandler<T> extends GridKeyDownHandler<T> {
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyPressHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyPressHandler.java
new file mode 100644 (file)
index 0000000..58f48f3
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyPressHandler;
+
+/**
+ * Handler for {@link GridKeyPressEvent}s that happen when active cell is in the
+ * footer of the Grid.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface FooterKeyPressHandler<T> extends GridKeyPressHandler<T> {
+}
\ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyUpHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyUpHandler.java
new file mode 100644 (file)
index 0000000..d6bcddf
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyUpHandler;
+
+/**
+ * Handler for {@link GridKeyUpEvent}s that happen when active cell is in the
+ * footer of the Grid.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface FooterKeyUpHandler<T> extends GridKeyUpHandler<T> {
+}
\ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyDownEvent.java b/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyDownEvent.java
new file mode 100644 (file)
index 0000000..8af65db
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.google.gwt.dom.client.BrowserEvents;
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.client.ui.grid.Grid.AbstractGridKeyEvent;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyDownHandler;
+
+/**
+ * Represents native key down event in Grid.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public class GridKeyDownEvent<T> extends
+        AbstractGridKeyEvent<T, GridKeyDownHandler<T>> {
+
+    public GridKeyDownEvent(Grid<T> grid) {
+        super(grid);
+    }
+
+    @Override
+    protected void dispatch(GridKeyDownHandler<T> handler) {
+        super.dispatch(handler);
+        if ((activeSection == GridSection.BODY && handler instanceof BodyKeyDownHandler)
+                || (activeSection == GridSection.HEADER && handler instanceof HeaderKeyDownHandler)
+                || (activeSection == GridSection.FOOTER && handler instanceof FooterKeyDownHandler)) {
+            handler.onKeyDown(this);
+        }
+    }
+
+    @Override
+    protected String getBrowserEventType() {
+        return BrowserEvents.KEYDOWN;
+    }
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyPressEvent.java b/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyPressEvent.java
new file mode 100644 (file)
index 0000000..6f06bc6
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.google.gwt.dom.client.BrowserEvents;
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.client.ui.grid.Grid.AbstractGridKeyEvent;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyPressHandler;
+
+/**
+ * Represents native key press event in Grid.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public class GridKeyPressEvent<T> extends
+        AbstractGridKeyEvent<T, GridKeyPressHandler<T>> {
+
+    public GridKeyPressEvent(Grid<T> grid) {
+        super(grid);
+    }
+
+    @Override
+    protected void dispatch(GridKeyPressHandler<T> handler) {
+        super.dispatch(handler);
+        if ((activeSection == GridSection.BODY && handler instanceof BodyKeyPressHandler)
+                || (activeSection == GridSection.HEADER && handler instanceof HeaderKeyPressHandler)
+                || (activeSection == GridSection.FOOTER && handler instanceof FooterKeyPressHandler)) {
+            handler.onKeyPress(this);
+        }
+    }
+
+    @Override
+    protected String getBrowserEventType() {
+        return BrowserEvents.KEYPRESS;
+    }
+
+}
\ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyUpEvent.java b/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyUpEvent.java
new file mode 100644 (file)
index 0000000..d289dba
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.google.gwt.dom.client.BrowserEvents;
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.client.ui.grid.Grid.AbstractGridKeyEvent;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyUpHandler;
+
+/**
+ * Represents native key up event in Grid.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public class GridKeyUpEvent<T> extends
+        AbstractGridKeyEvent<T, GridKeyUpHandler<T>> {
+
+    public GridKeyUpEvent(Grid<T> grid) {
+        super(grid);
+    }
+
+    @Override
+    protected void dispatch(GridKeyUpHandler<T> handler) {
+        super.dispatch(handler);
+        if ((activeSection == GridSection.BODY && handler instanceof BodyKeyUpHandler)
+                || (activeSection == GridSection.HEADER && handler instanceof HeaderKeyUpHandler)
+                || (activeSection == GridSection.FOOTER && handler instanceof FooterKeyUpHandler)) {
+            handler.onKeyUp(this);
+        }
+    }
+
+    @Override
+    protected String getBrowserEventType() {
+        return BrowserEvents.KEYUP;
+    }
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyDownHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyDownHandler.java
new file mode 100644 (file)
index 0000000..28c9a8e
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyDownHandler;
+
+/**
+ * Handler for {@link GridKeyDownEvent}s that happen when active cell is in the
+ * header of the Grid.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface HeaderKeyDownHandler<T> extends GridKeyDownHandler<T> {
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyPressHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyPressHandler.java
new file mode 100644 (file)
index 0000000..607c304
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyPressHandler;
+
+/**
+ * Handler for {@link GridKeyPressEvent}s that happen when active cell is in the
+ * header of the Grid.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface HeaderKeyPressHandler<T> extends GridKeyPressHandler<T> {
+}
\ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyUpHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyUpHandler.java
new file mode 100644 (file)
index 0000000..bfa3dde
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyUpHandler;
+
+/**
+ * Handler for {@link GridKeyUpEvent}s that happen when active cell is in the
+ * header of the Grid.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface HeaderKeyUpHandler<T> extends GridKeyUpHandler<T> {
+}
\ No newline at end of file
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
new file mode 100644 (file)
index 0000000..fe81380
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.components.grid.basicfeatures.client;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Test;
+import org.openqa.selenium.Keys;
+import org.openqa.selenium.interactions.Actions;
+
+import com.vaadin.testbench.By;
+import com.vaadin.tests.components.grid.basicfeatures.GridBasicClientFeaturesTest;
+
+public class GridClientKeyEventsTest extends GridBasicClientFeaturesTest {
+
+    private List<String> eventOrder = Arrays.asList("keydown", "keyup",
+            "keypress");
+
+    @Test
+    public void testBodyKeyEvents() throws IOException {
+        openTestURL();
+
+        getGridElement().getCell(2, 2).click();
+
+        new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
+
+        for (int i = 0; i < 3; ++i) {
+            assertEquals("Body key event handler was not called.", "(2, 2) "
+                    + eventOrder.get(i) + " 13",
+                    findElements(By.className("v-label")).get(i * 3).getText());
+
+            assertTrue("Header key event handler got called unexpectedly.",
+                    findElements(By.className("v-label")).get(i * 3 + 1)
+                            .getText().isEmpty());
+            assertTrue("Footer key event handler got called unexpectedly.",
+                    findElements(By.className("v-label")).get(i * 3 + 2)
+                            .getText().isEmpty());
+        }
+
+    }
+
+    @Test
+    public void testHeaderKeyEvents() throws IOException {
+        openTestURL();
+
+        getGridElement().getHeaderCell(0, 2).click();
+
+        new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
+
+        for (int i = 0; i < 3; ++i) {
+            assertEquals("Header key event handler was not called.", "(0, 2) "
+                    + eventOrder.get(i) + " 13",
+                    findElements(By.className("v-label")).get(i * 3 + 1)
+                            .getText());
+
+            assertTrue("Body key event handler got called unexpectedly.",
+                    findElements(By.className("v-label")).get(i * 3).getText()
+                            .isEmpty());
+            assertTrue("Footer key event handler got called unexpectedly.",
+                    findElements(By.className("v-label")).get(i * 3 + 2)
+                            .getText().isEmpty());
+        }
+    }
+
+    @Test
+    public void testFooterKeyEvents() throws IOException {
+        openTestURL();
+
+        selectMenuPath("Component", "Footer", "Append row");
+        getGridElement().getFooterCell(0, 2).click();
+
+        new Actions(getDriver()).sendKeys(Keys.ENTER).perform();
+
+        for (int i = 0; i < 3; ++i) {
+            assertEquals("Footer key event handler was not called.", "(0, 2) "
+                    + eventOrder.get(i) + " 13",
+                    findElements(By.className("v-label")).get(i * 3 + 2)
+                            .getText());
+
+            assertTrue("Body key event handler got called unexpectedly.",
+                    findElements(By.className("v-label")).get(i * 3).getText()
+                            .isEmpty());
+            assertTrue("Header key event handler got called unexpectedly.",
+                    findElements(By.className("v-label")).get(i * 3 + 1)
+                            .getText().isEmpty());
+
+        }
+    }
+
+}
index 8a5a75da380ea52567605de660800f792f0fe531..a7210236d4509c82a5b3774951eceedbd74f1272 100644 (file)
@@ -25,8 +25,11 @@ import com.google.gwt.event.dom.client.ClickEvent;
 import com.google.gwt.event.dom.client.ClickHandler;
 import com.google.gwt.user.client.ui.Button;
 import com.google.gwt.user.client.ui.HTML;
+import com.vaadin.client.ui.VLabel;
+import com.vaadin.client.ui.grid.Cell;
 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;
@@ -36,6 +39,18 @@ import com.vaadin.client.ui.grid.GridHeader.HeaderRow;
 import com.vaadin.client.ui.grid.Renderer;
 import com.vaadin.client.ui.grid.datasources.ListDataSource;
 import com.vaadin.client.ui.grid.datasources.ListSorter;
+import com.vaadin.client.ui.grid.keyevents.BodyKeyDownHandler;
+import com.vaadin.client.ui.grid.keyevents.BodyKeyPressHandler;
+import com.vaadin.client.ui.grid.keyevents.BodyKeyUpHandler;
+import com.vaadin.client.ui.grid.keyevents.FooterKeyDownHandler;
+import com.vaadin.client.ui.grid.keyevents.FooterKeyPressHandler;
+import com.vaadin.client.ui.grid.keyevents.FooterKeyUpHandler;
+import com.vaadin.client.ui.grid.keyevents.GridKeyDownEvent;
+import com.vaadin.client.ui.grid.keyevents.GridKeyPressEvent;
+import com.vaadin.client.ui.grid.keyevents.GridKeyUpEvent;
+import com.vaadin.client.ui.grid.keyevents.HeaderKeyDownHandler;
+import com.vaadin.client.ui.grid.keyevents.HeaderKeyPressHandler;
+import com.vaadin.client.ui.grid.keyevents.HeaderKeyUpHandler;
 import com.vaadin.client.ui.grid.renderers.DateRenderer;
 import com.vaadin.client.ui.grid.renderers.HtmlRenderer;
 import com.vaadin.client.ui.grid.renderers.NumberRenderer;
@@ -236,7 +251,9 @@ public class GridBasicClientFeaturesWidget extends
         createFooterMenu();
 
         grid.getElement().getStyle().setZIndex(0);
-        add(grid);
+        addNorth(grid, 400);
+
+        createKeyHandlers();
     }
 
     private void createStateMenu() {
@@ -602,7 +619,7 @@ public class GridBasicClientFeaturesWidget extends
     }
 
     /**
-     * Creates a renderer for a {@link Renderers}
+     * Creates a renderer for a {@link Renderers}
      */
     @SuppressWarnings("rawtypes")
     private final Renderer createRenderer(Renderers renderer) {
@@ -629,4 +646,110 @@ public class GridBasicClientFeaturesWidget extends
             return new TextRenderer();
         }
     }
+
+    /**
+     * Creates a collection of handlers for all the grid key events
+     */
+    private void createKeyHandlers() {
+        final List<VLabel> labels = new ArrayList<VLabel>();
+        for (int i = 0; i < 9; ++i) {
+            VLabel tmp = new VLabel();
+            addNorth(tmp, 20);
+            labels.add(tmp);
+        }
+
+        // Key Down Events
+        grid.addKeyDownHandler(new BodyKeyDownHandler<List<Data>>() {
+            private final VLabel label = labels.get(0);
+
+            @Override
+            public void onKeyDown(GridKeyDownEvent<List<Data>> event) {
+                updateLabel(label, event);
+            }
+        });
+
+        grid.addKeyDownHandler(new HeaderKeyDownHandler<List<Data>>() {
+            private final VLabel label = labels.get(1);
+
+            @Override
+            public void onKeyDown(GridKeyDownEvent<List<Data>> event) {
+                updateLabel(label, event);
+            }
+        });
+
+        grid.addKeyDownHandler(new FooterKeyDownHandler<List<Data>>() {
+            private final VLabel label = labels.get(2);
+
+            @Override
+            public void onKeyDown(GridKeyDownEvent<List<Data>> event) {
+                updateLabel(label, event);
+            }
+        });
+
+        // Key Up Events
+        grid.addKeyUpHandler(new BodyKeyUpHandler<List<Data>>() {
+            private final VLabel label = labels.get(3);
+
+            @Override
+            public void onKeyUp(GridKeyUpEvent<List<Data>> event) {
+                updateLabel(label, event);
+            }
+        });
+
+        grid.addKeyUpHandler(new HeaderKeyUpHandler<List<Data>>() {
+            private final VLabel label = labels.get(4);
+
+            @Override
+            public void onKeyUp(GridKeyUpEvent<List<Data>> event) {
+                updateLabel(label, event);
+            }
+        });
+
+        grid.addKeyUpHandler(new FooterKeyUpHandler<List<Data>>() {
+            private final VLabel label = labels.get(5);
+
+            @Override
+            public void onKeyUp(GridKeyUpEvent<List<Data>> event) {
+                updateLabel(label, event);
+            }
+        });
+
+        // Key Press Events
+        grid.addKeyPressHandler(new BodyKeyPressHandler<List<Data>>() {
+            private final VLabel label = labels.get(6);
+
+            @Override
+            public void onKeyPress(GridKeyPressEvent<List<Data>> event) {
+                updateLabel(label, event);
+            }
+        });
+
+        grid.addKeyPressHandler(new HeaderKeyPressHandler<List<Data>>() {
+            private final VLabel label = labels.get(7);
+
+            @Override
+            public void onKeyPress(GridKeyPressEvent<List<Data>> event) {
+                updateLabel(label, event);
+            }
+        });
+
+        grid.addKeyPressHandler(new FooterKeyPressHandler<List<Data>>() {
+            private final VLabel label = labels.get(8);
+
+            @Override
+            public void onKeyPress(GridKeyPressEvent<List<Data>> event) {
+                updateLabel(label, event);
+            }
+        });
+
+    }
+
+    private void updateLabel(VLabel label,
+            AbstractGridKeyEvent<List<Data>, ?> 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);
+    }
 }