]> source.dussan.org Git - vaadin-framework.git/commitdiff
Send ComboBox selection with RPC (#19929)
authorHenri Sara <hesara@vaadin.com>
Fri, 6 Nov 2015 08:54:10 +0000 (10:54 +0200)
committerTeemu Suo-Anttila <teemusa@vaadin.com>
Thu, 21 Jul 2016 09:37:46 +0000 (09:37 +0000)
Send the selection from the client to the server with RPC.

Change-Id: Ic32d869c3dc2a8806f9c7cfd1f3db8263a5379a9

client/src/main/java/com/vaadin/client/ui/VFilterSelect.java
client/src/main/java/com/vaadin/client/ui/combobox/ComboBoxConnector.java
server/src/main/java/com/vaadin/ui/ComboBox.java
server/src/test/java/com/vaadin/tests/server/components/ComboBoxValueChangeTest.java
shared/src/main/java/com/vaadin/shared/ui/combobox/ComboBoxServerRpc.java

index 1bca00f6a7cb2b9fe64d7187cea5e27c75aa7947..5e1ee616427c62ca89f119dbba7df3f2b201a984 100644 (file)
@@ -1858,7 +1858,7 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler,
 
         if (!(newKey.equals(selectedOptionKey) || ("".equals(newKey) && selectedOptionKey == null))) {
             selectedOptionKey = newKey;
-            connector.sendSelection(new String[] { selectedOptionKey });
+            connector.sendSelection(selectedOptionKey);
             afterUpdateClientVariables();
 
             // currentPage = -1; // forget the page
index 6c42f4e27c1cc316b24e7d371eb0e234239a20bb..702afe5c907b62c1255c993240e7534b44aef3b8 100644 (file)
@@ -404,10 +404,9 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
      * @since
      */
     public void requestFirstPage() {
+        sendSelection(null);
         getConnection().updateVariable(getConnectorId(), "filter", "", false);
-        getConnection().updateVariable(getConnectorId(), "page", 0, false);
-        getConnection().updateVariable(getConnectorId(), "selected",
-                new String[] {}, immediate);
+        getConnection().updateVariable(getConnectorId(), "page", 0, true);
     }
 
     /**
@@ -440,9 +439,8 @@ public class ComboBoxConnector extends AbstractFieldConnector implements
      * @param selection
      *            the current selection
      */
-    public void sendSelection(String[] selection) {
-        getConnection().updateVariable(getConnectorId(), "selected", selection,
-                immediate);
+    public void sendSelection(String selection) {
+        rpc.setSelectedItem(selection);
     }
 
     /**
index 2caa95fa6130da1c7cd9ff199e777a16b59049d5..597410bd485a035fed52d2f734ad45bbb0a6e683 100644 (file)
@@ -89,6 +89,20 @@ public class ComboBox extends AbstractSelect implements
                 }
             }
         }
+
+        @Override
+        public void setSelectedItem(String item) {
+            if (item == null) {
+                setValue(null, true);
+            } else {
+                final Object id = itemIdMapper.get(item);
+                if (id != null && id.equals(getNullSelectionItemId())) {
+                    setValue(null, true);
+                } else {
+                    setValue(id, true);
+                }
+            }
+        }
     };
 
     FocusAndBlurServerRpcImpl focusBlurRpc = new FocusAndBlurServerRpcImpl(this) {
@@ -267,8 +281,7 @@ public class ComboBox extends AbstractSelect implements
 
             boolean nullFilteredOut = isFilteringNeeded();
             // null option is needed and not filtered out, even if not on
-            // current
-            // page
+            // current page
             boolean nullOptionVisible = needNullSelectOption
                     && !nullFilteredOut;
 
@@ -735,29 +748,6 @@ public class ComboBox extends AbstractSelect implements
         // Not calling super.changeVariables due the history of select
         // component hierarchy
 
-        // Selection change
-        if (variables.containsKey("selected")) {
-            final String[] ka = (String[]) variables.get("selected");
-
-            // Single select mode
-            if (ka.length == 0) {
-
-                // Allows deselection only if the deselected item is visible
-                final Object current = getValue();
-                final Collection<?> visible = getVisibleItemIds();
-                if (visible != null && visible.contains(current)) {
-                    setValue(null, true);
-                }
-            } else {
-                final Object id = itemIdMapper.get(ka[0]);
-                if (id != null && id.equals(getNullSelectionItemId())) {
-                    setValue(null, true);
-                } else {
-                    setValue(id, true);
-                }
-            }
-        }
-
         String newFilter;
         if ((newFilter = (String) variables.get("filter")) != null) {
             // this is a filter request
index 8bc1eb87df1afde952118760a754539a6cbc455f..415bd11cf615a5e7fdaa3e0507014e9ed7bc8854 100644 (file)
@@ -1,10 +1,10 @@
 package com.vaadin.tests.server.components;
 
-import java.util.HashMap;
-import java.util.Map;
-
 import org.junit.Before;
 
+import com.vaadin.server.ServerRpcManager;
+import com.vaadin.server.ServerRpcMethodInvocation;
+import com.vaadin.shared.ui.combobox.ComboBoxServerRpc;
 import com.vaadin.ui.AbstractField;
 import com.vaadin.ui.ComboBox;
 
@@ -19,16 +19,28 @@ public class ComboBoxValueChangeTest extends
 
     @Before
     public void setUp() {
-        ComboBox combo = new ComboBox();
+        ComboBox combo = new ComboBox() {
+            @Override
+            public String getConnectorId() {
+                return "id";
+            }
+        };
         combo.addItem("myvalue");
         super.setUp(combo);
     }
 
     @Override
     protected void setValue(AbstractField<Object> field) {
-        Map<String, Object> variables = new HashMap<String, Object>();
-        variables.put("selected", new String[] { "myvalue" });
-        ((ComboBox) field).changeVariables(field, variables);
+        ComboBox combo = (ComboBox) field;
+        ServerRpcMethodInvocation invocation = new ServerRpcMethodInvocation(
+                combo.getConnectorId(), ComboBoxServerRpc.class,
+                "setSelectedItem", 1);
+        invocation.setParameters(new Object[] { "myvalue" });
+        try {
+            ServerRpcManager.applyInvocation(combo, invocation);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
     }
 
 }
index 04d93c906262e061b1f3ec1aaa9479c7b3f74600..38704b5169b33d9d93da7342d7ea8c4ac1da5447 100644 (file)
@@ -31,4 +31,12 @@ public interface ComboBoxServerRpc extends ServerRpc {
      *            user entered string value for the new item
      */
     public void createNewItem(String itemValue);
+
+    /**
+     * Set the current selection.
+     * 
+     * @param item
+     *            the id of a single item or null to deselect the current value
+     */
+    public void setSelectedItem(String item);
 }