From 849894ae8d61cea654e25533be175a2173679921 Mon Sep 17 00:00:00 2001 From: Niklas Nyholm Date: Fri, 11 Jul 2014 13:37:41 +0300 Subject: [PATCH] Added support for focus and blur listeners in NativeSelect (#6847) Implemented BlurNotifier and FocusNotifier interfaces in NativeSelect Change-Id: Ief38d72edee05bef07f0d57367811bab2c6def50 --- .../com/vaadin/client/ui/VNativeSelect.java | 11 ++ .../nativeselect/NativeSelectConnector.java | 24 +++- server/src/com/vaadin/ui/NativeSelect.java | 79 +++++++++++- ...ativeSelectsFocusAndBlurListenerTests.java | 120 ++++++++++++++++++ 4 files changed, 232 insertions(+), 2 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsFocusAndBlurListenerTests.java diff --git a/client/src/com/vaadin/client/ui/VNativeSelect.java b/client/src/com/vaadin/client/ui/VNativeSelect.java index 8156732f6f..879c54cae8 100644 --- a/client/src/com/vaadin/client/ui/VNativeSelect.java +++ b/client/src/com/vaadin/client/ui/VNativeSelect.java @@ -19,7 +19,9 @@ package com.vaadin.client.ui; import java.util.ArrayList; import java.util.Iterator; +import com.google.gwt.event.dom.client.BlurHandler; import com.google.gwt.event.dom.client.ChangeEvent; +import com.google.gwt.event.dom.client.FocusHandler; import com.google.gwt.user.client.ui.ListBox; import com.vaadin.client.BrowserInfo; import com.vaadin.client.UIDL; @@ -117,6 +119,14 @@ public class VNativeSelect extends VOptionGroupBase implements Field { } } + public void addFocusHandler(FocusHandler handler) { + select.addFocusHandler(handler); + } + + public void addBlurHandler(BlurHandler handler) { + select.addBlurHandler(handler); + } + @Override public void setHeight(String height) { select.setHeight(height); @@ -143,4 +153,5 @@ public class VNativeSelect extends VOptionGroupBase implements Field { public void focus() { select.setFocus(true); } + } diff --git a/client/src/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java b/client/src/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java index 46a8f37122..c52b762e58 100644 --- a/client/src/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java +++ b/client/src/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java @@ -16,16 +16,38 @@ package com.vaadin.client.ui.nativeselect; +import com.google.gwt.event.dom.client.BlurEvent; +import com.google.gwt.event.dom.client.BlurHandler; +import com.google.gwt.event.dom.client.FocusEvent; +import com.google.gwt.event.dom.client.FocusHandler; import com.vaadin.client.ui.VNativeSelect; import com.vaadin.client.ui.optiongroup.OptionGroupBaseConnector; +import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc; import com.vaadin.shared.ui.Connect; import com.vaadin.ui.NativeSelect; @Connect(NativeSelect.class) -public class NativeSelectConnector extends OptionGroupBaseConnector { +public class NativeSelectConnector extends OptionGroupBaseConnector implements + BlurHandler, FocusHandler { + + public NativeSelectConnector() { + super(); + getWidget().addFocusHandler(this); + getWidget().addBlurHandler(this); + } @Override public VNativeSelect getWidget() { return (VNativeSelect) super.getWidget(); } + + @Override + public void onFocus(FocusEvent event) { + getRpcProxy(FocusAndBlurServerRpc.class).focus(); + } + + @Override + public void onBlur(BlurEvent event) { + getRpcProxy(FocusAndBlurServerRpc.class).blur(); + } } diff --git a/server/src/com/vaadin/ui/NativeSelect.java b/server/src/com/vaadin/ui/NativeSelect.java index 51f5536588..ee7bfc3e5c 100644 --- a/server/src/com/vaadin/ui/NativeSelect.java +++ b/server/src/com/vaadin/ui/NativeSelect.java @@ -19,6 +19,12 @@ package com.vaadin.ui; import java.util.Collection; import com.vaadin.data.Container; +import com.vaadin.event.FieldEvents; +import com.vaadin.event.FieldEvents.BlurEvent; +import com.vaadin.event.FieldEvents.BlurListener; +import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcImpl; +import com.vaadin.event.FieldEvents.FocusEvent; +import com.vaadin.event.FieldEvents.FocusListener; import com.vaadin.server.PaintException; import com.vaadin.server.PaintTarget; @@ -29,13 +35,23 @@ import com.vaadin.server.PaintTarget; * better choice. */ @SuppressWarnings("serial") -public class NativeSelect extends AbstractSelect { +public class NativeSelect extends AbstractSelect implements + FieldEvents.BlurNotifier, FieldEvents.FocusNotifier { // width in characters, mimics TextField private int columns = 0; + FocusAndBlurServerRpcImpl focusBlurRpc = new FocusAndBlurServerRpcImpl(this) { + + @Override + protected void fireEvent(Event event) { + NativeSelect.this.fireEvent(event); + } + }; + public NativeSelect() { super(); + registerRpc(focusBlurRpc); } public NativeSelect(String caption, Collection options) { @@ -118,4 +134,65 @@ public class NativeSelect extends AbstractSelect { } } + @Override + public void addFocusListener(FocusListener listener) { + addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener, + FocusListener.focusMethod); + } + + /** + * @deprecated As of 7.0, replaced by + * {@link #addFocusListener(FocusListener)} + **/ + @Override + @Deprecated + public void addListener(FocusListener listener) { + addFocusListener(listener); + } + + @Override + public void removeFocusListener(FocusListener listener) { + removeListener(FocusEvent.EVENT_ID, FocusEvent.class, listener); + } + + /** + * @deprecated As of 7.0, replaced by + * {@link #removeFocusListener(FocusListener)} + **/ + @Override + @Deprecated + public void removeListener(FocusListener listener) { + removeFocusListener(listener); + } + + @Override + public void addBlurListener(BlurListener listener) { + addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener, + BlurListener.blurMethod); + } + + /** + * @deprecated As of 7.0, replaced by {@link #addBlurListener(BlurListener)} + **/ + @Override + @Deprecated + public void addListener(BlurListener listener) { + addBlurListener(listener); + } + + @Override + public void removeBlurListener(BlurListener listener) { + removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener); + } + + /** + * @deprecated As of 7.0, replaced by + * {@link #removeBlurListener(BlurListener)} + **/ + @Override + @Deprecated + public void removeListener(BlurListener listener) { + removeBlurListener(listener); + } + } diff --git a/uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsFocusAndBlurListenerTests.java b/uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsFocusAndBlurListenerTests.java new file mode 100644 index 0000000000..29c8c27883 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsFocusAndBlurListenerTests.java @@ -0,0 +1,120 @@ +/* + * 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.nativeselect; + +import java.util.Collections; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.remote.DesiredCapabilities; + +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * + * @since + * @author Vaadin Ltd + */ +public class NativeSelectsFocusAndBlurListenerTests extends MultiBrowserTest { + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.tb3.MultiBrowserTest#getBrowsersToTest() + */ + @Override + public List getBrowsersToTest() { + return Collections.singletonList(Browser.CHROME + .getDesiredCapabilities()); + } + + @Test + public void testFocusListener() throws InterruptedException { + setDebug(true); + openTestURL(); + Thread.sleep(1000); + menu("Component"); + menuSub("Listeners"); + menuSub("Focus listener"); + + getDriver().findElement(By.tagName("body")).click(); + + WebElement select = getDriver().findElement(By.tagName("select")); + select.click(); + + String bodytext = getDriver().findElement(By.tagName("body")).getText(); + + Assert.assertTrue(bodytext.contains("FocusEvent")); + + } + + @Test + public void testBlurListener() throws InterruptedException { + setDebug(true); + openTestURL(); + Thread.sleep(1000); + menu("Component"); + menuSub("Listeners"); + menuSub("Blur listener"); + + getDriver().findElement(By.tagName("body")).click(); + + WebElement select = getDriver().findElement(By.tagName("select")); + select.click(); + + getDriver().findElement(By.tagName("body")).click(); + + String bodytext = getDriver().findElement(By.tagName("body")).getText(); + + Assert.assertTrue(bodytext.contains("BlurEvent")); + + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.tb3.AbstractTB3Test#getUIClass() + */ + @Override + protected Class getUIClass() { + return NativeSelects.class; + } + + /** + * @since + * @param string + */ + private void menuSub(String string) { + getDriver().findElement(By.xpath("//span[text() = '" + string + "']")) + .click(); + new Actions(getDriver()).moveByOffset(100, 0).build().perform(); + } + + /** + * @since + * @param string + */ + private void menu(String string) { + getDriver().findElement(By.xpath("//span[text() = '" + string + "']")) + .click(); + + } + +} -- 2.39.5