Add detection logic to distinguish bweteen click and drag.
Change-Id: I43129183e990266243bfaafe83396f52b09b16d4
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
+import com.google.gwt.event.dom.client.MouseUpEvent;
+import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.dom.client.TouchStartEvent;
import com.google.gwt.event.dom.client.TouchStartHandler;
import com.google.gwt.user.client.Command;
public class VDragAndDropWrapper extends VCustomComponent implements
VHasDropHandler {
+ /**
+ * Minimum pixel delta is used to detect click from drag. #12838
+ */
+ private static final int MIN_PX_DELTA = 4;
private static final String CLASSNAME = "v-ddwrapper";
protected static final String DRAGGABLE = "draggable";
/** For internal use only. May be removed or replaced in the future. */
public boolean hasTooltip = false;
+ private int startX = 0;
+ private int startY = 0;
public VDragAndDropWrapper() {
super();
-
hookHtml5Events(getElement());
setStyleName(CLASSNAME);
+
addDomHandler(new MouseDownHandler() {
@Override
- public void onMouseDown(MouseDownEvent event) {
+ public void onMouseDown(final MouseDownEvent event) {
if (getConnector().isEnabled()
&& event.getNativeEvent().getButton() == Event.BUTTON_LEFT
&& startDrag(event.getNativeEvent())) {
event.preventDefault(); // prevent text selection
+ startX = event.getClientX();
+ startY = event.getClientY();
}
}
}, MouseDownEvent.getType());
+ addDomHandler(new MouseUpHandler() {
+
+ @Override
+ public void onMouseUp(final MouseUpEvent event) {
+ final int deltaX = Math.abs(event.getClientX() - startX);
+ final int deltaY = Math.abs(event.getClientY() - startY);
+ if ((deltaX + deltaY) < MIN_PX_DELTA) {
+ setFocusOnLastElement(event);
+ }
+ }
+
+ private void setFocusOnLastElement(final MouseUpEvent event) {
+ Element el = event.getRelativeElement();
+ getLastChildElement(el).focus();
+ }
+
+ private Element getLastChildElement(Element el) {
+ do {
+ if (el == null) {
+ break;
+ }
+ el = el.getFirstChildElement();
+ } while (el.getFirstChildElement() != null);
+ return el;
+ }
+
+ }, MouseUpEvent.getType());
+
addDomHandler(new TouchStartHandler() {
@Override
--- /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.draganddropwrapper;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.DragAndDropWrapper;
+import com.vaadin.ui.DragAndDropWrapper.DragStartMode;
+import com.vaadin.ui.TextArea;
+import com.vaadin.ui.VerticalLayout;
+
+/**
+ * Test UI for text area inside {@link DragAndDropWrapper}: text area should
+ * obtain focus on click.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class DragAndDropFocusObtain extends AbstractTestUI {
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ VerticalLayout dndLayout = new VerticalLayout();
+ TextArea area = new TextArea();
+ area.setValue("text");
+ dndLayout.addComponent(area);
+
+ DragAndDropWrapper wrapper = new DragAndDropWrapper(dndLayout);
+ wrapper.setDragStartMode(DragStartMode.COMPONENT);
+ addComponent(wrapper);
+ }
+
+ @Override
+ protected String getTestDescription() {
+ return "Text fields/areas inside Drag and Drop Wrappers should get focus inside DnD wrapper on click.";
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 12838;
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2000-2013 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.draganddropwrapper;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.interactions.Actions;
+
+import com.vaadin.testbench.By;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+import com.vaadin.ui.DragAndDropWrapper;
+
+/**
+ * Test for text area inside {@link DragAndDropWrapper}: text area should obtain
+ * focus on click.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class DragAndDropFocusObtainTest extends MultiBrowserTest {
+
+ @Test
+ public void testTextAreaDndImage() {
+ openTestURL();
+
+ WebElement wrapper = driver.findElement(By.className("v-ddwrapper"));
+ Actions actions = new Actions(driver);
+ actions.click(wrapper);
+ actions.perform();
+
+ WebElement focusedElement = driver.findElement(By
+ .className("v-textarea-focus"));
+ Assert.assertNotNull("Text area did not obtain focus after click",
+ focusedElement);
+
+ }
+
+}