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;
* 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;
}
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;
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;
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();
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.
*/
*/
private SelectionModel<T> selectionModel;
- private final ActiveCellHandler activeCellHandler;
+ protected final ActiveCellHandler activeCellHandler;
private final LazySorter lazySorter = new LazySorter();
}
});
- // Sink header events
+ // Sink header events and key events
sinkEvents(getHeader().getConsumedEvents());
+ sinkEvents(Arrays.asList(BrowserEvents.KEYDOWN, BrowserEvents.KEYUP,
+ BrowserEvents.KEYPRESS));
}
@Override
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.
*/
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}.
*
* @return the root element of the row
*/
- public Element getElement();
+ public TableRowElement getElement();
}
\ No newline at end of file
--- /dev/null
+/*
+ * 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);
+ }
+
+}
--- /dev/null
+/*
+ * 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> {
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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> {
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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> {
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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());
+
+ }
+ }
+
+}
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;
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;
createFooterMenu();
grid.getElement().getStyle().setZIndex(0);
- add(grid);
+ addNorth(grid, 400);
+
+ createKeyHandlers();
}
private void createStateMenu() {
}
/**
- * Creates a a renderer for a {@link Renderers}
+ * Creates a renderer for a {@link Renderers}
*/
@SuppressWarnings("rawtypes")
private final Renderer createRenderer(Renderers renderer) {
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);
+ }
}