diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2017-09-25 14:07:12 +0300 |
---|---|---|
committer | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2017-09-25 14:07:12 +0300 |
commit | 49fcca9fe06af69e5928b2076397a84049cd3838 (patch) | |
tree | 326feb2ed4bb475f160b3b18020d97781946ca30 | |
parent | f5d8dd7bf4a58af048f0ee13061b1bde5a95facb (diff) | |
download | vaadin-framework-49fcca9fe06af69e5928b2076397a84049cd3838.tar.gz vaadin-framework-49fcca9fe06af69e5928b2076397a84049cd3838.zip |
Fix TreeGrid double click event in hierarchy column (#10070)
Fixes #9868
6 files changed, 167 insertions, 39 deletions
diff --git a/client/src/main/java/com/vaadin/client/widget/treegrid/TreeGrid.java b/client/src/main/java/com/vaadin/client/widget/treegrid/TreeGrid.java index 328d594fe1..99951d1b4f 100644 --- a/client/src/main/java/com/vaadin/client/widget/treegrid/TreeGrid.java +++ b/client/src/main/java/com/vaadin/client/widget/treegrid/TreeGrid.java @@ -21,7 +21,9 @@ import com.google.gwt.event.shared.HandlerRegistration; import com.vaadin.client.widget.escalator.EscalatorUpdater; import com.vaadin.client.widget.escalator.Row; import com.vaadin.client.widget.grid.events.BodyClickHandler; +import com.vaadin.client.widget.grid.events.BodyDoubleClickHandler; import com.vaadin.client.widget.treegrid.events.TreeGridClickEvent; +import com.vaadin.client.widget.treegrid.events.TreeGridDoubleClickEvent; import com.vaadin.client.widgets.Grid; import com.vaadin.shared.data.HierarchicalDataCommunicatorConstants; @@ -110,6 +112,12 @@ public class TreeGrid extends Grid<JsonObject> { } @Override + public HandlerRegistration addBodyDoubleClickHandler( + BodyDoubleClickHandler handler) { + return addHandler(handler, TreeGridDoubleClickEvent.TYPE); + } + + @Override protected EscalatorUpdater createBodyUpdater() { return new BodyUpdater(); } diff --git a/client/src/main/java/com/vaadin/client/widget/treegrid/events/TreeGridClickEvent.java b/client/src/main/java/com/vaadin/client/widget/treegrid/events/TreeGridClickEvent.java index ac653c40a7..255c5ee765 100644 --- a/client/src/main/java/com/vaadin/client/widget/treegrid/events/TreeGridClickEvent.java +++ b/client/src/main/java/com/vaadin/client/widget/treegrid/events/TreeGridClickEvent.java @@ -20,16 +20,14 @@ import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.EventTarget; import com.vaadin.client.WidgetUtil; import com.vaadin.client.renderers.HierarchyRenderer; -import com.vaadin.client.widget.escalator.RowContainer; -import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler; import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridClickHandler; import com.vaadin.client.widget.grid.events.GridClickEvent; import com.vaadin.client.widget.treegrid.TreeGrid; -import com.vaadin.shared.ui.grid.GridConstants; +import com.vaadin.client.widgets.Grid; /** - * Class to set as value of {@link com.vaadin.client.widgets.Grid#clickEvent}. - * <br/> + * Represents native mouse click event in TreeGrid. + * <p> * Differs from {@link GridClickEvent} only in allowing events to originate form * hierarchy widget. * @@ -56,37 +54,10 @@ public class TreeGridClickEvent extends GridClickEvent { } @Override - protected void dispatch( - AbstractGridMouseEventHandler.GridClickHandler handler) { - EventTarget target = getNativeEvent().getEventTarget(); - if (!Element.is(target)) { - // Target is not an element - return; - } - - // Ignore event if originated from child widget - // except when from hierarchy widget - Element targetElement = Element.as(target); - if (getGrid().isElementInChildWidget(targetElement) - && !HierarchyRenderer - .isElementInHierarchyWidget(targetElement)) { - return; - } - - final RowContainer container = getGrid().getEscalator() - .findRowContainer(targetElement); - if (container == null) { - // No container for given element - return; - } - - GridConstants.Section section = GridConstants.Section.FOOTER; - if (container == getGrid().getEscalator().getHeader()) { - section = GridConstants.Section.HEADER; - } else if (container == getGrid().getEscalator().getBody()) { - section = GridConstants.Section.BODY; - } - - doDispatch(handler, section); + protected boolean ignoreEventFromTarget(Grid<?> grid, + Element targetElement) { + // Do not ignore when element is in hierarchy renderer + return super.ignoreEventFromTarget(grid, targetElement) + && !HierarchyRenderer.isElementInHierarchyWidget(targetElement); } } diff --git a/client/src/main/java/com/vaadin/client/widget/treegrid/events/TreeGridDoubleClickEvent.java b/client/src/main/java/com/vaadin/client/widget/treegrid/events/TreeGridDoubleClickEvent.java new file mode 100644 index 0000000000..cfef09511b --- /dev/null +++ b/client/src/main/java/com/vaadin/client/widget/treegrid/events/TreeGridDoubleClickEvent.java @@ -0,0 +1,63 @@ +/* + * Copyright 2000-2016 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.widget.treegrid.events; + +import com.google.gwt.dom.client.BrowserEvents; +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.EventTarget; +import com.vaadin.client.WidgetUtil; +import com.vaadin.client.renderers.HierarchyRenderer; +import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridDoubleClickHandler; +import com.vaadin.client.widget.grid.events.GridDoubleClickEvent; +import com.vaadin.client.widget.treegrid.TreeGrid; +import com.vaadin.client.widgets.Grid; + +/** + * Represents native mouse double click event in TreeGrid. + * <p> + * Differs from {@link GridDoubleClickEvent} only in allowing events to + * originate form hierarchy widget. + * + * @author Vaadin Ltd + * @since + */ +public class TreeGridDoubleClickEvent extends GridDoubleClickEvent { + + public static final Type<GridDoubleClickHandler> TYPE = new Type<>( + BrowserEvents.DBLCLICK, new TreeGridDoubleClickEvent()); + + @Override + public Type<GridDoubleClickHandler> getAssociatedType() { + return TYPE; + } + + @Override + public TreeGrid getGrid() { + EventTarget target = getNativeEvent().getEventTarget(); + if (!Element.is(target)) { + return null; + } + return WidgetUtil.findWidget(Element.as(target), TreeGrid.class, false); + } + + @Override + protected boolean ignoreEventFromTarget(Grid<?> grid, + Element targetElement) { + // Do not ignore when element is in hierarchy renderer + return super.ignoreEventFromTarget(grid, targetElement) + && !HierarchyRenderer.isElementInHierarchyWidget(targetElement); + } +} diff --git a/client/src/main/java/com/vaadin/client/widgets/Grid.java b/client/src/main/java/com/vaadin/client/widgets/Grid.java index 8d65052aa2..81b050b63d 100755 --- a/client/src/main/java/com/vaadin/client/widgets/Grid.java +++ b/client/src/main/java/com/vaadin/client/widgets/Grid.java @@ -2422,8 +2422,8 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, } Element targetElement = Element.as(target); - if (grid.isElementInChildWidget(targetElement)) { - // Target is some widget inside of Grid + if (ignoreEventFromTarget(grid, targetElement)) { + // Event on this target should be ignored return; } @@ -2444,6 +2444,24 @@ public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>, doDispatch(handler, section); } + /** + * Returns whether the mouse event on the target element should be + * ignored. + * + * @param grid + * the {@code Grid} instance from which the event originated + * @param targetElement + * the element from which the event originated + * @return {@code true} if the event should be ignored, {@code false} if + * it should be handled + * @since + */ + protected boolean ignoreEventFromTarget(Grid<?> grid, + Element targetElement) { + // Target is some widget inside of Grid + return grid.isElementInChildWidget(targetElement); + } + protected abstract void doDispatch(HANDLER handler, Section section); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridDoubleClick.java b/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridDoubleClick.java new file mode 100644 index 0000000000..d3f7f815f9 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridDoubleClick.java @@ -0,0 +1,32 @@ +package com.vaadin.tests.components.treegrid; + +import com.vaadin.annotations.Widgetset; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.tests.data.bean.HierarchicalTestBean; +import com.vaadin.ui.Notification; +import com.vaadin.ui.TreeGrid; + +@Widgetset("com.vaadin.DefaultWidgetSet") +public class TreeGridDoubleClick extends AbstractTestUI { + @Override + protected void setup(VaadinRequest request) { + TreeGrid<HierarchicalTestBean> grid = new TreeGrid<>(); + grid.addColumn(HierarchicalTestBean::toString).setCaption("String") + .setId("string"); + grid.addColumn(HierarchicalTestBean::getDepth).setCaption("Depth") + .setId("depth"); + grid.addColumn(HierarchicalTestBean::getIndex) + .setCaption("Index on this depth").setId("index"); + grid.setHierarchyColumn("string"); + grid.setDataProvider(new LazyHierarchicalDataProvider(3, 3)); + + grid.addItemClickListener(event -> { + if (event.getMouseEventDetails().isDoubleClick()) { + Notification.show("Double click"); + } + }); + + addComponent(grid); + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridDoubleClickTest.java b/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridDoubleClickTest.java new file mode 100644 index 0000000000..ba472f0dd3 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridDoubleClickTest.java @@ -0,0 +1,36 @@ +package com.vaadin.tests.components.treegrid; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; + +import com.vaadin.testbench.ElementQuery; +import com.vaadin.testbench.elements.NotificationElement; +import com.vaadin.testbench.elements.TreeGridElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +import static org.junit.Assert.assertTrue; + +public class TreeGridDoubleClickTest extends SingleBrowserTest { + + @Test + public void double_click_on_hierarchy_renderer() { + openTestURL(); + + TreeGridElement grid = $(TreeGridElement.class).first(); + WebElement hierarchyCell = grid + .findElement(By.className("v-treegrid-node")); + new Actions(getDriver()).doubleClick(hierarchyCell).perform(); + + assertTrue("Double click is not handled", + isDoubleClickNotificationPresent()); + } + + private boolean isDoubleClickNotificationPresent() { + ElementQuery<NotificationElement> notification = $( + NotificationElement.class); + return notification.exists() && "Double click" + .equals(notification.first().getCaption()); + } +} |