]> source.dussan.org Git - vaadin-framework.git/commitdiff
Added support for focus and blur listeners in NativeSelect (#6847)
authorNiklas Nyholm <niklas.nyholm@procountor.com>
Fri, 11 Jul 2014 10:37:41 +0000 (13:37 +0300)
committerVaadin Code Review <review@vaadin.com>
Wed, 20 Aug 2014 12:25:43 +0000 (12:25 +0000)
Implemented BlurNotifier and FocusNotifier interfaces in NativeSelect

Change-Id: Ief38d72edee05bef07f0d57367811bab2c6def50

client/src/com/vaadin/client/ui/VNativeSelect.java
client/src/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java
server/src/com/vaadin/ui/NativeSelect.java
uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsFocusAndBlurListenerTests.java [new file with mode: 0644]

index 8156732f6f85a5cc88842358fc0f2f7069068747..879c54cae879fad5341f78d8079a187e361d58cd 100644 (file)
@@ -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);
     }
+
 }
index 46a8f371227df0b30a9b94fa9f0ae9f8a95ea203..c52b762e5895153a8c3bea16d214c8337cd44b85 100644 (file)
 
 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();
+    }
 }
index 51f55365880b236d0ae89e8ea9cffdc41302e505..ee7bfc3e5c3c82e87aa9665da89f003e9c8f61ac 100644 (file)
@@ -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 (file)
index 0000000..29c8c27
--- /dev/null
@@ -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<DesiredCapabilities> 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();
+
+    }
+
+}